From 79d840da0026017d1b23e5a44c683ec4d2b71dfa Mon Sep 17 00:00:00 2001 From: yujialong <479214531@qq.com> Date: Tue, 18 May 2021 16:00:19 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E9=80=80=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/page/Login.vue | 1 + src/store/index.js | 6 +++++- src/utils/api.js | 1 + src/utils/core.js | 24 +++++++++++++++++++++++- src/utils/http.js | 24 ++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/components/page/Login.vue b/src/components/page/Login.vue index 7de98d5..944a76e 100644 --- a/src/components/page/Login.vue +++ b/src/components/page/Login.vue @@ -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}); 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("dataTime", { dataTime : res.message.user.dataTime}); let redirect = decodeURIComponent(this.$route.query.redirect || '/dashboard') this.$router.replace(redirect) }).catch(res => {}); diff --git a/src/store/index.js b/src/store/index.js index ee3d201..3dafac7 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -27,7 +27,8 @@ const store = new Vuex.Store({ assesmentId: '', staffId: '', studentId: '', - answerHistory: [] + answerHistory: [], + dataTime: '', }, mutations:{ courseIdData(state,payload){ @@ -39,6 +40,9 @@ const store = new Vuex.Store({ userPhoto(state,payload){ state.avatar = payload.avatar }, + dataTime(state,payload){ + state.dataTime = payload.dataTime + }, customerData (state, payload) { state.customerId = payload.customer_id }, diff --git a/src/utils/api.js b/src/utils/api.js index 0808c85..c723873 100644 --- a/src/utils/api.js +++ b/src/utils/api.js @@ -10,6 +10,7 @@ if(process.env.NODE_ENV === 'development'){ export default { logins: `${host}/liuwanr/userInfo/logins`, //登录  + queryToken: `${host}/liuwanr/userInfo/queryToken`, save: `${host}/evaluation/tms/userInfo/add`, //注册 updateLogInNumber: `${host}/liuwanr/userInfo/updateLogInNumber`, //用户登录修改登录次数和登陆时间 uploadUserAvatars: `${host}/liuwanr/userInfo/uploadUserAvatars`, //上传头像 diff --git a/src/utils/core.js b/src/utils/core.js index 8e55c08..98860b6 100644 --- a/src/utils/core.js +++ b/src/utils/core.js @@ -179,6 +179,27 @@ function encodeString(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{ orderfounder, projectPermissions, @@ -195,5 +216,6 @@ export default{ getCaption, toSubSystem, formatDate, - encodeString + encodeString, + logout } \ No newline at end of file diff --git a/src/utils/http.js b/src/utils/http.js index 647d884..77d35d5 100644 --- a/src/utils/http.js +++ b/src/utils/http.js @@ -1,8 +1,10 @@ import axios from 'axios'; import QS from 'qs'; import store from '../store/index' +import api from './api' import { Message } from 'element-ui' import router from '../router/index' +import core from './core' // 环境的切换 // 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请求 */ export function get(url, params){ + getToken() return new Promise((resolve, reject) =>{ axios.get(url, { params: params @@ -184,6 +205,7 @@ export function get(url, params){ * post方法,对应post请求 */ export function post(url, params) { + getToken() return new Promise((resolve, reject) => { axios.post(url,params) .then(res => { @@ -256,6 +278,7 @@ export function post(url, params) { * delete方法,对应delete请求 */ export function del(url, params){ + getToken() return new Promise((resolve, reject) =>{ axios.delete(url, { params: params @@ -330,6 +353,7 @@ export function post(url, params) { * @param {*} params */ export function put(url, params){ + getToken() return new Promise((resolve, reject) =>{ axios.put(url, params) .then(res => {