userInfo.ts 3.1 KB

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