微信授权登录,获取授权凭证改为后端获取

This commit is contained in:
姚亚男
2021-11-18 12:04:24 +08:00
parent e5b09a6c56
commit a1d42ee12f
11 changed files with 55 additions and 167 deletions
@@ -1,89 +0,0 @@
// components/getPhoneNumer/getPhoneNumber.js
const app = getApp()
Component({
/**
* 组件的属性列表
*/
properties: {
appid:{
type: String,
value:''
},
secret: {
type: String,
value:''
}
},
// 可以定义外部样式类 影响组件的样式
externalClasses: ['class'],
options: {
// 配置样式是否可以被父组件影响 详细见文档
styleIsolation: 'apply-shared'
},
/**
* 组件的初始数据
*/
data: {
// session_key
session_key:'',
// 用户openid
openid:''
},
/**
* 组件的方法列表
*/
methods: {
getPhoneNumber(e) {
console.log(e);
const that = this
wx.checkSession({
success: (res) => {
console.log(res);
if(e.detail.errMsg == 'getPhoneNumber:fail user deny'){
console.log("用户拒绝了授权");
return
}else{
console.log("用户同意了授权");
let key = that.data.session_key
let phone = e.detail.encryptedData;
let iv = e.detail.iv;
const RdWXBizDataCrypt = require('./utils/RdWXBizDataCrypt');
const pc = new RdWXBizDataCrypt(that.data.appid, key);
const data = pc.decryptData(phone, iv);
console.log(data.phoneNumber); //当前手机号码
that.triggerEvent("myevent",data)
}
},
fail:(err) => {
console.log("登录状态失效,请重新登录",err);
}
})
},
},
/**
* 组件生命周期
*/
lifetimes: {
attached(){
const that = this
wx.login({
timeout: 2000,
success(res){
wx.request({
url: `https://api.weixin.qq.com/sns/jscode2session?appid=${that.data.appid}&secret=${that.data.secret}&js_code=${res.code}&grant_type=authorization_code`,
success(result){
that.setData({
session_key:result.data.session_key,
openid:result.data.openid
})
}
})
}
})
}
}
})
@@ -1,4 +0,0 @@
{
"component": true,
"usingComponents": {}
}
@@ -1,6 +0,0 @@
<!--components/getPhoneNumer/getPhoneNumber.wxml-->
<view>
<button open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber" class="button">
<slot></slot>
</button>
</view>
@@ -1 +0,0 @@
/* components/getPhoneNumer/getPhoneNumber.wxss */