diff --git a/mock/module/user.ts b/mock/module/user.ts index 004810d..6dde547 100644 --- a/mock/module/user.ts +++ b/mock/module/user.ts @@ -16,14 +16,60 @@ const userInfo = { permissions: [ { label: '主控台', - value: 'dashboard_console', + key: 'dashboard_console', icon: 'icon-park-outline:alarm', }, { label: '监控页', - value: 'dashboard_monitor', + key: 'dashboard_monitor', icon: 'icon-park-outline:anchor', }, + { + label: 'test1', + key: '/test1', + icon: 'icon-park-outline:alarm', + }, + { + label: 'test2', + key: '/test2', + icon: 'icon-park-outline:pic', + }, + { + label: 'test3', + key: '/test3', + icon: 'icon-park-outline:tool', + }, + { + label: '舞,舞,舞', + key: 'dance-dance-dance', + icon: 'icon-park-outline:command', + children: [ + { + label: '饮品', + key: 'beverage', + children: [ + { + label: '威士忌', + key: 'whisky', + }, + ], + }, + { + label: '食物', + key: 'food', + children: [ + { + label: '三明治', + key: 'sandwich', + }, + ], + }, + { + label: '过去增多,未来减少', + key: 'the-past-increases-the-future-recedes', + }, + ], + }, ], }; diff --git a/src/layouts/components/sider/Menu.vue b/src/layouts/components/sider/Menu.vue index 77cbb8d..c7e176e 100644 --- a/src/layouts/components/sider/Menu.vue +++ b/src/layouts/components/sider/Menu.vue @@ -4,7 +4,7 @@ :collapsed-width="64" :collapsed-icon-size="24" :indent="20" - :options="menuOptions" + :options="routesStore.menus" @update:value="handleClickMenu" /> @@ -12,64 +12,15 @@ diff --git a/src/store/modules/auth.ts b/src/store/modules/auth.ts index e4d77fa..f96149f 100644 --- a/src/store/modules/auth.ts +++ b/src/store/modules/auth.ts @@ -4,6 +4,7 @@ import { setUserInfo, getUserInfo, getToken, setToken, clearAuthStorage } from ' import { router } from '@/router'; import { useAppRouter } from '@/hook'; import { unref } from 'vue'; +import { useRouteStore } from './route'; export const useAuthStore = defineStore('auth-store', { state: () => { @@ -49,7 +50,7 @@ export const useAuthStore = defineStore('auth-store', { // 等待数据写入完成 const catchSuccess = await this.catchUserInfo(data); // 初始化侧边菜单 - + await this.setRouterStore(data.permissions); // 登录写入信息成功 if (catchSuccess) { // 进行重定向跳转 @@ -81,5 +82,11 @@ export const useAuthStore = defineStore('auth-store', { return catchSuccess; }, + + /* 将路由表等信息推送到routeStore */ + async setRouterStore(permissions: Auth.UserInfoPermissions[]) { + const { setMenus } = useRouteStore(); + setMenus(permissions); + }, }, }); diff --git a/src/store/modules/route.ts b/src/store/modules/route.ts index 07f229a..4811568 100644 --- a/src/store/modules/route.ts +++ b/src/store/modules/route.ts @@ -1,15 +1,28 @@ import { defineStore } from 'pinia'; +import { renderIcon } from '@/utils/icon'; +interface RuutesStatus { + menus: any; +} export const useRouteStore = defineStore('route-store', { - state: () => { + state: (): RuutesStatus => { return { menus: [], }; }, actions: { - setMenus(data: any) { - // TODO不应为any - this.menus = data; + setMenus(data: Auth.UserInfoPermissions[]) { + this.menus = this.transformAuthRoutesToMenus(data); + }, + // 将返回的路由表渲染成侧边栏 + transformAuthRoutesToMenus(data: Auth.UserInfoPermissions[]) { + return data.map((item) => { + item.icon = renderIcon(item.icon); + if (item.children) { + this.transformAuthRoutesToMenus(item.children); + } + return item; + }); }, }, }); diff --git a/src/types/business.d.ts b/src/types/business.d.ts index a06d2ee..f31966c 100644 --- a/src/types/business.d.ts +++ b/src/types/business.d.ts @@ -26,6 +26,12 @@ declare namespace Auth { /* token */ token: string; /* 权限路由 */ - permissions: []; + permissions: UserInfoPermissions[]; + } + interface UserInfoPermissions { + label: string; + key: string; + icon: any; + children: UserInfoPermissions[]; } } diff --git a/src/types/system.d.ts b/src/types/system.d.ts index 995e772..5890ef9 100644 --- a/src/types/system.d.ts +++ b/src/types/system.d.ts @@ -12,3 +12,10 @@ declare namespace Service { successCode: number | string; } } +/** 菜单项配置 */ +type GlobalMenuOption = import('naive-ui').MenuOption & { + key: string; + label: string; + icon?: () => import('vue').VNodeChild; + children?: GlobalMenuOption[]; +};