98b7600797
对标安卓正式版、对接 app-server(8770)的 SwiftUI 客户端。iOS 无障碍受限、无比价核心,做自成一体:美团 CPS 导购 + 福利(签到/任务/金币)+ 查看(比价记录/省钱战绩)。 功能完整 M0-M5:脚手架、短信登录(JWT+Keychain+401 自动刷新)、首页券 feed 导购、比价记录+省钱战绩、福利、我的(资料/反馈/设置/注销)。 单 target、0 三方依赖、XcodeGen 生成工程;Debug/Release 双 plist 分流 ATS。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
37 lines
1.4 KiB
Swift
37 lines
1.4 KiB
Swift
import Foundation
|
|
|
|
enum APIError: Error, LocalizedError {
|
|
case invalidResponse // 不是 HTTPURLResponse / URL 构造失败
|
|
case http(status: Int, detail: String?)
|
|
case unauthorized // 401 且刷新失败 → 需重新登录
|
|
case decoding(Error)
|
|
case network(Error)
|
|
|
|
var errorDescription: String? {
|
|
switch self {
|
|
case .invalidResponse:
|
|
return "服务器响应异常"
|
|
case .http(let status, let detail):
|
|
// 后端返回中文 detail(友好提示)直接透出;英文/技术性 detail 按状态码给中文兜底
|
|
if let detail, detail.contains(where: { !$0.isASCII }) {
|
|
return detail
|
|
}
|
|
switch status {
|
|
case 400: return "请求有误,请重试"
|
|
case 401: return "登录已过期,请重新登录"
|
|
case 403: return "账号状态异常,请联系客服"
|
|
case 404: return "内容不存在"
|
|
case 429: return "操作太频繁,请稍后再试"
|
|
case 500...: return "服务器繁忙,请稍后重试"
|
|
default: return "请求失败(\(status))"
|
|
}
|
|
case .unauthorized:
|
|
return "登录已过期,请重新登录"
|
|
case .decoding:
|
|
return "数据解析失败"
|
|
case .network(let e):
|
|
return "网络异常:\(e.localizedDescription)"
|
|
}
|
|
}
|
|
}
|