小程序订阅消息开发
2020-08-27 本文已影响0人
小李不小
如何调起客户端小程序订阅消息界面,返回用户订阅消息的操作结果。
tmplIds: [id1,id2]是模板消息-数组格式,没有的话,可以去小程序微信公众号后台-订阅消息-我的模板里面去创建。
wx.requestSubscribeMessage({
tmplIds: ['0CgtNJBaSmrqp0RildekEy2_YX7r730NK_NbVwA2c-U','gsXEP6txpuZM6FPm0yZotznqIV4TO-8G0KN-QMwJNWA'],
success (res) {
console.log('res----',res)
if(res.errMsg=='requestSubscribeMessage:ok'){
console.log('订阅成功走实名流程',)
}
},
fail(err){
wx.showToast({
title: `订阅失败${errtext || ''}`,
icon: 'none',
duration: 2000
})
}
})
效果图:这是长期订阅的

注意:拒绝之后,就无法拉起(不做处理),取消之后,可以再次拉起
-----xhtml
<button class='btn' bindtap='submit' >
提交
</button>
<view class='show-tips'>
<van-popup show="{{ showCodeTips }}" bind:close="tipsClose">
<view class='tips-title'>请求授权</view>
<view class='tips-code-text'>需要开启您的订阅消息,请打开设置</view>
<view class='tips-btn'>
<view bindtap='tipsCodeClose' class='btn-clor wait-see'>取消</view>
<view class='btn-clor'>
<button class='allow-btn' bindtap="openSetting">确定</button>
</view>
</view>
</van-popup>
</view>
------js
submit(){
const that=this;
const id='模板id';
const idtwo='模板id';
wx.getSetting({
withSubscriptions: true,
success (res) {
const itemSettings=res.subscriptionsSetting.itemSettings;
console.log('subscriptionsSetting---',res.subscriptionsSetting)
if(res.subscriptionsSetting.mainSwitch == false){ //未开启订阅,去弹窗提示开启订阅
that.setData({
showCodeTips:true
})
}else if(itemSettings != undefined){//已经订阅过了
if(res.subscriptionsSetting.mainSwitch == true &&
(itemSettings[id]=="accept" || itemSettings[idtwo]=="accept")){
//已经开启订阅和开启接收订阅消息
console.log('已经开启了接收订阅和订阅过了,')
}else if(
res.subscriptionsSetting.mainSwitch == true &&
(itemSettings[id]=="reject" && itemSettings[idtwo]=="reject")
){
//开启订阅,拒绝订阅授权
console.log('开启订阅,拒绝订阅授权')
}
}else{ //没有订阅过任何长期消息
console.log('拉起消息接收订阅',res)
//已经开启了消息接收订阅,直接弹窗订阅选择授权
wx.requestSubscribeMessage({
tmplIds: ['id1','idtwo'],
success (res) {
console.log('res----',res)
if(res.errMsg=='requestSubscribeMessage:ok'){
console.log('',)
}
},
fail(err){
let errtext;
if(err.errCode){
errtext=`失败状态码:${err.errCode}`
}
wx.showToast({
title: `订阅失败,请联系管理员,${errtext || ''}`,
icon: 'none',
duration: 2000
})
console.log('err---',err)
console.log('订阅成功走实名流程',)
}
})
}
}
})
},
如何判断当前是否开了接收消息 ,和用户已经订阅过了就,就需要用到getSetting 必须带 withSubscriptions:true属性
1 mainSwitch: true, // 订阅消息总开关 (用户在设置中已经开了订阅就为true,反之未false)
1 下面图片 mainSwitch: true 的状态

2 下面图片 mainSwitch: false的状态

SYS_MSG_TYPE_INTERACTIVE: 'accept' 订阅成功返回状态
zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: 'reject', // 拒绝订阅放回的状态
ke_OZC_66gZxALLcsuI7ilCJSP2OJ2vWo2ooUPpkWrw: 'ban', 后台禁用返回的状态
wx.getSetting({
withSubscriptions: true,
success (res) {
console.log(res.authSetting)
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
console.log(res.subscriptionsSetting)
// res.subscriptionsSetting = {
// mainSwitch: true, // 订阅消息总开关
// itemSettings: { // 每一项开关
// SYS_MSG_TYPE_INTERACTIVE: 'accept', // 小游戏系统订阅消息
// SYS_MSG_TYPE_RANK: 'accept'
// zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: 'reject', // 普通一次性订阅消息
// ke_OZC_66gZxALLcsuI7ilCJSP2OJ2vWo2ooUPpkWrw: 'ban',
// }
// }
}
})