初始的云函数方法
// demo-uniapp/uniCloud-aliyun/cloudfunctions/login/index.js
'use strict';
exports.main = async (event, context) => {
// event里包含着客户端提交的参数
const res = await uniCloud.getPhoneNumber({
// appid: '__UNI__xxx', // 替换成自己开通一键登录的应用的DCloud appid
provider: 'univerify',
apiKey: 'xxx', // 在开发者中心开通服务并获取apiKey
apiSecret: 'xxx', // 在开发者中心开通服务并获取apiSecret
access_token: event.access_token,
openid: event.openid
})
console.log(res); // res里包含手机号
// 执行用户信息入库等操作,正常情况下不要把完整手机号返回给前端
// 如果数据库在uniCloud上,可以直接入库
// 如果数据库不在uniCloud上,可以通过 uniCloud.httpclient API,将手机号通过http方式传递给其他服务器的接口,详见:https://uniapp.dcloud.net.cn/uniCloud/cf-functions?id=httpclient
return {
code: 0,
message: '获取手机号成功',
data:res
}
}
注意这里后端直接根据上面云函数调用
https://XXX.next.bspapp.com/getPhoneNumber 去传参access_token与openid会调用失败
access_token未找到
云函数代码需要调整
// event里包含着客户端提交的参数
const res = await uniCloud.getPhoneNumber({
// appid: '__UNI__xxx', // 替换成自己开通一键登录的应用的DCloud appid
provider: 'univerify',
apiKey: 'xxx', // 在开发者中心开通服务并获取apiKey
apiSecret: 'xxx', // 在开发者中心开通服务并获取apiSecret
access_token: event.queryStringParameters.access_token, ==注意这里event.queryStringParameters==
openid: event.queryStringParameters.openid ==注意这里event.queryStringParameters==
})
这样就能访问到用户手机号