方法:
/**
* 订阅消息
*/
export default function getSubscribeMessage(tmplId: string) {
return new Promise((resolve, reject) => {
// 获取配置
wx.getSetting({
withSubscriptions: true,
success: (res: any) => {
if (res.errMsg !== 'getSetting:ok') {
return reject(new Error('reject'))
}
const subscriptionsSetting = res.subscriptionsSetting
if (!subscriptionsSetting.mainSwitch) {
return reject(new Error('reject'))
}
const result = subscriptionsSetting.itemSettings && subscriptionsSetting.itemSettings[tmplId]
if (result) {
return resolve(result)
}
// 弹出授权
wx.requestSubscribeMessage({
tmplIds: [tmplId],
success(res: any) {
if (res.errMsg === 'requestSubscribeMessage:ok') {
resolve(res[tmplId])
}
return reject(new Error('reject'))
},
fail() {
return reject(new Error('reject'))
}
})
},
fail: () => {
return reject(new Error('reject'))
}
})
})
}