选煤厂生产管理平台前端代码
houzhongjian
2024-11-22 82c159b99ca114bc8189e681bde3b4491a81af1c
提交 | 用户 | 时间
82c159 1 import { defineStore } from 'pinia'
H 2 import { store } from '@/store'
3 import { cloneDeep } from 'lodash-es'
4 import remainingRouter from '@/router/modules/remaining'
5 import { flatMultiLevelRoutes, generateRoute } from '@/utils/routerHelper'
6 import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
7 import Layout from "@/layout/Layout.vue";
8
9 const { wsCache } = useCache()
10
11 export interface PermissionState {
12   routers: AppRouteRecordRaw[]
13   addRouters: AppRouteRecordRaw[]
14   menuTabRouters: AppRouteRecordRaw[]
15 }
16
17 export const usePermissionStore = defineStore('permission', {
18   state: (): PermissionState => ({
19     routers: [],
20     addRouters: [],
21     menuTabRouters: []
22   }),
23   getters: {
24     getRouters(): AppRouteRecordRaw[] {
25       return this.routers
26     },
27     getAddRouters(): AppRouteRecordRaw[] {
28       return flatMultiLevelRoutes(cloneDeep(this.addRouters))
29     },
30     getMenuTabRouters(): AppRouteRecordRaw[] {
31       return this.menuTabRouters
32     }
33   },
34   actions: {
35     async generateRoutes(): Promise<unknown> {
36       return new Promise<void>(async (resolve) => {
37         // 获得菜单列表,它在登录的时候,setUserInfoAction 方法中已经进行获取
38         let res: AppCustomRouteRecordRaw[] = []
39         if (wsCache.get(CACHE_KEY.ROLE_ROUTERS)) {
40           res = wsCache.get(CACHE_KEY.ROLE_ROUTERS) as AppCustomRouteRecordRaw[]
41         }
42         const routerMap: AppRouteRecordRaw[] = generateRoute(res)
43         // 动态路由,404一定要放到最后面
44         this.addRouters = routerMap.concat([
45           {
46             path: '/:path(.*)*',
47             redirect: '/404',
48             name: '404Page',
49             component: Layout,
50             meta: {
51               hidden: true,
52               breadcrumb: false
53             }
54           }
55         ])
56         console.log(this.addRouters)
57         // 渲染菜单的所有路由
58         this.routers = cloneDeep(remainingRouter).concat(routerMap)
59         resolve()
60       })
61     },
62     setMenuTabRouters(routers: AppRouteRecordRaw[]): void {
63       this.menuTabRouters = routers
64     }
65   },
66   persist: false
67 })
68
69 export const usePermissionStoreWithOut = () => {
70   return usePermissionStore(store)
71 }