wx.requestSubscribeMessage({
tmplIds: ['M8QtLtZ0jQzJDmNSoTVAeZg-Xw02UClF7Wf0cCSeqpY'],
async success (res) {
wx.showLoading({
title: '保存中...',
});
if (res['M8QtLtZ0jQzJDmNSoTVAeZg-Xw02UClF7Wf0cCSeqpY'] === 'accept') {
await dinnerApi.create({ foodids, kitchenid, date, sendMsg: true })
} else {
await dinnerApi.create({ foodids, kitchenid, date, sendMsg: false })
}
wx.hideLoading();
wx.showToast({
title: '保存成功',
icon: 'success'
})
wx.switchTab({
url: '/pages/index/index'
})
}
});
for (const user of users) {
const toUserOpenid = user.openid
const sendRes = await sendSubscribeMessage(toUserOpenid, foods, ingredients, remarks)
console.log('发送消息:',toUserOpenid,'----',sendRes)
}
async function sendSubscribeMessage(toUserOpenid, foods, ingredients, remarks) {
const accessToken = await getAccessToken();
const url = `https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${accessToken}`;
const messageData = {
touser: toUserOpenid,
template_id: 'M8QtLtZ0jQzJDmNSoTVAeZg-Xw02UClF7Wf0cCSeqpY',
page: '/pages/index/index',
data: {
thing1: {
value: formatString(foods)
},
thing2: {
value: formatString(ingredients)
},
thing4: {
value: remarks
}
}
};
const response = await axios.post(url, messageData);
return response.data;
}
首先, /cgi-bin/message/subscribe/send 接口是小程序订阅消息接口,并不是服务号模板消息接口。
其次,哪个openid订阅的只能下发那个openid,不支持A用户订阅,下发给B用户。
一次性订阅消息订阅1次可下发1次,次数可以累计。