问题
1.微信开发获取用户的openid
解决
1.获取微信的回调code这边就不多做赘述了,类似的文章很多。你们可以自己去查询。这边来说说获取到code之后怎么去微信那边获取openid和微信的个人信息的
直接上代码:
A.新建一个云函数 在云函数里面写:qizh
//通过获取到的code 去查询用户的信息
let appid = 'xxxxxx'; //你自己的appid
let secret = 'xxxxxx'; //你自己的appsecret
let url =
"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret +
"&code=" +
event.code + "&grant_type=authorization_code"
let res = await uniCloud.httpclient.request(
url, // 请求路径,
{
dataType: "JSON"
}
);
let openid = res.data.openid
//这边已经获取到了微信用户在这个公众号的唯一ID了
//然后通过这个接口返回值openid和access_token去请求
//拉取用户信息
// 请求路径,
let urlink = "https://api.weixin.qq.com/sns/userinfo?access_token=" + res.data.access_token +
"&openid=" +
res.data.openid + "&lang=zh_CN"
let getWxUserInfo = await uniCloud.httpclient.request(urlink,
{
dataType: "json"
})
这边、的getWxUserInfo 就是和获取到的微信的用户信息 包括头像 昵称 性别,等 具体的可以去看微信的文档
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#3
我是大脸猫 一个自学前端毫无理论基础的代码渣渣= =