解决登录过期的问题

This commit is contained in:
weipengfei 2023-09-08 17:08:18 +08:00
parent ca6d3fc8c3
commit 571237bb0e
5 changed files with 140 additions and 103 deletions

View File

@ -124,7 +124,7 @@
title: '正在登录中'
})
let res = await loginAccount(that.formData);
encrypt.encode('ACT', this.formData);
encrypt.encode('ACT', that.formData);
this.$store.commit('SET_USERINFO', {
user: data,
token: res.data.token

View File

@ -36,6 +36,7 @@
<script>
import { Toast } from '../../libs/uniApi';
import { changePassword } from "@/api/oaUser.js"
import encrypt from "@/utils/encrypt.js"
export default {
data() {
return {
@ -91,6 +92,10 @@
mask: true,
title: '加载中'
})
//
let nowData = encrypt.decode('ACT');
nowData.password = this.formData.password;
encrypt.encode('ACT', nowData);
// #ifdef APP-PLUS
return uni.switchTab({
url: '/pages/oaHome/oaHome',

View File

@ -1,9 +1,13 @@
import { commonAuth } from '@/api/pubic.js'
import { loginMobile } from '@/api/user.js'
import { loginAccount } from '@/api/oaUser.js'
import Routine from '@/libs/routine.js'
import Cache from '@/utils/cache';
import encrypt from '@/utils/encrypt.js';
import oaHttp from '@/utils/oahttp.js';
const state = {
userInfo: JSON.parse(Cache.get('USER_INFO')||'{}') || null,
userInfo: JSON.parse(Cache.get('USER_INFO') || '{}') || null,
token: Cache.get("TOKEN") || null
};
@ -18,13 +22,13 @@ const mutations = {
uni.showModal({
content: '登录已过期,是否重新登录?',
success(e) {
if(e.confirm) uni.reLaunch({
url:'/pages/oaLogin/oaLogin'
if (e.confirm) uni.reLaunch({
url: '/pages/oaLogin/oaLogin'
})
}
})
},
CLEAR(state){
CLEAR(state) {
state.userInfo = null;
state.token = null;
Cache.clear('USER_INFO')
@ -37,16 +41,36 @@ const mutations = {
Cache.set("USER_INFO", data.result.user, time)
Cache.set("TOKEN", data.result.token, time)
},
SET_USERINFO(state, data){
SET_USERINFO(state, data) {
let time = Cache.time();
state.userInfo = data.user
state.token = data.token
Cache.set("USER_INFO", data.user, time)
Cache.set("TOKEN", data.token, time)
}
},
SET_TOKEN(state, data) {
let time = Cache.time();
state.token = data.token;
Cache.set("TOKEN", data.token, time);
},
};
const actions = {
RE_LOGIN({ state, commit }, data) {
return new Promise((resolve, reject) => {
let fromData = encrypt.decode('ACT');
loginAccount({ ...fromData }).then((res) => {
commit('SET_TOKEN', res.data);
oaHttp[data.method](data.url, data.data, data.opt).then((e) => {
resolve(e);
}).catch((err) => {
reject(err)
})
}).catch((err) => {
reject(err)
})
})
},
MobileLogin({ state, commit }, force) {
let data = {
auth_token: uni.getStorageSync('auth_token'),
@ -88,4 +112,4 @@ export default {
state,
mutations,
actions
};
};

View File

@ -1,11 +1,15 @@
import Base64 from "@/utils/base64.js"
import cryptoJS from "crypto-js"
const key_code = 'gxzhptxiaochengxu_1';
// 加密
const encode = (key, data='')=>{
try{
let str;
typeof data == 'object'? str = JSON.stringify(data) : str = data;
uni.setStorageSync(key, Base64.encode(str))
str = cryptoJS.AES.encrypt(str, key_code).toString();
uni.setStorageSync(key, str)
return true;
}catch(e){
console.log(e);
@ -17,7 +21,7 @@ const encode = (key, data='')=>{
const decode = (key)=>{
try{
let str = uni.getStorageSync(key);
str = Base64.decode(str);
str = cryptoJS.AES.decrypt(str, key_code).toString(cryptoJS.enc.Utf8);
isJSON(str) ? str = JSON.parse(str) : null;
return str;
}catch(e){

View File

@ -1,118 +1,122 @@
import {
HTTP_REQUEST_URL_THREE,
HEADER,
TOKENNAME,
HTTP_REQUEST_URL_THREE,
HEADER,
TOKENNAME,
} from '@/config/app';
import {
Toast
Toast
} from '../libs/uniApi';
// import { checkLogin } from '../libs/login';
import store from '../store';
function toLogin() {
uni.showToast({
title: '请先登录',
icon: 'none',
duration: 1000
});
uni.showToast({
title: '请先登录',
icon: 'none',
duration: 1000
});
}
function baseRequestTwo(url, method, data, {
noAuth = false,
noVerify = false
noAuth = false,
noVerify = false
}) {
let Url = HTTP_REQUEST_URL_THREE,
header = HEADER;
if (!noAuth) {
// 已经未登录了,禁止请求
if (!store.state.config.request) return Promise.reject({
msg: '未登录'
});
//登录过期自动登录
if (!store.state.app.token) {
toLogin();
store.commit("SET_REQUEST", false);
return Promise.reject({
msg: '未登录'
});
}
}
let Url = HTTP_REQUEST_URL_THREE,
header = HEADER;
if (!noAuth) {
// 已经未登录了,禁止请求
if (!store.state.config.request) return Promise.reject({
msg: '未登录'
});
//登录过期自动登录
if (!store.state.app.token) {
toLogin();
store.commit("SET_REQUEST", false);
return Promise.reject({
msg: '未登录'
});
}
}
// if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
if (store.state.app.token) header[TOKENNAME] = store.state.app.token;
// if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
if (store.state.app.token) header[TOKENNAME] = store.state.app.token;
// header[TOKENNAME] = 'Bearer sdjflidshjgfkbdasgjmasbgvhauuiavhkesvndkaesbvkjsdbv';
return new Promise((reslove, reject) => {
// uni.showLoading({
// title: '加载中'
// })
uni.request({
// url: Url + '/api/v1' + url,
url: Url + '/api' + url,
method: method || 'GET',
header: {
...header
},
data: method != 'GET' ? data || {} : {},
params: method == 'GET' ? data : {},
success: (res) => {
// uni.hideLoading()
if (noVerify)
// reslove(res.data, res);
reslove(res.data);
else if (res.data.code == -1) {
store.commit("LOGOUT");
store.commit("SET_REQUEST", false);
reject(res.data);
} else if (res.data.code == 0) {
// uni.hideLoading();
if(res.data.msg!='无登录信息'){
// header[TOKENNAME] = 'Bearer sdjflidshjgfkbdasgjmasbgvhauuiavhkesvndkaesbvkjsdbv';
return new Promise((reslove, reject) => {
// uni.showLoading({
// title: '加载中'
// })
uni.request({
// url: Url + '/api/v1' + url,
url: Url + '/api' + url,
method: method || 'GET',
header: {
...header
},
data: method != 'GET' ? data || {} : {},
params: method == 'GET' ? data : {},
success: (res) => {
if (noVerify)
reslove(res.data);
else if (res.data.code == -1) {
// 如果登录超时,自动重新登录并且继续发送请求
store.dispatch("RE_LOGIN", {
url: url,
method: method,
data: data,
opt: {
noAuth,
noVerify
}
}).then((e)=>{
reslove(e);
}).catch((err)=>{
reject(res.data);
})
// store.commit("SET_REQUEST", false);
} else if (res.data.code == 0) {
if (res.data.msg != '无登录信息') {
uni.showToast({
title: res.data.msg || '请检查网络',
icon: 'none',
title: res.data.msg || '请检查网络',
icon: 'none',
})
}
reject(res.data);
} else if (res.data.code == 1) {
store.commit("SET_REQUEST");
reslove(res.data);
} else if (res.data.code == 200) {
store.commit("SET_REQUEST");
reslove(res.data.data);
} else if ([410000, 410001, 410002, 40000].indexOf(res.data.code) !== -1) {
toLogin();
reject(res.data);
} else if (res.data.code == 501) {
// uni.reLaunch({
// url: '/pages/error/index'
// })
reject(res.data);
} else {
// uni.hideLoading();
uni.showToast({
title: res.data.msg || '请检查网络',
icon: 'none'
})
reject(res.data.msg || '请检查网络');
}
},
fail: (message) => {
// uni.hideLoading()
uni.showToast({
title: '网络错误',
icon: 'none'
})
reject('请求失败');
}
})
});
reject(res.data);
} else if (res.data.code == 1) {
store.commit("SET_REQUEST");
reslove(res.data);
} else if (res.data.code == 200) {
store.commit("SET_REQUEST");
reslove(res.data.data);
} else if ([410000, 410001, 410002, 40000].indexOf(res.data.code) !== -1) {
toLogin();
reject(res.data);
} else if (res.data.code == 501) {
reject(res.data);
} else {
uni.showToast({
title: res.data.msg || '请检查网络',
icon: 'none'
})
reject(res.data.msg || '请检查网络');
}
},
fail: (message) => {
// uni.hideLoading()
uni.showToast({
title: '网络错误',
icon: 'none'
})
reject('请求失败');
}
})
});
}
const oahttp = {};
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
oahttp[method] = (api, data, opt) => baseRequestTwo(api, method, data, opt || {})
oahttp[method] = (api, data, opt) => baseRequestTwo(api, method, data, opt || {})
});
export default oahttp;