选煤厂安全管理系统前端代码
houzhongjian
2024-12-05 91f388437368c1e34218d7d56421153a6d63019e
提交 | 用户 | 时间
37b204 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'
91f388 6 import {CACHE_KEY, useSessionCache} from '@/hooks/web/useCache'
37b204 7 import Layout from "@/layout/Layout.vue";
H 8
91f388 9 const { wsSessionCache } = useSessionCache()
37b204 10
H 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[] = []
91f388 39         if (wsSessionCache.get(CACHE_KEY.ROLE_ROUTERS)) {
H 40           res = wsSessionCache.get(CACHE_KEY.ROLE_ROUTERS) as AppCustomRouteRecordRaw[]
37b204 41         }
H 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         // 渲染菜单的所有路由
57         this.routers = cloneDeep(remainingRouter).concat(routerMap)
58         resolve()
59       })
60     },
61     setMenuTabRouters(routers: AppRouteRecordRaw[]): void {
62       this.menuTabRouters = routers
63     }
64   },
65   persist: false
66 })
67
68 export const usePermissionStoreWithOut = () => {
69   return usePermissionStore(store)
70 }