订阅消息如果在网络请求后调用会出现 requestSubscribeMessage:fail can only be invoked by user TAP gesture.的情况
如果在网络请求前调用则不会出现这样的问题
模拟器在模拟的情况不会出现问题但在真机的情况下会出现问题
手机型号: iphone 8 Plus
运行系统: IOS 13.3.1
微信版本 :7.0.11
基础库版本 : 2.10.3
<view class="intro"> <button bindtap="testError" > 立即购买</button></view>
const app = getApp()
Page({
data: {
tmplIds: [所需要的小程序订阅消息ID]
},
onLoad: function () {
console.log('代码片段是一种迷你、可分享的小程序或小游戏项目,可用于分享小程序和小游戏的开发经验、展示组件和 API 的使用、复现开发问题和 Bug 等。可点击以下链接查看代码片段的详细文档:')
console.log('https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/devtools.html')
},
testError: function () {
var that = this;
that.data.tmplIds
ajaxOpration().then(() =>{
that.sendSubscribeMessage(that.data.tmplIds)
})
},
sendSubscribeMessage(tmplIds) {
return new Promise((resolve, reject) => {
if (!(tmplIds.length > 3)) {
const res = wx.getSystemInfoSync(),
platform = res.platform,
version = res.version.split(".")
console.log("当前设备" + platform + +"当前版本为:" + res.version);
if (platform == 'ios') {
if (version[2] >= 6) {
wx.requestSubscribeMessage({
tmplIds: tmplIds,
success(res) {
resolve(res);
},
fail(res) {
console.log(res.errMsg);
}
})
} else {
wx.requestSubscribeMessage({
tmplIds: [tmplIds[0]],
success(res) {
resolve(res);
},
fail(res) {
console.log(res.errMsg);
}
})
//tip.modal( "无法发送订阅消息, 请升级微信版本到7.0.6")
}
} else {
if (version[2] >= 7) {
wx.requestSubscribeMessage({
tmplIds: tmplIds,
success(res) {
resolve(res);
},
fail(res) {
console.log(res.errMsg);
}
})
} else {
wx.requestSubscribeMessage({
tmplIds: [tmplIds[0]],
success(res) {
resolve(res);
},
fail(res) {
console.log(res.errMsg);
}
})
}
}
} else {
tip.modal("模板Id的参数过多")
}
})
}
})
function ajaxOpration() {
return new Promise((resolve, reject) => {
//模拟网络请求
setTimeout(function () { resolve();}, 100);
})
}