userInfo.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { defineStore } from 'pinia';
  2. import { UserInfosStates } from './interface';
  3. import { Session } from '/@/utils/storage';
  4. import { request } from '../utils/service';
  5. /**
  6. * 用户信息
  7. * @methods setUserInfos 设置用户信息
  8. */
  9. export const useUserInfo = defineStore('userInfo', {
  10. state: (): UserInfosStates => ({
  11. userInfos: {
  12. avatar: '',
  13. username: '',
  14. name: '',
  15. email: '',
  16. mobile: '',
  17. gender: '',
  18. dept_info: {
  19. dept_id: 0,
  20. dept_name: '',
  21. },
  22. role_info: [
  23. {
  24. id: 0,
  25. name: '',
  26. },
  27. ],
  28. },
  29. }),
  30. actions: {
  31. async updateUserInfos() {
  32. let userInfos: any = await this.getApiUserInfo();
  33. this.userInfos.username = userInfos.data.name;
  34. this.userInfos.avatar = userInfos.data.avatar;
  35. this.userInfos.name = userInfos.data.name;
  36. this.userInfos.email = userInfos.data.email;
  37. this.userInfos.mobile = userInfos.data.mobile;
  38. this.userInfos.gender = userInfos.data.gender;
  39. this.userInfos.dept_info = userInfos.data.dept_info;
  40. this.userInfos.role_info = userInfos.data.role_info;
  41. Session.set('userInfo', this.userInfos);
  42. },
  43. async setUserInfos() {
  44. // 存储用户信息到浏览器缓存
  45. if (Session.get('userInfo')) {
  46. this.userInfos = Session.get('userInfo');
  47. } else {
  48. let userInfos: any = await this.getApiUserInfo();
  49. this.userInfos.username = userInfos.data.name;
  50. this.userInfos.avatar = userInfos.data.avatar;
  51. this.userInfos.name = userInfos.data.name;
  52. this.userInfos.email = userInfos.data.email;
  53. this.userInfos.mobile = userInfos.data.mobile;
  54. this.userInfos.gender = userInfos.data.gender;
  55. this.userInfos.dept_info = userInfos.data.dept_info;
  56. this.userInfos.role_info = userInfos.data.role_info;
  57. Session.set('userInfo', this.userInfos);
  58. }
  59. },
  60. async getApiUserInfo() {
  61. return request({
  62. url: '/api/system/user/user_info/',
  63. method: 'get',
  64. });
  65. },
  66. },
  67. });