WXML部分
<button type="primary" class="red myToast_but" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">一键授权</button>
js部分
getPhoneNumber(e) {
console.log(e)
let {
encryptedData,
iv
} = e.detail
let appid = 'appid'
// let secret = 'secret '
let secret = 'secret '
// console.log(encryptedData)
// console.log(iv)
wx.checkSession({
success: (res) => {
console.log(res)
wx.showToast({
title: '已报价',
icon: 'success',
duration: 2000,
})
},
fail() {
console.log("未登录")
wx.login({
success(res) {
console.log(res)
let code = res.code
console.log(code)
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&js_code=' + code + '&grant_type=authorization_code',
method: 'GET',
header: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
},
success: function(res) {
console.log(res);
let session_key = res.data.session_key
console.log(session_key);
wx.request({
url: "",
data: {
encryptedData: encryptedData,
encryptIv: iv,
sessionKey: session_key
},
method: 'GET',
header: {
'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
},
success(phoneNumber) {
console.log(phoneNumber)
wx.request({
url: '',
data: {
mobile: phoneNumber,
},
})
wx.showToast({
title: '成功',
icon: 'success',
duration: 2000
})
}
})
}
})
}
})
}
})
},