userInfo.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { defineStore } from 'pinia';
  2. import { Cookie, Session } from '@/utils/storage';
  3. import { getUserInfo } from '@/api/login/user';
  4. import { JSEncrypt } from 'jsencrypt';
  5. import { encryptionLoginKey } from '@/utils/constants';
  6. /**
  7. * @description 用户信息
  8. * @methods getUserInfos 获取用户信息
  9. * @methods setUserInfos 设置用户信息
  10. */
  11. export const useUserInfo = defineStore('userInfo', {
  12. state: (): UserInfosStates => ({
  13. userInfos: {
  14. account: '', // 账号
  15. name: '', // 姓名
  16. phoneNo: '', //电话
  17. staffNo: '', // 工号
  18. id: '',
  19. photo: '', //头像
  20. authBtnList: [], // 授权按钮数组
  21. defaultTelNo: '', // 默认分机号
  22. defaultTelGroup: '', // 默认分机组
  23. token: '',
  24. showTelControl: false, // 是否展示坐席操作电话面板
  25. orgName: '', // 组织名称
  26. roles: [], // 角色
  27. isCenter: false, // 当前本部门是否是中心
  28. monitor: false, // 是否是班长
  29. encryptionHeader: '', // 加密用户信息(用户数据共享平台加密header)
  30. },
  31. }),
  32. actions: {
  33. async setUserInfos(buttons: string[]) {
  34. if (Session.get('userInfo')) {
  35. //有缓存 取缓存
  36. this.userInfos = Session.get('userInfo');
  37. } else {
  38. this.userInfos = (await this.getApiUserInfo(buttons)) as any;
  39. }
  40. },
  41. async getApiUserInfo(buttons: string[]) {
  42. try {
  43. // 个人信息
  44. let userInfo: any = await getUserInfo();
  45. this.userInfos.name = userInfo.result?.user.name ?? '暂无名称';
  46. this.userInfos.account = userInfo.result?.user.account ?? '';
  47. this.userInfos.phoneNo = userInfo.result?.user.phoneNo ?? '';
  48. this.userInfos.staffNo = userInfo.result?.user.staffNo ?? '';
  49. this.userInfos.defaultTelNo = userInfo.result.user.defaultTelNo ?? '';
  50. this.userInfos.defaultTelGroup = userInfo.result.user.defaultTelGroup ?? '';
  51. this.userInfos.id = userInfo.result?.user.id ?? '';
  52. this.userInfos.roles = userInfo.result?.user.roles ?? [];
  53. this.userInfos.token = Cookie.get('token') ?? '';
  54. this.userInfos.photo = '';
  55. this.userInfos.orgName = userInfo.result?.user.organization?.name ?? '';
  56. this.userInfos.isCenter = userInfo.result?.user.organization?.isCenter ?? false;
  57. this.userInfos.monitor = userInfo.result?.monitor ?? false;
  58. //授权按钮
  59. this.userInfos.showTelControl = buttons.includes('public:seat:panel'); // 查询是否有展示面板权限
  60. this.userInfos.authBtnList = buttons;
  61. // 新建一个JSEncrypt对象
  62. const encryptor = new JSEncrypt({ default_key_size: '2048' });
  63. encryptor.setPublicKey(encryptionLoginKey); // publicKey为公钥
  64. // 加密数据
  65. const submitObj: { userId: string; userName: string; orgName: string } = {
  66. userId: this.userInfos.id,
  67. userName: this.userInfos.name,
  68. orgName: this.userInfos.orgName,
  69. };
  70. this.userInfos.encryptionHeader = <string>encryptor.encrypt(JSON.stringify(submitObj));
  71. Session.set('userInfo', this.userInfos);
  72. return this.userInfos;
  73. } catch (error) {
  74. this.userInfos = {
  75. account: '暂无名称', // 账号
  76. name: '', // 姓名
  77. phoneNo: '', //电话
  78. staffNo: '', // 工号
  79. id: '',
  80. photo: '', //头像
  81. authBtnList: [], // 授权按钮数组
  82. defaultTelNo: '', // 默认分机号
  83. defaultTelGroup: '', // 默认分机组
  84. token: '',
  85. showTelControl: false, // 是否展示坐席操作电话面板
  86. orgName: '', // 组织名称
  87. roles: [], // 角色
  88. isCenter: false, // 当前部门是否是中心
  89. monitor: false, // 是否是班长
  90. encryptionHeader: '', // 加密用户信息(用户数据共享平台加密header)
  91. };
  92. Session.set('userInfo', this.userInfos);
  93. return this.userInfos;
  94. }
  95. },
  96. },
  97. });