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

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 -1
View File
@@ -16,7 +16,7 @@ App({
success(result){
wxBind((res)=>{
let params = {
openId: res.data.openid,
openId: res.result.openid,
rsaPublicKey: result.rsaPublicKey
}
UserApi.wxLogin(params).then(res=>{
+31 -17
View File
@@ -1,40 +1,54 @@
import {appId, secret} from '../js/project.config'
export function wxBind(callback){
import {
appId,
secret
} from '../js/project.config'
import UserApi from '../js/http/userApi'
export function wxBind(callback) {
// 获取openid进行手机号和微信的绑定
let count = 1
wx.login({
success (res) {
success(res) {
if (res.code && count === 1) {
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data: {
appid: appId,
secret: secret,
js_code: res.code,
grant_type: 'authorization_code'
},
success (res) { // 成功拿到openId
let params = {
appid: appId,
secret: secret,
js_code: res.code,
grant_type: 'authorization_code'
}
UserApi.getWxLoginPromise(params).then(res => {
if (res.success) { // 成功拿到openId
// 微信和该账号绑定
if(count === 1){ // 回调会莫名回调两次,未知原因
if (count === 1) { // 回调会莫名回调两次,未知原因
callback(res)
}
count++
}
}else [
wx.showToast({
title: res.message,
icon: 'none'
})
]
})
} else {
console.log('授权失败!' + res.message)
}
},
fail(err) {
wx.showToast({
title: err.message,
icon: 'none'
})
}
})
}
// 检验是否登录
export function isLogin(callback){
export function isLogin(callback) {
wx.getStorage({
key: 'X-Access-Token',
success(res){
success(res) {
callback()
},
fail(){
fail() {
wx.navigateTo({
url: '../login/login',
})
+2 -10
View File
@@ -1,21 +1,13 @@
import http from './http.js'
let URL_CONFIG = '/jero-boot/'
const UserApi = {
// // 登录
// login: (params) => http.post(`${URL_CONFIG}sys/login`, params),
// // 获得公钥
// getRSAPublicKey: (params) => http.get(`${URL_CONFIG}sys/getRSAPublicKey`, params),
// // 图片的验证码
// getImgCode: (params, urlParam) => http.get(`${URL_CONFIG}sys/randomImage/${urlParam}`, params),
// // 退出
// logout: (params) => http.post(`${URL_CONFIG}sys/logout`, params),
// //修改密码
// upDatePass:(params) => http.put(`${URL_CONFIG}sys/user/updatePassword`, params),
/**
* author: yaoyanan
* **/
// 获得公钥
getRSAPublicKey: (params) => http.get(`${URL_CONFIG}sys/getRSAPublicKey`, params),
// 获取微信登录凭证
getWxLoginPromise: (params) => http.get(`${URL_CONFIG}company/jscode2session`, params),
// 发送短信验证码
sendCode: (params) => http.get(`${URL_CONFIG}company/sendVerificationCode`, params),
// 手机短信登录
@@ -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 */
+11 -13
View File
@@ -44,7 +44,7 @@ Page({
if (res.success) {
this.countDown()
wx.showToast({
title: '发送成功'+ res.result,
title: '发送成功',
icon: 'none',
duration: 2000
})
@@ -272,26 +272,24 @@ Page({
wx.login({
success (res) {
if (res.code) {
//发起网络请求
wx.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
data: {
appid: 'wx24914e866df035fb',
secret: '0d2830f26f57722716249f2b2236f409',
js_code: res.code,
grant_type: 'authorization_code'
},
success (res) { // 成功拿到openId
let params = {
appid: appId,
secret: secret,
js_code: res.code,
grant_type: 'authorization_code'
}
UserApi.getWxLoginPromise(params).then(res => {
if (res.success) { // 成功拿到openId
// 微信和该账号绑定
let params = {
openId: res.openId,
openId: res.result.openid,
rsaPublicKey: that.data.rsaPublicKey
}
UserApi.wxBind(params).then(res=>{})
}
})
} else {
console.log('授权失败!' + res.errMsg)
console.log('授权失败!' + res.message)
}
}
})
+6 -22
View File
@@ -97,7 +97,6 @@ Page({
wx.getStorage({
key: 'authorize',
success(result){
console.log('result=======', result)
// 做过查看微信用户信息
if(result.data){ // 做过用户信息获取
wx.showToast({
@@ -106,7 +105,7 @@ Page({
})
wxBind((res)=>{
let params = {
openId: res.data.openid,
openId: res.result.openid,
rsaPublicKey: that.data.rsaPublicKey
}
UserApi.wxLogin(params).then(res=>{
@@ -116,14 +115,14 @@ Page({
key: 'X-Access-Token',
data: res.result.token
})
wx.switchTab({
url: '../home/home',
})
wx.setStorageSync('authorize', true) // 设置有过微信授权
wx.setStorage({
key: 'avatarUrl',
data: result.avatarUrl
})
wx.switchTab({
url: '../home/home',
})
}else{ // 首次登录不能直接用微信快捷登录
wx.showToast({
title: '暂无授权手机号',
@@ -138,7 +137,6 @@ Page({
},
fail(err){
// 没有做过查看微信用户信息
console.log('err========', err)
that.getUserInfo()
}
})
@@ -149,13 +147,11 @@ Page({
wx.showModal({
title: '温馨提示',
content: `亲,授权微信登录后才能正常使用小程序功能`,
cancelColor: 'cancelColor',
success(res) {
if (res.confirm) {
wx.getUserProfile({
desc: '获取你的昵称、头像、地区及性别',
success(result){
console.log('result用于完善用户信息', result)
let userInfoTemp = JSON.parse(result.rawData)
wx.setStorage({
key: 'userInfoTemp',
@@ -170,7 +166,7 @@ Page({
})
wxBind((res)=>{
let params = {
openId: res.data.openid,
openId: res.result.openid,
rsaPublicKey: that.data.rsaPublicKey
}
UserApi.wxLogin(params).then(res=>{
@@ -195,7 +191,6 @@ Page({
})
},
fail(err){
console.log('err===========', err)
wx.setStorageSync('authorize', false)
}
})
@@ -204,17 +199,6 @@ Page({
}
}
})
// wx.showModal({
// title: '错误提示',
// content: `图片大于${that.data.maxSize}MB`,
// showCancel: false,
// success(res) {
// if (res.confirm) {
// console.log('用户点击确定')
// }
// }
// })
},
// 登录
formSubmit(e) {
@@ -293,7 +277,7 @@ Page({
const that = this
wxBind((res)=>{
let params = {
openId: res.data.openid,
openId: res.result.openid,
rsaPublicKey: that.data.rsaPublicKey
}
// 需要判断是否绑定过微信,再进行绑定请求
+1 -1
View File
@@ -190,7 +190,7 @@ Page({
// 获取openid进行手机号和微信的绑定
wxBind((res)=>{
let params = {
openId: res.data.openid,
openId: res.result.openid,
rsaPublicKey: that.data.rsaPublicKey
}
// 需要判断是否绑定过微信,再进行绑定请求
+1 -1
View File
@@ -178,7 +178,7 @@ Page({
const that = this
wxBind((res)=>{
let params = {
openId: res.data.openid,
openId: res.result.openid,
rsaPublicKey: that.data.rsaPublicKey
}
// 需要判断是否绑定过微信,再进行绑定请求