小程序:登录授权
wxml
<button open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="onGotUserInfo">立即授权</button>
js
onGotUserInfo: function () {
var that = this;
wx.getUserInfo({
success: function (res) {
console.log(res)
that.setData({
rawData: res.rawData,
signature: res.signature,
encryptedData: res.encryptedData,
iv:res.iv
})
},
fail: function () {
}
})
// // 登录授权
wx.login({
success: res => {
// ------ 获取凭证 ------
var code = res.code;
var rawData = that.data.rawData;
var signature = that.data.signature;
var encryptedData = that.data.encryptedData;
var iv = that.data.iv;
// console.log(code)
if (code) {
// console.log(\'获取用户登录凭证:\' + code);
// ------ 发送凭证 ------
wx.request({
url: \'https://p.yun-coupons.com/atst/Mobile/Small/wxLogin\',
data: { code: code, rawData: rawData, signature: signature, encryptedData: encryptedData,iv:iv},
method: \'GET\',
header: {
\'Content-Type\': \'application/json\'
},
success: function (res) {
console.log(res.data.openId)
if (res.statusCode == 200) {
// console.log("获取到的openid为:" + res.data)
// that.globalData.openid = res.data
wx.setStorageSync(\'openid\', res.data.openId)
// console.log(res.data.openId)
wx.navigateTo({
url: \'../my/my\',
})
} else {
console.log(res.errMsg)
wx.navigateTo({
url: \'../index/index\',
})
}
},
})
} else {
console.log(\'获取用户登录失败:\' + res.errMsg);
}
}
})
},
请发表评论