From 43f36d640c755413bbaec37dcfcff6dd932b399a Mon Sep 17 00:00:00 2001 From: piexlmax Date: Fri, 7 Jan 2022 19:09:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86token=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E6=97=A0=E6=B3=95=E9=80=80=E5=87=BA=E7=9A=84bug=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=AD=97=E6=AE=B5=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E4=B8=BA=E7=A9=BA=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/permission.js | 7 +++---- web/src/pinia/modules/dictionary.js | 10 +++++----- web/src/utils/request.js | 12 ++++++++---- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/web/src/permission.js b/web/src/permission.js index 606fc953..fdeb34c2 100644 --- a/web/src/permission.js +++ b/web/src/permission.js @@ -7,9 +7,8 @@ let asyncRouterFlag = 0 const whiteList = ['Login', 'Init'] -const getRouter = async() => { +const getRouter = async(userStore) => { const routerStore = useRouterStore() - const userStore = useUserStore() await routerStore.SetAsyncRouter() await userStore.GetUserInfo() const asyncRouters = routerStore.asyncRouters @@ -45,7 +44,7 @@ router.beforeEach(async(to, from, next) => { if (token) { if (!asyncRouterFlag && whiteList.indexOf(from.name) < 0) { asyncRouterFlag++ - await getRouter() + await getRouter(userStore) } next({ name: userStore.userInfo.authority.defaultRouter }) } else { @@ -57,7 +56,7 @@ router.beforeEach(async(to, from, next) => { // 添加flag防止多次获取动态路由和栈溢出 if (!asyncRouterFlag && whiteList.indexOf(from.name) < 0) { asyncRouterFlag++ - await getRouter() + await getRouter(userStore) next({ ...to, replace: true }) } else { if (to.matched.length) { diff --git a/web/src/pinia/modules/dictionary.js b/web/src/pinia/modules/dictionary.js index 3b70db50..53692228 100644 --- a/web/src/pinia/modules/dictionary.js +++ b/web/src/pinia/modules/dictionary.js @@ -6,8 +6,8 @@ import { ref } from 'vue' export const useDictionaryStore = defineStore('dictionary', () => { const dictionaryMap = ref({}) - const setDictionaryMap = (dictionaryMap) => { - dictionaryMap.value = { ...dictionaryMap.value, ...dictionaryMap } + const setDictionaryMap = (dictionaryRes) => { + dictionaryMap.value = { ...dictionaryMap.value, ...dictionaryRes } } const getDictionary = async(type) => { @@ -16,7 +16,7 @@ export const useDictionaryStore = defineStore('dictionary', () => { } else { const res = await findSysDictionary({ type }) if (res.code === 0) { - const dictionaryMap = {} + const dictionaryRes = {} const dict = [] res.data.resysDictionary.sysDictionaryDetails && res.data.resysDictionary.sysDictionaryDetails.forEach(item => { dict.push({ @@ -24,8 +24,8 @@ export const useDictionaryStore = defineStore('dictionary', () => { value: item.value }) }) - dictionaryMap[res.data.resysDictionary.type] = dict - setDictionaryMap(dictionaryMap) + dictionaryRes[res.data.resysDictionary.type] = dict + setDictionaryMap(dictionaryRes) return dictionaryMap.value[type] } } diff --git a/web/src/utils/request.js b/web/src/utils/request.js index 4faa2e1e..4d4a7edb 100644 --- a/web/src/utils/request.js +++ b/web/src/utils/request.js @@ -2,6 +2,7 @@ import axios from 'axios' // 引入axios import { ElMessage, ElMessageBox } from 'element-plus' import { useUserStore } from '@/pinia/modules/user' import { emitter } from '@/utils/bus.js' +import router from '@/router/index' const service = axios.create({ baseURL: import.meta.env.VITE_BASE_API, @@ -57,9 +58,9 @@ service.interceptors.request.use( // http response 拦截器 service.interceptors.response.use( response => { + const userStore = useUserStore() closeLoading() if (response.headers['new-token']) { - const userStore = useUserStore() userStore.setToken(response.headers['new-token']) } if (response.data.code === 0 || response.headers.success === 'true') { @@ -74,8 +75,9 @@ service.interceptors.response.use( type: 'error' }) if (response.data.data && response.data.data.reload) { - const userStore = useUserStore() - userStore.LoginOut() + userStore.token = '' + localStorage.clear() + router.push({ name: 'Login', replace: true }) } return response.data.msg ? response.data : response } @@ -95,7 +97,9 @@ service.interceptors.response.use( }) .then(() => { const userStore = useUserStore() - userStore.LoginOut() + userStore.token = '' + localStorage.clear() + router.push({ name: 'Login', replace: true }) }) break case 404: