userInfo.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { defineStore } from 'pinia';
  2. import { Session } from '/@/utils/storage';
  3. import { getAPI } from '/@/utils/axios-utils';
  4. import { SysAuthApi, SysConstApi, SysDictTypeApi } from '/@/api-services/api';
  5. /**
  6. * 用户信息
  7. * @methods setUserInfos 设置用户信息
  8. */
  9. export const useUserInfo = defineStore('userInfo', {
  10. state: (): UserInfosState => ({
  11. userInfos: {} as any,
  12. constList: [] as any,
  13. dictList: {} as any,
  14. dictListInt: {} as any,
  15. }),
  16. getters: {
  17. // // 获取系统常量列表
  18. // async getSysConstList(): Promise<any[]> {
  19. // var res = await getAPI(SysConstApi).apiSysConstListGet();
  20. // this.constList = res.data.result ?? [];
  21. // return this.constList;
  22. // },
  23. },
  24. actions: {
  25. // 存储用户信息到浏览器缓存
  26. async setUserInfos() {
  27. if (Session.get('userInfo')) {
  28. this.userInfos = Session.get('userInfo');
  29. } else {
  30. const userInfos = <UserInfos>await this.getApiUserInfo();
  31. this.userInfos = userInfos;
  32. }
  33. },
  34. // 存储常量信息到浏览器缓存
  35. async setConstList() {
  36. if (Session.get('constList')) {
  37. this.constList = Session.get('constList');
  38. } else {
  39. const constList = <any[]>await this.getSysConstList();
  40. Session.set('constList', constList);
  41. this.constList = constList;
  42. }
  43. },
  44. // 存储字典信息到浏览器缓存
  45. async setDictList() {
  46. var res = await getAPI(SysDictTypeApi).apiSysDictTypeAllDictListGet();
  47. this.dictList = res.data.result;
  48. // if (Session.get('dictList')) {
  49. // this.dictList = Session.get('dictList');
  50. // } else {
  51. // const dictList = <any[]>await this.getAllDictList();
  52. // Session.set('dictList', dictList);
  53. // this.dictList = dictList;
  54. // }
  55. },
  56. // 获取当前用户信息
  57. getApiUserInfo() {
  58. return new Promise((resolve) => {
  59. getAPI(SysAuthApi)
  60. .apiSysAuthUserInfoGet()
  61. .then(async (res: any) => {
  62. if (res.data.result == null) return;
  63. var d = res.data.result;
  64. const userInfos = {
  65. id: d.id,
  66. account: d.account,
  67. realName: d.realName,
  68. phone: d.phone,
  69. idCardNum: d.idCardNum,
  70. email: d.email,
  71. accountType: d.accountType,
  72. avatar: d.avatar ?? '/favicon.ico',
  73. address: d.address,
  74. signature: d.signature,
  75. orgId: d.orgId,
  76. orgName: d.orgName,
  77. posName: d.posName,
  78. roles: d.roleIds,
  79. authBtnList: d.buttons,
  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. resolve(userInfos);
  86. });
  87. });
  88. },
  89. // 获取常量集合
  90. getSysConstList() {
  91. return new Promise((resolve) => {
  92. getAPI(SysConstApi)
  93. .apiSysConstListGet()
  94. .then(async (res: any) => {
  95. resolve(res.data.result ?? []);
  96. });
  97. });
  98. },
  99. // 获取字典集合
  100. getAllDictList() {
  101. return new Promise((resolve) => {
  102. if (this.dictList) {
  103. resolve(this.dictList);
  104. } else {
  105. getAPI(SysDictTypeApi)
  106. .apiSysDictTypeAllDictListGet()
  107. .then((res: any) => {
  108. resolve(res.data.result ?? []);
  109. });
  110. }
  111. });
  112. },
  113. // 根据字典类型和代码取字典项
  114. getDictItemByCode(typePCode: string, val: string) {
  115. if (val) {
  116. const _val = val.toString();
  117. const ds = this.getDictDatasByCode(typePCode);
  118. for (let index = 0; index < ds.length; index++) {
  119. const element = ds[index];
  120. if (element.code == _val) {
  121. return element;
  122. }
  123. }
  124. }
  125. return {};
  126. },
  127. // 根据字典类型和值取描述
  128. getDictLabelByVal(typePCode: string, val: string) {
  129. if (val) {
  130. const _val = val.toString();
  131. const ds = this.getDictDatasByCode(typePCode);
  132. for (let index = 0; index < ds.length; index++) {
  133. const element = ds[index];
  134. if (element.value == _val) {
  135. return element;
  136. }
  137. }
  138. }
  139. return {};
  140. },
  141. // 根据字典类型和描述取值
  142. getDictValByLabel(typePCode: string, label: string) {
  143. if (!label) return '';
  144. const ds = this.getDictDatasByCode(typePCode);
  145. for (let index = 0; index < ds.length; index++) {
  146. const element = ds[index];
  147. if (element.name == label) {
  148. return element;
  149. }
  150. }
  151. },
  152. // 根据字典类型获取字典数据
  153. getDictDatasByCode(dictTypeCode: string) {
  154. return this.dictList[dictTypeCode] || [];
  155. },
  156. // 根据字典类型获取字典数据(值转为数字类型)
  157. getDictIntDatasByCode(dictTypeCode: string) {
  158. var ds = this.dictListInt[dictTypeCode];
  159. if (ds) {
  160. return ds;
  161. } else {
  162. ds = this.dictList[dictTypeCode].map((element: { code: any }) => {
  163. var d = { ...element };
  164. d.code = element.code - 0;
  165. return d;
  166. });
  167. this.dictListInt[dictTypeCode] = ds;
  168. return ds;
  169. }
  170. },
  171. },
  172. });