WechatSub.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\api\model\subscribe;
  3. use app\common\model\Wechat;
  4. use think\cache\driver\Redis;
  5. use app\api\model\User as UserModel;
  6. use think\facade\Log;
  7. //微信订阅类
  8. class WechatSub {
  9. //订阅消息类型名称=>模板id,在这里配置好
  10. public $template_type_arr = [
  11. "li_ji_can_tuan" =>'AgOUjZCUMdRo0hwPgLrVSwztkrPys3FAq9OgL4waBaCI',//裂变拼团
  12. "li_ji_kai_tuan" =>'VG3VUaZKE4NkhSt9rn7xFhYlAjtopXACI1Y7HbIOf28',//裂变拼团
  13. "xia_dan_faqi_tuan"=>'M4Nw40B4Hpn4C8NkCiBDajEqz1St_CRRjGlkxDOVP1g',//裂变拼团
  14. "normal_li_ji_can_tuan"=>'M4Nw40B4Hpn4C8NkCiBDav26LNWbn29NF9Djzkx058I', //普通拼团 参团
  15. 'normal_xia_dan_faqi_tuan'=>'QfKtAZax5fw8oEgPbcky_bk2QD4Q4pwj60igE57jHnk', //下单发起拼团普通拼团 拼团
  16. 'kj_finish'=>'oNL877zWzgGRjiLz9UtdsamGG63-a6q2jz_q4MBYzaY', //砍价完成 订阅消息
  17. 'order'=>'quBeiNEzZ21zB6pwXT138NfMSbjlhvywAMYzbYM0zJU', //确认订单下单后订阅功能+
  18. 'miaosha'=>'DG72UoVMCncwZym2g0LP9-OKqp71ThdQs9wuare-L_w',
  19. ];
  20. public $template_id = '';
  21. public function __construct(string $template_type){
  22. $this->template_id = $this->template_type_arr[$template_type]??'';
  23. }
  24. //获取订阅缓存key
  25. private function getCacheKey($user_id,$template_id){
  26. $key ='subscribe:user_id:'.$user_id.'template_id:'.$template_id;
  27. return $key;
  28. }
  29. //秒杀订阅key
  30. public function getMiaoShaKey($user_id,$template_id,$extra_id){
  31. $key ='subscribe:user_id:'. $user_id.'template_id:'.$template_id.'extra_id:'.$extra_id;
  32. return $key;
  33. }
  34. //用户添加订阅
  35. public function addSub($user_id){
  36. $rds = new Redis(config('cache.stores.redis'));
  37. $key = $this->getCacheKey($user_id,$this->template_id);
  38. $num = $rds->set($key,1,100000);//写10000秒
  39. }
  40. //检查用户是否订阅
  41. public function checkSub($user_id){
  42. $rds = new Redis(config('cache.stores.redis'));
  43. $key = $this->getCacheKey($user_id,$this->template_id);
  44. $res = $rds->get($key);
  45. if($res){
  46. $flag = true;
  47. }else{
  48. $flag = false;
  49. }
  50. Log::error('user sub:the key flag:'.$flag);
  51. return $flag;
  52. }
  53. //检查key是否在缓存中
  54. public function checkKey($key){
  55. $rds = new Redis(config('cache.stores.redis'));
  56. $res = $rds->get($key);
  57. if($res){
  58. $flag = true;
  59. }else{
  60. $flag = false;
  61. }
  62. return $flag;
  63. }
  64. //取消用户订阅
  65. public function cancelSub($user_id){
  66. $rds = new Redis(config('cache.stores.redis'));
  67. $key = $this->getCacheKey($user_id,$this->template_id);
  68. $res = $rds->del($key);
  69. return true;
  70. }
  71. //推送
  72. //$page= 'pages/activity/pages/groupShopping/groupFission/groupFission
  73. //$data =['thing1'=>["value"=>"拼团有礼"],'thing3'=>["value"=>'活动已结束,去看看活动结果']];
  74. public function pushSub($user_id,$page,$data){
  75. $template_id = $this->template_id;
  76. if(!$this->checkSub($user_id)){
  77. return false;
  78. }
  79. $openid = UserModel::where('user_id',$user_id)->value('open_id')??'';
  80. $weixinToken = (new Wechat)->weixinTokenCache();
  81. $accessToken = $weixinToken['access_token'];
  82. $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $accessToken;
  83. $data['touser']= $openid;
  84. $data['template_id'] = $template_id;
  85. $data['page'] = $page;
  86. $data['miniprogram_state'] = env('APP_ENV')=="production"?'formal':'trial';
  87. $data['data']= $data;
  88. $res = post_curl($url,json_encode($data));
  89. return $res;
  90. }
  91. //推送
  92. //$page= 'pages/activity/pages/groupShopping/groupFission/groupFission
  93. //$data =['thing1'=>["value"=>"拼团有礼"],'thing3'=>["value"=>'活动已结束,去看看活动结果']];
  94. public function pushSubByKey($user_id,$page,$data,$key){
  95. $template_id = $this->template_id;
  96. if(!$this->checkKey($key)){
  97. return false;
  98. }
  99. $openid = UserModel::where('user_id',$user_id)->value('open_id')??'';
  100. $weixinToken = (new Wechat)->weixinTokenCache();
  101. $accessToken = $weixinToken['access_token'];
  102. $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $accessToken;
  103. $data['touser']= $openid;
  104. $data['template_id'] = $template_id;
  105. $data['page'] = $page;
  106. $data['miniprogram_state'] = env('APP_ENV')=="production"?'formal':'trial';
  107. $data['data']= $data;
  108. $res = post_curl($url,json_encode($data));
  109. return $res;
  110. }
  111. }
  112. ?>