import { queryTaAccounts } from '@/services/api/account' import { loginStore } from './login' import { createStore } from '../base' import eventBus from '@/services/bus' /** * 账号存储对象 */ export const accountStore = createStore({ state() { return { loading: false, accountList: [], accountId: 0, } }, getters: { // 当前资金账户信息 accountInfo() { return this.state.accountList.find((e) => e.accountid === this.state.accountId) }, // 冻结资金 freezeMargin() { const { freezecharge = 0, freezemargin = 0, otherfreezemargin = 0, outamountfreeze = 0 } = this.getters.accountInfo ?? {} return freezecharge + freezemargin + otherfreezemargin + outamountfreeze }, // 可用资金 avaiableMoney() { const { currentbalance = 0 } = this.getters.accountInfo ?? {} return currentbalance - this.getters.freezeMargin } }, actions: { // 获取资金账户列表 getAccountList() { this.state.loading = true return queryTaAccounts({ data: { loginID: loginStore.getters.loginId }, success: (res) => { const dataList = res.data if (dataList.length) { this.state.accountList = dataList // 查找当前选中的资金账户 const account = dataList.find((e) => e.accountid === this.state.accountId) if (account) { this.state.loading = false } else { // 如果不存在,默认选中第一个账户 this.state.accountId = dataList[0].accountid } } else { this.state.loading = false this.actions.reset() } }, fail: () => { this.state.loading = false } }) }, // 重置数据 reset() { this.state.accountList = [] this.state.accountId = 0 } } }) // 接收资金变动通知 export const moneyChangedNotify = eventBus.$on('MoneyChangedNotify', () => { accountStore.actions.getAccountList() })