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)" } } }