131 lines
3.0 KiB
TypeScript
131 lines
3.0 KiB
TypeScript
import { RouteRecordRaw } from 'vue-router'
|
|
import XEUtils from 'xe-utils'
|
|
import i18n from '@/locales'
|
|
import { routes } from '@/router'
|
|
const { t } = i18n.global
|
|
export interface MenuNode {
|
|
key: string
|
|
name: string
|
|
keyPath: string
|
|
icon?: string
|
|
path: string
|
|
hidden: boolean
|
|
parentKey?: string
|
|
url?: string
|
|
children?: MenuNode[]
|
|
}
|
|
|
|
export function router2Menu(r: RouteRecordRaw & { parentKey?: string; keyPath?: string }): MenuNode {
|
|
const title = String(r.meta?.title)
|
|
const keyPath = r.keyPath ?? String(r.name)
|
|
return {
|
|
key: String(r.name),
|
|
name: t(title),
|
|
icon: String(r.meta?.icon),
|
|
hidden: !!r.meta?.hideInMenu,
|
|
parentKey: r.parentKey,
|
|
path: r.path,
|
|
keyPath: keyPath,
|
|
}
|
|
}
|
|
|
|
export function dynamicsRouters() {
|
|
return XEUtils.mapTree(
|
|
routes as (RouteRecordRaw & {
|
|
parentKey?: string
|
|
keyPath?: string
|
|
})[],
|
|
(item) => {
|
|
const keyPath = item.keyPath ?? String(item.name)
|
|
if (item.children) {
|
|
item.children.map((c: RouteRecordRaw & { parentKey?: string; keyPath?: string }) => {
|
|
return Object.assign(c, {
|
|
parentKey: String(item.name),
|
|
keyPath: c.keyPath ? c.keyPath : keyPath + '.' + String(c.name),
|
|
})
|
|
})
|
|
}
|
|
return Object.assign(item, { keyPath })
|
|
},
|
|
)
|
|
}
|
|
|
|
export function menuTree() {
|
|
return XEUtils.mapTree(
|
|
routes as (RouteRecordRaw & {
|
|
parentKey?: string
|
|
keyPath?: string
|
|
})[],
|
|
(item) => {
|
|
const keyPath = item.keyPath ?? String(item.name)
|
|
if (item.children) {
|
|
item.children.map((c: RouteRecordRaw & { parentKey?: string; keyPath?: string }) => {
|
|
return Object.assign(c, {
|
|
parentKey: String(item.name),
|
|
keyPath: c.keyPath ? c.keyPath : keyPath + '.' + String(c.name),
|
|
})
|
|
})
|
|
}
|
|
return router2Menu(item)
|
|
},
|
|
)
|
|
}
|
|
|
|
export function hasPermission(key?: string) {
|
|
console.log(key)
|
|
// return useUserStore().hasPermission(key);
|
|
return true
|
|
}
|
|
|
|
export function permissions() {
|
|
return XEUtils.toTreeArray(menuTree(), { clear: true }).filter((item) => !item.hidden)
|
|
}
|
|
|
|
export function permissionTree() {
|
|
return XEUtils.toArrayTree(permissions(), { parentKey: 'parentKey', key: 'key' })
|
|
}
|
|
|
|
export function fliteredMenus() {
|
|
return XEUtils.toTreeArray(menuTree(), { clear: true })
|
|
.filter((item) => {
|
|
return !item.hidden
|
|
})
|
|
.filter((item) => {
|
|
return hasPermission(item.keyPath)
|
|
})
|
|
}
|
|
|
|
export function fliteredMenuTree() {
|
|
return XEUtils.toArrayTree(fliteredMenus(), { strict: true, parentKey: 'parentKey', key: 'key' })
|
|
}
|
|
/**
|
|
* 全部菜单树
|
|
* @returns 菜单树
|
|
*/
|
|
export function menus() {
|
|
return fliteredMenuTree()
|
|
}
|
|
|
|
/**
|
|
* 顶部菜单,split为true则返回一级菜单,为false则为全部菜单树
|
|
* @param splite 是否拆分
|
|
* @returns 菜单树
|
|
*/
|
|
export function topMenus(splite: boolean) {
|
|
if (splite) {
|
|
return menus().map((item) => {
|
|
item.children = []
|
|
return item
|
|
})
|
|
}
|
|
return menus()
|
|
}
|
|
|
|
/**
|
|
* 侧面拆分菜单
|
|
* @returns 菜单树
|
|
*/
|
|
export function sideMenu(top: string) {
|
|
return menus().find((item) => item.key === top)?.children || []
|
|
}
|