From 2f1881cc81a433aa09b21cf49d9c2addf5a2e58f Mon Sep 17 00:00:00 2001 From: Seven Date: Wed, 17 Dec 2025 02:09:07 +0700 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E9=92=B1=E5=8C=85?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=93=B6=E8=A1=8C=E8=B4=A6=E6=88=B7=E5=92=8C=E4=BD=99=E9=A2=9D?= =?UTF-8?q?=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96API=E8=AF=B7=E6=B1=82=E5=92=8C=E5=93=8D?= =?UTF-8?q?=E5=BA=94=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auto-imports.d.ts | 12 ++++++ src/App.vue | 10 ++--- src/api/index.ts | 25 +++++++++--- src/store/wallet.ts | 40 +++++++++++++++++-- .../trade-settings/bank-management/index.vue | 7 +++- src/views/user/components/wallet-card.vue | 9 ++++- src/views/withdraw/index.vue | 6 +-- vite.config.ts | 2 +- 8 files changed, 89 insertions(+), 22 deletions(-) diff --git a/auto-imports.d.ts b/auto-imports.d.ts index db8c17d..9c86d5a 100644 --- a/auto-imports.d.ts +++ b/auto-imports.d.ts @@ -364,6 +364,7 @@ declare module 'vue' { interface GlobalComponents {} interface ComponentCustomProperties { readonly EffectScope: UnwrapRef + readonly acceptHMRUpdate: UnwrapRef readonly asyncComputed: UnwrapRef readonly autoResetRef: UnwrapRef readonly computed: UnwrapRef @@ -377,6 +378,7 @@ declare module 'vue' { readonly createEventHook: UnwrapRef readonly createGlobalState: UnwrapRef readonly createInjectionState: UnwrapRef + readonly createPinia: UnwrapRef readonly createReactiveFn: UnwrapRef readonly createRef: UnwrapRef readonly createReusableTemplate: UnwrapRef @@ -388,11 +390,13 @@ declare module 'vue' { readonly debouncedWatch: UnwrapRef readonly defineAsyncComponent: UnwrapRef readonly defineComponent: UnwrapRef + readonly defineStore: UnwrapRef readonly eagerComputed: UnwrapRef readonly effectScope: UnwrapRef readonly emailPattern: UnwrapRef readonly extendRef: UnwrapRef readonly formatBalance: UnwrapRef + readonly getActivePinia: UnwrapRef readonly getCurrentInstance: UnwrapRef readonly getCurrentScope: UnwrapRef readonly getCurrentWatcher: UnwrapRef @@ -407,6 +411,11 @@ declare module 'vue' { readonly isRef: UnwrapRef readonly isShallow: UnwrapRef readonly makeDestructurable: UnwrapRef + readonly mapActions: UnwrapRef + readonly mapGetters: UnwrapRef + readonly mapState: UnwrapRef + readonly mapStores: UnwrapRef + readonly mapWritableState: UnwrapRef readonly markRaw: UnwrapRef readonly nextTick: UnwrapRef readonly numberPattern: UnwrapRef @@ -450,9 +459,12 @@ declare module 'vue' { readonly refWithControl: UnwrapRef readonly resolveComponent: UnwrapRef readonly resolveRef: UnwrapRef + readonly setActivePinia: UnwrapRef + readonly setMapStoreSuffix: UnwrapRef readonly shallowReactive: UnwrapRef readonly shallowReadonly: UnwrapRef readonly shallowRef: UnwrapRef + readonly storeToRefs: UnwrapRef readonly syncRef: UnwrapRef readonly syncRefs: UnwrapRef readonly templateRef: UnwrapRef diff --git a/src/App.vue b/src/App.vue index a01567a..fd0ffa2 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,12 +1,8 @@ diff --git a/src/api/index.ts b/src/api/index.ts index aa2b482..1cf37ca 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -8,15 +8,21 @@ const client = treaty(window.location.origin, { }, }); +export interface SafeClientOptions { + silent?: boolean; + immediate?: boolean; +} + export async function safeClient( requestPromise: () => Promise<{ data: T; error: E }>, - options: { silent?: boolean; immediate?: boolean } = {}, + options: SafeClientOptions = {}, ) { const { immediate = true } = options; const data = ref(null); const error = ref(null); + let responseCallback: ((data: T, error: E) => void) | null = null; - const executeRequest = async () => { + const execute = async () => { const res = await requestPromise(); if (res.error) { @@ -34,13 +40,22 @@ export async function safeClient( } data.value = res.data; error.value = res.error; + + // 调用注册的回调函数 + if (responseCallback) { + responseCallback(res.data, res.error); + } }; - if (immediate) { - await executeRequest(); + function onFetchResponse(callback: (data: T, error: E) => void) { + responseCallback = callback; } - return { data, error, refresh: executeRequest }; + if (immediate) { + await execute(); + } + + return { data, error, refresh: execute, onFetchResponse }; } export { client }; diff --git a/src/store/wallet.ts b/src/store/wallet.ts index ca3232b..8e06202 100644 --- a/src/store/wallet.ts +++ b/src/store/wallet.ts @@ -1,16 +1,50 @@ -import type { BalancesData } from "@/api/types"; +import type { BalancesData, BankAccountsData } from "@/api/types"; import { defineStore } from "pinia"; +import { client, safeClient } from "@/api"; interface State { - balances: BalancesData | null; + balances: BalancesData; + bankAccounts: BankAccountsData["data"]; } export const useWalletStore = defineStore("wallet", () => { const state = reactive({ - balances: null, + balances: [], + bankAccounts: [], }); + const balances = computed(() => state.balances); + const bankAccounts = computed(() => state.bankAccounts); + + async function initializeWallet() { + updateBalances(); + updateBankAccounts(); + } + + async function updateBalances(data?: BalancesData) { + if (data) { + state.balances = data; + return; + } + const { data: balances } = await safeClient(() => client.api.asset.balances.get(), { silent: true }); + state.balances = balances.value || []; + } + + async function updateBankAccounts(data?: BankAccountsData["data"]) { + if (data) { + state.bankAccounts = data; + return; + } + const { data: bankAccounts } = await safeClient(() => client.api.bank_account.get(), { silent: true }); + state.bankAccounts = bankAccounts.value?.data || []; + } + return { state, + balances, + bankAccounts, + initializeWallet, + updateBalances, + updateBankAccounts, }; }); diff --git a/src/views/trade-settings/bank-management/index.vue b/src/views/trade-settings/bank-management/index.vue index c745cc1..ce16b5a 100644 --- a/src/views/trade-settings/bank-management/index.vue +++ b/src/views/trade-settings/bank-management/index.vue @@ -11,10 +11,15 @@ import { client, safeClient } from "@/api"; const { t } = useI18n(); const router = useRouter(); +const { updateBankAccounts } = useWalletStore(); -const { data, refresh } = await safeClient(() => client.api.bank_account.get()); +const { data, refresh, onFetchResponse } = await safeClient(() => client.api.bank_account.get()); const bankCards = computed(() => data.value?.data || []); +onFetchResponse((data) => { + data?.data && updateBankAccounts(data.data); +}); + function handleAddCard() { router.push("/trade-settings/bank-management/add"); } diff --git a/src/views/user/components/wallet-card.vue b/src/views/user/components/wallet-card.vue index 293f771..2b18a1c 100644 --- a/src/views/user/components/wallet-card.vue +++ b/src/views/user/components/wallet-card.vue @@ -3,7 +3,8 @@ import RechargeChannel from "./recharge-channel.vue"; const { t } = useI18n(); const router = useRouter(); -const { state } = useWalletStore(); +const walletStore = useWalletStore(); +const { balances } = storeToRefs(walletStore); const rechargeInstance = ref(); function onCloseModal() { @@ -12,12 +13,16 @@ function onCloseModal() { function handleWithdraw() { router.push("/withdraw/index"); } + +onUpdated(() => { + walletStore.updateBalances(); +});