feat(auth): enhance login error handling with detailed error code mapping
All checks were successful
Frontend CI / ota-android (push) Successful in 1m29s
Frontend CI / build-and-push-web (push) Successful in 2m31s
Frontend CI / build-android-apk (push) Successful in 36m5s

- Add backend error_code support (INVALID_CREDENTIALS, USER_BANNED, etc.)
- Improve network error detection with code=0 fallback
- Add granular HTTP status code handling (400, 401, 403, 429, 5xx)
- Update LoginScreen to display authStore error or generic message
- Include notification service improvements:
  - Keep JPush connection alive in background via setKeepLongConnInBackground
  - Add manual sound/vibration for local notifications via preferences
- Fix SearchScreen layout by accounting for floating tab bar and safe area
This commit is contained in:
lafay
2026-04-28 17:25:27 +08:00
parent cf9feeae68
commit d2120a257d
5 changed files with 98 additions and 25 deletions

View File

@@ -62,32 +62,61 @@ async function cacheUser(user: User): Promise<void> {
}
// ── 将 API 错误转换为用户可读的中文提示 ──
// 优先使用后端返回的 error_code 精确匹配,兜底使用 HTTP 状态码和 message
function resolveLoginError(error: any): string {
const code: number = error?.code ?? 0;
const msg: string = error?.message ?? '';
const msg: string = String(error?.message ?? '');
const errorCode: string = String(error?.errorCode ?? '').toUpperCase();
// 网络层错误(无法连接服务器)
// 网络层错误(无法连接服务器)—— code 为 0 通常是请求未发出
if (
code === 0 ||
msg.includes('Network request failed') ||
msg.includes('网络请求失败') ||
msg.includes('Failed to fetch') ||
code === 500
msg.includes('Failed to fetch')
) {
return '无法连接到服务器,请检查网络连接后重试';
}
// 账号或密码错误
if (code === 401 || msg.includes('密码') || msg.includes('用户名') || msg.includes('incorrect')) {
// 优先使用后端 error_code 精确匹配
switch (errorCode) {
case 'INVALID_CREDENTIALS':
return '用户名或密码错误,请重新输入';
case 'USER_BANNED':
return '账号已被封禁,请联系管理员';
case 'USER_BLOCKED':
return '该账号存在异常,暂时无法登录';
case 'USER_NOT_FOUND':
return '用户不存在,请检查输入';
case 'BAD_REQUEST':
return '请求参数有误,请检查输入';
case 'UNAUTHORIZED':
return '登录已过期,请重新登录';
case 'FORBIDDEN':
return '没有访问权限,请联系管理员';
case 'INTERNAL_ERROR':
return '服务器内部错误,请稍后重试';
case 'TOO_MANY_REQUESTS':
case 'RATE_LIMITED':
return '登录尝试过于频繁,请稍后再试';
}
// 兜底:按 HTTP 状态码分类
if (code === 401 || code === 400) {
return '用户名或密码错误,请重新输入';
}
// 账号被禁用/封禁
if (code === 403 || msg.includes('禁止') || msg.includes('封禁') || msg.includes('forbidden')) {
if (code === 403) {
return '账号已被禁用,请联系管理员';
}
if (code === 429) {
return '登录尝试过于频繁,请稍后再试';
}
if (code >= 500) {
return '服务器开小差了,请稍后重试';
}
// 服务端明确返回了 message直接用
if (msg && msg !== '登录失败') {
// 最后兜底:如果后端返回了有意义的 message直接用
if (msg && msg !== '登录失败' && msg !== '请求失败') {
return msg;
}