diff --git a/src/App.vue b/src/App.vue index 6026b58b..d8e53f4c 100644 --- a/src/App.vue +++ b/src/App.vue @@ -53,7 +53,6 @@ function beforeunloadHandler() { } async function init() { - await validateToken(); initDict("content_type"); //内容分类 initDict("project_level"); //项目级别 initDict("project_sys"); //培训分类 @@ -64,17 +63,6 @@ async function init() { initDict("band"); //band } -async function validateToken() { - try{ - await request(VALIDATE_TOKEN) - await getUserInfo(); - await getUserPermission(); - }catch (e) { - console.log('token失效 跳转到登录页') - } - -} - function unloadHandler() { time2.value = new Date().getTime() - time1.value; if (time2.value <= 5) { diff --git a/src/api/request.js b/src/api/request.js index 9798c01c..7380f9fa 100644 --- a/src/api/request.js +++ b/src/api/request.js @@ -413,6 +413,7 @@ export async function request(_url, params) { localStorage.removeItem('refreshPage') return Promise.reject(res); }else if(res.code=== 1001){ + setCookie('token',getCookieForName('refreshToken')) request(REFRESH_TOKEN).then((res)=>{ if(res.code===0 || res.code === 200){ setCookie('token',res.data) diff --git a/src/main.js b/src/main.js index ae142895..ad4a39af 100644 --- a/src/main.js +++ b/src/main.js @@ -16,6 +16,9 @@ import 'element-plus/dist/index.css' import "@/assets/scss/common.scss" import Antd from 'ant-design-vue'; import 'ant-design-vue/dist/antd.css'; +import {getCookieForName} from "@/api/method"; +import {request} from "@/api/request"; +import {VALIDATE_TOKEN} from "@/api/apis"; // import axios from 'axios' // axios.defaults.withCredentials = true; // import zhCN from 'ant-design-vue/es/locale/zh_CN'; @@ -25,7 +28,24 @@ app.config.warnHandler = () => null; // app.use(ElementPlus, { // locale: zhCn, // }) + +router.beforeEach(async (to, from, next) => { + if (!getCookieForName("token")) { + window.location.href = process.env.VUE_APP_LOGIN_URL + encodeURIComponent(window.location.protocol + process.env.VUE_APP_BOE_API_URL + process.env.VUE_APP_BASE + router.currentRoute.value.fullPath) + } + //第一次进入 没有用户信息 + if(!store.state.userInfo.userId){ + try{ + await request(VALIDATE_TOKEN) + await getUserInfo(); + await getUserPermission(); + next(); + }catch (e){ + console.log('token失效 跳转到登录页') + } + } +}) app.use(Antd); app.use(router); app.use(store); -app.mount('#app'); +app.mount('#app'); \ No newline at end of file