index.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { defineStore, acceptHMRUpdate } from "pinia";
  2. export const useStore = defineStore<
  3. string,
  4. SotreState,
  5. SoreGetters,
  6. SotreActions
  7. >("storeInfo", {
  8. state: () => ({
  9. userInfo: {}, // 当前的用户信息
  10. routers: [], // 返回的所有路由
  11. asyncRoutesMark: false, // 是否添加过路由
  12. personnelList: [], // 人员列表
  13. }),
  14. getters: {
  15. getRoutersList() {
  16. return this.routers;
  17. },
  18. getToken() {
  19. return this.userInfo?.id || "";
  20. },
  21. getPersonnelListItems(val) {
  22. let value = val;
  23. if(Array.isArray(val)) {
  24. value = val[0]
  25. }
  26. return this.personnelList.find(item => item.value == value)
  27. },
  28. },
  29. actions: {
  30. // 方法
  31. setRouters(arr) {
  32. this.routers = arr;
  33. },
  34. setAsyncRoutesMark(val) {
  35. this.asyncRoutesMark = val;
  36. },
  37. setValue(val, key) {
  38. this[key] = val;
  39. },
  40. getRouterConfig(path) {
  41. return this.routers.find((item: any) => item.path === path);
  42. },
  43. getFunctionList(path) {
  44. const config = this.getRouterConfig(path);
  45. if (!config) {
  46. return [];
  47. }
  48. return config.functionList || [];
  49. },
  50. getUserInfoVal(val) {
  51. return this.userInfo[val];
  52. },
  53. clearStore() {
  54. localStorage.clear();
  55. sessionStorage.clear();
  56. this.userInfo = {};
  57. this.routers = [];
  58. this.personnelList = [];
  59. this.asyncRoutesMark = false;
  60. },
  61. },
  62. persist: {
  63. storage: sessionStorage, // 存储在 sessionStorage 中
  64. }, // 是否持久化
  65. });
  66. if (import.meta.hot) {
  67. import.meta.hot.accept(acceptHMRUpdate(useStore, import.meta.hot));
  68. }