修改router 刷新404
This commit is contained in:
parent
c44622cad6
commit
c83470087e
@ -1,5 +1,6 @@
|
|||||||
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
|
import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
|
||||||
import type { MenuInfo } from '@/api/auth'
|
import { useUserStore } from '@/stores/user'
|
||||||
|
//import type { MenuInfo } from '@/api/auth'
|
||||||
|
|
||||||
// 动态导入所有页面组件
|
// 动态导入所有页面组件
|
||||||
const modules = import.meta.glob('/src/views/**/*.vue')
|
const modules = import.meta.glob('/src/views/**/*.vue')
|
||||||
@ -34,7 +35,7 @@ const routes: RouteRecordRaw[] = [
|
|||||||
component: () => import('@/views/dashboard/index.vue'),
|
component: () => import('@/views/dashboard/index.vue'),
|
||||||
meta: { title: '首页', icon: 'HomeOutline' }
|
meta: { title: '首页', icon: 'HomeOutline' }
|
||||||
},
|
},
|
||||||
|
|
||||||
// 个人中心
|
// 个人中心
|
||||||
{
|
{
|
||||||
path: 'profile',
|
path: 'profile',
|
||||||
@ -216,11 +217,16 @@ const routes: RouteRecordRaw[] = [
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
// {
|
||||||
|
// path: '/:pathMatch(.*)',
|
||||||
|
// name: 'NotFound',
|
||||||
|
// component: () => import('@/views/error/404.vue'),
|
||||||
|
// meta: { title: '404', requiresAuth: false }
|
||||||
|
// }
|
||||||
{
|
{
|
||||||
path: '/:pathMatch(.*)*',
|
path: '/:pathMatch(.*)',
|
||||||
name: 'NotFound',
|
|
||||||
component: () => import('@/views/error/404.vue'),
|
component: () => import('@/views/error/404.vue'),
|
||||||
meta: { title: '404', requiresAuth: false }
|
// meta: { title: '404', requiresAuth: false }
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -232,47 +238,34 @@ const router = createRouter({
|
|||||||
// 已添加的动态路由
|
// 已添加的动态路由
|
||||||
const addedRouteNames = new Set<string>()
|
const addedRouteNames = new Set<string>()
|
||||||
|
|
||||||
// 动态路由是否已初始化
|
|
||||||
let dynamicRoutesInitialized = false
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据菜单动态添加新路由(只添加静态路由中没有的)
|
* 根据菜单动态添加新路由(只添加静态路由中没有的)
|
||||||
*/
|
*/
|
||||||
export function addDynamicRoutes(menus: MenuInfo[]) {
|
export function addDynamicRoutes(menus: any) { //MenuInfo[]
|
||||||
console.log('[动态路由] 开始处理菜单:', menus)
|
//console.log('[动态路由] 开始处理菜单:', menus)
|
||||||
|
const addRoutes = (menuList: any) => { //MenuInfo[]
|
||||||
// 获取 Layout 路由的现有子路由路径
|
|
||||||
const getLayoutChildPaths = () => {
|
|
||||||
const layoutRoute = router.getRoutes().find(r => r.name === 'Layout')
|
|
||||||
if (layoutRoute && layoutRoute.children) {
|
|
||||||
return new Set(layoutRoute.children.map(c => c.path))
|
|
||||||
}
|
|
||||||
return new Set<string>()
|
|
||||||
}
|
|
||||||
|
|
||||||
const addRoutes = (menuList: MenuInfo[]) => {
|
|
||||||
for (const menu of menuList) {
|
for (const menu of menuList) {
|
||||||
// 只处理菜单类型(type=2),且有 path
|
// 只处理菜单类型(type=2),且有 path
|
||||||
if (menu.type === 2 && menu.path) {
|
if (menu.type === 2 && menu.path) {
|
||||||
const routeName = 'Dynamic-' + menu.id
|
const routeName = 'Dynamic-' + menu.id
|
||||||
|
|
||||||
// 规范化路径,去掉开头的 /
|
// 检查是否已经有同路径的静态路由
|
||||||
|
const existingRoutes = router.getRoutes()
|
||||||
const menuPath = menu.path.startsWith('/') ? menu.path.slice(1) : menu.path
|
const menuPath = menu.path.startsWith('/') ? menu.path.slice(1) : menu.path
|
||||||
|
const pathExists = existingRoutes.some(r => r.path === '/' + menuPath || r.path === menuPath)
|
||||||
// 检查是否已经有同路径的路由(检查 Layout 的子路由)
|
|
||||||
const layoutChildPaths = getLayoutChildPaths()
|
|
||||||
const pathExists = layoutChildPaths.has(menuPath) || layoutChildPaths.has('/' + menuPath)
|
|
||||||
|
|
||||||
if (pathExists) {
|
if (pathExists) {
|
||||||
console.log(`[动态路由] 跳过(已存在): ${menuPath}`)
|
// console.log(`[动态路由] 跳过(已存在): ${menuPath}`)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (addedRouteNames.has(routeName)) {
|
if (addedRouteNames.has(routeName)) {
|
||||||
console.log(`[动态路由] 跳过(已添加): ${menuPath}`)
|
//console.log(`[动态路由] 跳过(已添加): ${menuPath}`)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断是否是外链菜单
|
// 判断是否是外链菜单
|
||||||
if (menu.isFrame === 1 && menu.component) {
|
if (menu.isFrame === 1 && menu.component) {
|
||||||
// 外链菜单,使用 iframe 组件
|
// 外链菜单,使用 iframe 组件
|
||||||
@ -292,36 +285,11 @@ export function addDynamicRoutes(menus: MenuInfo[]) {
|
|||||||
} else if (menu.component) {
|
} else if (menu.component) {
|
||||||
// 普通菜单,加载组件
|
// 普通菜单,加载组件
|
||||||
const componentName = menu.component.startsWith('/') ? menu.component.slice(1) : menu.component
|
const componentName = menu.component.startsWith('/') ? menu.component.slice(1) : menu.component
|
||||||
|
const componentPath = `/src/views/${componentName}.vue`
|
||||||
// 组件路径映射表(解决菜单component与实际文件路径不匹配的问题)
|
const component = modules[componentPath]
|
||||||
const componentMapping: Record<string, string> = {
|
|
||||||
'biz/toolUsage': 'biz/tool/usage/index',
|
//console.log(`[动态路由] 处理: path=${menuPath}, component=${componentPath}, 组件存在=${!!component}`)
|
||||||
'biz/toolUsage/index': 'biz/tool/usage/index',
|
|
||||||
'biz/tool/usage': 'biz/tool/usage/index',
|
|
||||||
}
|
|
||||||
|
|
||||||
// 查找映射关系
|
|
||||||
const mappedComponent = componentMapping[componentName] || componentName
|
|
||||||
|
|
||||||
// 尝试多种路径加载组件
|
|
||||||
let component = null
|
|
||||||
const possiblePaths = [
|
|
||||||
`/src/views/${mappedComponent}.vue`,
|
|
||||||
`/src/views/${mappedComponent}/index.vue`,
|
|
||||||
`/src/views/${componentName}.vue`,
|
|
||||||
`/src/views/${componentName}/index.vue`,
|
|
||||||
]
|
|
||||||
|
|
||||||
for (const path of possiblePaths) {
|
|
||||||
if (modules[path]) {
|
|
||||||
component = modules[path]
|
|
||||||
console.log(`[动态路由] 找到组件: ${path}`)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`[动态路由] 处理: path=${menuPath}, componentName=${componentName}, mappedComponent=${mappedComponent}, 组件存在=${!!component}`)
|
|
||||||
|
|
||||||
if (component) {
|
if (component) {
|
||||||
router.addRoute('Layout', {
|
router.addRoute('Layout', {
|
||||||
path: menuPath,
|
path: menuPath,
|
||||||
@ -334,23 +302,22 @@ export function addDynamicRoutes(menus: MenuInfo[]) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
addedRouteNames.add(routeName)
|
addedRouteNames.add(routeName)
|
||||||
console.log(`[动态路由] ✓ 添加成功: ${menuPath}`)
|
//console.log(`[动态路由] ✓ 添加成功: ${menuPath}`)
|
||||||
} else {
|
} else {
|
||||||
console.warn(`[动态路由] ✗ 组件不存在: ${componentName}`)
|
//console.warn(`[动态路由] ✗ 组件不存在: ${componentPath}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 递归处理子菜单
|
// 递归处理子菜单
|
||||||
if (menu.children && menu.children.length > 0) {
|
if (menu.children && menu.children.length > 0) {
|
||||||
addRoutes(menu.children)
|
addRoutes(menu.children)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
addRoutes(menus)
|
addRoutes(menus)
|
||||||
dynamicRoutesInitialized = true
|
//console.log('[动态路由] 当前所有路由:', router.getRoutes().map(r => r.path))
|
||||||
console.log('[动态路由] 当前所有路由:', router.getRoutes().map(r => r.path))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -363,16 +330,15 @@ export function resetRouter() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
addedRouteNames.clear()
|
addedRouteNames.clear()
|
||||||
dynamicRoutesInitialized = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 路由守卫
|
// 路由守卫
|
||||||
router.beforeEach(async (to, _from, next) => {
|
router.beforeEach(async (to, _from, next) => {
|
||||||
const { useUserStore } = await import('@/stores/user')
|
console.log('路由测试');
|
||||||
const userStore = useUserStore()
|
const userStore = useUserStore()
|
||||||
|
|
||||||
document.title = `${to.meta.title || ''} - mes Admin`
|
//document.title = `${to.meta.title || ''} - CSY Admin`
|
||||||
|
|
||||||
if (to.meta.requiresAuth === false) {
|
if (to.meta.requiresAuth === false) {
|
||||||
next()
|
next()
|
||||||
return
|
return
|
||||||
@ -383,25 +349,47 @@ router.beforeEach(async (to, _from, next) => {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!userStore.user || !dynamicRoutesInitialized) {
|
|
||||||
|
if (userStore.init) {
|
||||||
try {
|
try {
|
||||||
if (!userStore.user) {
|
|
||||||
await userStore.getInfo()
|
await userStore.getInfo()
|
||||||
}
|
|
||||||
// 添加动态路由(只添加新的,不影响已有的)
|
// 添加动态路由(只添加新的,不影响已有的)
|
||||||
addDynamicRoutes(userStore.menus)
|
addDynamicRoutes(userStore.menus)
|
||||||
console.log('[路由守卫] 动态路由已添加,重新导航到:', to.path)
|
userStore.setinit(false)
|
||||||
|
// userStore.init = false
|
||||||
next({ ...to, replace: true })
|
next({ ...to, replace: true })
|
||||||
return
|
//return
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[路由守卫] 获取用户信息失败:', error)
|
|
||||||
userStore.logout()
|
userStore.logout()
|
||||||
|
//userStore.init = false
|
||||||
|
userStore.setinit(true)
|
||||||
next({ name: 'Login' })
|
next({ name: 'Login' })
|
||||||
return
|
//return
|
||||||
}
|
}
|
||||||
|
}else{
|
||||||
|
next()
|
||||||
}
|
}
|
||||||
|
|
||||||
next()
|
// if (!userStore.user) {
|
||||||
|
// try {
|
||||||
|
// await userStore.getInfo()
|
||||||
|
// // 添加动态路由(只添加新的,不影响已有的)
|
||||||
|
// addDynamicRoutes(userStore.menus)
|
||||||
|
// next({ ...to, replace: true })
|
||||||
|
// return
|
||||||
|
// } catch (error) {
|
||||||
|
// userStore.logout()
|
||||||
|
// next({ name: 'Login' })
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// if (to.meta.requiresAuth === false) {
|
||||||
|
// next()
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
export default router
|
export default router
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { defineStore } from 'pinia'
|
import { defineStore } from 'pinia'
|
||||||
import { ref, computed } from 'vue'
|
import { ref, computed } from 'vue'
|
||||||
import { authApi, type LoginParams, type UserInfo, type MenuInfo } from '@/api/auth'
|
import { authApi, type LoginParams, type UserInfo, type MenuInfo } from '@/api/auth'
|
||||||
|
import router, { resetRouter } from '@/router'
|
||||||
|
|
||||||
export const useUserStore = defineStore('user', () => {
|
export const useUserStore = defineStore('user', () => {
|
||||||
// 状态
|
// 状态
|
||||||
@ -10,11 +11,21 @@ export const useUserStore = defineStore('user', () => {
|
|||||||
const permissions = ref<string[]>([])
|
const permissions = ref<string[]>([])
|
||||||
const menus = ref<MenuInfo[]>([])
|
const menus = ref<MenuInfo[]>([])
|
||||||
|
|
||||||
|
const init = ref<any>(true)
|
||||||
|
|
||||||
// 计算属性
|
// 计算属性
|
||||||
const isLogin = computed(() => !!token.value)
|
const isLogin = computed(() => !!token.value)
|
||||||
const nickname = computed(() => user.value?.nickname || user.value?.username || '')
|
const nickname = computed(() => user.value?.nickname || user.value?.username || '')
|
||||||
const avatar = computed(() => user.value?.avatar || '')
|
const avatar = computed(() => user.value?.avatar || '')
|
||||||
|
|
||||||
|
const postNames = computed(() => user.value?.postNames || '')
|
||||||
|
|
||||||
|
const userid = computed(() => user.value?.id || '')
|
||||||
|
|
||||||
|
function setinit(v:any) {
|
||||||
|
init.value = v
|
||||||
|
}
|
||||||
|
|
||||||
// 登录
|
// 登录
|
||||||
async function login(params: LoginParams) {
|
async function login(params: LoginParams) {
|
||||||
const res = await authApi.login(params)
|
const res = await authApi.login(params)
|
||||||
@ -45,6 +56,11 @@ export const useUserStore = defineStore('user', () => {
|
|||||||
permissions.value = []
|
permissions.value = []
|
||||||
menus.value = []
|
menus.value = []
|
||||||
|
|
||||||
|
init.value = true
|
||||||
|
|
||||||
|
// 重置路由
|
||||||
|
resetRouter()
|
||||||
|
|
||||||
// 只有之前有 token 时才发送 logout 请求
|
// 只有之前有 token 时才发送 logout 请求
|
||||||
if (hadToken) {
|
if (hadToken) {
|
||||||
try {
|
try {
|
||||||
@ -54,9 +70,6 @@ export const useUserStore = defineStore('user', () => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 动态导入 router 避免循环依赖
|
|
||||||
const { default: router, resetRouter } = await import('@/router')
|
|
||||||
resetRouter()
|
|
||||||
router.push('/login')
|
router.push('/login')
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,6 +95,10 @@ export const useUserStore = defineStore('user', () => {
|
|||||||
isLogin,
|
isLogin,
|
||||||
nickname,
|
nickname,
|
||||||
avatar,
|
avatar,
|
||||||
|
init,
|
||||||
|
postNames,
|
||||||
|
userid,
|
||||||
|
setinit,
|
||||||
login,
|
login,
|
||||||
getInfo,
|
getInfo,
|
||||||
logout,
|
logout,
|
||||||
@ -90,7 +107,7 @@ export const useUserStore = defineStore('user', () => {
|
|||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
persist: {
|
persist: {
|
||||||
key: 'mes-user',
|
key: 'mars-user',
|
||||||
paths: ['token']
|
paths: ['token']
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user