user.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { toRefs, shallowReadonly, computed } from 'vue'
  2. import { queryLoginData } from '@/services/api/account'
  3. import { useLoginStore } from './login'
  4. import { VueStore } from '../base'
  5. import eventBus from '@/services/bus';
  6. interface StoreState {
  7. loading: boolean;
  8. userData: Model.LoginQueryRsp;
  9. }
  10. /**
  11. * 用户存储类
  12. */
  13. const store = new (class extends VueStore<StoreState>{
  14. constructor() {
  15. const state: StoreState = {
  16. loading: false,
  17. userData: {
  18. arearole: [],
  19. externalExchanges: [],
  20. goodsgroups: [],
  21. markets: [],
  22. systemParams: []
  23. },
  24. }
  25. super(state)
  26. eventBus.$on('UserChangeNtf',()=>{
  27. this.actions.getUserData()
  28. })
  29. }
  30. /** 是否已实名认证 */
  31. private hasAuth = computed(() => {
  32. const { userAccount } = this.state.userData
  33. return userAccount?.hasauth === 1
  34. })
  35. getters = {
  36. hasAuth: this.hasAuth
  37. }
  38. actions = {
  39. /** 获取用户数据 */
  40. getUserData: () => {
  41. const { getLoginId } = useLoginStore()
  42. this.state.loading = true
  43. return queryLoginData({
  44. data: {
  45. loginID: getLoginId()
  46. },
  47. success: (res) => {
  48. this.state.userData = res.data
  49. },
  50. complete: () => {
  51. this.state.loading = false
  52. }
  53. })
  54. },
  55. /** 获取用户数据 */
  56. getUserDataInfo: <K extends keyof Model.LoginQueryRsp>(key: K) => {
  57. return this.state.userData[key]!
  58. },
  59. /** 获取登录机构名称 */
  60. getAccountName: () => {
  61. const { userAccount } = this.state.userData
  62. return userAccount?.accountname
  63. },
  64. /** 获取登录用户信息 */
  65. getUserInfo: () => {
  66. const { userInfo } = this.state.userData
  67. return userInfo
  68. }
  69. }
  70. })
  71. export function useUserStore() {
  72. return shallowReadonly({
  73. ...toRefs(store.state),
  74. ...store.getters,
  75. ...store.actions,
  76. ...store.methods,
  77. })
  78. }