From 85a04fd06d6ab5aeb94f3b2a09034f050cbea24d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=BC=A0=E9=BE=99?= Date: Wed, 21 Sep 2022 17:14:41 +0800 Subject: [PATCH] fix: logout --- src/router/index.js | 36 ++++++++++++------ src/store/modules/permission/index.js | 3 ++ src/store/modules/tags/index.js | 4 ++ src/store/modules/user/index.js | 9 ++++- src/utils/auth/auth.js | 4 +- src/utils/http/helpers.js | 54 ++++++++++++--------------- src/utils/http/interceptors.js | 37 ++++++++++++------ 7 files changed, 91 insertions(+), 56 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 5c3645d..e30129b 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,22 +1,30 @@ import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router' import { setupRouterGuard } from './guard' -import { basicRoutes as routes, EMPTY_ROUTE, NOT_FOUND_ROUTE } from './routes' +import { basicRoutes, EMPTY_ROUTE, NOT_FOUND_ROUTE } from './routes' import { getToken, isNullOrWhitespace } from '@/utils' import { useUserStore, usePermissionStore } from '@/store' const isHash = import.meta.env.VITE_USE_HASH === 'true' export const router = createRouter({ history: isHash ? createWebHashHistory('/') : createWebHistory('/'), - routes, + routes: basicRoutes, scrollBehavior: () => ({ left: 0, top: 0 }), }) -export async function resetRouter() { - router.getRoutes().forEach((route) => { - const { name } = route - router.hasRoute(name) && router.removeRoute(name) - }) +export async function setupRouter(app) { await addDynamicRoutes() + setupRouterGuard(router) + app.use(router) +} + +export async function resetRouter() { + const basicRouteNames = getRouteNames(basicRoutes) + router.getRoutes().forEach((route) => { + const name = route.name + if (!basicRouteNames.includes(name)) { + router.removeRoute(name) + } + }) } export async function addDynamicRoutes() { @@ -44,8 +52,14 @@ export async function addDynamicRoutes() { } } -export async function setupRouter(app) { - await addDynamicRoutes() - setupRouterGuard(router) - app.use(router) +export function getRouteNames(routes) { + return routes.map((route) => getRouteName(route)).flat(1) +} + +function getRouteName(route) { + const names = [route.name] + if (route.children && route.children.length) { + names.push(...route.children.map((item) => getRouteName(item)).flat(1)) + } + return names } diff --git a/src/store/modules/permission/index.js b/src/store/modules/permission/index.js index 8dbad92..c551d18 100644 --- a/src/store/modules/permission/index.js +++ b/src/store/modules/permission/index.js @@ -53,5 +53,8 @@ export const usePermissionStore = defineStore('permission', { this.accessRoutes = accessRoutes return accessRoutes }, + resetPermission() { + this.$reset() + }, }, }) diff --git a/src/store/modules/tags/index.js b/src/store/modules/tags/index.js index 7f0771c..47f2079 100644 --- a/src/store/modules/tags/index.js +++ b/src/store/modules/tags/index.js @@ -57,5 +57,9 @@ export const useTagsStore = defineStore('tag', { router.push(filterTags[filterTags.length - 1].path) } }, + resetTags() { + this.setTags([]) + this.setActiveTag('') + }, }, }) diff --git a/src/store/modules/user/index.js b/src/store/modules/user/index.js index e317ae0..f14fb7f 100644 --- a/src/store/modules/user/index.js +++ b/src/store/modules/user/index.js @@ -1,4 +1,6 @@ import { defineStore } from 'pinia' +import { resetRouter } from '@/router' +import { useTagsStore, usePermissionStore } from '@/store' import { removeToken, toLogin } from '@/utils' import api from '@/api' @@ -34,8 +36,13 @@ export const useUserStore = defineStore('user', { } }, async logout() { + const { resetTags } = useTagsStore() + const { resetPermission } = usePermissionStore() removeToken() - this.userInfo = {} + resetTags() + resetPermission() + resetRouter() + this.$reset() toLogin() }, setUserInfo(userInfo = {}) { diff --git a/src/utils/auth/auth.js b/src/utils/auth/auth.js index 17a0926..afd7f7e 100644 --- a/src/utils/auth/auth.js +++ b/src/utils/auth/auth.js @@ -1,9 +1,11 @@ import { router } from '@/router' export function toLogin() { + const currentRoute = unref(router.currentRoute) + const needRedirect = !currentRoute.meta.requireAuth && !['/404', '/login'].includes(router.currentRoute.value.path) router.replace({ path: '/login', - query: { ...router.currentRoute.value.query, redirect: router.currentRoute.value.path }, + query: needRedirect ? { ...router.currentRoute.value.query, redirect: router.currentRoute.value.path } : {}, }) } diff --git a/src/utils/http/helpers.js b/src/utils/http/helpers.js index 7d3f454..450b2d7 100644 --- a/src/utils/http/helpers.js +++ b/src/utils/http/helpers.js @@ -1,5 +1,5 @@ import { useUserStore } from '@/store' -import { isNullOrUndef, removeToken, toLogin } from '@/utils' +import { isNullOrUndef } from '@/utils' export function addBaseParams(params) { if (!params.userId) { @@ -7,35 +7,27 @@ export function addBaseParams(params) { } } -export function resolveResError(error = {}) { - let { code, message } = error - if (isNullOrUndef(code)) { - // 未知错误 - code = -1 - message = message ?? '接口未知异常!' - } else { - switch (code) { - case 400: - message = message ?? '请求参数错误' - break - case 401: - message = message ?? '登录已过期' - removeToken() - toLogin() - break - case 403: - message = message ?? '没有权限' - break - case 404: - message = message ?? '资源或接口不存在' - break - case 500: - message = message ?? '服务器异常' - break - default: - message = message ?? '操作异常!' - break - } +export function resolveResError(code, message) { + switch (code) { + case 400: + message = message ?? '请求参数错误' + break + case 401: + message = message ?? '登录已过期' + useUserStore().logout() + break + case 403: + message = message ?? '没有权限' + break + case 404: + message = message ?? '资源或接口不存在' + break + case 500: + message = message ?? '服务器异常' + break + default: + message = message ?? `【${code}】: 未知异常!` + break } - return { code, message } + return message } diff --git a/src/utils/http/interceptors.js b/src/utils/http/interceptors.js index 73b4d51..dff6687 100644 --- a/src/utils/http/interceptors.js +++ b/src/utils/http/interceptors.js @@ -1,4 +1,4 @@ -import { getToken, toLogin } from '@/utils' +import { getToken } from '@/utils' import { resolveResError } from './helpers' export function reqResolve(config) { @@ -9,9 +9,7 @@ export function reqResolve(config) { const token = getToken() if (!token) { - // * 未登录或者token过期的情况下,跳转登录页重新登录 - toLogin() - return Promise.reject({ code: '-1', message: '未登录' }) + return Promise.reject({ code: 401, message: '登录已过期,请重新登录!' }) } /** @@ -28,18 +26,33 @@ export function reqReject(error) { } export function repResolve(response) { - const { noNeedTip } = response.config + // TODO: 处理不同的 response.headers + const { data, status, config, statusText } = response if (response.data?.code !== 0) { - const { code, message } = resolveResError(response?.data) - !noNeedTip && $message.error(message) + const code = data?.code ?? status + + /** 根据code处理对应的操作,并返回处理后的message */ + const message = resolveResError(code, data?.message ?? statusText) + + /** 需要错误提醒 */ + !config.noNeedTip && $message.error(message) return Promise.reject({ code, message, error: response?.data }) } - return Promise.resolve(response?.data) + return Promise.resolve(data) } export function repReject(error) { - const { noNeedTip } = error.response?.config || error.config - const { code, message } = resolveResError(error.response?.data) - !noNeedTip && $message.error(message) - return Promise.reject({ code, message, error }) + if (!error || !error.response) { + const code = error?.code + /** 根据code处理对应的操作,并返回处理后的message */ + const message = resolveResError(code, error.message) + $message?.error(message) + return Promise.reject({ code, message, error }) + } + const { data, status, config } = error.response + const code = data?.code ?? status + const message = resolveResError(code, data?.message ?? error.message) + /** 需要错误提醒 */ + !config?.noNeedTip && $message.error(message) + return Promise.reject({ code, message, error: error.response?.data }) }