account.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { toRefs, computed, reactive } from 'vue'
  2. import { queryTaAccounts } from '@/services/api/account'
  3. import { defineStore } from '../store'
  4. import { useLoginStore } from './login'
  5. import eventBus from '@/services/bus'
  6. /**
  7. * 账号存储对象
  8. * @returns
  9. */
  10. export const useAccountStore = defineStore(() => {
  11. const loginStore = useLoginStore()
  12. const state = reactive({
  13. loading: false,
  14. accountList: <Model.TaAccountsRsp[]>[],
  15. currentAccountId: 0,
  16. })
  17. // 资金账户计算列表
  18. const accountComputedList = computed(() => {
  19. const result: (Model.TaAccountsRsp & {
  20. freezeMargin: number; // 冻结资金
  21. avaiableMoney: number; // 可用资金
  22. })[] = []
  23. state.accountList.forEach((item) => {
  24. // 计算冻结资金
  25. const freezeMargin = item.freezecharge + item.freezemargin + item.otherfreezemargin + item.outamountfreeze
  26. // 计算可用资金
  27. const avaiableMoney = item.currentbalance - freezeMargin
  28. result.push({
  29. ...item,
  30. freezeMargin,
  31. avaiableMoney
  32. })
  33. })
  34. return result
  35. })
  36. // 当前资金账户信息
  37. const currentAccount = computed(() => {
  38. return {
  39. ...accountComputedList.value.find((e) => e.accountid === state.currentAccountId)
  40. }
  41. })
  42. // 获取资金账户列表
  43. const getAccountList = async () => {
  44. try {
  45. state.loading = true
  46. const res = await queryTaAccounts({
  47. data: {
  48. loginID: loginStore.loginId
  49. }
  50. })
  51. const data = res.data
  52. state.accountList = data
  53. // 查找当前选中的资金账户
  54. if (!data.every((e) => e.accountid === state.currentAccountId)) {
  55. state.currentAccountId = data[0]?.accountid ?? 0
  56. }
  57. } finally {
  58. state.loading = false
  59. }
  60. }
  61. /** 获取资金账户持仓列表 */
  62. const getAccountPositionList = () => {
  63. throw '获取资金账户持仓列表'
  64. }
  65. // 接收资金变动通知
  66. const moneyChangedNotify = eventBus.$on('MoneyChangedNotify', () => getAccountList())
  67. return {
  68. ...toRefs(state),
  69. accountComputedList,
  70. currentAccount,
  71. moneyChangedNotify,
  72. getAccountList,
  73. getAccountPositionList,
  74. }
  75. })