From 0684086f6d7d399dff8d3596c2b5bd5cfd5b2632 Mon Sep 17 00:00:00 2001 From: icssoa <615206459@qq.com> Date: Wed, 18 May 2022 15:33:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20router.href?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cool/router/index.ts | 11 ++++++++++- src/cool/service/request.ts | 15 ++++++--------- src/modules/base/store/user.ts | 5 +++-- src/shims-vue.d.ts | 5 +++++ 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/cool/router/index.ts b/src/cool/router/index.ts index a6db698..102b998 100644 --- a/src/cool/router/index.ts +++ b/src/cool/router/index.ts @@ -46,7 +46,7 @@ const routes: RouteRecordRaw[] = [ const router = createRouter({ history: config.app.router.mode == "history" ? createWebHistory() : createWebHashHistory(), routes -}); +}) as CoolRouter; // 路由守卫 router.beforeEach((to: any, _: any, next: NavigationGuardNext) => { @@ -75,6 +75,15 @@ router.beforeEach((to: any, _: any, next: NavigationGuardNext) => { next(); }); +// 自定义 +router.href = function (path: string) { + const url = import.meta.env.BASE_URL + path; + + if (url != location.pathname) { + location.href = url; + } +}; + let lock = false; // 错误监听 diff --git a/src/cool/service/request.ts b/src/cool/service/request.ts index 9694f5d..8051862 100644 --- a/src/cool/service/request.ts +++ b/src/cool/service/request.ts @@ -3,7 +3,7 @@ import NProgress from "nprogress"; import "nprogress/nprogress.css"; import { ElMessage } from "element-plus"; import { isDev, config } from "/@/cool"; -import { href, storage } from "/@/cool/utils"; +import { storage } from "/@/cool/utils"; import { useBaseStore } from "/$/base"; import { router } from "../router"; @@ -115,28 +115,25 @@ axios.interceptors.response.use( if (error.response) { const { status, config } = error.response; - const message = `${config.url} ${status}`; - - console.error(message); if (isDev) { - ElMessage.error(message); + ElMessage.error(`${config.url} ${status}`); } else { switch (status) { case 401: - router.push("/401"); + router.href("401"); break; case 403: - router.push("/403"); + router.href("403"); break; case 500: - router.push("/500"); + router.href("500"); break; case 502: - router.push("/502"); + router.href("502"); break; } } diff --git a/src/modules/base/store/user.ts b/src/modules/base/store/user.ts index bef5926..4c964b2 100644 --- a/src/modules/base/store/user.ts +++ b/src/modules/base/store/user.ts @@ -1,7 +1,7 @@ import { defineStore } from "pinia"; import { ref } from "vue"; import { href, storage } from "/@/cool/utils"; -import { service, config } from "/@/cool"; +import { service, config, router } from "/@/cool"; interface User { id: number; @@ -79,8 +79,9 @@ export const useUserStore = defineStore("user", function () { try { await service.base.comm.logout(); } catch {} + clear(); - location.href = "/login"; + router.href("login"); } // 获取用户信息 diff --git a/src/shims-vue.d.ts b/src/shims-vue.d.ts index 39981b8..2ae6901 100644 --- a/src/shims-vue.d.ts +++ b/src/shims-vue.d.ts @@ -1,5 +1,6 @@ /* eslint-disable */ import type { App } from "vue"; +import { Router } from "vue-router"; declare module "*.vue" { import type { DefineComponent } from "vue"; @@ -7,3 +8,7 @@ declare module "*.vue" { export default component; } + +declare interface CoolRouter extends Router { + href(path: string): void; +}