refactor(core): harden API parsing and improve type safety

This commit is contained in:
LTbinglingfeng
2026-02-08 09:42:00 +08:00
parent 3783bec983
commit 6c2cd761ba
39 changed files with 689 additions and 404 deletions

View File

@@ -15,11 +15,20 @@ import styles from './LoginPage.module.scss';
/**
* 将 API 错误转换为本地化的用户友好消息
*/
function getLocalizedErrorMessage(error: any, t: (key: string) => string): string {
const apiError = error as ApiError;
const status = apiError?.status;
const code = apiError?.code;
const message = apiError?.message || '';
type RedirectState = { from?: { pathname?: string } };
function getLocalizedErrorMessage(error: unknown, t: (key: string) => string): string {
const apiError = error as Partial<ApiError>;
const status = typeof apiError.status === 'number' ? apiError.status : undefined;
const code = typeof apiError.code === 'string' ? apiError.code : undefined;
const message =
error instanceof Error
? error.message
: typeof apiError.message === 'string'
? apiError.message
: typeof error === 'string'
? error
: '';
// 根据 HTTP 状态码判断
if (status === 401) {
@@ -99,7 +108,7 @@ export function LoginPage() {
setAutoLoginSuccess(true);
// 延迟跳转,让用户看到成功动画
setTimeout(() => {
const redirect = (location.state as any)?.from?.pathname || '/';
const redirect = (location.state as RedirectState | null)?.from?.pathname || '/';
navigate(redirect, { replace: true });
}, 1500);
} else {
@@ -135,7 +144,7 @@ export function LoginPage() {
});
showNotification(t('common.connected_status'), 'success');
navigate('/', { replace: true });
} catch (err: any) {
} catch (err: unknown) {
const message = getLocalizedErrorMessage(err, t);
setError(message);
showNotification(`${t('notification.login_failed')}: ${message}`, 'error');
@@ -155,7 +164,7 @@ export function LoginPage() {
);
if (isAuthenticated && !autoLoading && !autoLoginSuccess) {
const redirect = (location.state as any)?.from?.pathname || '/';
const redirect = (location.state as RedirectState | null)?.from?.pathname || '/';
return <Navigate to={redirect} replace />;
}