123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- <?php
- namespace app\api\controller;
- use app\api\model\CustomBlocks;
- use app\api\service\User as UserService;
- use app\common\model\SmsCode;
- use think\cache\driver\Redis;
- use app\common\model\PushLog;
- use app\api\model\subscribe\WechatSub;
- use app\common\model\ms\MsActivityGood;
- use think\facade\Queue;
- /**
- * 默认控制器
- * Class Index
- * @package app\api\controller
- */
- class Index extends Controller
- {
- //秒杀活动订阅
- public function miaoshasubscribe(){
- $param = $this->request->param();
- $template_id = $param['template_id']??'';
- $ms_activity_id = $param['ms_activity_id']??0;
- if(empty($template_id)){
- return $this->renderError("缺少template_id");
- }
- $user = UserService::getCurrentLoginUser(true);
- $key = (new WechatSub('miaosha'))->getMiaoShaKey($user->user_id,$template_id,$ms_activity_id);
- $rds = new Redis(config('cache.stores.redis'));
- if(!empty($user->open_id)){
- $rds->set($key,1,1000000);
- }
- $value = $rds->get($key);
- $ms_id = $param['ms_id']??0;
- $jobHandlerClassName = 'app\job\MiaoshaPushSubscribe';
- $jobQueueName = "orderWaitCommissionsQueue";
- //数据
- $data = [
- 'ms_activity_id'=>$ms_activity_id,
- 'ms_id' => $ms_id,
- 'user_id'=> $user->user_id,
- 'template_id'=>$template_id,
- ];
- $msGood = MsActivityGood::where('id',$ms_id)->find();
- if($msGood){
- $start = strtotime($msGood->start_time);
- $dif_second = $start - time() -300;
- if($dif_second>0){
- $isPushed = Queue::later($dif_second, $jobHandlerClassName, $data ,$jobQueueName);
- }
- }
- return $this->renderSuccess(compact('value'));
- }
- //取消秒杀活动订阅
- public function cancelMiaoshaSubscribe(){
- $param = $this->request->param();
- $template_id = $param['template_id']??'';
- $ms_activity_id = $param['ms_activity_id']??0;
- if(empty($template_id)){
- return $this->renderError("缺少template_id");
- }
- $user = UserService::getCurrentLoginUser(true);
- $key = (new WechatSub('miaosha'))->getMiaoShaKey($user->user_id,$template_id,$ms_activity_id);
- $rds = new Redis(config('cache.stores.redis'));
- if(!empty($user->open_id)){
- $rds->del($key);
- }
- return $this->renderSuccess();
- }
- //检查是否订阅了秒杀消息
- public function checkMiaoshaSubscribe(){
- $param = $this->request->param();
- $template_id = $param['template_id']??'';
- $ms_activity_id = $param['ms_activity_id']??0;
- if(empty($template_id)){
- return $this->renderError("缺少template_id");
- }
- $user = UserService::getCurrentLoginUser(false);
- $user_id = $user->user_id??0;
- if(empty($user_id)){
- $flag = false;
- }
- else{
- $key = (new WechatSub('miaosha'))->getMiaoShaKey($user->user_id,$template_id,$ms_activity_id);
- $rds = new Redis(config('cache.stores.redis'));
- $flag = false;
- if(!empty($user->open_id)){
- $v1 = $rds->get($key);
- if(!empty($v1)){
- $flag = true;
- }
- }
- }
- return $this->renderSuccess(compact('flag'));
- }
- //订阅
- public function subscribe(){
- $param = $this->request->param();
- $template_id = $param['template_id']??'';
- // $ms_activity_id = $param['ms_activity_id']??0;
- if(empty($template_id)){
- return $this->renderError("缺少template_id");
- }
- $user = UserService::getCurrentLoginUser(true);
- $key ='subscribe:open_id:'.$user->open_id.'template_id:'.$template_id;
- $key2 ='subscribe:user_id:'.$user->user_id.'template_id:'.$template_id;
- $rds = new Redis(config('cache.stores.redis'));
- if(!empty($user->open_id)){
- $rds->set($key,1,1000000);
- $rds->set($key2,1,1000000);
- }
- $value1 = $rds->get($key);
- $value2 = $rds->get($key2);
- $value = $value1.$value2;
- return $this->renderSuccess(compact('value'));
- }
- //取消订阅
- public function cancelSubscribe(){
- $param = $this->request->param();
- $template_id = $param['template_id']??'';
- if(empty($template_id)){
- return $this->renderError("缺少template_id");
- }
- $user = UserService::getCurrentLoginUser(true);
- $key ='subscribe:open_id:'.$user->open_id.'template_id:'.$template_id;
- $key2 ='subscribe:user_id:'.$user->user_id.'template_id:'.$template_id;
- $rds = new Redis(config('cache.stores.redis'));
- if(!empty($user->open_id)){
- $rds->del($key);
- $rds->del($key2);
- }
- return $this->renderSuccess();
- }
- //检查是否订阅了消息
- public function checkSubscribe(){
- $param = $this->request->param();
- $template_id = $param['template_id']??'';
- // $ms_activity_id = $param['ms_activity_id']??0;
- if(empty($template_id)){
- return $this->renderError("缺少template_id");
- }
- $user = UserService::getCurrentLoginUser(false);
- $user_id = $user->user_id??0;
- if(empty($user_id)){
- $flag = false;
- }
- else{
- $key ='subscribe:open_id:'.$user->open_id.'template_id:'.$template_id;
- $key2 ='subscribe:user_id:'.$user->user_id.'template_id:'.$template_id;
- // if($ms_activity_id>0){
- // $key.='ms_activity_id:'.$ms_activity_id;
- // $key2 = 'ms_activity_id:'.$ms_activity_id;
- // }
- $rds = new Redis(config('cache.stores.redis'));
- $flag = false;
- if(!empty($user->open_id)){
- $v1 = $rds->get($key);
- $v2 = $rds->get($key2);
- if(!empty($v1)||!empty($v2)){
- $flag = true;
- }
- }
- }
- return $this->renderSuccess(compact('flag'));
- }
- public function index()
- {
- // 自定义内容区
- $model = new CustomBlocks();
- $custom_blocks = $model->getList();
- return $this->renderSuccess(compact('custom_blocks'));
- }
- public function sms($mobile,$type = 0){
- $userinfo = UserService::getCurrentLoginUser(false);
- if($userinfo){
- $key = 'sms:user_id:'.$userinfo->user_id;
- $rds = new Redis(config('cache.stores.redis'));
- $num = $rds->get($key);
- if($num==5){
- return $this->renderError('你获取短信验证码的次数太多了,请稍后再试');
- }
- if($num==null){
- $num = 1;
- }else{
- $num++;
- }
- $rds->set($key,$num,300);
- }
-
- $data['mobile'] = $mobile;
- $data['user_id'] = $userinfo->user_id??0;
- $code = rand(100000,999999);
- $data['code'] = $code ;
- $data['type'] = $type;
- $content = '验证码:'.$data['code'].',您正在进行身份校验,5分钟内有效,请及时输入。';
- $smsCode = new SmsCode;
- $smsCode->save($data);
- PushLog::addSmsMon($content,$mobile,1);
- return $this->renderSuccess();
- }
- }
|