123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace app\expand\controller;
- use app\common\service\HelperService;
- use think\Validate;
- /**
- * 卡券类
- * Class Coupons
- * @package app\expand\controller
- */
- class Coupons extends BaseAuth
- {
- private $_Account = null;
- public function __construct(){
-
- parent::__construct();
- $this->_Account = $this->getKey($this->_apiCode);
- //验证是否具有访问这个接口的权限
- if(!isset($this->_Account['Coupons'])){
- HelperService::returnJson(['code'=>400,'msg'=>'coupons interface unauthorized access','data'=>[]]);
- }
- }
- /**
- * 领取卡券
- */
- public function receive(){
-
- $params = $this->_params;
- $rule = [
- 'company_no|集团编号'=>'require|max:100',
- 'mobile|手机号'=>'require|max:100',
- 'other|备注信息'=>'require|max:255',
- 'title|卡券标题'=>'require|max:255',
- 'sub_title|卡券副标题'=>'require|max:255',
- 'start_ts|开始时间'=>'require|number',
- 'end_ts|截至时间'=>'require|number',
- 'batch|批次号'=>'require|max:255',
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
- }
-
- $this->connectionRedis(2);
- $receiveCode = null;
-
- while(true){
- //1小时内不重复就不会重复了
- $receiveCode = HelperService::createOrderNum();
- $couponsKey = "coupons_".$receiveCode;
-
- if(!$this->_redisClient->exists($couponsKey)){
- $this->_redisClient->set($couponsKey,'yes',3600);
- break;
- }
- }
-
- try{
- $this->connectionMysql('cs_coupons')->insertGetId([
- 'company_no'=>$params['company_no'],
- 'mobile'=>$params['mobile'],
- 'coupons_code'=>$receiveCode,
- 'create_ts'=>time(),
- 'other'=>$params['other'],
- 'title'=>$params['title'],
- 'sub_title'=>$params['sub_title'],
- 'start_ts'=>$params['start_ts'],
- 'end_ts'=>$params['end_ts'],
- 'batch'=>$params['batch']
- ]);
-
- HelperService::returnJson([
- 'code'=>200,
- 'msg'=>'succes',
- 'data'=>[
- 'coupons_code'=>$receiveCode
- ]
- ]);
- } catch (\Exception $ex){
-
- HelperService::returnJson([
- 'code'=>400,
- 'msg'=>$ex->getMessage(),
- 'data'=>[]
- ]);
- }
-
- }
-
- /**
- * 获取卡券列表
- */
- public function getList(){
- $params = $this->_params;
- $rule = [
- 'company_no|集团编号'=>'require|max:100',
- 'mobile|手机号'=>'require|max:100',
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
- }
-
- //处理掉过期券
- $this->connectionMysql('cs_coupons')
- ->where(['end_ts'=>['lt',time()]])->update(['status'=>4]);
-
- //获取当前用户的券列表
- $couponsList = $this->connectionMysql('cs_coupons')->where([
- 'mobile'=>$params['mobile'],
- 'company_no'=>$params['company_no'],
- ])->order('create_ts desc')->select();
-
- $couponsList = empty($couponsList)?[]:$couponsList;
- foreach($couponsList as &$item){
- $item['status'] = $this->_getCouponsStatus($item['status']);
- }
-
- HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>$couponsList]);
- }
-
- private function _getCouponsStatus($status){
- switch ($status){
- case 2:
- return '已使用';
- case 3:
- return '已作废';
- case 4:
- return '已过期';
- case 1:
- return '未使用';
- default :
- return 'NULL';
- }
- }
- /**
- * 核销卡券
- */
- public function check(){
-
- $params = $this->_params;
- $rule = [
- 'company_no|集团编号'=>'require|max:100',
- 'code|卡券编码'=>'require|max:100',
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError(),'data'=>[]]);
- }
-
- $csCoupons = $this->connectionMysql('cs_coupons')->where([
- 'coupons_code'=>$params['code'],
- 'company_no'=>$params['company_no'],
- ])->find();
-
- if(empty($csCoupons)){
- HelperService::returnJson(['code'=>400,'msg'=>'卡券不存在','data'=>[]]);
- }
-
- if($csCoupons['status']>1){
- $msg = $this->_getCouponsStatus($csCoupons['status']);
- HelperService::returnJson(['code'=>400,'msg'=>$msg,'data'=>[]]);
- }
-
- if($csCoupons['start_ts']>time()){
- HelperService::returnJson(['code'=>400,'msg'=>'还没到卡券使用开始时间','data'=>[]]);
- }
-
- if($csCoupons['end_ts']<time()){
- HelperService::returnJson(['code'=>400,'msg'=>'卡券已过期','data'=>[]]);
- }
-
- $this->connectionMysql('cs_coupons')->where([
- 'coupons_code'=>$params['code'],
- ])->update(['status'=>2,'use_ts'=>time()]);
- if($csCoupons['batch'] == '201905231431002'){
- $time = time();
- $api_code = 'CHENSEN';
- $sign = sha1(md5(base64_encode($api_code.$time))."433E269CD14DBF44257C08612CE51EC9");
- $url = "https://csapi.ahamu.cn/V1/CouponsReceive?api_code=$api_code&request_ts=$time&signKey=$sign";
- $data = [
- 'company_no'=>'9004',
- 'mobile'=>$csCoupons['mobile'],
- 'other'=>'凭此券可享 36.9元主餐任选优惠,此券不与其他优惠同享',
- 'title'=>'36.9主餐任选券',
- 'sub_title'=>'适用范围:南京西路店,新大陆店',
- 'start_ts'=>strtotime(date('Y-m-d')),
- 'end_ts'=>strtotime(date("Y-m-d 23:59:59"))+15*86400,
- 'batch'=>'201905171431003'
- ];
- HelperService::httpPost($url,json_encode($data),true);
- }
- HelperService::returnJson(['code'=>200,'msg'=>'success','data'=>[]]);
- }
-
- }
|