解决登录过期的问题

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'),

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

@ -52,22 +52,30 @@ function baseRequestTwo(url, method, data, {
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);
// 如果登录超时,自动重新登录并且继续发送请求
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) {
// uni.hideLoading();
if(res.data.msg!='无登录信息'){
if (res.data.msg != '无登录信息') {
uni.showToast({
title: res.data.msg || '请检查网络',
icon: 'none',
@ -84,12 +92,8 @@ function baseRequestTwo(url, method, data, {
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'