index.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. },
  22. actions: {
  23. // 方法
  24. setRouters(arr) {
  25. this.routers = arr;
  26. },
  27. setAsyncRoutesMark(val) {
  28. this.asyncRoutesMark = val;
  29. },
  30. setValue(val, key) {
  31. this[key] = val;
  32. },
  33. getRouterConfig(path) {
  34. return this.routers.find((item: any) => item.path === path);
  35. },
  36. getFunctionList(path) {
  37. const config = this.getRouterConfig(path);
  38. if (!config) {
  39. return [];
  40. }
  41. return config.functionList || [];
  42. },
  43. getUserInfoVal(val) {
  44. return this.userInfo[val];
  45. },
  46. clearStore() {
  47. localStorage.clear();
  48. sessionStorage.clear();
  49. this.userInfo = {};
  50. this.routers = [];
  51. this.personnelList = [];
  52. this.asyncRoutesMark = false;
  53. },
  54. },
  55. persist: {
  56. storage: sessionStorage, // 存储在 sessionStorage 中
  57. }, // 是否持久化
  58. });
  59. if (import.meta.hot) {
  60. import.meta.hot.accept(acceptHMRUpdate(useStore, import.meta.hot));
  61. }