当前位置: 代码网 > it编程>编程语言>Php > ThinkPhP5整合微信小程序订阅消息实用代码

ThinkPhP5整合微信小程序订阅消息实用代码

2024年05月18日 Php 我要评论
前端demowxml<view clas="index" style="background: white"> <button bindtap="clickss">触发一下&

前端demo

wxml

<view clas="index" style="background: white">
  <button bindtap="clickss">触发一下</button>
</view>

wxjs

clickss:function (){
    const templateid = 'rzm7nzon5p2sa1m6aeipmmz-90u1_8pfmjrv_nf5bhk'; // 订阅消息模版id
    wx.requestsubscribemessage({
      tmplids: [templateid],
      success(res) {
        if (res[templateid] == 'accept') {
          //用户同意了订阅,允许订阅消息
          wx.showtoast({
            title: '订阅成功'
          })
        } else {
          //用户拒绝了订阅,禁用订阅消息
          wx.showtoast({
            title: '订阅失败'
          })
        }
      },
      fail(res) {
        console.log('ooooooooooooooo', res)
      },
      complete(res) {
        console.log(res)
      }
    })
  },

后端推送代码

namespace app\api\controller;
use app\common\controller\api;
use fast\http;
use think\cache;
class subtomsg extends api
{
    protected $noneedlogin = ['*'];
    protected $noneedright = ['*'];
    /**
     * @return mixed
     * 获取access_token
     * 时效3600秒
     */
    public function getaccess_tonken()
    {
        if (cache::has('token')) {
            return cache::get('token');
        } else {
            $appid = '';
            $appsecret = '';
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
            //api接口
            $content = http::get($url);
            if ($content) {
                $content = json_decode($content, true);
            }
            if (!empty($content['access_token'])) {
                session('access_token', $content['access_token']);
                cache::set('token', $content['access_token'], 3600);
                return $content['access_token'];
            }
        }
    }
    /**
     * @param $openid 目标用户openid
     * @param $send 发送数据串
     */
    public function send($send)
    {
        /* $send格式demo
        $openid = 'o9vhc5bxmc1ptnczd1iwvuy4jup0';//目标用户的openid
        $templateid = 'laohzjvj5j9tz97jom4fnlmud2bkwv-g5dputyswuya';
        $data = array(
            'phrase1' => array('value' => '刘洋'),
            'time2' => array('value' => date('y-m-d h:i:s', time())),
            'thing3' => array('value' => "老王很棒的哟", 'color' => '#333333'),
        );
        $send = array(
            "touser" => $openid,
            "template_id" => $templateid,
            "data" => $data,
        );*/
        $access_token = self::getaccess_tonken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $access_token;
        $ret = http::post($url, urldecode(json_encode($send)));
        $this->success('', json_decode($ret));
        /*
         * 发送成功
         * {
            "code": 1,
            "msg": "",
            "time": "1619316449",
            "data": {
                "errcode": 0,
                "errmsg": "ok",
                "msgid": 1840866241775747074
            }
        }*/
        /*
         * 失败的情况
         * {
            "code": 1,
            "msg": "",
            "time": "1619316019",
            "data": {
                "errcode": 43101,
                "errmsg": "user refuse to accept the msg rid: 6084cd33-3bd5fcdb-4ed3439f"
            }
        }*/
    }
	//用于测试
    public function send_text()
    {
        $openid = 'o9vhc5bxmc1ptnczd1iwvuy4jup0';//目标用户的openid
        // $templateid="";//订阅消息的模板id
        $templateid = 'laohzjvj5j9tz97jom4fnlmud2bkwv-g5dputyswuya';
//        $templateid = get_addon_config('dymsg')['noticetemplateid'];
        $data = array(
            'phrase1' => array('value' => '刘洋'),
            'time2' => array('value' => date('y-m-d h:i:s', time())),
            'thing3' => array('value' => "老刘很棒的哟", 'color' => '#333333'),
        );
        $access_token = self::getaccess_tonken();
        $url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" . $access_token;
        $send = array(
            "touser" => $openid,
            "template_id" => $templateid,
            "data" => $data,
        );
        $ret = http::post($url, urldecode(json_encode($send)));
        $this->success('', json_decode($ret));
        /*
         * 发送成功
         * {
            "code": 1,
            "msg": "",
            "time": "1619316449",
            "data": {
                "errcode": 0,
                "errmsg": "ok",
                "msgid": 1840866241775747074
            }
        }*/
        /*
         * 失败的情况
         * {
            "code": 1,
            "msg": "",
            "time": "1619316019",
            "data": {
                "errcode": 43101,
                "errmsg": "user refuse to accept the msg rid: 6084cd33-3bd5fcdb-4ed3439f"
            }
        }*/
    }
}

到此这篇关于thinkphp5整合微信小程序订阅消息实用代码的文章就介绍到这了,更多相关thinkphp5整合微信小程序订阅消息内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com