update: 优化菜单路由处理

This commit is contained in:
xieyonghong 2023-02-22 15:51:13 +08:00
parent 9d8b31713c
commit 1460ebcdbb
4 changed files with 33 additions and 62 deletions

View File

@ -24,7 +24,6 @@ router.beforeEach((to, from, next) => {
const userInfo = useUserInfo()
const system = useSystem()
const menu = useMenuStore()
if (!menu.siderMenus.length) {
userInfo.getUserInfo().then(() => {
system.getSystemVersion().then((menuData: any[]) => {

View File

@ -36,54 +36,52 @@ type MenuStateType = {
siderMenus: MenuItem[]
}
// export const useMenusStore = defineStore('menu', () => {
// const state = reactive<MenuStateType>({
// menus: {},
// siderMenus: []
// })
//
// const hasPermission = (code: string | string[]) => {
// if (!code || !(code as string[]).length) {
// console.warn(`function hasPermission: 没有传入${code}`)
// return false
// }
//
// }
//
// const queryMenuTree = (): Promise<any[]> => {
//
// }
//
// return {
// state,
// hasPermission,
// queryMenuTree
// }
// })
export const useMenuStore = defineStore({
id: 'menu',
state: () => ({
state: (): MenuStateType => ({
menus: {},
siderMenus: []
}),
getters: {
hasPermission(state) {
return (menuCode: string | string[]) => {
if (!menuCode) {
return (code: string | string[]) => {
if (!code) {
return true
}
if (!!Object.keys(state.menus).length) {
if (typeof menuCode === 'string') {
return !!this.menus[menuCode].buttons
let codes: string[] = []
if (typeof code === 'string') {
codes.push(code)
} else {
codes = code
}
return menuCode.some(code => !!this.menus[code])
return codes.some(_c => {
const menu_code = _c.split(':')
if (menu_code.length > 1) {
return !!this.menus[menu_code[0]]?.buttons?.includes(menu_code[1])
}
return false
})
}
return false
}
}
},
actions: {
hasMenu(code: string) {
return this.menus[code]?.path
},
jumpPage(code: string, params: Record<string, any>, query: Record<string, any>) {
const path = this.menus[code]?.path
if (path) {
router.push({
path, params, query
})
}
},
queryMenuTree(isCommunity = false): Promise<any[]> {
return new Promise(async (res) => {
//过滤非集成的菜单
@ -94,11 +92,11 @@ export const useMenuStore = defineStore({
const handleMenuItem = (menu: any) => {
if (isArray(menu)) {
menu.forEach(menuItem => {
this.menus[menuItem.code] = {
this.menus[menuItem.name] = {
path: menuItem.path,
buttons: menuItem.buttons
buttons: menuItem.meta.buttons
}
if (menuItem.children && menuItem.length) {
if (menuItem.children && menuItem.children.length) {
handleMenuItem(menuItem.children)
}
})

View File

@ -96,29 +96,3 @@ export const modifySearchColumnValue = (e: any, column: object) => {
});
return e;
};
// /**
// * 根据code返回对应的菜单path
// * @param code 菜单code
// */
// export const getMenu = (code: string): string | undefined => {
// const menuStore = useMenuStore()
// return menuStore.menus[code]?.path
// }
//
// /**
// * 根据菜单code跳转对应路由
// * @param code 菜单code
// * @param params 路由params参数
// * @param query 路由query参数
// */
// export const jumpPath = (code: string, params?: any, query?: any) => {
// const path = getMenu(code)
// if (path) {
// router.push({
// path, params, query
// })
// } else {
// console.warn(`menus中没有${code}`)
// }
// }

View File

@ -178,7 +178,7 @@ const findDetailRouteItem = (code: string, url: string): Partial<MenuItem> | nul
const detailComponent = resolveComponent(`${code}/Detail`)
if (detailComponent) {
return {
url: `${url}/Detail/:id`,
url: `${url}/detail/:id`,
code: `${code}/Detail`,
component: detailComponent,
name: '详情信息',