ADD: 模块激活流程

This commit is contained in:
贾同学
2025-10-14 19:00:47 +08:00
parent 7afccb58fd
commit 55ff45f9a9
16 changed files with 976 additions and 811 deletions

View File

@@ -3,14 +3,14 @@ import { useUserStore } from '@/stores/modules/user'
import { useAuthStore } from '@/stores/modules/auth'
import { LOGIN_URL, ROUTER_WHITE_LIST } from '@/config'
import { initDynamicRouter } from '@/routers/modules/dynamicRouter'
import { staticRouter, errorRouter } from '@/routers/modules/staticRouter'
import { staticRouter } from '@/routers/modules/staticRouter'
import NProgress from '@/config/nprogress'
const mode = import.meta.env.VITE_ROUTER_MODE
const routerMode = {
hash: () => createWebHashHistory(),
history: () => createWebHistory(),
hash: () => createWebHashHistory(),
history: () => createWebHistory()
}
/**
@@ -30,76 +30,80 @@ const routerMode = {
* @param meta.isKeepAlive ==> 当前路由是否缓存
* */
const router = createRouter({
history: routerMode[mode](),
routes: [...staticRouter],
// 不区分路由大小写,非严格模式下提供了更宽松的路径匹配
strict: false,
// 页面刷新时,滚动条位置还原
scrollBehavior: () => ({ left: 0, top: 0 }),
history: routerMode[mode](),
routes: [...staticRouter],
// 不区分路由大小写,非严格模式下提供了更宽松的路径匹配
strict: false,
// 页面刷新时,滚动条位置还原
scrollBehavior: () => ({ left: 0, top: 0 })
})
/**
* @description 路由拦截 beforeEach
* */
router.beforeEach(async (to, from, next) => {
const userStore = useUserStore()
const authStore = useAuthStore()
// 1.NProgress 开始
NProgress.start()
// 2.动态设置标题
const title = import.meta.env.VITE_GLOB_APP_TITLE
document.title = to.meta.title ? `${to.meta.title} - ${title}` : title
const userStore = useUserStore()
const authStore = useAuthStore()
// 1.NProgress 开始
NProgress.start()
// 2.动态设置标题
const title = import.meta.env.VITE_GLOB_APP_TITLE
document.title = to.meta.title ? `${to.meta.title} - ${title}` : title
// 3.判断是访问登陆页,有 Token 就在当前页面,没有 Token 重置路由到登陆页
if (to.path.toLocaleLowerCase() === LOGIN_URL) {
if (userStore.accessToken) return next(from.fullPath)
resetRouter()
return next()
}
// 3.判断是访问登陆页,有 Token 就在当前页面,没有 Token 重置路由到登陆页
if (to.path.toLocaleLowerCase() === LOGIN_URL) {
if (userStore.accessToken) return next(from.fullPath)
resetRouter()
return next()
}
// 4.判断访问页面是否在路由白名单地址(静态路由)中,如果存在直接放行
if (ROUTER_WHITE_LIST.includes(to.path)) return next()
// 4.判断访问页面是否在路由白名单地址(静态路由)中,如果存在直接放行
if (ROUTER_WHITE_LIST.includes(to.path)) return next()
// 5.判断是否有 Token没有重定向到 login 页面
if (!userStore.accessToken) return next({ path: LOGIN_URL, replace: true })
// 5.判断是否有 Token没有重定向到 login 页面
if (!userStore.accessToken) return next({ path: LOGIN_URL, replace: true })
// 6.如果没有菜单列表,就重新请求菜单列表并添加动态路由
if (!authStore.authMenuListGet.length) {
await initDynamicRouter()
return next({ ...to, replace: true })
}
// 6.如果没有菜单列表,就重新请求菜单列表并添加动态路由
if (!authStore.authMenuListGet.length) {
await initDynamicRouter()
return next({ ...to, replace: true })
}
// 7.存储 routerName 做按钮权限筛选
authStore.setRouteName(to.name as string)
// 8.正常访问页面
next()
// 7.存储 routerName 做按钮权限筛选
await authStore.setRouteName(to.name as string)
// 8. 当前页面是否有激活信息,没有就刷新
const activateInfo = authStore.activateInfo
if (!Object.keys(activateInfo).length) {
await authStore.setActivateInfo()
}
// 9.正常访问页面
next()
})
/**
* @description 重置路由
* */
export const resetRouter = () => {
const authStore = useAuthStore()
authStore.flatMenuListGet.forEach(route => {
const { name } = route
if (name && router.hasRoute(name)) router.removeRoute(name)
})
const authStore = useAuthStore()
authStore.flatMenuListGet.forEach(route => {
const { name } = route
if (name && router.hasRoute(name)) router.removeRoute(name)
})
}
/**
* @description 路由跳转错误
* */
router.onError(error => {
NProgress.done()
//console.warn('路由错误', error.message)
NProgress.done()
//console.warn('路由错误', error.message)
})
/**
* @description 路由跳转结束
* */
router.afterEach(() => {
NProgress.done()
NProgress.done()
})
export default router