小程序私密消息:authPrivateMessage验证,valid总是false,多手机结果不一致
依照文档实现私密消息:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/share/private-message.html
- 云函数+云调用
云端代码片段:
async function createActivityId (event){
try {
const result = await cloud.openapi.updatableMessage.createActivityId({
openid: cloud.getWXContext().OPENID
})
// result 结构
// { errCode: 0, errMsg: 'openapi.templateMessage.send:ok' }
return result
} catch (err) {
// 错误处理
// err.errCode !== 0
throw err
}
}
云函数日志:
2. 页面onLoad中获取activityId,并updateShareMenu
onLoad: function (options) {
this.setShareTicket()
},
setShareTicket: async function () {
const activityId = await createActivityId()
console.log(activityId)
if (activityId) {
wx.updateShareMenu({
withShareTicket: true,
isPrivateMessage: true,
activityId: activityId
})
}
},
onShareAppMessage() {
const { openId, userInfo } = this.data
return {
title: userInfo.nickName,
imageUrl: '/images/wx_share.jpeg',
path: `/pages/other/index?openId=${openId}`
}
},
wx.updateShareMenu({
withShareTicket: true,
isPrivateMessage: true,
activityId: 'xxx',
})
3.app.js的onShow中authPrivateMessage
App({
globalData: {},
onLaunch(options) {
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力')
} else {
wx.cloud.init({
env: 'prod',
traceUser: true,
})
}
},
onShow(options) {
const { shareTicket,referrerInfo } = options
console.log(shareTicket,referrerInfo)
if(shareTicket){
this.authPrivate(shareTicket)
}
},
authPrivate:function(shareTicket,callback){
wx.authPrivateMessage({
shareTicket: shareTicket,
success(res) {
console.log('authPrivateMessage success', res)
// res
// {
// errMsg: 'authPrivateMessage:ok'
// valid: true
// iv: 'xxxx',
// encryptedData: 'xxxxxx'
// }
},
fail(res) {
console.log('authPrivateMessage fail', res)
}
})
}
})
4.分别在Android和IOS测试
Android结果:
authPrivateMessage success
Object {
encryptedData: "<Undefined>"
errMsg: "authPrivateMessage:ok"
iv: "<Undefined>"
valid: false
}
IOS结果:
authPrivateMessage fail
Object {
errMsg: "authPrivateMessage:fail data unknow error code"
err_code: "1"
}
Android system info:
System: Android 9, WeChat 8.0.1.1841
Network: wifi
UA: Mozilla/5.0 (Linux; Android 9; HWI-AL00 Build/HUAWEIHWI-AL00; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/78.0.3904.62 XWEB/2759 MMWEBSDK/201201 Mobile Safari/537.36 MMWEBID/2806 MicroMessenger/8.0.1.1841(0x2800015D) Process/appbrand0 WeChat/arm64 Weixin NetType/WIFI Language/zh_CN ABI/arm64 MiniProgramEnv/android
WeChatLib: 2.16.0 (2021.3.10 13:24:55)
Android 关于手机
IOS system info:(无法复制,就用图片吧)