Fcoupons.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace app\expand\controller;
  3. use app\common\service\HelperService;
  4. use think\Validate;
  5. /**
  6. * F码类V2
  7. * Class Fcoupons
  8. * @package app\expand\controller
  9. */
  10. class Fcoupons extends BaseAuth
  11. {
  12. private $_Account = null;
  13. public function __construct(){
  14. parent::__construct();
  15. $this->_Account = $this->getKey($this->_apiCode);
  16. //验证是否具有访问这个接口的权限
  17. if(!isset($this->_Account['Fcoupons'])){
  18. HelperService::returnJson(['code'=>400,'msg'=>'Fcoupons interface unauthorized access','data'=>[]]);
  19. }
  20. }
  21. /**
  22. * 获得用户F码列表
  23. */
  24. public function getFcouponsList(){
  25. $params = $this->_params;
  26. $rule = [
  27. 'mobile|用户标识'=>'require|mobile',
  28. ];
  29. $validate = new Validate($rule);
  30. if(!$validate->check($params)){
  31. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
  32. }
  33. $fCouponList = $this->_getFcouponsList([
  34. 'new_coupons.api_code'=>$this->_apiCode,
  35. 'new_coupons.status'=>'10',
  36. 'mobile'=>$params['mobile']
  37. ]);
  38. HelperService::returnJson(['code'=>200, 'msg'=>'success', 'data'=>(array)$fCouponList]);
  39. }
  40. /**
  41. * 给用户发放F码
  42. */
  43. public function sendFcoupons(){
  44. $params = $this->_params;
  45. $rule = [
  46. 'mobile|用户标识'=>'require|mobile',
  47. 'couponId|券ID'=>'require',
  48. 'endTs|到期时间'=>'require',
  49. 'other|备注信息'=>'max:255',
  50. 'isFormat|是否格式化日期'=>'number'
  51. ];
  52. $validate = new Validate($rule);
  53. if(!$validate->check($params)){
  54. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
  55. }
  56. $check = $this->connectionMysql('new_coupons_batch')->where([
  57. 'api_code'=>$this->_apiCode,
  58. 'batch'=>$params['couponId'],
  59. 'status'=>'10'
  60. ])->find();
  61. if(empty($check)){
  62. HelperService::returnJson(['code'=>400, 'msg'=>'券ID不存在/已经删除', 'data'=>[]]);
  63. }
  64. $startTs= isset($params['startTs'])?strtotime($params['startTs']):time();
  65. $other = isset($params['other'])?$params['other']:"";
  66. $isFormat = isset($params['isFormat'])?$params['isFormat']:0;
  67. if(strtotime($params['endTs'])<=time()){
  68. HelperService::returnJson(['code'=>400, 'msg'=>'到期时间不得小于当前时间', 'data'=>[]]);
  69. }
  70. $coupon_sn = HelperService::createOrderNum();
  71. $data = [
  72. 'mobile'=>$params['mobile'],
  73. 'batch'=>$params['couponId'],
  74. 'end_ts'=>$this->_formatTime(strtotime($params['endTs']),$isFormat,'end'),
  75. 'start_ts'=>$this->_formatTime($startTs,$isFormat,'start'),
  76. 'status'=>'10',
  77. 'other'=>$other,
  78. 'add_ts'=>time(),
  79. 'coupons_sn'=>$coupon_sn,
  80. 'api_code'=>$this->_apiCode
  81. ];
  82. $res = $this->connectionMysql('new_coupons')->insertGetId($data);
  83. if($res === false){
  84. HelperService::returnJson(['code'=>400, 'msg'=>'发放失败,请重试', 'data'=>[]]);
  85. }
  86. HelperService::returnJson(['code'=>200, 'msg'=>'success', 'data'=>['code'=>$coupon_sn]]);
  87. }
  88. /**
  89. * 获取卡券数据
  90. * @param type $where
  91. * @return type
  92. */
  93. private function _getFcouponsList($where=[]){
  94. return $this->connectionMysql('new_coupons')
  95. ->where($where)->alias('new_coupons')
  96. ->field('new_coupons_batch.title,new_coupons_batch.sub_title,
  97. new_coupons_batch.use_into as user_intro,new_coupons_batch.use_range as user_range,
  98. new_coupons_batch.logo,new_coupons.mobile,new_coupons.coupons_sn as code,
  99. new_coupons.add_ts,new_coupons.other,new_coupons.use_ts,new_coupons.start_ts,
  100. new_coupons.status,new_coupons.end_ts')
  101. ->join('new_coupons_batch new_coupons_batch','new_coupons_batch.batch = new_coupons.batch','left')->select();
  102. }
  103. /**
  104. * 格式化时间
  105. * @param type $time
  106. * @param type $isFormat
  107. * @param type $type start/end 格式开始时间 or结束时间
  108. */
  109. private function _formatTime($time,$isFormat=0,$type='start'){
  110. if(empty($isFormat)){
  111. return $time;
  112. }
  113. switch ($type){
  114. case 'start':
  115. return strtotime(date('Y-m-d 00:00:00',$time));
  116. case 'end':
  117. return strtotime(date('Y-m-d 23:59:59',$time));
  118. default :
  119. return $time;
  120. }
  121. }
  122. /**
  123. * 核销F码
  124. */
  125. public function checkFcoupon(){
  126. $params = $this->_params;
  127. $rule = [
  128. 'mobile|用户标识'=>'require',
  129. 'code|卡券编码'=>'require',
  130. ];
  131. $validate = new Validate($rule);
  132. if(!$validate->check($params)){
  133. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
  134. }
  135. $list = $this->connectionMysql('new_coupons')->where([
  136. 'api_code'=>$this->_apiCode,
  137. 'status'=>'10',
  138. 'coupons_sn'=>$params['code'],
  139. 'mobile'=>$params['mobile']
  140. ])->find();
  141. if(empty($list)){
  142. HelperService::returnJson(['code'=>400, 'msg'=>'用户卡券码不正确', 'data'=>[]]);
  143. }
  144. $res = $this->connectionMysql('new_coupons')->where([
  145. 'api_code'=>$this->_apiCode,
  146. 'status'=>'10',
  147. 'coupons_sn'=>$params['code'],
  148. 'mobile'=>$params['mobile']
  149. ])->update(['use_ts'=>time(),'status'=>20]);
  150. if($res === false){
  151. HelperService::returnJson(['code'=>400, 'msg'=>'用户卡券核销失败', 'data'=>[]]);
  152. }
  153. HelperService::returnJson(['code'=>200, 'msg'=>'success', 'data'=>[]]);
  154. }
  155. /**
  156. * insertCache
  157. * 会员信息缓存
  158. */
  159. public function insertCache(){
  160. $params = $this->_params;
  161. $rule = [
  162. 'mobile|会员手机号'=>'require',
  163. 'content|key'=>'require',
  164. ];
  165. $validate = new Validate($rule);
  166. if(!$validate->check($params)){
  167. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
  168. }
  169. $res = $this->connectionMysql('member_cache')->insert([
  170. 'api_code'=>$this->_apiCode,
  171. 'value'=>$params['mobile'],
  172. 'code'=>$params['content'],
  173. 'add_ts'=>time(),
  174. ]);
  175. if($res === false){
  176. HelperService::returnJson(['code'=>400, 'msg'=>'添加失败', 'data'=>[]]);
  177. }
  178. HelperService::returnJson(['code'=>200, 'msg'=>'添加成功', 'data'=>[]]);
  179. }
  180. /**
  181. * 获取缓存信息
  182. */
  183. public function getMemberCache(){
  184. $params = $this->_params;
  185. $rule = [
  186. 'content|key'=>'require',
  187. ];
  188. $validate = new Validate($rule);
  189. if(!$validate->check($params)){
  190. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
  191. }
  192. // $this->connectionMysql('member_cache')->where(['add_ts','<',time()-300])->delete();
  193. // $res = $this->connectionMysql('member_cache')->where([time() - add_ts>300])->find();
  194. $res = $this->connectionMysql('member_cache')->where([
  195. 'code'=>$params['content']
  196. ])->find();
  197. if(!$res){
  198. HelperService::returnJson(['code'=>400, 'msg'=>'会员码已失效', 'data'=>[]]);
  199. }
  200. HelperService::returnJson(['code'=>200, 'msg'=>'success', 'data'=>$res]);
  201. }
  202. }