135 lines
4.0 KiB
Swift
135 lines
4.0 KiB
Swift
//
|
||
// APIProvider.swift
|
||
// HealthyZG
|
||
//
|
||
// Created by 林 on 2020/5/18.
|
||
// Copyright © 2020 Lin. All rights reserved.
|
||
//
|
||
|
||
import Foundation
|
||
import Moya
|
||
import RxSwift
|
||
|
||
// swiftlint:disable:next identifier_name
|
||
public let GatewayStatusCodeKey = "GatewayStatusCodeKey"
|
||
|
||
typealias Parameters = [String: Any]
|
||
|
||
private func JSONResponseDataFormatter(_ data: Data) -> String {
|
||
do {
|
||
let dataAsJSON = try JSONSerialization.jsonObject(with: data)
|
||
let prettyData = try JSONSerialization.data(withJSONObject: dataAsJSON, options: .prettyPrinted)
|
||
return String(data: prettyData, encoding: .utf8) ?? String(data: data, encoding: .utf8) ?? ""
|
||
} catch {
|
||
return String(data: data, encoding: .utf8) ?? ""
|
||
}
|
||
}
|
||
|
||
private func reversedPrint(_ separator: String, terminator: String, items: Any...) {
|
||
for item in items {
|
||
if let msg = item as? String {
|
||
LogInfo(msg)
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - Provider setup
|
||
let requestClosure = { (endpoint: Endpoint, done: MoyaProvider.RequestResultClosure) in
|
||
do {
|
||
var request = try endpoint.urlRequest()
|
||
// Modify the request however you like.
|
||
request.timeoutInterval = 15 //设置请求超时时间
|
||
done(.success(request))
|
||
} catch {
|
||
done(.failure(MoyaError.underlying(error, nil)))
|
||
}
|
||
}
|
||
|
||
let plugins: [PluginType] = [
|
||
SignPlugin(),
|
||
NetworkLoggerPlugin(configuration: .init(formatter: .init(responseData: JSONResponseDataFormatter), logOptions: .verbose)),
|
||
NetworkActivityPlugin(networkActivityClosure: { change, _ in
|
||
#if !SHARE_EXTENSION
|
||
DispatchQueue.main.async {
|
||
switch change {
|
||
case .began:
|
||
UIApplication.shared.isNetworkActivityIndicatorVisible = true
|
||
case .ended:
|
||
UIApplication.shared.isNetworkActivityIndicatorVisible = false
|
||
}
|
||
}
|
||
#endif
|
||
})
|
||
]
|
||
|
||
public let APIProvider = MoyaProvider<MultiTarget>(requestClosure: requestClosure,
|
||
plugins: plugins).rx
|
||
public extension Error {
|
||
var underlyingError: NSError? {
|
||
guard let moyaError = self as? MoyaError else { return nil }
|
||
guard case let .underlying(error, _) = moyaError else { return nil }
|
||
return error as NSError
|
||
}
|
||
|
||
var gatewayStatusCode: Int {
|
||
let error = underlyingError ?? self as NSError
|
||
return error.code
|
||
}
|
||
|
||
var gatewayMessage: String? {
|
||
let error = underlyingError ?? self as NSError
|
||
return error.userInfo[NSLocalizedDescriptionKey] as? String
|
||
}
|
||
|
||
var responseData: Response? {
|
||
guard let moyaError = self as? MoyaError else { return nil }
|
||
guard case let .underlying(_, response) = moyaError else { return nil }
|
||
return response
|
||
}
|
||
|
||
var isShowErrorTips: Bool {
|
||
let error = underlyingError ?? self as NSError
|
||
return error.userInfo["isShowTip"] as? Bool ?? true
|
||
}
|
||
}
|
||
|
||
public extension MoyaError {
|
||
var innerError: NSError? {
|
||
switch self {
|
||
case let .encodableMapping(error):
|
||
return error as NSError
|
||
case let .objectMapping(error, _):
|
||
return error as NSError
|
||
case let .underlying(error, _):
|
||
return error as NSError
|
||
case let .parameterEncoding(error):
|
||
return error as NSError
|
||
default:
|
||
return nil
|
||
}
|
||
}
|
||
}
|
||
|
||
// 网关状态码
|
||
enum GatewayStatusCode: Int {
|
||
// 请求成功
|
||
case success = 0
|
||
// 版本过低
|
||
case outdateVersion = 3
|
||
/** ============== 风控 ============== */
|
||
case riskControl = 701
|
||
/** ============== 用户权限相关 200~300 ============== */
|
||
// 登录失效
|
||
case userLoginExpair = 1001003
|
||
// 权限不足
|
||
case noAuthority = 500
|
||
// 审核中
|
||
case review = 201
|
||
/** ============== 业务 ============== */
|
||
// 您创建的圈子个数已达上限,请升级会员等级
|
||
case groupLimit = 20009
|
||
|
||
/** ============== 未知错误 ============== */
|
||
case unknownError = -9999
|
||
}
|