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

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
+32 -18
View File
@@ -1,43 +1,57 @@
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',
})
}
})
}
}
+3 -11
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),
// 手机短信登录
@@ -75,4 +67,4 @@ const UserApi = {
queryDataMemberList: (params) => http.get(`${URL_CONFIG}jero/memberProjectSubitem/pageCompany`, params),
}
export default UserApi
export default UserApi