mirror of
https://github.com/continew-org/continew-admin-ui.git
synced 2025-09-21 18:57:08 +08:00
refactor: 优化路由守卫代码(同步 GiDemo 更新)
This commit is contained in:
@@ -9,8 +9,6 @@ import ArcoVueIcon from '@arco-design/web-vue/es/icon'
|
|||||||
import App from './App.vue'
|
import App from './App.vue'
|
||||||
import router from './router'
|
import router from './router'
|
||||||
|
|
||||||
import '@/router/guard'
|
|
||||||
|
|
||||||
// 使用动画库
|
// 使用动画库
|
||||||
import 'animate.css/animate.min.css'
|
import 'animate.css/animate.min.css'
|
||||||
|
|
||||||
@@ -25,6 +23,8 @@ import 'virtual:svg-icons-register'
|
|||||||
|
|
||||||
// 自定义指令
|
// 自定义指令
|
||||||
import directives from './directives'
|
import directives from './directives'
|
||||||
|
|
||||||
|
// 状态管理
|
||||||
import pinia from '@/stores'
|
import pinia from '@/stores'
|
||||||
|
|
||||||
// 对特定组件进行默认配置
|
// 对特定组件进行默认配置
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { Button, Message, Notification, Space } from '@arco-design/web-vue'
|
import { Button, Message, Notification, Space } from '@arco-design/web-vue'
|
||||||
import NProgress from 'nprogress'
|
import NProgress from 'nprogress'
|
||||||
import router from '@/router'
|
import type { Router } from 'vue-router'
|
||||||
import { useRouteStore, useUserStore } from '@/stores'
|
import { useRouteStore, useUserStore } from '@/stores'
|
||||||
import { getToken } from '@/utils/auth'
|
import { getToken } from '@/utils/auth'
|
||||||
import { isHttp } from '@/utils/validate'
|
import { isHttp } from '@/utils/validate'
|
||||||
@@ -36,7 +36,10 @@ const handleNotification = () => {
|
|||||||
closable: true,
|
closable: true,
|
||||||
position: 'bottomRight',
|
position: 'bottomRight',
|
||||||
footer: () => {
|
footer: () => {
|
||||||
return h(Space, {}, () => [h(Button, { type: 'primary', onClick: () => onUpdateSystem(id) }, '更新'), h(Button, { type: 'secondary', onClick: () => onCloseUpdateSystem(id) }, '关闭')])
|
return h(Space, {}, () => [h(Button, {
|
||||||
|
type: 'primary',
|
||||||
|
onClick: () => onUpdateSystem(id),
|
||||||
|
}, '更新'), h(Button, { type: 'secondary', onClick: () => onCloseUpdateSystem(id) }, '关闭')])
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -75,64 +78,67 @@ export const resetHasRouteFlag = () => {
|
|||||||
hasRouteFlag = false
|
hasRouteFlag = false
|
||||||
}
|
}
|
||||||
|
|
||||||
router.beforeEach(async (to, from, next) => {
|
/** 初始化路由守卫 */
|
||||||
NProgress.start()
|
export const setupRouterGuard = (router: Router) => {
|
||||||
const userStore = useUserStore()
|
router.beforeEach(async (to, from, next) => {
|
||||||
const routeStore = useRouteStore()
|
NProgress.start()
|
||||||
// 判断该用户是否登录
|
const userStore = useUserStore()
|
||||||
if (getToken()) {
|
const routeStore = useRouteStore()
|
||||||
if (to.path === '/login') {
|
// 判断该用户是否登录
|
||||||
// 如果已经登录,并准备进入 Login 页面,则重定向到主页
|
if (getToken()) {
|
||||||
next()
|
if (to.path === '/login') {
|
||||||
} else {
|
// 如果已经登录,并准备进入 Login 页面,则重定向到主页
|
||||||
if (!hasRouteFlag) {
|
|
||||||
try {
|
|
||||||
await userStore.getInfo()
|
|
||||||
if (userStore.userInfo.pwdExpired && to.path !== '/pwdExpired') {
|
|
||||||
Message.warning('密码已过期,请修改密码')
|
|
||||||
next('/pwdExpired')
|
|
||||||
}
|
|
||||||
const accessRoutes = await routeStore.generateRoutes()
|
|
||||||
accessRoutes.forEach((route) => {
|
|
||||||
if (!isHttp(route.path)) {
|
|
||||||
router.addRoute(route) // 动态添加可访问路由表
|
|
||||||
}
|
|
||||||
})
|
|
||||||
hasRouteFlag = true
|
|
||||||
// 确保添加路由已完成
|
|
||||||
// 设置 replace: true, 因此导航将不会留下历史记录
|
|
||||||
next({ ...to, replace: true })
|
|
||||||
} catch (error: any) {
|
|
||||||
// 过程中发生任何错误,都直接重置 Token,并重定向到登录页面
|
|
||||||
await userStore.logoutCallBack()
|
|
||||||
next(`/login?redirect=${to.path}`)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
next()
|
next()
|
||||||
|
} else {
|
||||||
|
if (!hasRouteFlag) {
|
||||||
|
try {
|
||||||
|
await userStore.getInfo()
|
||||||
|
if (userStore.userInfo.pwdExpired && to.path !== '/pwdExpired') {
|
||||||
|
Message.warning('密码已过期,请修改密码')
|
||||||
|
next('/pwdExpired')
|
||||||
|
}
|
||||||
|
const accessRoutes = await routeStore.generateRoutes()
|
||||||
|
accessRoutes.forEach((route) => {
|
||||||
|
if (!isHttp(route.path)) {
|
||||||
|
router.addRoute(route) // 动态添加可访问路由表
|
||||||
|
}
|
||||||
|
})
|
||||||
|
hasRouteFlag = true
|
||||||
|
// 确保添加路由已完成
|
||||||
|
// 设置 replace: true, 因此导航将不会留下历史记录
|
||||||
|
next({ ...to, replace: true })
|
||||||
|
} catch (error: any) {
|
||||||
|
// 过程中发生任何错误,都直接重置 Token,并重定向到登录页面
|
||||||
|
await userStore.logoutCallBack()
|
||||||
|
next(`/login?redirect=${to.path}`)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 如果没有 Token
|
||||||
|
if (whiteList.includes(to.path)) {
|
||||||
|
// 如果在免登录的白名单中,则直接进入
|
||||||
|
next()
|
||||||
|
} else {
|
||||||
|
// 其他没有访问权限的页面将被重定向到登录页面
|
||||||
|
next('/login')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// 如果没有 Token
|
// 生产环境开启检测版本更新
|
||||||
if (whiteList.includes(to.path)) {
|
const isProd = import.meta.env.PROD
|
||||||
// 如果在免登录的白名单中,则直接进入
|
if (isProd) {
|
||||||
next()
|
await compareTag()
|
||||||
} else {
|
|
||||||
// 其他没有访问权限的页面将被重定向到登录页面
|
|
||||||
next('/login')
|
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
|
|
||||||
// 生产环境开启检测版本更新
|
router.onError(() => {
|
||||||
const isProd = import.meta.env.PROD
|
NProgress.done()
|
||||||
if (isProd) {
|
})
|
||||||
await compareTag()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
router.onError(() => {
|
router.afterEach(() => {
|
||||||
NProgress.done()
|
NProgress.done()
|
||||||
})
|
})
|
||||||
|
}
|
||||||
router.afterEach(() => {
|
|
||||||
NProgress.done()
|
|
||||||
})
|
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
import { createRouter, createWebHistory } from 'vue-router'
|
import { createRouter, createWebHistory } from 'vue-router'
|
||||||
import { useRouteStore } from '@/stores'
|
import { useRouteStore } from '@/stores'
|
||||||
import { constantRoutes, systemRoutes } from '@/router/route'
|
import { constantRoutes, systemRoutes } from '@/router/route'
|
||||||
|
import { setupRouterGuard } from '@/router/guard'
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(import.meta.env.BASE_URL),
|
history: createWebHistory(import.meta.env.BASE_URL),
|
||||||
@@ -8,6 +9,8 @@ const router = createRouter({
|
|||||||
scrollBehavior: () => ({ left: 0, top: 0 }),
|
scrollBehavior: () => ({ left: 0, top: 0 }),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
setupRouterGuard(router)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @description 重置路由
|
* @description 重置路由
|
||||||
* @description 注意:所有动态路由路由必须带有 name 属性,否则可能会不能完全重置干净
|
* @description 注意:所有动态路由路由必须带有 name 属性,否则可能会不能完全重置干净
|
||||||
|
Reference in New Issue
Block a user