解决登录过期的问题
This commit is contained in:
parent
ca6d3fc8c3
commit
571237bb0e
@ -124,7 +124,7 @@
|
|||||||
title: '正在登录中'
|
title: '正在登录中'
|
||||||
})
|
})
|
||||||
let res = await loginAccount(that.formData);
|
let res = await loginAccount(that.formData);
|
||||||
encrypt.encode('ACT', this.formData);
|
encrypt.encode('ACT', that.formData);
|
||||||
this.$store.commit('SET_USERINFO', {
|
this.$store.commit('SET_USERINFO', {
|
||||||
user: data,
|
user: data,
|
||||||
token: res.data.token
|
token: res.data.token
|
||||||
|
@ -36,6 +36,7 @@
|
|||||||
<script>
|
<script>
|
||||||
import { Toast } from '../../libs/uniApi';
|
import { Toast } from '../../libs/uniApi';
|
||||||
import { changePassword } from "@/api/oaUser.js"
|
import { changePassword } from "@/api/oaUser.js"
|
||||||
|
import encrypt from "@/utils/encrypt.js"
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
@ -91,6 +92,10 @@
|
|||||||
mask: true,
|
mask: true,
|
||||||
title: '加载中'
|
title: '加载中'
|
||||||
})
|
})
|
||||||
|
// 密码修改后重新加密缓存
|
||||||
|
let nowData = encrypt.decode('ACT');
|
||||||
|
nowData.password = this.formData.password;
|
||||||
|
encrypt.encode('ACT', nowData);
|
||||||
// #ifdef APP-PLUS
|
// #ifdef APP-PLUS
|
||||||
return uni.switchTab({
|
return uni.switchTab({
|
||||||
url: '/pages/oaHome/oaHome',
|
url: '/pages/oaHome/oaHome',
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
import { commonAuth } from '@/api/pubic.js'
|
import { commonAuth } from '@/api/pubic.js'
|
||||||
import { loginMobile } from '@/api/user.js'
|
import { loginMobile } from '@/api/user.js'
|
||||||
|
import { loginAccount } from '@/api/oaUser.js'
|
||||||
import Routine from '@/libs/routine.js'
|
import Routine from '@/libs/routine.js'
|
||||||
import Cache from '@/utils/cache';
|
import Cache from '@/utils/cache';
|
||||||
|
import encrypt from '@/utils/encrypt.js';
|
||||||
|
import oaHttp from '@/utils/oahttp.js';
|
||||||
|
|
||||||
const state = {
|
const state = {
|
||||||
userInfo: JSON.parse(Cache.get('USER_INFO')||'{}') || null,
|
userInfo: JSON.parse(Cache.get('USER_INFO') || '{}') || null,
|
||||||
token: Cache.get("TOKEN") || null
|
token: Cache.get("TOKEN") || null
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -18,13 +22,13 @@ const mutations = {
|
|||||||
uni.showModal({
|
uni.showModal({
|
||||||
content: '登录已过期,是否重新登录?',
|
content: '登录已过期,是否重新登录?',
|
||||||
success(e) {
|
success(e) {
|
||||||
if(e.confirm) uni.reLaunch({
|
if (e.confirm) uni.reLaunch({
|
||||||
url:'/pages/oaLogin/oaLogin'
|
url: '/pages/oaLogin/oaLogin'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
CLEAR(state){
|
CLEAR(state) {
|
||||||
state.userInfo = null;
|
state.userInfo = null;
|
||||||
state.token = null;
|
state.token = null;
|
||||||
Cache.clear('USER_INFO')
|
Cache.clear('USER_INFO')
|
||||||
@ -37,16 +41,36 @@ const mutations = {
|
|||||||
Cache.set("USER_INFO", data.result.user, time)
|
Cache.set("USER_INFO", data.result.user, time)
|
||||||
Cache.set("TOKEN", data.result.token, time)
|
Cache.set("TOKEN", data.result.token, time)
|
||||||
},
|
},
|
||||||
SET_USERINFO(state, data){
|
SET_USERINFO(state, data) {
|
||||||
let time = Cache.time();
|
let time = Cache.time();
|
||||||
state.userInfo = data.user
|
state.userInfo = data.user
|
||||||
state.token = data.token
|
state.token = data.token
|
||||||
Cache.set("USER_INFO", data.user, time)
|
Cache.set("USER_INFO", data.user, time)
|
||||||
Cache.set("TOKEN", data.token, 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 = {
|
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) {
|
MobileLogin({ state, commit }, force) {
|
||||||
let data = {
|
let data = {
|
||||||
auth_token: uni.getStorageSync('auth_token'),
|
auth_token: uni.getStorageSync('auth_token'),
|
||||||
@ -88,4 +112,4 @@ export default {
|
|||||||
state,
|
state,
|
||||||
mutations,
|
mutations,
|
||||||
actions
|
actions
|
||||||
};
|
};
|
@ -1,11 +1,15 @@
|
|||||||
import Base64 from "@/utils/base64.js"
|
import Base64 from "@/utils/base64.js"
|
||||||
|
import cryptoJS from "crypto-js"
|
||||||
|
|
||||||
|
const key_code = 'gxzhptxiaochengxu_1';
|
||||||
|
|
||||||
// 加密
|
// 加密
|
||||||
const encode = (key, data='')=>{
|
const encode = (key, data='')=>{
|
||||||
try{
|
try{
|
||||||
let str;
|
let str;
|
||||||
typeof data == 'object'? str = JSON.stringify(data) : str = data;
|
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;
|
return true;
|
||||||
}catch(e){
|
}catch(e){
|
||||||
console.log(e);
|
console.log(e);
|
||||||
@ -17,7 +21,7 @@ const encode = (key, data='')=>{
|
|||||||
const decode = (key)=>{
|
const decode = (key)=>{
|
||||||
try{
|
try{
|
||||||
let str = uni.getStorageSync(key);
|
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;
|
isJSON(str) ? str = JSON.parse(str) : null;
|
||||||
return str;
|
return str;
|
||||||
}catch(e){
|
}catch(e){
|
||||||
|
190
utils/oahttp.js
190
utils/oahttp.js
@ -1,118 +1,122 @@
|
|||||||
import {
|
import {
|
||||||
HTTP_REQUEST_URL_THREE,
|
HTTP_REQUEST_URL_THREE,
|
||||||
HEADER,
|
HEADER,
|
||||||
TOKENNAME,
|
TOKENNAME,
|
||||||
} from '@/config/app';
|
} from '@/config/app';
|
||||||
import {
|
import {
|
||||||
Toast
|
Toast
|
||||||
} from '../libs/uniApi';
|
} from '../libs/uniApi';
|
||||||
// import { checkLogin } from '../libs/login';
|
// import { checkLogin } from '../libs/login';
|
||||||
import store from '../store';
|
import store from '../store';
|
||||||
|
|
||||||
function toLogin() {
|
function toLogin() {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: '请先登录',
|
title: '请先登录',
|
||||||
icon: 'none',
|
icon: 'none',
|
||||||
duration: 1000
|
duration: 1000
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function baseRequestTwo(url, method, data, {
|
function baseRequestTwo(url, method, data, {
|
||||||
noAuth = false,
|
noAuth = false,
|
||||||
noVerify = false
|
noVerify = false
|
||||||
}) {
|
}) {
|
||||||
let Url = HTTP_REQUEST_URL_THREE,
|
let Url = HTTP_REQUEST_URL_THREE,
|
||||||
header = HEADER;
|
header = HEADER;
|
||||||
if (!noAuth) {
|
if (!noAuth) {
|
||||||
// 已经未登录了,禁止请求
|
// 已经未登录了,禁止请求
|
||||||
if (!store.state.config.request) return Promise.reject({
|
if (!store.state.config.request) return Promise.reject({
|
||||||
msg: '未登录'
|
msg: '未登录'
|
||||||
});
|
});
|
||||||
//登录过期自动登录
|
//登录过期自动登录
|
||||||
if (!store.state.app.token) {
|
if (!store.state.app.token) {
|
||||||
toLogin();
|
toLogin();
|
||||||
store.commit("SET_REQUEST", false);
|
store.commit("SET_REQUEST", false);
|
||||||
return Promise.reject({
|
return Promise.reject({
|
||||||
msg: '未登录'
|
msg: '未登录'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + 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;
|
if (store.state.app.token) header[TOKENNAME] = store.state.app.token;
|
||||||
|
|
||||||
// header[TOKENNAME] = 'Bearer sdjflidshjgfkbdasgjmasbgvhauuiavhkesvndkaesbvkjsdbv';
|
// header[TOKENNAME] = 'Bearer sdjflidshjgfkbdasgjmasbgvhauuiavhkesvndkaesbvkjsdbv';
|
||||||
return new Promise((reslove, reject) => {
|
return new Promise((reslove, reject) => {
|
||||||
// uni.showLoading({
|
// uni.showLoading({
|
||||||
// title: '加载中'
|
// title: '加载中'
|
||||||
// })
|
// })
|
||||||
uni.request({
|
uni.request({
|
||||||
// url: Url + '/api/v1' + url,
|
// url: Url + '/api/v1' + url,
|
||||||
url: Url + '/api' + url,
|
url: Url + '/api' + url,
|
||||||
method: method || 'GET',
|
method: method || 'GET',
|
||||||
header: {
|
header: {
|
||||||
...header
|
...header
|
||||||
|
},
|
||||||
},
|
data: method != 'GET' ? data || {} : {},
|
||||||
data: method != 'GET' ? data || {} : {},
|
params: method == 'GET' ? data : {},
|
||||||
params: method == 'GET' ? data : {},
|
success: (res) => {
|
||||||
success: (res) => {
|
if (noVerify)
|
||||||
// uni.hideLoading()
|
reslove(res.data);
|
||||||
if (noVerify)
|
else if (res.data.code == -1) {
|
||||||
// reslove(res.data, res);
|
// 如果登录超时,自动重新登录并且继续发送请求
|
||||||
reslove(res.data);
|
store.dispatch("RE_LOGIN", {
|
||||||
else if (res.data.code == -1) {
|
url: url,
|
||||||
store.commit("LOGOUT");
|
method: method,
|
||||||
store.commit("SET_REQUEST", false);
|
data: data,
|
||||||
reject(res.data);
|
opt: {
|
||||||
} else if (res.data.code == 0) {
|
noAuth,
|
||||||
// uni.hideLoading();
|
noVerify
|
||||||
if(res.data.msg!='无登录信息'){
|
}
|
||||||
|
}).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({
|
uni.showToast({
|
||||||
title: res.data.msg || '请检查网络',
|
title: res.data.msg || '请检查网络',
|
||||||
icon: 'none',
|
icon: 'none',
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
reject(res.data);
|
reject(res.data);
|
||||||
} else if (res.data.code == 1) {
|
} else if (res.data.code == 1) {
|
||||||
store.commit("SET_REQUEST");
|
store.commit("SET_REQUEST");
|
||||||
reslove(res.data);
|
reslove(res.data);
|
||||||
} else if (res.data.code == 200) {
|
} else if (res.data.code == 200) {
|
||||||
store.commit("SET_REQUEST");
|
store.commit("SET_REQUEST");
|
||||||
reslove(res.data.data);
|
reslove(res.data.data);
|
||||||
} else if ([410000, 410001, 410002, 40000].indexOf(res.data.code) !== -1) {
|
} else if ([410000, 410001, 410002, 40000].indexOf(res.data.code) !== -1) {
|
||||||
toLogin();
|
toLogin();
|
||||||
reject(res.data);
|
reject(res.data);
|
||||||
} else if (res.data.code == 501) {
|
} else if (res.data.code == 501) {
|
||||||
// uni.reLaunch({
|
reject(res.data);
|
||||||
// url: '/pages/error/index'
|
} else {
|
||||||
// })
|
uni.showToast({
|
||||||
reject(res.data);
|
title: res.data.msg || '请检查网络',
|
||||||
} else {
|
icon: 'none'
|
||||||
// uni.hideLoading();
|
})
|
||||||
uni.showToast({
|
reject(res.data.msg || '请检查网络');
|
||||||
title: res.data.msg || '请检查网络',
|
}
|
||||||
icon: 'none'
|
},
|
||||||
})
|
fail: (message) => {
|
||||||
reject(res.data.msg || '请检查网络');
|
// uni.hideLoading()
|
||||||
}
|
uni.showToast({
|
||||||
},
|
title: '网络错误',
|
||||||
fail: (message) => {
|
icon: 'none'
|
||||||
// uni.hideLoading()
|
})
|
||||||
uni.showToast({
|
reject('请求失败');
|
||||||
title: '网络错误',
|
}
|
||||||
icon: 'none'
|
})
|
||||||
})
|
});
|
||||||
reject('请求失败');
|
|
||||||
}
|
|
||||||
})
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const oahttp = {};
|
const oahttp = {};
|
||||||
|
|
||||||
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
|
['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;
|
export default oahttp;
|
Loading…
x
Reference in New Issue
Block a user