user-store.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //@ts-nocheck
  2. import { defineStore } from "pinia";
  3. import { getUserInfoByCode } from "@/api/login";
  4. import { useSystemStore } from "@/pinia/system-store";
  5. export declare type UserInfo = {
  6. avatar: string;
  7. token: string;
  8. sid: string;
  9. ip: string;
  10. menus: string[]; // 菜单列表,字符串数组
  11. code: string;
  12. codeRs: string;
  13. name: string;
  14. deptCode: string;
  15. deptName: string;
  16. ybCode: string;
  17. roles: number[]; // 角色列表,字符串数组
  18. partTimeDept: string[]; // 兼职部门列表,字符串数组
  19. partTimeDeptCode: string;
  20. partTimeDeptName: string;
  21. partTimeDeptMap: Record<string, string>; // 兼职部门映射,键为部门编码,值为部门名称
  22. };
  23. let initUserInfo = false;
  24. export const useUserStore = defineStore("user", {
  25. state: (): {
  26. userInfo: UserInfo;
  27. randomSid: string;
  28. } => ({
  29. userInfo: {
  30. avatar: "",
  31. token: "",
  32. sid: "",
  33. ip: "",
  34. menus: [],
  35. code: "",
  36. codeRs: "",
  37. name: "",
  38. deptCode: "",
  39. deptName: "",
  40. ybCode: "",
  41. roles: [],
  42. partTimeDept: [],
  43. partTimeDeptCode: "",
  44. partTimeDeptName: "",
  45. partTimeDeptMap: {},
  46. },
  47. randomSid: "",
  48. }),
  49. getters: {
  50. getUserInfo: async (state): Promise<boolean> => {
  51. if (initUserInfo) return true;
  52. if (localStorage.token !== null) {
  53. const userInfo = await getUserInfoByCode().catch(() => {
  54. return null;
  55. });
  56. if (userInfo !== null) {
  57. state.userInfo = userInfo;
  58. useSystemStore().setConfig(userInfo.userConfigJson);
  59. initUserInfo = true;
  60. return true;
  61. } else {
  62. return false;
  63. }
  64. }
  65. },
  66. getToken: () => {
  67. return localStorage.token;
  68. },
  69. getSid(state): string {
  70. return state.userInfo.sid + state.randomSid;
  71. },
  72. },
  73. actions: {
  74. setToken(val: string) {
  75. localStorage.token = val;
  76. },
  77. setUserInfo(val: UserInfo) {
  78. localStorage.token = val.token;
  79. this.userInfo = val;
  80. },
  81. setRandomSid(val: string) {
  82. this.randomSid = val;
  83. },
  84. },
  85. });