| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import { computed, toRefs, shallowReadonly } from 'vue'
- import { queryTaAccounts } from '@/services/api/account'
- import { useLoginStore } from './login'
- import { VueStore } from '../base'
- import eventBus from '@/services/bus'
- interface StoreState {
- loading: boolean;
- accountList: Ermcp.TaAccountsRsp[]; // 资金账户列表
- currentAccountId: number; // 当前资金账户ID
- }
- /**
- * 账号存储类
- */
- const store = new (class extends VueStore<StoreState> {
- constructor() {
- const state: StoreState = {
- loading: false,
- accountList: [],
- currentAccountId: 0,
- }
- super(state)
- // 接收资金变动通知
- eventBus.$on('MoneyChangedNotify', () => {
- this.actions.getAccountList()
- })
- }
- /** 当前资金账户信息 */
- private currentAccountInfo = computed(() => {
- return this.state.accountList.find((e) => e.accountid === this.state.currentAccountId)
- })
- getters = {
- currentAccountInfo: this.currentAccountInfo
- }
- actions = {
- /** 获取资金账户列表 */
- getAccountList: () => {
- const { getLoginId } = useLoginStore()
- this.state.loading = true
- return queryTaAccounts({
- data: {
- loginID: getLoginId()
- },
- success: (res) => {
- const dataList = res.data
- if (dataList.length) {
- this.state.accountList = dataList
- // 查找当前选中的资金账户
- const account = dataList.find((e) => e.accountid === this.state.currentAccountId)
- if (account) {
- this.state.loading = false
- } else {
- // 如果不存在,默认选中第一个账户
- this.state.currentAccountId = dataList[0].accountid
- }
- } else {
- this.state.loading = false
- this.actions.reset()
- }
- },
- fail: () => {
- this.state.loading = false
- }
- })
- },
- /** 重置数据 */
- reset: () => {
- this.state.accountList = []
- this.state.currentAccountId = 0
- }
- }
- })
- export function useAccountStore() {
- return shallowReadonly({
- ...toRefs(store.state),
- ...store.getters,
- ...store.actions,
- ...store.methods,
- })
- }
|