调用JS如下:
tuisong() {
console.log(this.data.rec)
var item = {
"time1": {
"value": this.dateFormat(this.data.rec[1].cdate)
},
"thing2": {
"value": this.data.rec[1].line
},
"thing3": {
"value": this.data.rec[1].postion
},
"thing4": {
"value": this.data.rec[1].status
}
}
wx.requestSubscribeMessage({
tmplIds: ['R88OQ7kzfTRYe0yFXbdtz_gtPQtlPGc-bt9V-TzQAZE'], //这里填入我们生成的模板id
success: res => {
if (res.errMsg === 'requestSubscribeMessage:ok') {
console.log('授权成功', res)
wx.cloud.callFunction({
name: 'getopenid',
}).then(res=>{
console.log('当前OPENID是',res.result.openid)
this.setData({
openid:res.result.openid
})
wx.cloud.callFunction({
name: 'send',
data: {
data: item,
openid: this.data.openid,
templateId: 'R88OQ7kzfTRYe0yFXbdtz_gtPQtlPGc-bt9V-TzQAZE',
}}).then(res=>{
console.log('订阅成功', res)
})
.catch(res=>{
console.log('订阅失败', res)
})
})
}
},
fail: res => {
console.log('授权失败', res)
}
})
},
云函数如下:
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
exports.main = async (event, context) => {
return cloud.openapi.subscribeMessage.send({
touser: event.openid,
page: 'pages/query/index/index',
data: event.data,
templateId: event.templateId,
})
};
错误提示:
订阅失败 Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID 063184d2-56ce-474d-83f2-2349b16d26f2, cloud function service error code -504002, error message TypeError: Do not know how to serialize a BigInt
at JSON.stringify (<anonymous>)
at callback (:19586/var/runtime/node12/CallbackContext.js:31)
at :19586/var/runtime/node12/CallbackContext.js:81
at :19586/var/runtime/node12/Runtime.engine.js:237
at processTicksAndRejections (:19586/appservice/internal/process/task_queues.js:97); at cloud.callFunction api;
at new t (WASubContext.js?t=wechat&s=1666316963773&v=2.14.1:17)
at u (WASubContext.js?t=wechat&s=1666316963773&v=2.14.1:17)
at l (WASubContext.js?t=wechat&s=1666316963773&v=2.14.1:17)
at Function.success (WASubContext.js?t=wechat&s=1666316963773&v=2.14.1:17)
at f (WASubContext.js?t=wechat&s=1666316963773&v=2.14.1:2)
at WASubContext.js?t=wechat&s=1666316963773&v=2.14.1:2
at f (WASubContext.js?t=wechat&s=1666316963773&v=2.14.1:26)
at Function.<anonymous> (WASubContext.js?t=wechat&s=1666316963773&v=2.14.1:26)
at f (WASubContext.js?t=wechat&s=1666316963773&v=2.14.1:2)
at WASubContext.js?t=wechat&s=1666316963773&v=2.14.1:2
wx.requestSubscribeMessage({
tmplIds: ['LQh_C30ykrNusZ1grLMzVakWiNn6BX_yStMquKCkXa8','HvGlhNaNy3r4Qpp-GtXNQUasb-6EcrH-cQHVcl2La7w'],
//在数组中填入刚刚复制的模版id
success(res) {
console.log(res)//我们可以输出res来看看是怎么样的(大家可以试试)
//然后我们判断用户是否点击了同意授权
if(res[ 'LQh_C30ykrNusZ1grLMzVakWiNn6BX_yStMquKCkXa8','HvGlhNaNy3r4Qpp-GtXNQUasb-6EcrH-cQHVcl2La7w']=='accept'){
wx.hideLoading(),
wx.showToast({
title: '您已订阅下次消息通知,记得及时查看哦!',
icon:"none"
})
}else{
//用户没有点击同意订阅
wx.showToast({
title: '为了更好的服务,请订阅一下哦',
icon:"none"
})
return;
}
}
});
这样试一下,每一步都把判断的参数打印试一下,确保能成功调起订阅,然后再调用其他方法或者函数
各位大神请帮忙看看,为啥老是订阅失败,但小程序又可以接收到订阅消息哦