获取code 换取 openid
uni.login({
provider: 'weixin',
success: (res) => {
console.log(res)
uniCloud.callFunction({
name: 'openid',
data: {
code: res.code
}
}).then(opid => {
console.log(opid,'openid');
})
},
fail(res) { // 登录失败
uni.closeAuthView() //关闭一键登录弹出窗口
console.log('失败')
},
})
云函数 :
'use strict';
exports.main = async (event, context) => {
let appid = "XXX";//你自己的appid
let secret = "XXXXXXX";//你自己的appsecret
let url =
"https://api.weixin.qq.com/sns/jscode2session?appid=" +
appid +
"&secret=" +
secret +
"&js_code=" +
event.code +
"&grant_type=authorization_code";
let res = await uniCloud.httpclient.request(
url ,// 请求路径,
{
dataType:"json"
}
);
let openid = res.data
return openid
};