Loading... ### 基本数据 ```js const dome = reactive({ tmplIds: ['bsvNf03z1LliuT4g51krszSOA3jBpBSCcMir'], // 模板id AppId: 'wxf90b3527413bae', // 小程序AppId AppSecret: '39615750caf43fb4bc8eb308ad836d08' // 小程序AppSecret }) ``` ### 小程序订阅消息 ```js // 订阅消息 const openAuth = async () => { // 请求微信订阅某个模板 const { errMsg } = await uni.requestSubscribeMessage({ tmplIds: dome.tmplIds }) // 获取jscode,方便后端获取openid const { code } = await wx.login({ provider: 'weixin' }) // 将已订阅消息发给后端,后端定时推送消息 const res = await api.subscribeLive({ js_code: code, msgTmpID: dome.tmplIds[0], liveId: 123 }) if( res.code != 1 ) return uni.showToast({ title: '订阅失败', duration: 2000, icon: 'error' }) uni.showToast({ title: '订阅成功', duration: 2000 }) } ``` ### 后端定时推送消息(参考接口) ```js const queryAccessToken = async () => { return await uni.request({ url: 'https://api.weixin.qq.com/cgi-bin/token', data: { appid: dome.AppId, secret: dome.AppSecret, grant_type: 'client_credential' } }) } const sendMsgs = async () => { const access_token = await queryAccessToken() const res = await uni.request({ url: 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=' + access_token, method: 'POST', data: { touser: uni.getStorageSync('theOpenId'), // 用户openid template_id: dome.tmplIds[0], // 第一个模板id page: "pkgMain/common/index/index", // 点击消息卡片跳转地址 data: { // data是模板内容,属性名为模板中所给,value值是需要传递的。 thing4: { value: '直播标题' }, thing1: { value: '直播简介' }, time3: { value: '直播时间' }, thing2: { value: '主播人' } } } }) console.log(res) } ``` 最后修改:2024 年 04 月 24 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏
1 条评论
每个标点都承载着思考的重量。