index.ts 1.5 KB

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