userInfo.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { defineStore } from 'pinia';
  2. import { Local, Session } from '/@/utils/storage';
  3. import Watermark from '/@/utils/watermark';
  4. import { useThemeConfig } from '/@/stores/themeConfig';
  5. import { getAPI } from '/@/utils/axios-utils';
  6. import { SysAuthApi, SysConstApi, SysDictTypeApi } from '/@/api-services/api';
  7. /**
  8. * 用户信息
  9. * @methods setUserInfos 设置用户信息
  10. */
  11. export const useUserInfo = defineStore('userInfo', {
  12. state: (): UserInfosState => ({
  13. userInfos: {} as any,
  14. constList: [] as any,
  15. dictList: {} as any,
  16. }),
  17. getters: {
  18. // // 获取系统常量列表
  19. // async getSysConstList(): Promise<any[]> {
  20. // var res = await getAPI(SysConstApi).apiSysConstListGet();
  21. // this.constList = res.data.result ?? [];
  22. // return this.constList;
  23. // },
  24. },
  25. actions: {
  26. // 存储用户信息到浏览器缓存
  27. async setUserInfos() {
  28. this.userInfos = Session.get('userInfo') ?? <UserInfos>await this.getApiUserInfo();
  29. },
  30. // 存储常量信息到浏览器缓存
  31. async setConstList() {
  32. this.constList = Session.get('constList') ?? <any[]>await this.getSysConstList();
  33. if (!Session.get('constList')) Session.set('constList', this.constList);
  34. },
  35. // 存储字典信息到浏览器缓存
  36. async setDictList() {
  37. var dictList = await getAPI(SysDictTypeApi).apiSysDictTypeAllDictListGet().then(res => res.data.result ?? {});
  38. var dictListTemp = JSON.parse(JSON.stringify(dictList));
  39. await Promise.all(Object.keys(dictList).map(async (key) => {
  40. // dictList[key].forEach((da: any, index: any) => {
  41. // setDictLangMessageAsync(dictListTemp[key][index]);
  42. // });
  43. // 如果 key 以 "Enum" 结尾,则转换 value 为数字
  44. if (key.endsWith("Enum")) {
  45. dictListTemp[key].forEach((e: any) => e.value = Number(e.value));
  46. }
  47. }))
  48. this.dictList = dictListTemp;
  49. },
  50. // 获取当前用户信息
  51. getApiUserInfo() {
  52. return new Promise((resolve) => {
  53. getAPI(SysAuthApi)
  54. .apiSysAuthUserInfoGet()
  55. .then(async (res: any) => {
  56. if (res.data.result == null) {
  57. resolve({} as UserInfos);
  58. return;
  59. }
  60. var d = res.data.result;
  61. const userInfos = {
  62. id: d.id,
  63. account: d.account,
  64. realName: d.realName,
  65. phone: d.phone,
  66. idCardNum: d.idCardNum,
  67. email: d.email,
  68. accountType: d.accountType,
  69. avatar: d.avatar ?? '/app-icons/aidop-logo.png',
  70. address: d.address,
  71. signature: d.signature,
  72. orgId: d.orgId,
  73. orgName: d.orgName,
  74. posName: d.posName,
  75. roles: d.roleIds,
  76. authBtnList: d.buttons,
  77. tenantId: d.tenantId,
  78. currentTenantId: d.currentTenantId,
  79. langCode: d.langCode,
  80. time: new Date().getTime(),
  81. };
  82. // vue-next-admin 提交Id:225bce7 提交消息:admin-23.03.26:发布v2.4.32版本
  83. // 增加了下面代码,引起当前会话的用户信息不会刷新,如:重新提交的头像不更新,需要新开一个页面才能正确显示
  84. // Session.set('userInfo', userInfos);
  85. // 用户水印
  86. const storesThemeConfig = useThemeConfig();
  87. storesThemeConfig.themeConfig.watermarkText = d.watermarkText ?? '';
  88. if (storesThemeConfig.themeConfig.isWatermark) Watermark.set(storesThemeConfig.themeConfig.watermarkText);
  89. else Watermark.del();
  90. Local.remove('themeConfig');
  91. Local.set('themeConfig', storesThemeConfig.themeConfig);
  92. resolve(userInfos);
  93. })
  94. .catch(() => resolve({} as UserInfos));
  95. });
  96. },
  97. // 获取常量集合
  98. getSysConstList() {
  99. return new Promise((resolve) => {
  100. getAPI(SysConstApi)
  101. .apiSysConstListGet()
  102. .then(async (res: any) => {
  103. resolve(res.data.result ?? []);
  104. });
  105. });
  106. },
  107. // 根据常量类名获取常量数据
  108. getConstDataByTypeCode(typeCode: string) {
  109. return this.constList.find((item: any) => item.code === typeCode)?.data?.result || [];
  110. },
  111. // 根据常量类名和编码获取常量值
  112. getConstItemNameByType(typeCode: string, itemCode: string) {
  113. const data = this.getConstDataByTypeCode(typeCode);
  114. return data.find((item: any) => item.code === itemCode)?.name;
  115. },
  116. // 根据字典类型获取字典数据
  117. getDictDataByCode(dictTypeCode: string) {
  118. return this.dictList[dictTypeCode] || [];
  119. }
  120. },
  121. });
  122. // 处理字典国际化, 默认显示字典中的label值
  123. // const setDictLangMessageAsync = async (dict: any) => {
  124. // dict.langMessage = `message.dictType.${dict.typeCode}_${dict.value}`;
  125. // const text = dict.langMessage;
  126. // dict.label = text !== dict.langMessage ? text : dict.label;
  127. // }