From 5f6efabe89cba1339a938696e8d93fc9f03834f4 Mon Sep 17 00:00:00 2001 From: yujialong <479214531@qq.com> Date: Tue, 10 Aug 2021 17:48:19 +0800 Subject: [PATCH] fix --- README.md | 23 ----------------------- src/layouts/home/index.vue | 24 ++++++++++++++++++++++-- src/libs/util.js | 24 +++++++++++++++++++++++- src/pages/achievement/list/index.vue | 2 +- src/pages/assessment/list/index.vue | 10 +++++----- src/plugins/requests/index.js | 2 +- src/setting.js | 15 ++++++++++++--- vue.config.js | 2 +- 8 files changed, 65 insertions(+), 37 deletions(-) delete mode 100644 README.md diff --git a/README.md b/README.md deleted file mode 100644 index 81286df..0000000 --- a/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# 该项目是前端项目基础模板 -## 拉取该项目就可以直接上手开发. - ---- - -### 注意事项: -1. api里面存放接口地址 -2. assets里面存放静态文件,注意图片跟svg最好分开,方便后期管理 -3. components存放公共组件 -4. layouts存放页面布局组件,注意导航跟头部是分开的,放在一起太臃肿 -5. libs存放工具包.封装了cookie,localStorate,sessionStorage的增删改查; auth是用来生成角色权限里控制的按钮,生成后添加到store里面; route是做动态路由的,同时把生成的路由集合添加到store里面 -6. mixins可以自定义mixin -7. pages存放页面,一个模块为一个文件夹,默认有个list文件夹,默认跳转到list,要添加子模块的话直接建list的同级文件夹,里面统一命名index.vue -8. plugins里面aliplayer是阿里云播放器,按需引用; auth是按钮级别的鉴权; filters可以自定义filter; requests是全局请求配置文件; throttle为防抖 -9. router为路由,跟pages一样,一个模块为一个文件夹,存放在modules里面; permission为路由全局控制; route里引用modules里面定义的路由模块,每加一个模块就要在这里引用 -10. store存放store模块,直接在modules里定义文件就会自动引用,一个模块为一个文件夹,模块之间勿混淆使用 -11. styles存放全局scss. default里设置项目主题色; font存放字体文件; layout存放布局样式; lib里面有定义工具class,可以直接引用里面的class; pages为页面的公用样式; common为全局公共样式; var可以定义mixin和全局变量 - ---- - -#### 全局配置都放在setting.js里面,项目参数,比如默认密码,平台id,都放在这里,页面里直接引入该文件即可,不要在页面里写死公共使用的系统参数。 - -#### vue.config.js里要定义固定端口,不然项目启动多了会乱,每开一个项目就开一个新的端口,每个项目端口不要冲突 \ No newline at end of file diff --git a/src/layouts/home/index.vue b/src/layouts/home/index.vue index 9c66f89..d6858f8 100644 --- a/src/layouts/home/index.vue +++ b/src/layouts/home/index.vue @@ -18,6 +18,8 @@ import vHead from '../header' import navbar from '../navbar' import vFooter from '../footer' +import { mapState, mapActions } from 'vuex' +import util from '@/libs/util' import Setting from '@/setting' export default { data() { @@ -30,8 +32,26 @@ export default { navbar, vFooter }, - created() { - + mounted() { + this.autoLogout() + }, + methods: { + ...mapActions('user', [ + 'logout' + ]), + autoLogout(){ + let lastTime = new Date().getTime() + document.onmousedown = () => { + lastTime = new Date().getTime() + } + + setInterval(() => { + if(util.local.get(Setting.tokenKey) && (new Date().getTime() - lastTime) > Setting.autoLogoutTime){ + util.errorMsg('用户登录过期,请重新登录') + setTimeout(this.logout,1500) + } + },1000) + } } }; diff --git a/src/libs/util.js b/src/libs/util.js index ad89f22..0250dc6 100644 --- a/src/libs/util.js +++ b/src/libs/util.js @@ -1,8 +1,12 @@ import cookies from './util.cookies' import {_local,_session} from './util.db' import { Message } from 'element-ui' +import store from '@/store' +import axios from 'axios' +import api from '@/api' +import Setting from '@/setting' - +let logout = false const roleList = { '1': '超级管理员', '13': '管理员', @@ -156,6 +160,24 @@ const util = { // 传入roleId,返回角色名称 getRoleName(roleId){ return roleList[roleId] || '未知状态' + }, + // 登录互踢 + getToken(){ + if(process.env.NODE_ENV != 'production'){ + if(store.state.user.dataTime && !logout){ + axios.get(`${api.queryToken}?token=${_local.get(Setting.tokenKey)}`).then(res => { + if(store.state.user.dataTime && (res.data.message != store.state.user.dataTime)){ + logout || Message.error('您已在另一台设备登录,本次登录已下线!') + logout = true + setTimeout(() => { + _local.remove(Setting.storeKey) + _local.remove(Setting.tokenKey) + location.reload() + },1500) + } + }).catch(err => {}) + } + } } } diff --git a/src/pages/achievement/list/index.vue b/src/pages/achievement/list/index.vue index f7325ba..21f1a99 100644 --- a/src/pages/achievement/list/index.vue +++ b/src/pages/achievement/list/index.vue @@ -132,7 +132,7 @@ export default { month: function(val){ if(val){ let unit = 24 * 60 * 60 * 1000 - this.date = [this.formatDate('yyyy-MM-dd',new Date(new Date().getTime() - unit * 30 * val)),this.formatDate('yyyy-MM-dd',new Date(new Date().getTime() + unit))] + this.date = [util.formatDate('yyyy-MM-dd',new Date(new Date().getTime() - unit * 30 * val)),util.formatDate('yyyy-MM-dd',new Date(new Date().getTime() + unit))] }else{ this.date = [] } diff --git a/src/pages/assessment/list/index.vue b/src/pages/assessment/list/index.vue index 9e51e0c..fc3ce3d 100644 --- a/src/pages/assessment/list/index.vue +++ b/src/pages/assessment/list/index.vue @@ -209,7 +209,7 @@ export default { let hours = Math.floor(total / (60 * 60)) let minutes = Math.floor(total % (60 * 60) / 60) let seconds = Math.floor(total % (60 * 60) % 60) - time = `${that.formateTime(hours)}:${that.formateTime(minutes)}:${that.formateTime(seconds)}` + time = `${util.formateTime(hours)}:${util.formateTime(minutes)}:${util.formateTime(seconds)}` }else{ clearInterval(timer) } @@ -257,10 +257,10 @@ export default { }, mounted() { this.getData() - // this.timer = setInterval(this.getData,1000) - // this.$once('hook:beforeDestroy',() => { - // clearInterval(this.timer) - // }) + this.timer = setInterval(this.getData,1000) + this.$once('hook:beforeDestroy',() => { + clearInterval(this.timer) + }) }, methods: { getData() { diff --git a/src/plugins/requests/index.js b/src/plugins/requests/index.js index 346ae8a..1800bf4 100644 --- a/src/plugins/requests/index.js +++ b/src/plugins/requests/index.js @@ -2,7 +2,6 @@ import axios from 'axios' import util from '@/libs/util' import router from '@/router/index' import Setting from '@/setting' -import store from '@/store' const service = axios.create({ baseURL: Setting.apiBaseURL, @@ -13,6 +12,7 @@ service.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8' // 请求拦截器 service.interceptors.request.use(config => { + util.getToken() let token = util.local.get(Setting.tokenKey) if(token) config.headers.token = token return config diff --git a/src/setting.js b/src/setting.js index 4f0aede..ae31bd4 100644 --- a/src/setting.js +++ b/src/setting.js @@ -40,7 +40,10 @@ const Setting = { },{ id: 7, label: '量化投资策略建模实验教学系统' - },] : (isBeta ? [{ + },{ + id: 8, + label: '大数据分析实验教学系统' + }] : (isBeta ? [{ id: 1, label: 'Python程序设计教学系统' } @@ -91,6 +94,12 @@ const Setting = { },{ id: 8, label: '大数据分析实验教学系统' + },{ + id: 9, + label: 'Python数据清洗教学实验系统' + },{ + id: 10, + label: 'Python数据采集(爬虫)教学实验系统' }]), // 长时间未操作,自动退出登录时间 autoLogoutTime: 3600000, @@ -107,11 +116,11 @@ const Setting = { /** * 免登录保存的token的key */ - tokenKey: 'oc_token', + tokenKey: 'oc_server_token', /** * sessionStorage里state的key */ - storeKey: 'oc_store', + storeKey: 'oc_server_store', /** * 默认密码 */ diff --git a/vue.config.js b/vue.config.js index a832daa..f1cda53 100644 --- a/vue.config.js +++ b/vue.config.js @@ -14,6 +14,6 @@ module.exports = {   outputDir: Setting.outputDir,   assetsDir: Setting.assetsDir, devServer: { - port: 8094 // 固定端口 + port: 8081 // 固定端口 } } \ No newline at end of file