登陆修改

This commit is contained in:
孙佳艺
2026-06-16 13:16:01 +08:00
parent 1b15561e0a
commit 5afe837dec
5 changed files with 182 additions and 63 deletions
+5 -3
View File
@@ -14,6 +14,7 @@ interface RequestOptions {
auth?: boolean;
encryptBody?: boolean;
signal?: AbortSignal;
skipAuthRedirect?: boolean;
}
/** Convert snake_case string to camelCase */
@@ -110,7 +111,7 @@ export async function apiRequest<T>(path: string, options: RequestOptions = {}):
// Handle error responses
if (!res.ok) {
const msg = parsed?.detail || `请求失败 (${res.status})`;
if (res.status === 401) {
if (res.status === 401 && !options.skipAuthRedirect) {
clearToken();
window.location.href = '/login';
}
@@ -122,11 +123,12 @@ if (!res.ok) {
// Convenience methods
export const api = {
get: <T>(path: string, options: boolean | { auth?: boolean; signal?: AbortSignal } = true) => {
get: <T>(path: string, options: boolean | { auth?: boolean; signal?: AbortSignal; skipAuthRedirect?: boolean } = true) => {
const opts = typeof options === 'boolean' ? { auth: options } : options;
return apiRequest<T>(path, opts);
},
post: <T>(path: string, body?: unknown, auth = true) => apiRequest<T>(path, { method: 'POST', body, auth }),
post: <T>(path: string, body?: unknown, auth = true, skipAuthRedirect = false) =>
apiRequest<T>(path, { method: 'POST', body, auth, skipAuthRedirect }),
put: <T>(path: string, body?: unknown, auth = true) => apiRequest<T>(path, { method: 'PUT', body, auth }),
delete: <T>(path: string, auth = true) => apiRequest<T>(path, { method: 'DELETE', auth }),
};