1.特殊场景:环境共享的小程序可以正常获取手机号,但是享受环境共享的小程序,操作失败了
// 捕获异常:
{
"errorCode":1,
"errorMessage":"user code exception caught",
"stackTrace":"Error: errCode: 40013 | errMsg: openapi.phonenumber.getPhoneNumber:fail invalid appid hint: [nIOdJHnre-trsqYa] rid: 我不晓得这个东西要保密不\n at callWXOpenAPI (/var/user/node_modules/wx-server-sdk/index.js:2397:31)\n at processTicksAndRejections (internal/process/task_queues.js:97:5)\n at async Runtime.exports.main [as handler] (/var/user/index.js:14:20)",
"statusCode":430
}
这是相关代码片段:
const openid = event.userInfo.openId || await cloud.getWXContext().OPENID; //解决:获取不到上下文的OPENID
try {
const result = await cloud.openapi.phonenumber.getPhoneNumber({
code: event.form.code
});
// { errCode: 0, errMsg: 'openapi.templateMessage.send:ok' }
if (result.errCode == 0 || result.phoneInfo) {
...
}
} catch (err) {
// err.errCode !== 0
throw err
}
2.我尝试用 getOpenData 解决,解读返回的信息,我应该发现了盲点:
const temp = await cloud.getOpenData({
list: [event.form.cloudID]
});
return temp;
环境共享的小程序,是可以获取到手机号;但是享受环境共享的小程序,返回信息如下:
"list": [
{
"cloudID": "59_-这个可能是有敏感信息吧",
"errCode": -601007,
"errMsg": "cloudID not belong to user."
}
],
"errMsg": "getOpenData:ok",
"errCode": 0
3.引用 “跨商通” 提供的处理方法:
const openid = event.userInfo.openId || await cloud.getWXContext().OPENID;
try {
const result = await cloud.openapi({
appid: openid
}).phonenumber.getPhoneNumber({
code: event.form.code
});
} catch (err) {
throw err
}
好像不支持这样写,"wx-server-sdk": "~2.6.1",捕获异常:
{
"errorCode":1,
"errorMessage":"user code exception caught",
"stackTrace":"Error: errCode: -501001 resource system error | errMsg: phonenumber.getPhoneNumber:fail wx api error: -605107\n at callGeneralOpenAPI (/var/user/node_modules/wx-server-sdk/index.js:426:19)\n at processTicksAndRejections (internal/process/task_queues.js:97:5)\n at async callWXOpenAPI (/var/user/node_modules/wx-server-sdk/index.js:2364:28)\n at async Runtime.exports.main [as handler] (/var/user/index.js:17:20)",
"statusCode":430
}
周末还在写代码的程序员在线求解答,来个人呀!
------
解决方式:关电脑睡一觉,醒来后就好了,是官方干了啥吗?毕竟,环境共享已经开通了N天了,不影响上面的问题。
最终代码:
try {
const result = await cloud.openapi({
appid: appid
}).phonenumber.getPhoneNumber({
code: event.form.code
});
if (result.errCode == 0 && result.phoneInfo) {
...
} else {
...
}
} catch (error) {
throw error;
}
参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/guide/resource-sharing/guidance.html
cloud.openapi({appid:"FROM_APPID"}).phonenumber.getPhoneNumber
https://developers.weixin.qq.com/community/minihome/doc/000ca6ee0f8bb0b99e8e372c451c00
你确定你这里appid传的是openid
await cloud.openapi({ appid: openid })