index.ts 1.3 KB

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