没返回状态
import { templateId } from '@/config';
/**
* 主函数:检查订阅消息权限并请求订阅
* @param {Function} callback 回调函数
*/
export const callbackSubscribe = (callback) => {
checkSubscribePermission(callback);
};
/**
* 检查订阅消息权限
* @param {Function} callback 回调函数
*/
const checkSubscribePermission = (callback) => {
uni.getSetting({
withSubscriptions: true,
success: (res) => {
const subscriptionSetting = res.subscriptionsSetting || {};
console.log("检查订阅消息权限", subscriptionSetting)
if (subscriptionSetting.mainSwitch === true) {
if (subscriptionSetting.itemSettings && subscriptionSetting.itemSettings[templateId] !== 'accept') {
showSettingTip();
} else {
requestSubscribeMessageCall(callback);
}
} else {
// 用户未开启订阅消息总开关,提示用户开启
uni.showModal({
title: '提示',
content: '您未开启订阅消息权限,是否前往设置开启?',
success: (modalRes) => {
if (modalRes.confirm) {
uni.openSetting({
success: (settingRes) => {
console.log('未开启订阅设置页面打开成功', settingRes);
},
fail: (err) => {
console.error('未开启订阅打开设置页面失败', err);
}
});
}
}
});
}
},
fail: (err) => {
console.error('检查订阅消息权限失败', err);
}
});
};
/**
* 请求订阅消息
* @param {Function} callback 回调函数
*/
const requestSubscribeMessageCall = (callback) => {
uni.requestSubscribeMessage({
tmplIds: [templateId], // 需要订阅的模板ID
success: (subscriptionRes) => {
console.log("请求订阅消息", subscriptionRes)
if (subscriptionRes[templateId] === 'accept') {
// 用户同意订阅
callback && callback();
} else {
// 用户拒绝订阅
console.log('用户拒绝了订阅');
// 提示用户前往设置页面重新授权
}
},
fail: (err) => {
console.error('订阅失败:', err);
}
});
}
/**
* 提示用户前往设置页面重新授权
* @param {Function} callback 回调函数
*/
const showSettingTip = (callback) => {
uni.showModal({
title: '提示',
content: '您拒绝了订阅消息,是否前往设置页面重新授权?',
success: (res) => {
console.log("提示用户前往设置页面重新授权", res);
if (res.confirm) {
// 用户点击确认,打开设置页面
uni.openSetting({
success: (settingRes) => {
console.log('设置页面打开成功', settingRes);
},
fail: (err) => {
console.error('打开设置页面失败', err);
}
});
}
}
});
};
有返回状态
import { templateId } from '@/config';
/**
* 主函数:检查订阅消息权限并请求订阅
* @param {Function} callback 回调函数, 接收一个布尔参数表示是否成功订阅
*/
export const callbackSubscribe = (callback) => {
checkAndRequestSubscribe(callback);
};
/**
* 检查订阅消息权限并请求订阅
* @param {Function} callback 回调函数, 接收一个布尔参数表示是否成功订阅
*/
const checkAndRequestSubscribe = async (callback) => {
try {
const settingResult = await getSettingWithSubscriptions();
const subscriptionSetting = settingResult.subscriptionsSetting || {};
if (!subscriptionSetting.mainSwitch) {
// 用户未开启订阅消息总开关,提示用户开启
await promptToEnableMainSwitch(callback);
return;
}
if (subscriptionSetting.itemSettings && subscriptionSetting.itemSettings[templateId] !== 'accept') {
// 提示用户前往设置页面重新授权
await promptToReauthorize(callback);
return;
}
// 请求订阅消息
const subscribed = await requestSubscribeMessage();
callback(subscribed);
} catch (error) {
console.error('处理订阅过程中发生错误:', error);
callback(false);
}
};
/**
* 获取设置信息(包含订阅状态)
* @returns {Promise<Object>} 设置信息
*/
const getSettingWithSubscriptions = () => new Promise((resolve, reject) => {
uni.getSetting({
withSubscriptions: true,
success: resolve,
fail: reject
});
});
/**
* 提示用户开启订阅消息总开关
* @param {Function} callback 回调函数, 接收一个布尔参数表示是否成功订阅
*/
const promptToEnableMainSwitch = (callback) => new Promise((resolve) => {
uni.showModal({
title: '提示',
content: '您未开启订阅消息权限,是否前往设置开启?',
success: (modalRes) => {
if (modalRes.confirm) {
openSetting().then(() => resolve());
} else {
callback(false);
}
}
});
});
/**
* 提示用户前往设置页面重新授权
* @param {Function} callback 回调函数, 接收一个布尔参数表示是否成功订阅
*/
const promptToReauthorize = (callback) => new Promise((resolve) => {
uni.showModal({
title: '提示',
content: '您拒绝了订阅消息,是否前往设置页面重新授权?',
success: (res) => {
if (res.confirm) {
openSetting().then(() => resolve());
} else {
callback(false);
}
}
});
});
/**
* 打开设置页面
* @returns {Promise<void>}
*/
const openSetting = () => new Promise((resolve, reject) => {
uni.openSetting({
success: (settingRes) => {
console.log('设置页面打开成功', settingRes);
resolve();
},
fail: (err) => {
console.error('打开设置页面失败', err);
reject(err);
}
});
});
/**
* 请求订阅消息
* @returns {Promise<boolean>} 是否成功订阅
*/
const requestSubscribeMessage = () => new Promise((resolve) => {
uni.requestSubscribeMessage({
tmplIds: [templateId], // 需要订阅的模板ID
success: (subscriptionRes) => {
console.log("请求订阅消息", subscriptionRes);
resolve(subscriptionRes[templateId] === 'accept');
},
fail: (err) => {
console.error('订阅失败:', err);
resolve(false);
}
});
});
微信小程序发送订阅推送
https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-message-management/subscribe-message/sendMessage.html
请求
// 发送数据示例 数据示例
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV,
})
exports.main = async (event, context) => {
try {
const result = await cloud.openapi.subscribeMessage.send({
"touser": 'OPENID',
"page": 'index',
"lang": 'zh_CN',
"data": {
"number01": {
"value": '339208499'
},
"date01": {
"value": '2015年01月05日'
},
"site01": {
"value": 'TIT创意园'
},
"site02": {
"value": '广州市新港中路397号'
}
},
"templateId": 'TEMPLATE_ID',
"miniprogramState": 'developer' // 开发版本
})
return result
} catch (err) {
return err
}
}
返回数据示例
{
"errcode":0,
"errmsg":"ok"
}