|
|
@@ -1,57 +1,90 @@
|
|
|
import { defineAsyncComponent, Component } from 'vue'
|
|
|
-import { useRoute } from 'vue-router'
|
|
|
+import { useRoute, useRouter } from 'vue-router'
|
|
|
import { storageData } from '@/stores'
|
|
|
import { AuthType } from '@/constants/enum/menu'
|
|
|
import { AuthMenu } from './interface'
|
|
|
|
|
|
export function useAuth(code?: string) {
|
|
|
- const route = useRoute();
|
|
|
- const menus = storageData.getMenus();
|
|
|
- const componentMap = new Map<string, Component>();
|
|
|
+ const route = useRoute()
|
|
|
+ const router = useRouter()
|
|
|
+ const accountMenus = storageData.getAccountMenus()
|
|
|
+ const componentMap = new Map<string, Component>()
|
|
|
|
|
|
/**
|
|
|
- * 获取权限菜单(只需到二级菜单)
|
|
|
+ * 获取路由菜单(无限级)
|
|
|
+ * @param level
|
|
|
* @returns
|
|
|
*/
|
|
|
- const getAuthMenu = () => {
|
|
|
- const filter = (item: Ermcp.AccountMenu, parentPath = ''): AuthMenu => ({
|
|
|
- path: (parentPath ? parentPath + '/' : '') + item.url,
|
|
|
- name: item.code,
|
|
|
- label: item.title,
|
|
|
- icon: item.icon,
|
|
|
- })
|
|
|
+ const getMenus = (level = 0) => {
|
|
|
+ // 过滤层级
|
|
|
+ const filterLevel = (list: Ermcp.AccountMenu[], n: number): Ermcp.AccountMenu[] => {
|
|
|
+ if (level) {
|
|
|
+ return list.map((e) => ({ ...e, children: n <= 1 ? [] : filterLevel(e.children ?? [], n - 1) }))
|
|
|
+ }
|
|
|
+ return list
|
|
|
+ }
|
|
|
+ // 过滤菜单
|
|
|
+ const filterMenu = (list: Ermcp.AccountMenu[], parentPath = '') => {
|
|
|
+ const result: AuthMenu[] = []
|
|
|
+ list.forEach((e) => {
|
|
|
+ if (e.authType === AuthType.Menu) {
|
|
|
+ const routePath = (parentPath ? parentPath + '/' : '') + e.url
|
|
|
+ result.push({
|
|
|
+ path: routePath,
|
|
|
+ name: e.code,
|
|
|
+ label: e.title,
|
|
|
+ icon: e.icon,
|
|
|
+ children: e.children ? filterMenu(e.children, routePath) : [],
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return result
|
|
|
+ }
|
|
|
+ return filterMenu(filterLevel(accountMenus, level))
|
|
|
+ }
|
|
|
|
|
|
- return menus.reduce((res, cur) => {
|
|
|
- if (cur.authType === AuthType.Menu) {
|
|
|
- const item = filter(cur);
|
|
|
- if (cur.children) {
|
|
|
- item.children = cur.children.map((e) => filter(e, cur.url));
|
|
|
+ /**
|
|
|
+ * 获取路由子菜单
|
|
|
+ * @param routeName
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ const getChildrenMenus = (routeName?: string) => {
|
|
|
+ const filter = (list: AuthMenu[]): AuthMenu[] => {
|
|
|
+ if (routeName) {
|
|
|
+ for (const item of list) {
|
|
|
+ const { name, children } = item
|
|
|
+ if (name === routeName) return children ?? []
|
|
|
+ if (children) {
|
|
|
+ const res = filter(children)
|
|
|
+ if (res.length) return res
|
|
|
+ }
|
|
|
}
|
|
|
- res.push(item);
|
|
|
}
|
|
|
- return res;
|
|
|
- }, [] as AuthMenu[])
|
|
|
+ return []
|
|
|
+ }
|
|
|
+ return filter(getMenus())
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取权限子菜单
|
|
|
* @returns
|
|
|
*/
|
|
|
- const getChildrenMenu = () => {
|
|
|
- const filter = (items: Ermcp.AccountMenu[], name?: string): Ermcp.AccountMenu | undefined => {
|
|
|
- if (name) {
|
|
|
- for (let i = 0; i < items.length; i++) {
|
|
|
- const { code, children } = items[i];
|
|
|
- if (code === name) return items[i];
|
|
|
+ const getChildrenAuth = () => {
|
|
|
+ const routeName = code ?? route.name?.toString()
|
|
|
+ const filter = (list: Ermcp.AccountMenu[]): Ermcp.AccountMenu | undefined => {
|
|
|
+ if (routeName) {
|
|
|
+ for (const item of list) {
|
|
|
+ const { code, children } = item;
|
|
|
+ if (code === routeName) return item;
|
|
|
if (children) {
|
|
|
- const res = filter(children, name);
|
|
|
+ const res = filter(children);
|
|
|
if (res) return res;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
return undefined;
|
|
|
}
|
|
|
- return filter(menus, code ?? route.name?.toString());
|
|
|
+ return filter(accountMenus);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -59,7 +92,7 @@ export function useAuth(code?: string) {
|
|
|
* @returns
|
|
|
*/
|
|
|
const getAuth = (authType: AuthType) => {
|
|
|
- const auth = getChildrenMenu();
|
|
|
+ const auth = getChildrenAuth();
|
|
|
if (auth && auth.children) {
|
|
|
return auth.children.reduce((res, cur) => {
|
|
|
if (cur.authType === authType) {
|
|
|
@@ -114,9 +147,12 @@ export function useAuth(code?: string) {
|
|
|
const getAuthComponent = (filtered: string[] = [], reverse = false) => authFilter(AuthType.Component, filtered, reverse);
|
|
|
|
|
|
return {
|
|
|
- menus,
|
|
|
+ route,
|
|
|
+ router,
|
|
|
+ accountMenus,
|
|
|
componentMap,
|
|
|
- getAuthMenu,
|
|
|
+ getMenus,
|
|
|
+ getChildrenMenus,
|
|
|
getAuthButton,
|
|
|
getAuthComponent,
|
|
|
}
|