tp6+uniapp 微信小程序订阅消息

不多说,只为效果

获取模板id

uniapp

uni.requestSubscribeMessage({
    //此处填写刚才申请模板的模板ID
    tmplIds: ['模板ID'],
    success: function(res) {
        if(res['errMsg'] == 'requestSubscribeMessage:ok' )
            uni.showToast({
                title: '订阅成功',
                icon: "none",
                duration: 2000
            })
        },
        fail: function(res) {
            uni.showToast({
            title: '订阅失败',
            icon: "none",
            duration: 2000
        })
    },
})

tp6

    public function update_tv_push()
    {
            $set = set::find(1);//小程序信息
            //判断下是否为空
            if (!empty($set['set_appid']) && !empty($set['set_appsecret']) && !empty($set['set_sendMessage'])){
                $appid = $set['set_appid'];//小程序appid
                $appsecret = $set['set_appsecret'];//小程序appsecret
                $r = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret");
                $data = json_decode($r, true);
                $url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' . $data['access_token'];
                $data = array(
                    'touser' => 'openid', //发给谁openid
                    'template_id' => '模板id',
                    'page' => 'pages/index/进入地址?参数1=**&参数2=**',
                    'data' => array(
                        'thing2' => array(
                            'value' => '测试',
                        ),
                        'thing1' => array(
                            'value' => '10000' . '元',
                        )
                        // 这里如果有日期时间格式,'value'=>'2019-12-11 18:36
                    )
                );
                $data = json_encode($data);
                $result = $this->curl_post($url, $data);
                $result = json_decode($result);
                if ($result->errcode == '0' && $result->errmsg == 'ok') {
                    return json(['code' => 200, 'message' => '推送成功']);
                } else {
                    return json(['code' => 200, 'message' => $result]);
                }
            }else{
                return json(['code' => 200, 'message' => '小程序未设置,推送失败']);
            }
    }

    private function curl_post($url, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $response = curl_exec($ch);
        curl_close($ch);
        return $response;
    }

猜你喜欢

转载自blog.csdn.net/weixin_43453621/article/details/130584833