更新环境配置,修正服务地址;新增 RWA 产品管理相关组件及功能;优化表格数据展示及状态渲染逻辑
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import { ref } from 'vue';
|
||||
import type { Ref, WatchSource } from 'vue';
|
||||
import { ref, watch } from 'vue';
|
||||
import { treaty } from '@elysiajs/eden';
|
||||
import type { App } from '@riwa/api-types';
|
||||
import { getServiceBaseURL } from '@/utils/service';
|
||||
import { localStg } from '@/utils/storage';
|
||||
import { $t } from '@/locales';
|
||||
|
||||
const isHttpProxy = import.meta.env.DEV && import.meta.env.VITE_HTTP_PROXY === 'Y';
|
||||
const { baseURL } = getServiceBaseURL(import.meta.env, isHttpProxy);
|
||||
@@ -14,37 +16,104 @@ const client = treaty<App>(baseURL, {
|
||||
}
|
||||
});
|
||||
|
||||
async function safeClient<T, E>(
|
||||
requestFactory: () => Promise<{ data: T; error: E }>,
|
||||
options: { silent?: boolean; immediate?: boolean } = {}
|
||||
) {
|
||||
const { immediate = true } = options;
|
||||
const data = ref<T>();
|
||||
const error = ref<E>();
|
||||
export interface SafeClientOptions {
|
||||
silent?: boolean;
|
||||
immediate?: boolean;
|
||||
watchSource?: WatchSource; // 用于监听的响应式数据源
|
||||
}
|
||||
|
||||
const executeRequest = async () => {
|
||||
const res = await requestFactory();
|
||||
export interface SafeClientReturn<T, E> {
|
||||
data: Ref<T | null>;
|
||||
error: Ref<E | null>;
|
||||
isPending: Ref<boolean>;
|
||||
execute: () => Promise<void>;
|
||||
onFetchResponse: (callback: (data: T, error: E) => void) => void;
|
||||
stopWatching?: () => void;
|
||||
}
|
||||
|
||||
if (res.error) {
|
||||
export type RequestPromise<T, E> = Promise<{ data: T; error: E; status?: number; response?: Response }>;
|
||||
|
||||
export function safeClient<T, E>(
|
||||
requestPromise: (() => RequestPromise<T, E>) | RequestPromise<T, E>,
|
||||
options: SafeClientOptions = {}
|
||||
): SafeClientReturn<T, E> & Promise<SafeClientReturn<T, E>> {
|
||||
const { immediate = true, watchSource } = options;
|
||||
const data = ref<T | null>(null);
|
||||
const error = ref<E | null>(null);
|
||||
const isPending = ref(false);
|
||||
let responseCallback: ((data: T, error: E) => void) | null = null;
|
||||
let stopWatcher: (() => void) | undefined;
|
||||
|
||||
const execute = async () => {
|
||||
isPending.value = true;
|
||||
let request: () => RequestPromise<T, E>;
|
||||
if (typeof requestPromise !== 'function') {
|
||||
request = () => Promise.resolve(requestPromise);
|
||||
} else {
|
||||
request = requestPromise;
|
||||
}
|
||||
|
||||
const res = await request().finally(() => {
|
||||
isPending.value = false;
|
||||
});
|
||||
|
||||
if (res.error && res.status === 418) {
|
||||
if (!options.silent) {
|
||||
window.$message?.error('An error occurred while processing your request.');
|
||||
const msg = $t((res.error as any).value.code, {
|
||||
...(res.error as any).value.context
|
||||
});
|
||||
window.$message?.create(msg, {
|
||||
type: 'error'
|
||||
});
|
||||
}
|
||||
|
||||
throw res.error;
|
||||
}
|
||||
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
|
||||
function stopWatching() {
|
||||
if (stopWatcher) {
|
||||
stopWatcher();
|
||||
stopWatcher = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果提供了 watchSource,则监听其变化
|
||||
if (watchSource) {
|
||||
stopWatcher = watch(
|
||||
watchSource,
|
||||
() => {
|
||||
execute();
|
||||
},
|
||||
{ immediate: false } // 不立即执行,避免与 immediate 选项冲突
|
||||
);
|
||||
}
|
||||
|
||||
const result: SafeClientReturn<T, E> = {
|
||||
data: data as Ref<T | null>,
|
||||
error: error as Ref<E | null>,
|
||||
isPending,
|
||||
execute,
|
||||
onFetchResponse,
|
||||
stopWatching
|
||||
};
|
||||
|
||||
const promise = immediate ? execute().then(() => result) : Promise.resolve(result);
|
||||
|
||||
Object.assign(promise, result);
|
||||
|
||||
return promise as SafeClientReturn<T, E> & Promise<SafeClientReturn<T, E>>;
|
||||
}
|
||||
|
||||
export { client, safeClient };
|
||||
export { client };
|
||||
|
||||
Reference in New Issue
Block a user