目录
(1)wxss 点击按钮获取手机号(客户端调试)
(2)js 使用微信官方文档wx.getUserInfo获取用户信息
用wx.login获取小程序的临时授权码code
wx.login({
success: function (res) { //请求自己后台获取用户openid
console.log(res.code, "临时授权码")
},
})
(3)调用后端写的登录接口获取 openid 和 sessionKey
wx.request({
url:"http://xx.xx.186.29:xxxx/api/xxxx/wx/user/appid/login",
method: "GET",
data: {
appid: appid,//微信公众平台创建小程序拿到的唯一值
secret: secret,//微信公众平台创建小程序拿到的唯一值
code: res.code//临时授权码
},
success: function (response) {
var openid = response.data.openid;
var sessionKey = response.data.sessionKey;
that.setData({
openid: response.data.openid,
sessionKey: response.data.sessionKey,
})
console.log(sessionKey);
}
})
(4)然后调用微信官方文档接口 url: "https://api.weixin.qq.com/cgi-bin/token",获取 access_token
获取手机号 | 微信开放文档
wx.request({
url: "https://api.weixin.qq.com/cgi-bin/token",
method: "GET",
data: {
appid: appid,
secret:secret",
grant_type: "client_credential"
},
header: {
'Content-Type': 'application/json',
},
success: res => {
console.log(res);
that.setData({
access_token: res.data.access_token,
sessionKey: res.data.sessionKey,
})
console.log("请求成功access_token", res.data.access_token);
var ACCESS_TOKEN=res.data.access_token
},
fail: err => {
console.log("请求失败", err);
}
})
(5)最后根据官方文档接口"https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token=" + ACCESS_TOKEN,获取手机号
获取授权帐号调用令牌 | 微信开放文档
var ACCESS_TOKEN=res.data.access_token
wx.request({
url:"https://api.weixin.qq.com/wxa/business/getuserphonenumber?access_token="+ACCESS_TOKEN,
method: "POST",
data: { code: e.detail.code, },
header: { 'Content-Type': 'application/json', },
success: res => { console.log("请求成功手机号码", res.data); },
fail: err => { console.log("请求失败手机号码", err); } })