1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import { defineStore } from "pinia";
- import { store } from "@/store";
- import type { cacheType } from "./types";
- import { constantMenus } from "@/router";
- import { useMultiTagsStoreHook } from "./multiTags";
- import { debounce, getKeyList } from "@pureadmin/utils";
- import { ascending, filterTree, filterNoPermissionTree } from "@/router/utils";
- export const usePermissionStore = defineStore({
- id: "pure-permission",
- state: () => ({
-
- constantMenus,
-
- wholeMenus: [],
-
- cachePageList: []
- }),
- actions: {
-
- handleWholeMenus(routes: any[]) {
- this.wholeMenus = filterNoPermissionTree(
- filterTree(ascending(this.constantMenus.concat(routes)))
- );
-
- this.wholeMenus = this.wholeMenus.reduce((accumulator, current) => {
- const x = accumulator.find(item => item.path === current.path);
- if (!x) {
- return accumulator.concat([current]);
- } else {
- return accumulator;
- }
- }, []);
-
- },
- cacheOperate({ mode, name }: cacheType) {
- const delIndex = this.cachePageList.findIndex(v => v === name);
- switch (mode) {
- case "refresh":
- this.cachePageList = this.cachePageList.filter(v => v !== name);
- break;
- case "add":
- this.cachePageList.push(name);
- break;
- case "delete":
- delIndex !== -1 && this.cachePageList.splice(delIndex, 1);
- break;
- }
-
- debounce(() => {
- let cacheLength = this.cachePageList.length;
- const nameList = getKeyList(useMultiTagsStoreHook().multiTags, "name");
- while (cacheLength > 0) {
- nameList.findIndex(v => v === this.cachePageList[cacheLength - 1]) ===
- -1 &&
- this.cachePageList.splice(
- this.cachePageList.indexOf(this.cachePageList[cacheLength - 1]),
- 1
- );
- cacheLength--;
- }
- })();
- },
-
- clearAllCachePage() {
- this.wholeMenus = [];
- this.cachePageList = [];
- }
- }
- });
- export function usePermissionStoreHook() {
- return usePermissionStore(store);
- }
|