You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.7 KiB

  1. import { defineStore } from "pinia";
  2. import { store } from "/@/store";
  3. import { cacheType } from "./types";
  4. import { constantRoutesArr, ascending, filterTree } from "/@/router/index";
  5. export const usePermissionStore = defineStore({
  6. id: "pure-permission",
  7. state: () => ({
  8. // 静态路由
  9. constantRoutes: constantRoutesArr,
  10. wholeRoutes: [],
  11. buttonAuth: [],
  12. // 缓存页面keepAlive
  13. cachePageList: []
  14. }),
  15. actions: {
  16. asyncActionRoutes(routes) {
  17. if (this.wholeRoutes.length > 0) return;
  18. this.wholeRoutes = filterTree(
  19. ascending(this.constantRoutes.concat(routes))
  20. );
  21. const getButtonAuth = (arrRoutes: Array<string>) => {
  22. if (!arrRoutes || !arrRoutes.length) return;
  23. arrRoutes.forEach((v: any) => {
  24. if (v.meta && v.meta.authority) {
  25. this.buttonAuth.push(...v.meta.authority);
  26. }
  27. if (v.children) {
  28. getButtonAuth(v.children);
  29. }
  30. });
  31. };
  32. getButtonAuth(this.wholeRoutes);
  33. },
  34. async changeSetting(routes) {
  35. await this.asyncActionRoutes(routes);
  36. },
  37. cacheOperate({ mode, name }: cacheType) {
  38. switch (mode) {
  39. case "add":
  40. this.cachePageList.push(name);
  41. this.cachePageList = [...new Set(this.cachePageList)];
  42. break;
  43. case "delete":
  44. // eslint-disable-next-line no-case-declarations
  45. const delIndex = this.cachePageList.findIndex(v => v === name);
  46. delIndex !== -1 && this.cachePageList.splice(delIndex, 1);
  47. break;
  48. }
  49. },
  50. // 清空缓存页面
  51. clearAllCachePage() {
  52. this.cachePageList = [];
  53. }
  54. }
  55. });
  56. export function usePermissionStoreHook() {
  57. return usePermissionStore(store);
  58. }