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;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值