feat: 更新钱包管理功能,添加银行账户和余额的初始化逻辑,优化API请求和响应处理

This commit is contained in:
2025-12-17 02:09:07 +07:00
parent d375d12583
commit 2f1881cc81
8 changed files with 89 additions and 22 deletions

View File

@@ -8,15 +8,21 @@ const client = treaty<App>(window.location.origin, {
},
});
export interface SafeClientOptions {
silent?: boolean;
immediate?: boolean;
}
export async function safeClient<T, E>(
requestPromise: () => Promise<{ data: T; error: E }>,
options: { silent?: boolean; immediate?: boolean } = {},
options: SafeClientOptions = {},
) {
const { immediate = true } = options;
const data = ref<T | null>(null);
const error = ref<E | null>(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<T, E>(
}
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 };