Index.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\model\CustomBlocks;
  4. use app\api\service\User as UserService;
  5. use app\common\model\SmsCode;
  6. use think\cache\driver\Redis;
  7. use app\common\model\PushLog;
  8. use app\api\model\subscribe\WechatSub;
  9. use app\common\model\ms\MsActivityGood;
  10. use think\facade\Queue;
  11. /**
  12. * 默认控制器
  13. * Class Index
  14. * @package app\api\controller
  15. */
  16. class Index extends Controller
  17. {
  18. //秒杀活动订阅
  19. public function miaoshasubscribe(){
  20. $param = $this->request->param();
  21. $template_id = $param['template_id']??'';
  22. $ms_activity_id = $param['ms_activity_id']??0;
  23. if(empty($template_id)){
  24. return $this->renderError("缺少template_id");
  25. }
  26. $user = UserService::getCurrentLoginUser(true);
  27. $key = (new WechatSub('miaosha'))->getMiaoShaKey($user->user_id,$template_id,$ms_activity_id);
  28. $rds = new Redis(config('cache.stores.redis'));
  29. if(!empty($user->open_id)){
  30. $rds->set($key,1,1000000);
  31. }
  32. $value = $rds->get($key);
  33. $ms_id = $param['ms_id']??0;
  34. $jobHandlerClassName = 'app\job\MiaoshaPushSubscribe';
  35. $jobQueueName = "orderWaitCommissionsQueue";
  36. //数据
  37. $data = [
  38. 'ms_activity_id'=>$ms_activity_id,
  39. 'ms_id' => $ms_id,
  40. 'user_id'=> $user->user_id,
  41. 'template_id'=>$template_id,
  42. ];
  43. $msGood = MsActivityGood::where('id',$ms_id)->find();
  44. if($msGood){
  45. $start = strtotime($msGood->start_time);
  46. $dif_second = $start - time() -300;
  47. if($dif_second>0){
  48. $isPushed = Queue::later($dif_second, $jobHandlerClassName, $data ,$jobQueueName);
  49. }
  50. }
  51. return $this->renderSuccess(compact('value'));
  52. }
  53. //取消秒杀活动订阅
  54. public function cancelMiaoshaSubscribe(){
  55. $param = $this->request->param();
  56. $template_id = $param['template_id']??'';
  57. $ms_activity_id = $param['ms_activity_id']??0;
  58. if(empty($template_id)){
  59. return $this->renderError("缺少template_id");
  60. }
  61. $user = UserService::getCurrentLoginUser(true);
  62. $key = (new WechatSub('miaosha'))->getMiaoShaKey($user->user_id,$template_id,$ms_activity_id);
  63. $rds = new Redis(config('cache.stores.redis'));
  64. if(!empty($user->open_id)){
  65. $rds->del($key);
  66. }
  67. return $this->renderSuccess();
  68. }
  69. //检查是否订阅了秒杀消息
  70. public function checkMiaoshaSubscribe(){
  71. $param = $this->request->param();
  72. $template_id = $param['template_id']??'';
  73. $ms_activity_id = $param['ms_activity_id']??0;
  74. if(empty($template_id)){
  75. return $this->renderError("缺少template_id");
  76. }
  77. $user = UserService::getCurrentLoginUser(false);
  78. $user_id = $user->user_id??0;
  79. if(empty($user_id)){
  80. $flag = false;
  81. }
  82. else{
  83. $key = (new WechatSub('miaosha'))->getMiaoShaKey($user->user_id,$template_id,$ms_activity_id);
  84. $rds = new Redis(config('cache.stores.redis'));
  85. $flag = false;
  86. if(!empty($user->open_id)){
  87. $v1 = $rds->get($key);
  88. if(!empty($v1)){
  89. $flag = true;
  90. }
  91. }
  92. }
  93. return $this->renderSuccess(compact('flag'));
  94. }
  95. //订阅
  96. public function subscribe(){
  97. $param = $this->request->param();
  98. $template_id = $param['template_id']??'';
  99. // $ms_activity_id = $param['ms_activity_id']??0;
  100. if(empty($template_id)){
  101. return $this->renderError("缺少template_id");
  102. }
  103. $user = UserService::getCurrentLoginUser(true);
  104. $key ='subscribe:open_id:'.$user->open_id.'template_id:'.$template_id;
  105. $key2 ='subscribe:user_id:'.$user->user_id.'template_id:'.$template_id;
  106. $rds = new Redis(config('cache.stores.redis'));
  107. if(!empty($user->open_id)){
  108. $rds->set($key,1,1000000);
  109. $rds->set($key2,1,1000000);
  110. }
  111. $value1 = $rds->get($key);
  112. $value2 = $rds->get($key2);
  113. $value = $value1.$value2;
  114. return $this->renderSuccess(compact('value'));
  115. }
  116. //取消订阅
  117. public function cancelSubscribe(){
  118. $param = $this->request->param();
  119. $template_id = $param['template_id']??'';
  120. if(empty($template_id)){
  121. return $this->renderError("缺少template_id");
  122. }
  123. $user = UserService::getCurrentLoginUser(true);
  124. $key ='subscribe:open_id:'.$user->open_id.'template_id:'.$template_id;
  125. $key2 ='subscribe:user_id:'.$user->user_id.'template_id:'.$template_id;
  126. $rds = new Redis(config('cache.stores.redis'));
  127. if(!empty($user->open_id)){
  128. $rds->del($key);
  129. $rds->del($key2);
  130. }
  131. return $this->renderSuccess();
  132. }
  133. //检查是否订阅了消息
  134. public function checkSubscribe(){
  135. $param = $this->request->param();
  136. $template_id = $param['template_id']??'';
  137. // $ms_activity_id = $param['ms_activity_id']??0;
  138. if(empty($template_id)){
  139. return $this->renderError("缺少template_id");
  140. }
  141. $user = UserService::getCurrentLoginUser(false);
  142. $user_id = $user->user_id??0;
  143. if(empty($user_id)){
  144. $flag = false;
  145. }
  146. else{
  147. $key ='subscribe:open_id:'.$user->open_id.'template_id:'.$template_id;
  148. $key2 ='subscribe:user_id:'.$user->user_id.'template_id:'.$template_id;
  149. // if($ms_activity_id>0){
  150. // $key.='ms_activity_id:'.$ms_activity_id;
  151. // $key2 = 'ms_activity_id:'.$ms_activity_id;
  152. // }
  153. $rds = new Redis(config('cache.stores.redis'));
  154. $flag = false;
  155. if(!empty($user->open_id)){
  156. $v1 = $rds->get($key);
  157. $v2 = $rds->get($key2);
  158. if(!empty($v1)||!empty($v2)){
  159. $flag = true;
  160. }
  161. }
  162. }
  163. return $this->renderSuccess(compact('flag'));
  164. }
  165. public function index()
  166. {
  167. // 自定义内容区
  168. $model = new CustomBlocks();
  169. $custom_blocks = $model->getList();
  170. return $this->renderSuccess(compact('custom_blocks'));
  171. }
  172. public function sms($mobile,$type = 0){
  173. $userinfo = UserService::getCurrentLoginUser(false);
  174. if($userinfo){
  175. $key = 'sms:user_id:'.$userinfo->user_id;
  176. $rds = new Redis(config('cache.stores.redis'));
  177. $num = $rds->get($key);
  178. if($num==5){
  179. return $this->renderError('你获取短信验证码的次数太多了,请稍后再试');
  180. }
  181. if($num==null){
  182. $num = 1;
  183. }else{
  184. $num++;
  185. }
  186. $rds->set($key,$num,300);
  187. }
  188. $data['mobile'] = $mobile;
  189. $data['user_id'] = $userinfo->user_id??0;
  190. $code = rand(100000,999999);
  191. $data['code'] = $code ;
  192. $data['type'] = $type;
  193. $content = '验证码:'.$data['code'].',您正在进行身份校验,5分钟内有效,请及时输入。';
  194. $smsCode = new SmsCode;
  195. $smsCode->save($data);
  196. PushLog::addSmsMon($content,$mobile,1);
  197. return $this->renderSuccess();
  198. }
  199. }