自动退出

chengdu
yujialong 4 years ago
parent c454285c0a
commit 79d840da00
  1. 1
      src/components/page/Login.vue
  2. 6
      src/store/index.js
  3. 1
      src/utils/api.js
  4. 24
      src/utils/core.js
  5. 24
      src/utils/http.js

@ -302,6 +302,7 @@ export default {
this.$store.commit("userLoginData", { userId : this.userId,accountRole: this.roleId,studentId : this.studentId,schoolId: this.schoolId,token: res.message.user.token}); this.$store.commit("userLoginData", { userId : this.userId,accountRole: this.roleId,studentId : this.studentId,schoolId: this.schoolId,token: res.message.user.token});
res.message.user.userAvatars && this.$store.commit("userPhoto", { avatar : res.message.user.userAvatars}); res.message.user.userAvatars && this.$store.commit("userPhoto", { avatar : res.message.user.userAvatars});
this.$store.commit("userName", { userName : res.message.user.userName}); this.$store.commit("userName", { userName : res.message.user.userName});
this.$store.commit("dataTime", { dataTime : res.message.user.dataTime});
let redirect = decodeURIComponent(this.$route.query.redirect || '/dashboard') let redirect = decodeURIComponent(this.$route.query.redirect || '/dashboard')
this.$router.replace(redirect) this.$router.replace(redirect)
}).catch(res => {}); }).catch(res => {});

@ -27,7 +27,8 @@ const store = new Vuex.Store({
assesmentId: '', assesmentId: '',
staffId: '', staffId: '',
studentId: '', studentId: '',
answerHistory: [] answerHistory: [],
dataTime: '',
}, },
mutations:{ mutations:{
courseIdData(state,payload){ courseIdData(state,payload){
@ -39,6 +40,9 @@ const store = new Vuex.Store({
userPhoto(state,payload){ userPhoto(state,payload){
state.avatar = payload.avatar state.avatar = payload.avatar
}, },
dataTime(state,payload){
state.dataTime = payload.dataTime
},
customerData (state, payload) { customerData (state, payload) {
state.customerId = payload.customer_id state.customerId = payload.customer_id
}, },

@ -10,6 +10,7 @@ if(process.env.NODE_ENV === 'development'){
export default { export default {
logins: `${host}/liuwanr/userInfo/logins`, //登录  logins: `${host}/liuwanr/userInfo/logins`, //登录 
queryToken: `${host}/liuwanr/userInfo/queryToken`,
save: `${host}/evaluation/tms/userInfo/add`, //注册 save: `${host}/evaluation/tms/userInfo/add`, //注册
updateLogInNumber: `${host}/liuwanr/userInfo/updateLogInNumber`, //用户登录修改登录次数和登陆时间 updateLogInNumber: `${host}/liuwanr/userInfo/updateLogInNumber`, //用户登录修改登录次数和登陆时间
uploadUserAvatars: `${host}/liuwanr/userInfo/uploadUserAvatars`, //上传头像 uploadUserAvatars: `${host}/liuwanr/userInfo/uploadUserAvatars`, //上传头像

@ -179,6 +179,27 @@ function encodeString(str) {
return str return str
} }
function logout(){
store.replaceState({})
sessionStorage.removeItem('kd_client_username')
location.reload()
}
function autoLogout(){
let lastTime = new Date().getTime()
document.onmousedown = () => {
lastTime = new Date().getTime()
}
setInterval(() => {
if(sessionStorage.getItem('kd_client_username') && (new Date().getTime() - lastTime) > config.autoLogoutTime){
Message.error('用户登录过期,请重新登录')
setTimeout(logout,1500)
}
},1000)
}
autoLogout()
export default{ export default{
orderfounder, orderfounder,
projectPermissions, projectPermissions,
@ -195,5 +216,6 @@ export default{
getCaption, getCaption,
toSubSystem, toSubSystem,
formatDate, formatDate,
encodeString encodeString,
logout
} }

@ -1,8 +1,10 @@
import axios from 'axios'; import axios from 'axios';
import QS from 'qs'; import QS from 'qs';
import store from '../store/index' import store from '../store/index'
import api from './api'
import { Message } from 'element-ui' import { Message } from 'element-ui'
import router from '../router/index' import router from '../router/index'
import core from './core'
// 环境的切换 // 环境的切换
// if (process.env.NODE_ENV == 'development') { // if (process.env.NODE_ENV == 'development') {
@ -99,10 +101,29 @@ axios.interceptors.request.use(config => {
// } // }
// ); // );
let logout = false
export function getToken(){
// console.log(store.state.dataTime)
if(process.env.NODE_ENV == 'production'){
if(store.state.dataTime && !logout){
axios.get(`${api.queryToken}?token=${store.state.token}`).then(res => {
if(store.state.dataTime && (res.data.message != store.state.dataTime)){
logout || Message.error('您已在另一台设备登录,本次登录已下线!')
logout = true
setTimeout(() => {
core.logout()
},1500)
}
}).catch(err => {})
}
}
}
/** /**
* get方法对应get请求 * get方法对应get请求
*/ */
export function get(url, params){ export function get(url, params){
getToken()
return new Promise((resolve, reject) =>{ return new Promise((resolve, reject) =>{
axios.get(url, { axios.get(url, {
params: params params: params
@ -184,6 +205,7 @@ export function get(url, params){
* post方法对应post请求 * post方法对应post请求
*/ */
export function post(url, params) { export function post(url, params) {
getToken()
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios.post(url,params) axios.post(url,params)
.then(res => { .then(res => {
@ -256,6 +278,7 @@ export function post(url, params) {
* delete方法对应delete请求 * delete方法对应delete请求
*/ */
export function del(url, params){ export function del(url, params){
getToken()
return new Promise((resolve, reject) =>{ return new Promise((resolve, reject) =>{
axios.delete(url, { axios.delete(url, {
params: params params: params
@ -330,6 +353,7 @@ export function post(url, params) {
* @param {*} params * @param {*} params
*/ */
export function put(url, params){ export function put(url, params){
getToken()
return new Promise((resolve, reject) =>{ return new Promise((resolve, reject) =>{
axios.put(url, params) axios.put(url, params)
.then(res => { .then(res => {

Loading…
Cancel
Save