2022-01-04 22:28:18 +08:00
|
|
|
|
//
|
|
|
|
|
// HttpRequest.swift
|
|
|
|
|
// PushDeer
|
|
|
|
|
//
|
|
|
|
|
// Created by HEXT on 2022/1/4.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
import Moya
|
|
|
|
|
|
2022-01-10 00:26:29 +08:00
|
|
|
|
@MainActor
|
2022-01-04 22:28:18 +08:00
|
|
|
|
struct HttpRequest {
|
|
|
|
|
|
|
|
|
|
static let provider = MoyaProvider<PushDeerApi>(callbackQueue: DispatchQueue.main)
|
|
|
|
|
|
|
|
|
|
/// 统一处理接口请求, 并且封装成 Swift Concurrency 模式 (async / await)
|
|
|
|
|
static func request<T: Codable>(_ targetType: PushDeerApi, resultType: T.Type) async throws -> T {
|
|
|
|
|
return try await withCheckedThrowingContinuation { continuation in
|
|
|
|
|
provider.request(targetType) { result in
|
|
|
|
|
switch result {
|
|
|
|
|
case let .success(response):
|
|
|
|
|
do {
|
2022-02-27 23:02:58 +08:00
|
|
|
|
print(response)
|
|
|
|
|
if response.statusCode != 200 {
|
|
|
|
|
continuation.resume(throwing: NSError(domain: NSLocalizedString("接口报错", comment: "接口报错时提示"), code: response.statusCode, userInfo: [NSLocalizedDescriptionKey: NSLocalizedString("接口报错", comment: "接口报错时提示") + "(\(response.statusCode)"]))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
print((try? response.mapJSON()) ?? "返回值解析失败")
|
2022-01-04 22:28:18 +08:00
|
|
|
|
let result = try JSONDecoder().decode(ApiResult<T>.self, from: response.data)
|
|
|
|
|
print(result)
|
|
|
|
|
if let content = result.content, result.code == 0 {
|
|
|
|
|
continuation.resume(returning: content)
|
2022-01-10 00:26:29 +08:00
|
|
|
|
} else if result.code == 80403 {
|
|
|
|
|
AppState.shared.token = ""
|
2022-01-30 01:02:44 +08:00
|
|
|
|
continuation.resume(throwing: NSError(domain: result.error ?? NSLocalizedString("登录过期", comment: "token失效时提示"), code: result.code, userInfo: [NSLocalizedDescriptionKey: result.error ?? NSLocalizedString("登录过期", comment: "token失效时提示")]))
|
2022-01-04 22:28:18 +08:00
|
|
|
|
} else {
|
2022-01-30 01:02:44 +08:00
|
|
|
|
continuation.resume(throwing: NSError(domain: result.error ?? NSLocalizedString("接口报错", comment: "接口报错时提示"), code: result.code, userInfo: [NSLocalizedDescriptionKey: result.error ?? NSLocalizedString("接口报错", comment: "接口报错时提示")]))
|
2022-01-04 22:28:18 +08:00
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
print(error)
|
|
|
|
|
continuation.resume(throwing: error)
|
|
|
|
|
}
|
|
|
|
|
case let .failure(error):
|
|
|
|
|
print(error)
|
|
|
|
|
continuation.resume(throwing: error)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func fake() async throws -> TokenContent {
|
|
|
|
|
return try await request(.fake, resultType: TokenContent.self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func login(idToken: String) async throws -> TokenContent {
|
|
|
|
|
return try await request(.login(idToken: idToken), resultType: TokenContent.self)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 23:02:58 +08:00
|
|
|
|
static func wechatLogin(code: String) async throws -> TokenContent {
|
|
|
|
|
return try await request(.wechatLogin(code: code), resultType: TokenContent.self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 合并用户并将旧用户删除
|
|
|
|
|
/// | 参数 | 说明 |
|
|
|
|
|
/// | - | - |
|
|
|
|
|
/// | type | 字符串,必须为 apple 或者 wechat |
|
|
|
|
|
/// | tokenorcode | type 为 apple时此字段为 idToken,否则为 微信code |
|
|
|
|
|
static func mergeUser(type: String, tokenorcode: String) async throws -> ResultContent {
|
|
|
|
|
return try await request(.mergeUser(token: AppState.shared.token, type: type, tokenorcode: tokenorcode), resultType: ResultContent.self)
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-10 00:26:29 +08:00
|
|
|
|
static func getUserInfo() async throws -> UserInfoContent {
|
|
|
|
|
return try await request(.getUserInfo(token: AppState.shared.token), resultType: UserInfoContent.self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func regDevice() async throws -> DeviceContent {
|
|
|
|
|
return try await request(.regDevice(
|
|
|
|
|
token: AppState.shared.token,
|
|
|
|
|
name: UIDevice.current.name,
|
|
|
|
|
device_id: AppState.shared.deviceToken,
|
|
|
|
|
is_clip: AppState.shared.isAppClip ? 1 : 0
|
|
|
|
|
), resultType: DeviceContent.self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func rmDevice(id: Int) async throws -> ActionContent {
|
|
|
|
|
return try await request(.rmDevice(token: AppState.shared.token, id: id), resultType: ActionContent.self)
|
|
|
|
|
}
|
2022-01-15 22:13:23 +08:00
|
|
|
|
static func renameDevice(id: Int, name: String) async throws -> ActionContent {
|
|
|
|
|
return try await request(.renameDevice(token: AppState.shared.token, id: id, name: name), resultType: ActionContent.self)
|
|
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
|
static func getDevices() async throws -> DeviceContent {
|
|
|
|
|
return try await request(.getDevices(token: AppState.shared.token), resultType: DeviceContent.self)
|
|
|
|
|
}
|
|
|
|
|
static func loadDevices() {
|
2022-01-04 22:28:18 +08:00
|
|
|
|
_Concurrency.Task {
|
2022-01-10 00:26:29 +08:00
|
|
|
|
let result = try await getDevices()
|
2022-01-04 22:28:18 +08:00
|
|
|
|
AppState.shared.devices = result.devices
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-01-10 00:26:29 +08:00
|
|
|
|
|
|
|
|
|
static func genKey() async throws -> KeyContent {
|
|
|
|
|
return try await request(.genKey(token: AppState.shared.token), resultType: KeyContent.self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func regenKey(id: Int) async throws -> ActionContent {
|
|
|
|
|
return try await request(.regenKey(token: AppState.shared.token, id: id), resultType: ActionContent.self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func renameKey(id: Int, name: String) async throws -> ActionContent {
|
|
|
|
|
return try await request(.renameKey(token: AppState.shared.token, id: id, name: name), resultType: ActionContent.self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func rmKey(id: Int) async throws -> ActionContent {
|
|
|
|
|
return try await request(.rmKey(token: AppState.shared.token, id: id), resultType: ActionContent.self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func getKeys() async throws -> KeyContent {
|
|
|
|
|
return try await request(.getKeys(token: AppState.shared.token), resultType: KeyContent.self)
|
|
|
|
|
}
|
|
|
|
|
static func loadKeys() {
|
|
|
|
|
_Concurrency.Task {
|
|
|
|
|
let result = try await getKeys()
|
|
|
|
|
AppState.shared.keys = result.keys
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-27 23:02:58 +08:00
|
|
|
|
static func push(pushkey: String, text: String, desp: String, type: String) async throws -> ResultContent {
|
|
|
|
|
return try await request(.push(pushkey: pushkey, text: text, desp: desp, type: type), resultType: ResultContent.self)
|
2022-01-10 00:26:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func getMessages() async throws -> MessageContent {
|
|
|
|
|
return try await request(.getMessages(token: AppState.shared.token, limit: 100), resultType: MessageContent.self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func rmMessage(id: Int) async throws -> ActionContent {
|
|
|
|
|
return try await request(.rmMessage(token: AppState.shared.token, id: id), resultType: ActionContent.self)
|
|
|
|
|
}
|
2022-04-19 00:35:56 +08:00
|
|
|
|
|
|
|
|
|
static func rmAllMessage() async throws -> ActionContent {
|
|
|
|
|
return try await request(.rmAllMessage(token: AppState.shared.token), resultType: ActionContent.self)
|
|
|
|
|
}
|
2022-01-04 22:28:18 +08:00
|
|
|
|
}
|