沙钢智慧能源系统前端代码
houzhongjian
2024-12-05 f5c803e09e10af90988194aa4a2199f5085a50fb
提交 | 用户 | 时间
314507 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'
f5c803 6 import {CACHE_KEY, useSessionCache} from '@/hooks/web/useCache'
314507 7 import Layout from "@/layout/Layout.vue";
H 8
f5c803 9 const { wsSessionCache } = useSessionCache()
314507 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[] = []
f5c803 39         if (wsSessionCache.get(CACHE_KEY.ROLE_ROUTERS)) {
H 40           res = wsSessionCache.get(CACHE_KEY.ROLE_ROUTERS) as AppCustomRouteRecordRaw[]
314507 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 }