阶段十:完成注释门禁最终验收

This commit is contained in:
Christ committed 2026-08-15 05:52:50 +08:00
1 parent d1a165f897
commit f30a1f68f8
29 files changed
+509 -7253

No files matched your search

+45 -40
View File
@@ -5,20 +5,25 @@ type QueryParams = Record<string, string | number | boolean | undefined | null>;
type JsonValue = unknown;
export type RequestControlOptions = {
signal?: AbortSignal;
timeoutMs?: number;
// signal 表示signal。
signal?: AbortSignal;
// timeoutMs 表示timeoutMs。
timeoutMs?: number;
};
type RequestOptions = {
params?: QueryParams;
body?: JsonValue;
skipAuthLogout?: boolean;
// params 表示params。
params?: QueryParams;
// body 表示请求体。
body?: JsonValue;
// skipAuthLogout 表示skipAuthLogout。
skipAuthLogout?: boolean;
} & RequestControlOptions;
const defaultRequestTimeoutMs = 30_000;
const uploadRequestTimeoutMs = 10 * 60_000;
const defaultRequestTimeoutMs = 30_000; /* defaultRequestTimeoutMs 表示default接口请求对象TimeoutMs。 */
const uploadRequestTimeoutMs = 10 * 60_000; /* uploadRequestTimeoutMs 表示upload接口请求对象TimeoutMs。 */
let authLogoutPending = false;
let authLogoutPending = false; /* authLogoutPending 表示authLogoutPending。 */
const notifyAuthExpired = () => {
if (authLogoutPending || typeof window === 'undefined') return;
@@ -26,26 +31,26 @@ const notifyAuthExpired = () => {
window.dispatchEvent(new Event('auth:logout'));
queueMicrotask(() => {
authLogoutPending = false;
});
};
} /* 回调函数负责当前业务流程。 */);
}; /* notifyAuthExpired 表示notifyAuthExpired。 */
const buildQueryString = (params?: QueryParams): string => {
if (!params) return '';
const searchParams = new URLSearchParams();
for (const [key, rawVal] of Object.entries(params)) {
const searchParams = new URLSearchParams(); /* searchParams 表示searchParams。 */
for (const [key, rawVal] /* [key, rawVal] 表示keyrawVal。 */ of Object.entries(params)) {
if (rawVal === undefined || rawVal === null) continue;
searchParams.set(key, String(rawVal));
}
const qs = searchParams.toString();
const qs = searchParams.toString(); /* qs 表示qs。 */
return qs ? `?${qs}` : '';
};
}; /* buildQueryString 表示buildQueryString。 */
const request = async <T>(method: RequestMethod, url: string, options: RequestOptions = {}): Promise<T> => {
const qs = buildQueryString(options.params);
const fullUrl = `${url}${qs}`;
const qs = buildQueryString(options.params); /* qs 表示qs。 */
const fullUrl = `${url}${qs}`; /* fullUrl 表示full请求地址。 */
const control = controlledSignal(options.signal, options.timeoutMs ?? defaultRequestTimeoutMs);
let res: Response;
const control = controlledSignal(options.signal, options.timeoutMs ?? defaultRequestTimeoutMs); /* control 表示control。 */
let res: Response; /* res 表示接口响应结果。 */
try {
res = await fetch(fullUrl, {
method,
@@ -56,7 +61,7 @@ const request = async <T>(method: RequestMethod, url: string, options: RequestOp
},
body: options.body === undefined ? undefined : JSON.stringify(options.body),
});
} catch (error) {
} catch (error /* error 表示当前操作返回的错误。 */) {
if (control.signal.aborted) {
throw new Error(options.signal?.aborted ? '请求已取消' : '请求超时,请稍后重试');
}
@@ -65,13 +70,13 @@ const request = async <T>(method: RequestMethod, url: string, options: RequestOp
control.cleanup();
}
const contentType = res.headers.get('content-type') || '';
const isJson = contentType.includes('application/json');
const contentType = res.headers.get('content-type') || ''; /* contentType 表示contentType。 */
const isJson = contentType.includes('application/json'); /* isJson 表示isJson。 */
if (!res.ok) {
if (res.status === 401 && !options.skipAuthLogout) notifyAuthExpired();
// payload 是后端统一错误 DTO 或非 JSON 错误文本。
const payload = isJson ? await res.json().catch(() => undefined) : await res.text().catch(() => undefined);
const payload = isJson ? await res.json().catch(() => undefined /* 回调函数负责当前业务流程。 */) : await res.text().catch(() => undefined /* 回调函数负责当前业务流程。 */);
const message = errorMessageFromPayload(payload, res.status); // message 是统一错误 DTO 提取出的用户可见说明。
throw new Error(message);
}
@@ -82,16 +87,16 @@ const request = async <T>(method: RequestMethod, url: string, options: RequestOp
}
return (await res.json()) as T;
};
}; /* request 表示接口请求对象。 */
export const get = async <T>(url: string, params?: QueryParams, options?: RequestControlOptions): Promise<T> => request<T>('GET', url, { params, ...options });
export const post = async <T>(url: string, body?: JsonValue, options?: RequestControlOptions & { skipAuthLogout?: boolean }): Promise<T> => request<T>('POST', url, { body, ...options });
export const put = async <T>(url: string, body?: JsonValue, options?: RequestControlOptions): Promise<T> => request<T>('PUT', url, { body, ...options });
export const del = async <T>(url: string, params?: QueryParams, options?: RequestControlOptions): Promise<T> => request<T>('DELETE', url, { params, ...options });
export const get = async <T>(url: string, params?: QueryParams, options?: RequestControlOptions): Promise<T> => request<T>('GET', url, { params, ...options }); /* get 表示get。 */
export const post = async <T>(url: string, body?: JsonValue, options?: RequestControlOptions & { /* skipAuthLogout 表示skipAuthLogout。 */ skipAuthLogout?: boolean }): Promise<T> => request<T>('POST', url, { body, ...options }); /* post 表示post。 */
export const put = async <T>(url: string, body?: JsonValue, options?: RequestControlOptions): Promise<T> => request<T>('PUT', url, { body, ...options }); /* put 表示put。 */
export const del = async <T>(url: string, params?: QueryParams, options?: RequestControlOptions): Promise<T> => request<T>('DELETE', url, { params, ...options }); /* del 表示del。 */
export const postForm = async <T>(url: string, body: FormData, options: RequestControlOptions = {}): Promise<T> => {
const control = controlledSignal(options.signal, options.timeoutMs ?? uploadRequestTimeoutMs);
let res: Response;
const control = controlledSignal(options.signal, options.timeoutMs ?? uploadRequestTimeoutMs); /* control 表示control。 */
let res: Response; /* res 表示接口响应结果。 */
try {
res = await fetch(url, {
method: 'POST',
@@ -99,7 +104,7 @@ export const postForm = async <T>(url: string, body: FormData, options: RequestC
signal: control.signal,
body,
});
} catch (error) {
} catch (error /* error 表示当前操作返回的错误。 */) {
if (control.signal.aborted) {
throw new Error(options.signal?.aborted ? '请求已取消' : '上传超时,请稍后重试');
}
@@ -108,35 +113,35 @@ export const postForm = async <T>(url: string, body: FormData, options: RequestC
control.cleanup();
}
const contentType = res.headers.get('content-type') || '';
const isJson = contentType.includes('application/json');
const payload = isJson ? await res.json().catch(() => undefined) : await res.text().catch(() => undefined);
const contentType = res.headers.get('content-type') || ''; /* contentType 表示contentType。 */
const isJson = contentType.includes('application/json'); /* isJson 表示isJson。 */
const payload = isJson ? await res.json().catch(() => undefined /* 回调函数负责当前业务流程。 */) : await res.text().catch(() => undefined /* 回调函数负责当前业务流程。 */); /* payload 表示payload。 */
if (!res.ok) {
if (res.status === 401) notifyAuthExpired();
const message = errorMessageFromPayload(payload, res.status); // message 是上传失败时统一错误 DTO 提取出的说明。
const err = new Error(message) as Error & { payload?: unknown };
const err = new Error(message) as Error & { /* payload 表示payload。 */ payload?: unknown }; /* err 表示当前操作返回的错误。 */
err.payload = payload;
throw err;
}
return payload as T;
};
}; /* postForm 表示postForm。 */
const controlledSignal = (external: AbortSignal | undefined, timeoutMs: number) => {
const controller = new AbortController();
const abortFromExternal = () => controller.abort(external?.reason);
const controller = new AbortController(); /* controller 表示controller。 */
const abortFromExternal = () => controller.abort(external?.reason); /* abortFromExternal 表示abortFromExternal。 */
if (external?.aborted) abortFromExternal();
else external?.addEventListener('abort', abortFromExternal, { once: true });
const timer = globalThis.setTimeout(() => controller.abort(new DOMException('timeout', 'TimeoutError')), Math.max(1, timeoutMs));
const timer = globalThis.setTimeout(() => controller.abort(new DOMException('timeout', 'TimeoutError')) /* 回调函数负责当前业务流程。 */, Math.max(1, timeoutMs)); /* timer 表示timer。 */
return {
signal: controller.signal,
cleanup: () => {
globalThis.clearTimeout(timer);
external?.removeEventListener('abort', abortFromExternal);
},
} /* 回调函数负责当前业务流程。 */,
};
};
}; /* controlledSignal 表示controlledSignal。 */
/** 判断响应体是否符合统一 HTTP 错误 DTO。 */
const isApiErrorResponse = (payload: unknown): payload is ApiErrorResponse => {