
- 在APIEndpoints中新增用户信息更新端点。 - 实现UpdateUserRequest和UpdateUserResponse结构体,支持用户信息更新请求和响应。 - 在APIService中添加updateUser方法,处理用户信息更新请求。 - 更新AppSettingFeature以支持头像和昵称的修改,整合用户信息更新逻辑。 - 在AppSettingView中实现头像选择和昵称编辑功能,提升用户体验。
131 lines
4.7 KiB
Swift
131 lines
4.7 KiB
Swift
import Foundation
|
||
|
||
// MARK: - API Endpoints
|
||
|
||
/// API 端点枚举
|
||
///
|
||
/// 定义了应用中所有可用的 API 端点路径。
|
||
/// 使用枚举确保端点路径的类型安全和统一管理。
|
||
///
|
||
/// 添加新端点时,只需在此枚举中添加新的 case 即可。
|
||
///
|
||
/// 使用示例:
|
||
/// ```swift
|
||
/// let configPath = APIEndpoint.config.path // "/client/config"
|
||
/// ```
|
||
enum APIEndpoint: String, CaseIterable {
|
||
case config = "/client/config"
|
||
case configInit = "/client/init"
|
||
case login = "/oauth/token"
|
||
case ticket = "/oauth/ticket"
|
||
case emailGetCode = "/email/getCode" // 新增:邮箱验证码获取端点
|
||
case latestDynamics = "/dynamic/square/latestDynamics" // 新增:获取最新动态列表端点
|
||
case tcToken = "/tencent/cos/getToken" // 新增:腾讯云 COS Token 获取端点
|
||
case publishFeed = "/dynamic/square/publish" // 发布动态
|
||
case getUserInfo = "/user/get" // 新增:获取用户信息端点
|
||
case getMyDynamic = "/dynamic/getMyDynamic"
|
||
case updateUser = "/user/v2/update" // 新增:用户信息更新端点
|
||
|
||
// Web 页面路径
|
||
case userAgreement = "/modules/rule/protocol.html"
|
||
case privacyPolicy = "/modules/rule/privacy-wap.html"
|
||
|
||
|
||
var path: String {
|
||
return self.rawValue
|
||
}
|
||
}
|
||
|
||
// MARK: - API Configuration
|
||
|
||
/// API 配置结构体
|
||
///
|
||
/// 集中管理 API 相关的配置参数,包括:
|
||
/// - 服务器地址配置
|
||
/// - 请求超时设置
|
||
/// - 数据大小限制
|
||
/// - 默认请求头设置
|
||
///
|
||
/// 配置特点:
|
||
/// - 支持不同环境的服务器地址
|
||
/// - 防止资源超限的保护机制
|
||
/// - 自动添加认证和设备信息头部
|
||
struct APIConfiguration {
|
||
static var baseURL: String { AppConfig.baseURL }
|
||
static let timeout: TimeInterval = 30.0
|
||
static let maxDataSize: Int = 50 * 1024 * 1024 // 50MB 限制,防止资源超限
|
||
|
||
/// 构建完整的 URL
|
||
/// - Parameter endpoint: API 端点
|
||
/// - Returns: 完整的 URL 字符串
|
||
static func fullURL(for endpoint: APIEndpoint) -> String {
|
||
return baseURL + endpoint.path
|
||
}
|
||
|
||
/// 构建完整的 URL 对象
|
||
/// - Parameter endpoint: API 端点
|
||
/// - Returns: URL 对象,如果构建失败返回 nil
|
||
static func url(for endpoint: APIEndpoint) -> URL? {
|
||
return URL(string: fullURL(for: endpoint))
|
||
}
|
||
|
||
/// 构建Web页面的完整 URL
|
||
/// - Parameter endpoint: API 端点
|
||
/// - Returns: 完整的Web页面 URL 字符串
|
||
static func fullWebURL(for endpoint: APIEndpoint) -> String {
|
||
return baseURL + AppConfig.webPathPrefix + endpoint.path
|
||
}
|
||
|
||
/// 构建Web页面的完整 URL 对象
|
||
/// - Parameter endpoint: API 端点
|
||
/// - Returns: Web页面 URL 对象,如果构建失败返回 nil
|
||
static func webURL(for endpoint: APIEndpoint) -> URL? {
|
||
return URL(string: fullWebURL(for: endpoint))
|
||
}
|
||
|
||
/// 默认请求头配置
|
||
///
|
||
/// 返回所有 API 请求都需要的基础请求头,包括:
|
||
/// - Content-Type 和 Accept 头部
|
||
/// - 压缩支持配置
|
||
/// - 语言和版本信息
|
||
/// - 用户认证信息(如果已登录)
|
||
///
|
||
/// 这些头部会自动添加到每个请求中,确保服务器能够正确处理请求
|
||
static func defaultHeaders() async -> [String: String] {
|
||
var headers = [
|
||
"Content-Type": "application/json",
|
||
"Accept": "application/json",
|
||
"Accept-Encoding": "gzip, br",
|
||
"Accept-Language": Locale.current.language.languageCode?.identifier ?? "en",
|
||
"App-Version": Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "1.0.0",
|
||
"User-Agent": "YuMi/20.20.61 (iPhone; iOS 16.4; Scale/2.00)"
|
||
]
|
||
// 检查用户认证状态并添加相关 headers
|
||
let authStatus = await UserInfoManager.checkAuthenticationStatus()
|
||
if authStatus.canAutoLogin {
|
||
// 添加用户认证相关 headers(仅在 AccountModel 有效时)
|
||
if let userId = await UserInfoManager.getCurrentUserId() {
|
||
headers["pub_uid"] = userId
|
||
debugInfoSync("🔐 添加认证 header: pub_uid = \(userId)")
|
||
}
|
||
if let userTicket = await UserInfoManager.getCurrentUserTicket() {
|
||
headers["pub_ticket"] = userTicket
|
||
debugInfoSync("🔐 添加认证 header: pub_ticket = \(userTicket.prefix(20))...")
|
||
}
|
||
} else {
|
||
debugInfoSync("🔐 跳过认证 header 添加 - 认证状态: \(authStatus.description)")
|
||
}
|
||
return headers
|
||
}
|
||
}
|
||
|
||
// MARK: - Request Models
|
||
struct LoginRequest: Codable {
|
||
let username: String
|
||
let password: String
|
||
}
|
||
|
||
// MARK: - Empty Request
|
||
struct EmptyRequest: Codable {}
|