import { toRefs, computed, reactive } from 'vue' import { queryTaAccounts } from '@/services/api/account' import { defineStore } from '../store' import { useLoginStore } from './login' import eventBus from '@/services/bus' /** * 账号存储对象 * @returns */ export const useAccountStore = defineStore(() => { const loginStore = useLoginStore() const state = reactive({ loading: false, accountList: [], currentAccountId: 0, }) // 资金账户计算列表 const accountComputedList = computed(() => { const result: (Model.TaAccountsRsp & { freezeMargin: number; // 冻结资金 avaiableMoney: number; // 可用资金 })[] = [] state.accountList.forEach((item) => { // 计算冻结资金 const freezeMargin = item.freezecharge + item.freezemargin + item.otherfreezemargin + item.outamountfreeze // 计算可用资金 const avaiableMoney = item.currentbalance - freezeMargin result.push({ ...item, freezeMargin, avaiableMoney }) }) return result }) // 当前资金账户信息 const currentAccount = computed(() => { return { ...accountComputedList.value.find((e) => e.accountid === state.currentAccountId) } }) // 获取资金账户列表 const getAccountList = async () => { try { state.loading = true const res = await queryTaAccounts({ data: { loginID: loginStore.loginId } }) const data = res.data state.accountList = data // 查找当前选中的资金账户 if (!data.every((e) => e.accountid === state.currentAccountId)) { state.currentAccountId = data[0]?.accountid ?? 0 } } finally { state.loading = false } } /** 获取资金账户持仓列表 */ const getAccountPositionList = () => { throw '获取资金账户持仓列表' } // 接收资金变动通知 const moneyChangedNotify = eventBus.$on('MoneyChangedNotify', () => getAccountList()) return { ...toRefs(state), accountComputedList, currentAccount, moneyChangedNotify, getAccountList, getAccountPositionList, } })