From 845084f77f944fb589e10ab13bef7e7aa4cfde9c Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Fri, 19 Apr 2024 19:51:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=B3=BB=E7=BB=9F=E9=A6=96=E6=AC=A1?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=BC=95=E5=AF=BC=E6=A0=87=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index.ts | 58 ++++++++++++++++++++++------------------ src/store/modules/app.ts | 4 +++ 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/router/index.ts b/src/router/index.ts index ea60adc5..f5b566f0 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -142,16 +142,26 @@ router.afterEach((to, from, failure) => { }); /**无Token可访问页面地址白名单 */ -const WHITE_LIST: string[] = ['/login', '/auth-redirect', '/help', '/register', '/quick-build']; +const WHITE_LIST: string[] = [ + '/login', + '/auth-redirect', + '/help', + '/register', + '/quick-build', +]; /**全局路由-前置守卫 */ -router.beforeEach((to, from, next) => { +router.beforeEach(async (to, from, next) => { NProgress.start(); const token = getToken(); // 获取系统配置信息 const appStore = useAppStore(); if (!appStore.loginBackground) { - appStore.fnSysConf(); + await appStore.fnSysConf(); + console.log(to.path); + if (appStore.sysGuide && to.path !== '/quick-build') { + next({ name: 'QuickBuild' }); + } } // 没有token @@ -174,31 +184,27 @@ router.beforeEach((to, from, next) => { // 判断当前用户是否有角色信息 const user = useUserStore(); if (user.roles && user.roles.length === 0) { - // 获取用户信息 - user - .fnGetInfo() - .then(() => { - return useRouterStore().generateRoutes(); - }) - .then(accessRoutes => { - // 根据后台配置生成可访问的路由表 - if (accessRoutes && accessRoutes.length !== 0) { - for (const route of accessRoutes) { - // 动态添加可访问路由表,http开头会异常 - if (!validHttp(route.path)) { - router.addRoute(route); - } + try { + // 获取用户信息 + await user.fnGetInfo(); + // 获取路由信息 + const accessRoutes = await useRouterStore().generateRoutes(); + // 根据后台配置生成可访问的路由表 + if (accessRoutes && accessRoutes.length !== 0) { + for (const route of accessRoutes) { + // 动态添加可访问路由表,http开头会异常 + if (!validHttp(route.path)) { + router.addRoute(route); } } - // 刷新替换原先路由,确保addRoutes已完成 - next({ ...to, replace: true }); - }) - .catch(e => { - console.error(`[${to.path}]: ${e.message}`); - user.fnLogOut().finally(() => { - next({ name: 'Login' }); - }); - }); + } + // 刷新替换原先路由,确保addRoutes已完成 + next({ ...to, replace: true }); + } catch (error: any) { + console.error(`[${to.path}]: ${error.message}`); + await user.fnLogOut(); + next({ name: 'Login' }); + } } else { next(); } diff --git a/src/store/modules/app.ts b/src/store/modules/app.ts index e29f6a86..b28838f3 100644 --- a/src/store/modules/app.ts +++ b/src/store/modules/app.ts @@ -17,6 +17,8 @@ type AppStore = { /**服务版本 */ version: string; buildTime: string; + /**系统首次使用 */ + sysGuide: boolean; // 序列号 serialNum: string; /**应用版权声明 */ @@ -48,6 +50,7 @@ const useAppStore = defineStore('app', { version: `-`, buildTime: `-`, + sysGuide: false, serialNum: `-`, copyright: `Copyright ©2023 For ${import.meta.env.VITE_APP_NAME}`, logoType: 'icon', @@ -80,6 +83,7 @@ const useAppStore = defineStore('app', { if (res.code === RESULT_CODE_SUCCESS && res.data) { this.version = res.data.version; this.buildTime = res.data.buildTime; + this.sysGuide = res.data.sysGuide === 'true'; this.serialNum = res.data.serialNum; this.appName = res.data.title; this.copyright = res.data.copyright;