123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- <?php
-
- namespace app\api\model\subscribe;
- use app\common\model\Wechat;
- use think\cache\driver\Redis;
- use app\api\model\User as UserModel;
- use think\facade\Log;
- //微信订阅类
- class WechatSub {
- //订阅消息类型名称=>模板id,在这里配置好
- public $template_type_arr = [
- "li_ji_can_tuan" =>'AgOUjZCUMdRo0hwPgLrVSwztkrPys3FAq9OgL4waBaCI',//裂变拼团
- "li_ji_kai_tuan" =>'VG3VUaZKE4NkhSt9rn7xFhYlAjtopXACI1Y7HbIOf28',//裂变拼团
- "xia_dan_faqi_tuan"=>'M4Nw40B4Hpn4C8NkCiBDajEqz1St_CRRjGlkxDOVP1g',//裂变拼团
- "normal_li_ji_can_tuan"=>'M4Nw40B4Hpn4C8NkCiBDav26LNWbn29NF9Djzkx058I', //普通拼团 参团
- 'normal_xia_dan_faqi_tuan'=>'QfKtAZax5fw8oEgPbcky_bk2QD4Q4pwj60igE57jHnk', //下单发起拼团普通拼团 拼团
- 'kj_finish'=>'oNL877zWzgGRjiLz9UtdsamGG63-a6q2jz_q4MBYzaY', //砍价完成 订阅消息
- 'order'=>'quBeiNEzZ21zB6pwXT138NfMSbjlhvywAMYzbYM0zJU', //确认订单下单后订阅功能+
- 'miaosha'=>'DG72UoVMCncwZym2g0LP9-OKqp71ThdQs9wuare-L_w',
- ];
- public $template_id = '';
-
- public function __construct(string $template_type){
- $this->template_id = $this->template_type_arr[$template_type]??'';
- }
- //获取订阅缓存key
- private function getCacheKey($user_id,$template_id){
- $key ='subscribe:user_id:'.$user_id.'template_id:'.$template_id;
- return $key;
- }
- //秒杀订阅key
- public function getMiaoShaKey($user_id,$template_id,$extra_id){
- $key ='subscribe:user_id:'. $user_id.'template_id:'.$template_id.'extra_id:'.$extra_id;
- return $key;
- }
- //用户添加订阅
- public function addSub($user_id){
- $rds = new Redis(config('cache.stores.redis'));
- $key = $this->getCacheKey($user_id,$this->template_id);
- $num = $rds->set($key,1,100000);//写10000秒
- }
- //检查用户是否订阅
- public function checkSub($user_id){
- $rds = new Redis(config('cache.stores.redis'));
- $key = $this->getCacheKey($user_id,$this->template_id);
- $res = $rds->get($key);
- if($res){
- $flag = true;
- }else{
- $flag = false;
- }
- Log::error('user sub:the key flag:'.$flag);
- return $flag;
- }
- //检查key是否在缓存中
- public function checkKey($key){
- $rds = new Redis(config('cache.stores.redis'));
- $res = $rds->get($key);
- if($res){
- $flag = true;
- }else{
- $flag = false;
- }
- return $flag;
- }
- //取消用户订阅
- public function cancelSub($user_id){
- $rds = new Redis(config('cache.stores.redis'));
- $key = $this->getCacheKey($user_id,$this->template_id);
- $res = $rds->del($key);
- return true;
- }
- //推送
- //$page= 'pages/activity/pages/groupShopping/groupFission/groupFission
- //$data =['thing1'=>["value"=>"拼团有礼"],'thing3'=>["value"=>'活动已结束,去看看活动结果']];
- public function pushSub($user_id,$page,$data){
- $template_id = $this->template_id;
- if(!$this->checkSub($user_id)){
- return false;
- }
- $openid = UserModel::where('user_id',$user_id)->value('open_id')??'';
- $weixinToken = (new Wechat)->weixinTokenCache();
-
- $accessToken = $weixinToken['access_token'];
- $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $accessToken;
- $data['touser']= $openid;
- $data['template_id'] = $template_id;
- $data['page'] = $page;
- $data['miniprogram_state'] = env('APP_ENV')=="production"?'formal':'trial';
- $data['data']= $data;
- $res = post_curl($url,json_encode($data));
- return $res;
- }
- //推送
- //$page= 'pages/activity/pages/groupShopping/groupFission/groupFission
- //$data =['thing1'=>["value"=>"拼团有礼"],'thing3'=>["value"=>'活动已结束,去看看活动结果']];
- public function pushSubByKey($user_id,$page,$data,$key){
- $template_id = $this->template_id;
- if(!$this->checkKey($key)){
- return false;
- }
- $openid = UserModel::where('user_id',$user_id)->value('open_id')??'';
- $weixinToken = (new Wechat)->weixinTokenCache();
-
- $accessToken = $weixinToken['access_token'];
- $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $accessToken;
- $data['touser']= $openid;
- $data['template_id'] = $template_id;
- $data['page'] = $page;
- $data['miniprogram_state'] = env('APP_ENV')=="production"?'formal':'trial';
- $data['data']= $data;
- $res = post_curl($url,json_encode($data));
- return $res;
- }
-
- }
- ?>
|