feat
This commit is contained in:
@@ -4,9 +4,10 @@ import type { NotificationReturn } from '@arco-design/web-vue/es/notification/in
|
||||
import type { RouteRecordNormalized } from 'vue-router'
|
||||
import defaultSettings from '@/config/settings.json'
|
||||
import { userPmn } from '@/api/module/user'
|
||||
import { localMenuData } from '@/router/menu-data'
|
||||
import { localMenuData, transformMenuToRoutes, type ServerMenuItem } from '@/router/menu-data'
|
||||
import { buildTree } from '@/utils/tree'
|
||||
import SafeStorage, { AppStorageKey } from "@/utils/safeStorage";
|
||||
import router from '@/router'
|
||||
import { AppState } from './types'
|
||||
|
||||
|
||||
@@ -57,19 +58,54 @@ const useAppStore = defineStore('app', {
|
||||
const res = await userPmn({ id: userInfo.user_id, workspace: import.meta.env.VITE_APP_WORKSPACE })
|
||||
console.log('res', res)
|
||||
if (res.code === 0 && res?.details?.length) {
|
||||
console.log('buildTree', buildTree(res.details[0].permissions))
|
||||
// 使用 buildTree 将扁平数据构建为树结构
|
||||
const treeResult = buildTree(res.details[0].permissions as ServerMenuItem[], {
|
||||
orderKey: 'order'
|
||||
})
|
||||
console.log('buildTree', treeResult)
|
||||
|
||||
// 使用 transformMenuToRoutes 将树结构转换为路由配置
|
||||
const routes = transformMenuToRoutes(treeResult.rootItems as ServerMenuItem[])
|
||||
console.log('transformMenuToRoutes', routes)
|
||||
|
||||
// 动态注册路由
|
||||
routes.forEach((route) => {
|
||||
// 打印路由结构以便调试
|
||||
// console.log('Registering route:', JSON.stringify(route, (key, value) => {
|
||||
// if (typeof value === 'function') return '[Function]'
|
||||
// return value
|
||||
// }, 2))
|
||||
router.addRoute(route as any)
|
||||
})
|
||||
|
||||
this.serverMenu = routes as unknown as RouteRecordNormalized[]
|
||||
} else {
|
||||
// 如果接口返回数据为空,使用本地数据
|
||||
localMenuData.forEach((route) => {
|
||||
router.addRoute(route as any)
|
||||
})
|
||||
this.serverMenu = localMenuData as unknown as RouteRecordNormalized[]
|
||||
}
|
||||
// this.serverMenu = data
|
||||
|
||||
// 使用本地数据
|
||||
this.serverMenu = localMenuData as unknown as RouteRecordNormalized[]
|
||||
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
|
||||
// 接口失败时使用本地数据
|
||||
console.error('fetchServerMenuConfig error:', error)
|
||||
localMenuData.forEach((route) => {
|
||||
router.addRoute(route as any)
|
||||
})
|
||||
this.serverMenu = localMenuData as unknown as RouteRecordNormalized[]
|
||||
}
|
||||
},
|
||||
clearServerMenu() {
|
||||
// 清除动态注册的路由
|
||||
if (this.serverMenu && this.serverMenu.length > 0) {
|
||||
this.serverMenu.forEach((route) => {
|
||||
const routeName = (route as any).name
|
||||
if (routeName && typeof routeName === 'string') {
|
||||
router.removeRoute(routeName)
|
||||
}
|
||||
})
|
||||
}
|
||||
this.serverMenu = []
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user