account.ts 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { queryTaAccounts } from '@/services/api/account'
  2. import { loginStore } from './login'
  3. import { createStore } from '../base'
  4. import eventBus from '@/services/bus'
  5. /**
  6. * 账号存储对象
  7. */
  8. export const accountStore = createStore({
  9. state() {
  10. return {
  11. loading: false,
  12. accountList: <Ermcp.TaAccountsRsp[]>[],
  13. accountId: 0,
  14. }
  15. },
  16. getters: {
  17. // 当前资金账户信息
  18. accountInfo() {
  19. return this.state.accountList.find((e) => e.accountid === this.state.accountId)
  20. },
  21. // 冻结资金
  22. freezeMargin() {
  23. const { freezecharge = 0, freezemargin = 0, otherfreezemargin = 0, outamountfreeze = 0 } = this.getters.accountInfo ?? {}
  24. return freezecharge + freezemargin + otherfreezemargin + outamountfreeze
  25. },
  26. // 可用资金
  27. avaiableMoney() {
  28. const { currentbalance = 0 } = this.getters.accountInfo ?? {}
  29. return currentbalance - this.getters.freezeMargin
  30. }
  31. },
  32. actions: {
  33. // 获取资金账户列表
  34. getAccountList() {
  35. this.state.loading = true
  36. return queryTaAccounts({
  37. data: {
  38. loginID: loginStore.getters.loginId
  39. },
  40. success: (res) => {
  41. const dataList = res.data
  42. if (dataList.length) {
  43. this.state.accountList = dataList
  44. // 查找当前选中的资金账户
  45. const account = dataList.find((e) => e.accountid === this.state.accountId)
  46. if (account) {
  47. this.state.loading = false
  48. } else {
  49. // 如果不存在,默认选中第一个账户
  50. this.state.accountId = dataList[0].accountid
  51. }
  52. } else {
  53. this.state.loading = false
  54. this.actions.reset()
  55. }
  56. },
  57. fail: () => {
  58. this.state.loading = false
  59. }
  60. })
  61. },
  62. // 重置数据
  63. reset() {
  64. this.state.accountList = []
  65. this.state.accountId = 0
  66. }
  67. }
  68. })
  69. // 接收资金变动通知
  70. export const moneyChangedNotify = eventBus.$on('MoneyChangedNotify', () => {
  71. accountStore.actions.getAccountList()
  72. })