Coupons.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace app\expand\controller;
  3. use app\common\service\HelperService;
  4. use think\Validate;
  5. /**
  6. * 卡券类
  7. * Class Coupons
  8. * @package app\expand\controller
  9. */
  10. class Coupons 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['Coupons'])){
  18. HelperService::returnJson(['code'=>400,'msg'=>'coupons interface unauthorized access','data'=>[]]);
  19. }
  20. }
  21. /**
  22. * 领取卡券
  23. */
  24. public function receive(){
  25. $params = $this->_params;
  26. $rule = [
  27. 'company_no|集团编号'=>'require|max:100',
  28. 'mobile|手机号'=>'require|max:100',
  29. 'other|备注信息'=>'require|max:255',
  30. 'title|卡券标题'=>'require|max:255',
  31. 'sub_title|卡券副标题'=>'require|max:255',
  32. 'start_ts|开始时间'=>'require|number',
  33. 'end_ts|截至时间'=>'require|number',
  34. 'batch|批次号'=>'require|max:255',
  35. ];
  36. $validate = new Validate($rule);
  37. if(!$validate->check($params)){
  38. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
  39. }
  40. $this->connectionRedis(2);
  41. $receiveCode = null;
  42. while(true){
  43. //1小时内不重复就不会重复了
  44. $receiveCode = HelperService::createOrderNum();
  45. $couponsKey = "coupons_".$receiveCode;
  46. if(!$this->_redisClient->exists($couponsKey)){
  47. $this->_redisClient->set($couponsKey,'yes',3600);
  48. break;
  49. }
  50. }
  51. try{
  52. $this->connectionMysql('cs_coupons')->insertGetId([
  53. 'company_no'=>$params['company_no'],
  54. 'mobile'=>$params['mobile'],
  55. 'coupons_code'=>$receiveCode,
  56. 'create_ts'=>time(),
  57. 'other'=>$params['other'],
  58. 'title'=>$params['title'],
  59. 'sub_title'=>$params['sub_title'],
  60. 'start_ts'=>$params['start_ts'],
  61. 'end_ts'=>$params['end_ts'],
  62. 'batch'=>$params['batch']
  63. ]);
  64. HelperService::returnJson([
  65. 'code'=>200,
  66. 'msg'=>'succes',
  67. 'data'=>[
  68. 'coupons_code'=>$receiveCode
  69. ]
  70. ]);
  71. } catch (\Exception $ex){
  72. HelperService::returnJson([
  73. 'code'=>400,
  74. 'msg'=>$ex->getMessage(),
  75. 'data'=>[]
  76. ]);
  77. }
  78. }
  79. /**
  80. * 获取卡券列表
  81. */
  82. public function getList(){
  83. $params = $this->_params;
  84. $rule = [
  85. 'company_no|集团编号'=>'require|max:100',
  86. 'mobile|手机号'=>'require|max:100',
  87. ];
  88. $validate = new Validate($rule);
  89. if(!$validate->check($params)){
  90. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
  91. }
  92. //处理掉过期券
  93. $this->connectionMysql('cs_coupons')
  94. ->where(['end_ts'=>['lt',time()]])->update(['status'=>4]);
  95. //获取当前用户的券列表
  96. $couponsList = $this->connectionMysql('cs_coupons')->where([
  97. 'mobile'=>$params['mobile'],
  98. 'company_no'=>$params['company_no'],
  99. ])->order('create_ts desc')->select();
  100. $couponsList = empty($couponsList)?[]:$couponsList;
  101. foreach($couponsList as &$item){
  102. $item['status'] = $this->_getCouponsStatus($item['status']);
  103. }
  104. HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>$couponsList]);
  105. }
  106. private function _getCouponsStatus($status){
  107. switch ($status){
  108. case 2:
  109. return '已使用';
  110. case 3:
  111. return '已作废';
  112. case 4:
  113. return '已过期';
  114. case 1:
  115. return '未使用';
  116. default :
  117. return 'NULL';
  118. }
  119. }
  120. /**
  121. * 核销卡券
  122. */
  123. public function check(){
  124. $params = $this->_params;
  125. $rule = [
  126. 'company_no|集团编号'=>'require|max:100',
  127. 'code|卡券编码'=>'require|max:100',
  128. ];
  129. $validate = new Validate($rule);
  130. if(!$validate->check($params)){
  131. HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
  132. }
  133. $csCoupons = $this->connectionMysql('cs_coupons')->where([
  134. 'coupons_code'=>$params['code'],
  135. 'company_no'=>$params['company_no'],
  136. ])->find();
  137. if(empty($csCoupons)){
  138. HelperService::returnJson(['code'=>400,'msg'=>'卡券不存在','data'=>[]]);
  139. }
  140. if($csCoupons['status']>1){
  141. $msg = $this->_getCouponsStatus($csCoupons['status']);
  142. HelperService::returnJson(['code'=>400,'msg'=>$msg,'data'=>[]]);
  143. }
  144. if($csCoupons['start_ts']>time()){
  145. HelperService::returnJson(['code'=>400,'msg'=>'还没到卡券使用开始时间','data'=>[]]);
  146. }
  147. if($csCoupons['end_ts']<time()){
  148. HelperService::returnJson(['code'=>400,'msg'=>'卡券已过期','data'=>[]]);
  149. }
  150. $this->connectionMysql('cs_coupons')->where([
  151. 'coupons_code'=>$params['code'],
  152. ])->update(['status'=>2,'use_ts'=>time()]);
  153. if($csCoupons['batch'] == '201905231431002'){
  154. $time = time();
  155. $api_code = 'CHENSEN';
  156. $sign = sha1(md5(base64_encode($api_code.$time))."433E269CD14DBF44257C08612CE51EC9");
  157. $url = "https://csapi.ahamu.cn/V1/CouponsReceive?api_code=$api_code&request_ts=$time&signKey=$sign";
  158. $data = [
  159. 'company_no'=>'9004',
  160. 'mobile'=>$csCoupons['mobile'],
  161. 'other'=>'凭此券可享 36.9元主餐任选优惠,此券不与其他优惠同享',
  162. 'title'=>'36.9主餐任选券',
  163. 'sub_title'=>'适用范围:南京西路店,新大陆店',
  164. 'start_ts'=>strtotime(date('Y-m-d')),
  165. 'end_ts'=>strtotime(date("Y-m-d 23:59:59"))+15*86400,
  166. 'batch'=>'201905171431003'
  167. ];
  168. HelperService::httpPost($url,json_encode($data),true);
  169. }
  170. HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>[]]);
  171. }
  172. }