import { defineStore } from 'pinia'; import { appConfigStore } from '/nerv-base/store/modules/app-config'; export const authorizationService = defineStore({ id: 'authorizationService', state(): { userInfo: any; projectId: any; projectName: string; enterpriseName: any; userResourceList: any; initAllResourceList: string[]; initRouterList: string[]; initOpList: string[]; appConfig: any; initMenus?: object[]; allResourMenus?: object[]; } { return { appConfig: appConfigStore(), projectId: null, projectName: '', enterpriseName: null, userInfo: {}, userResourceList: [], initAllResourceList: [], initRouterList: [], initOpList: [], initMenus: [], allResourMenus: [], }; }, getters: { getPrjectId: (state: any) => state.projectId, getProjectName: (state: any) => state.projectName, getEnterpriseName: (state: any) => state.enterpriseName, getUserResource: (state: any) => state.userResource, getInitRouterList: (state: any) => state.initRouterList, getUserResourceList: (state: any) => state.userResourceList, getInitMenus: (state: any) => state.initMenus, getAllResourMenus: (state: any) => state.allResourMenus, getInitAllResourceList: (state: any) => state.initAllResourceList, }, actions: { clearAuthorization() { this.initMenus = []; this.initRouterList = []; this.initOpList = []; this.userResourceList = []; this.projectId = ''; this.projectName = ''; this.enterpriseName = ''; }, setProjectId(val: string) { this.projectId = val; }, clearProjectId() { this.projectId = ''; }, setProjectName(val: string) { this.projectName = val; }, setEnterpriseName(val: string) { this.enterpriseName = val; }, async initUserResource() { if (window.sessionStorage.getItem('checkResource') === 'false') { this.userResourceList = [JSON.parse(window.sessionStorage.getItem('resource'))]; this.initRouterList = []; this.dealResourceTree(this.userResourceList); } else { const res = await this.appConfig.userResource(); console.log(res, 'hshshhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh'); this.userResourceList = res.data ? res.data : []; if (this.appConfig.defaultResource) { this.userResourceList.push(this.appConfig.defaultResource); } this.initRouterList = []; this.dealResourceTree(this.userResourceList); } }, dealMenuResource(menu: object) { this.allResourMenus.push(menu); if (menu.menus?.length) { menu.menus.forEach((item) => { this.dealMenuResource(item); }); } }, //初始化菜单树 async initMenuResource() { const res = await this.appConfig.userResource(); console.log(res, 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'); this.initMenus = []; if (this.appConfig.resourceName && res.data && res.data.length) { res.data.forEach((item) => { if (item.code.includes('Custom_')) { this.initMenus.push(item); } if (item.code.includes(this.appConfig.resourceName)) { this.initMenus.push(item); } }); } else { this.initMenus = res.data ? res.data : []; } if (!res.data) { return; } this.allResourMenus = []; res.data.forEach((item) => { this.dealMenuResource(item); }); }, async initMenuResourceV2() { if (window.sessionStorage.getItem('checkResource') === 'false') { this.userResourceList = JSON.parse(window.sessionStorage.getItem('resource')).menus; this.initMenus = this.userResourceList ? this.userResourceList : []; this.allResourMenus = []; this.userResourceList.forEach((item) => { this.dealResourceTree(item); }); } }, addUserResource(val: any) { this.userResourceList.push(val); this.initRouterList = []; this.dealResourceTree(this.userResourceList); }, updateUserResource(res: any) { if (Object.prototype.toString.call(res) !== '[object Array]') return; if (!this.appConfig.resourceName) { this.userResourceList = res; this.initRouterList = []; this.initAllResourceList = []; this.dealResourceTree(res); } else { const initResource = []; res.forEach((item) => { if (item.code.includes('intelligent-cloud-')) { initResource.push(item); } if (item.code.includes('Custom_')) { initResource.push(item); } }); this.userResourceList = initResource; this.initRouterList = []; this.initAllResourceList = []; this.dealResourceTree(initResource); } }, //检查是否有路由权限 checkPermissionRouter(routeName: string): boolean { if (!this.appConfig.enablePermissions) { return true; } else { if (this.appConfig.resourceName) { const newName = `${this.appConfig.resourceName}${routeName}`; return this.initRouterList.indexOf(newName) === -1 ? false : true; } else { return this.initRouterList.indexOf(routeName) === -1 ? false : true; } } }, //检查是否有操作权限 checkPermission(actionName: string): boolean { if (!this.appConfig.enablePermissions) { return true; } else { if (this.appConfig.resourceName) { const newName = `${this.appConfig.resourceName}${actionName}`; return this.initOpList.indexOf(newName) === -1 ? false : true; } else { return this.initOpList.indexOf(actionName) === -1 ? false : true; } } }, //检查是否有全部的操作 checkAllPermission(actionName: string): boolean { if (!this.appConfig.enablePermissions) { return true; } else { if (this.appConfig.resourceName) { return this.initAllResourceList.indexOf(`${this.appConfig.resourceName}${actionName}`) === -1 ? false : true; } else { return this.initAllResourceList.indexOf(actionName) === -1 ? false : true; } } }, getApp() { return ((import.meta.env.VITE_PUBLIC_PATH || '') as string).replace(/\//g, ''); }, //判断是否为父节点 dealNoChildrenMenu(item: any) { if (item.type === 'noChildrenMenu') { return true; } else { if ( Object.prototype.toString.call(item.menus) === '[object Array]' && item.menus.length !== 0 ) { return true; } else { return false; } } }, //处理资源树 dealResourceTree(res: any) { if (Object.prototype.toString.call(res) !== '[object Array]') return; res?.forEach((item) => { if (item.type === 'menus' || item.type === 'noChildrenMenu') { this.initRouterList.push(item.code); this.initAllResourceList.push(item.code); if (this.dealNoChildrenMenu(item)) { this.initAllResourceList.push(item.code + 'Index'); this.initRouterList.push(item.code + 'Index'); } } if (item.type === 'op') { if (item.code && this.initRouterList.indexOf(item.code) === -1) { this.initRouterList.push(item.code); } this.initAllResourceList.push(item.code); this.initOpList.push(item.code); } if ( Object.prototype.toString.call(item.menus) === '[object Array]' && item.menus.length !== 0 ) { this.dealResourceTree(item.menus); } }); }, }, });