微信推送订阅消息

微信获取模板id

服务类目只支持一次性订阅 长期订阅可参考官网文档

模板id

#前端调起客户端小程序订阅消息界面
官网文档
官网

    wx.requestSubscribeMessage({
    
    
            tmplIds: ['对应的'], //模板id
            success(res){
    
    
              wx.request({
    
    
                url: app.globalData.base_url+'index/wx_login/sendMsg', // 后台写的发送订阅消息接口
                data: {
    
    
                  "openId":"接收人", //发送人的openid
                  "page":'index', //跳转的指定页面,不指定无跳转
                },
                method: 'POST',
                header: {
    
    
                  'content-type': 'application/json'
                },
                success:(res)=>{
    
    
                    console.log("sendMSG",res);
                }
              })
            },
            fail(res){
    
    
              console.log("推送失败",res);
         
            }
          })

后端接收发送订阅内容

/**
*  获取AccessToken
**/
public function getAccessToken(){
    
    
        $appid = $this->config['appid'];
        $appsecret = $this->config['secret'];
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
        $html = file_get_contents($url);
        $output = json_decode($html, true);
        $access_token = $output['access_token'];
        return $access_token;
    }

/**
     * 发送订阅消息
     */
    public function sendMsg(){
    
    
        $openid = input("openId", '', 'htmlspecialchars_decode');
        $page = input("page", '', 'htmlspecialchars_decode');
        $wxHelper = new WXLoginHelper();
        $access_token = $wxHelper->getAccessToken();
        $header_url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $access_token;
        $nowTime=date('Y-m-d H:i:s',time());
        $params_post = [
            'touser' => $openid,
            'template_id' => '模板id',
            'miniprogram_state' => 'developer',
            'page' => $page,//点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
            //根据模板添加  value对应字符串 不然会报错非法参数
            'data' => [
                'thing1' => [
                    'value' => '您有新的单据需要进行处理'
                ],
                'name2' => [
                    'value' => 'test'
                ],
                'date3' => [
                    'value' => ''.$nowTime
                ],
                'number4' => [
                    'value' => '1'
                ],
            ]
        ];

        $post = $this->curl_form(json_encode($params_post), $header_url);
        $result=[
            "openId"=>$openid,
            "page"=>$page,
            "token"=>$access_token,
            "post"=>$post
        ];
        echo json_encode($result);
    }

    //请求网络接口
    public function curl_form($post_data, $url)
    {
    
    
        $ch = curl_init();
        //设置变量
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//执行结果是否被返回,0是返回,1是不返回
        curl_setopt($ch, CURLOPT_HEADER, 0);//参数设置,是否显示头部信息,1为显示,0为不显示
        //表单数据,是正规的表单设置值为非0
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);//设置curl执行超时时间最大是多少
        //使用数组提供post数据时,CURL组件大概是为了兼容@filename这种上传文件的写法,
        //默认把content_type设为了multipart/form-data。虽然对于大多数web服务器并
        //没有影响,但是还是有少部分服务器不兼容。本文得出的结论是,在没有需要上传文件的
        //情况下,尽量对post提交的数据进行http_build_query,然后发送出去,能实现更好的兼容性,更小的请求数据包。
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $response = curl_exec($ch);
        //    释放cURL句柄
        return $response;
    }

#效果
效果1
效果2
效果3

猜你喜欢

转载自blog.csdn.net/Depressiom/article/details/125913147