123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace app\expand\controller;
- use app\common\service\HelperService;
- use think\Validate;
- /**
- * F码类V2
- * Class Fcoupons
- * @package app\expand\controller
- */
- class Fcoupons extends BaseAuth
- {
- private $_Account = null;
- public function __construct(){
- parent::__construct();
- $this->_Account = $this->getKey($this->_apiCode);
- //验证是否具有访问这个接口的权限
- if(!isset($this->_Account['Fcoupons'])){
- HelperService::returnJson(['code'=>400,'msg'=>'Fcoupons interface unauthorized access','data'=>[]]);
- }
- }
- /**
- * 获得用户F码列表
- */
- public function getFcouponsList(){
- $params = $this->_params;
- $rule = [
- 'mobile|用户标识'=>'require|mobile',
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
- }
-
- $fCouponList = $this->_getFcouponsList([
- 'new_coupons.api_code'=>$this->_apiCode,
- 'new_coupons.status'=>'10',
- 'mobile'=>$params['mobile']
- ]);
-
- HelperService::returnJson(['code'=>200, 'msg'=>'success', 'data'=>(array)$fCouponList]);
- }
- /**
- * 给用户发放F码
- */
- public function sendFcoupons(){
- $params = $this->_params;
- $rule = [
- 'mobile|用户标识'=>'require|mobile',
- 'couponId|券ID'=>'require',
- 'endTs|到期时间'=>'require',
- 'other|备注信息'=>'max:255',
- 'isFormat|是否格式化日期'=>'number'
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
- }
-
- $check = $this->connectionMysql('new_coupons_batch')->where([
- 'api_code'=>$this->_apiCode,
- 'batch'=>$params['couponId'],
- 'status'=>'10'
- ])->find();
-
- if(empty($check)){
- HelperService::returnJson(['code'=>400, 'msg'=>'券ID不存在/已经删除', 'data'=>[]]);
- }
-
- $startTs= isset($params['startTs'])?strtotime($params['startTs']):time();
- $other = isset($params['other'])?$params['other']:"";
- $isFormat = isset($params['isFormat'])?$params['isFormat']:0;
-
- if(strtotime($params['endTs'])<=time()){
- HelperService::returnJson(['code'=>400, 'msg'=>'到期时间不得小于当前时间', 'data'=>[]]);
- }
- $coupon_sn = HelperService::createOrderNum();
- $data = [
- 'mobile'=>$params['mobile'],
- 'batch'=>$params['couponId'],
- 'end_ts'=>$this->_formatTime(strtotime($params['endTs']),$isFormat,'end'),
- 'start_ts'=>$this->_formatTime($startTs,$isFormat,'start'),
- 'status'=>'10',
- 'other'=>$other,
- 'add_ts'=>time(),
- 'coupons_sn'=>$coupon_sn,
- 'api_code'=>$this->_apiCode
- ];
-
- $res = $this->connectionMysql('new_coupons')->insertGetId($data);
- if($res === false){
- HelperService::returnJson(['code'=>400, 'msg'=>'发放失败,请重试', 'data'=>[]]);
- }
- HelperService::returnJson(['code'=>200, 'msg'=>'success', 'data'=>['code'=>$coupon_sn]]);
- }
-
-
- /**
- * 获取卡券数据
- * @param type $where
- * @return type
- */
- private function _getFcouponsList($where=[]){
-
- return $this->connectionMysql('new_coupons')
- ->where($where)->alias('new_coupons')
- ->field('new_coupons_batch.title,new_coupons_batch.sub_title,
- new_coupons_batch.use_into as user_intro,new_coupons_batch.use_range as user_range,
- new_coupons_batch.logo,new_coupons.mobile,new_coupons.coupons_sn as code,
- new_coupons.add_ts,new_coupons.other,new_coupons.use_ts,new_coupons.start_ts,
- new_coupons.status,new_coupons.end_ts')
- ->join('new_coupons_batch new_coupons_batch','new_coupons_batch.batch = new_coupons.batch','left')->select();
- }
- /**
- * 格式化时间
- * @param type $time
- * @param type $isFormat
- * @param type $type start/end 格式开始时间 or结束时间
- */
- private function _formatTime($time,$isFormat=0,$type='start'){
-
- if(empty($isFormat)){
- return $time;
- }
-
- switch ($type){
- case 'start':
- return strtotime(date('Y-m-d 00:00:00',$time));
- case 'end':
- return strtotime(date('Y-m-d 23:59:59',$time));
- default :
- return $time;
- }
- }
- /**
- * 核销F码
- */
- public function checkFcoupon(){
- $params = $this->_params;
- $rule = [
- 'mobile|用户标识'=>'require',
- 'code|卡券编码'=>'require',
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
- }
-
- $list = $this->connectionMysql('new_coupons')->where([
- 'api_code'=>$this->_apiCode,
- 'status'=>'10',
- 'coupons_sn'=>$params['code'],
- 'mobile'=>$params['mobile']
- ])->find();
-
- if(empty($list)){
- HelperService::returnJson(['code'=>400, 'msg'=>'用户卡券码不正确', 'data'=>[]]);
- }
-
- $res = $this->connectionMysql('new_coupons')->where([
- 'api_code'=>$this->_apiCode,
- 'status'=>'10',
- 'coupons_sn'=>$params['code'],
- 'mobile'=>$params['mobile']
- ])->update(['use_ts'=>time(),'status'=>20]);
-
- if($res === false){
- HelperService::returnJson(['code'=>400, 'msg'=>'用户卡券核销失败', 'data'=>[]]);
- }
- HelperService::returnJson(['code'=>200, 'msg'=>'success', 'data'=>[]]);
- }
- /**
- * insertCache
- * 会员信息缓存
- */
- public function insertCache(){
- $params = $this->_params;
- $rule = [
- 'mobile|会员手机号'=>'require',
- 'content|key'=>'require',
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
- }
- $res = $this->connectionMysql('member_cache')->insert([
- 'api_code'=>$this->_apiCode,
- 'value'=>$params['mobile'],
- 'code'=>$params['content'],
- 'add_ts'=>time(),
- ]);
- if($res === false){
- HelperService::returnJson(['code'=>400, 'msg'=>'添加失败', 'data'=>[]]);
- }
- HelperService::returnJson(['code'=>200, 'msg'=>'添加成功', 'data'=>[]]);
- }
- /**
- * 获取缓存信息
- */
- public function getMemberCache(){
- $params = $this->_params;
- $rule = [
- 'content|key'=>'require',
- ];
- $validate = new Validate($rule);
- if(!$validate->check($params)){
- HelperService::returnJson(['code'=>400,'msg'=>$validate->getError()]);
- }
- // $this->connectionMysql('member_cache')->where(['add_ts','<',time()-300])->delete();
- // $res = $this->connectionMysql('member_cache')->where([time() - add_ts>300])->find();
- $res = $this->connectionMysql('member_cache')->where([
- 'code'=>$params['content']
- ])->find();
- if(!$res){
- HelperService::returnJson(['code'=>400, 'msg'=>'会员码已失效', 'data'=>[]]);
- }
- HelperService::returnJson(['code'=>200, 'msg'=>'success', 'data'=>$res]);
- }
- }
|