0订阅消息流程.png
import { getSubscribeMessageTemplate } from "@/api"
import fetch from "./fetch"
import cfg from "../../appIdInfo";
export const MessageCode = {
activity_check_notify: 'activity_check_notify'
}
export const SubscribeMessageResultStatus = {
/**。'accept'表示用户同意订阅该条id对应的模板消息 */
accept: 'accept',
/**'reject'表示用户拒绝订阅该条id对应的模板消息 */
reject: 'reject',
/** 'ban'表示已被后台封禁 */
ban: 'ban',
/** 'filter'表示该模板因为模板标题同名被后台过滤 */
filter: 'filter',
}
const subscribeMessageSetResultType = {
/**主开关关闭 +对应订阅消息的关闭 */
alwaysReject: 'reject',
alwaysAccept: 'accept',
unSet: 'unSet',
}
/**消息模板ID */
let msgTemplateId = ''
/**查询订阅设置的结果 存在这个变量 */
let subscribeMessageSetResult = '';
/**获取订阅消息模板ID */
const getMsgTemplate = async (code = MessageCode.activity_check_notify) => {
if (msgTemplateId) {
return
}
const res = await fetch.get(getSubscribeMessageTemplate, { code: code, appId: cfg[process.env.iid].appId })
msgTemplateId = res.map(item => item.priTmplId)[0];
}
/**是否已经初始化 */
let isInited = false;
const getSubscribeMessageSet = async () => {
const res = await wx.getSetting({
withSubscriptions: true
})
console.log('用户的订阅设置', res)
const subscriptionsSetting = res.subscriptionsSetting
subscribeMessageSetResult = subscribeMessageSetResultType.unSet;
if (!subscriptionsSetting) {
subscribeMessageSetResult = subscribeMessageSetResultType.unSet;
return
}
if (!subscriptionsSetting.mainSwitch) {
subscribeMessageSetResult = subscribeMessageSetResultType.alwaysReject
return
}
const itemSettings = subscriptionsSetting.itemSettings
if (itemSettings && itemSettings[msgTemplateId] == SubscribeMessageResultStatus.accept) {
subscribeMessageSetResult = subscribeMessageSetResultType.alwaysAccept
} else if (itemSettings && [SubscribeMessageResultStatus.reject, SubscribeMessageResultStatus.ban].includes(itemSettings[msgTemplateId])) {
subscribeMessageSetResult = subscribeMessageSetResultType.alwaysReject
}
}
/**初始化订阅 */
export const iniSubscribeMessage = async () => {
if (isInited) {
return
}
isInited = true
await getMsgTemplate()
await getSubscribeMessageSet()
console.log('初始化订阅 -用户的订阅设置是' + subscribeMessageSetResult)
console.log('获取到的模板id=' + msgTemplateId)
}
const callMsgDlg =async () => {
const templates = [msgTemplateId];
const mesRes = await wx.requestSubscribeMessage({
tmplIds: templates
})
console.log('订阅结果', mesRes)
//分析订阅结果 全部成功才叫订阅成功
const successResults = templates.filter(id => {
return (mesRes[id] == SubscribeMessageResultStatus.accept)
});
return successResults.length == templates.length;
}
/**
* 订阅消息 返回订阅结果 true订阅成功 false等于订阅失败
* @param {*} code
* @returns
*/
export const subscribeMessage = async () => {
if (!msgTemplateId) {
//订阅模板消息id还不存在 请在调用订阅时提前获取模板 调用这个获取模板 getMsgTemplate
return false
}
try {
let ret = ''
//订阅设置结果是总是拒绝时 就要打开设置
if (subscribeMessageSetResult === subscribeMessageSetResultType.alwaysReject) {
const settingResult = await wx.openSetting({ withSubscriptions: true })
console.log('打开设置弹框后的结果', settingResult)
const set = settingResult.subscriptionsSetting;
if (set && set.mainSwitch && set.itemSettings && set.itemSettings[msgTemplateId] == SubscribeMessageResultStatus.accept) {
//打开设置后获得用户授权 不需要再调用发起订阅api
console.log('通过打开用户设置页面 获得了用户订阅授权')
ret=true
} else {
ret=false
wx.showToast({
title: '消息订阅失败',
icon:'none'
})
}
} else {
ret=await callMsgDlg()
}
//这里用户订阅操作后 再次查询订阅设置 是为下次是否要打开设置做判断
getSubscribeMessageSet(true).then(() => {
console.log('操作订阅后的用户订阅设置是', subscribeMessageSetResult == subscribeMessageSetResultType.alwaysReject ? '拒绝' : '容许')
})
return ret;
} catch (error) {
console.log('订阅消息-失败', error)
return false
}
}
网友评论