You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
5.4 KiB
150 lines
5.4 KiB
1 year ago
|
//
|
||
|
// HPNetworkProvider.swift
|
||
|
// HPlusFit
|
||
|
//
|
||
|
// Created by lemo. on 2019/8/31.
|
||
|
// Copyright © 2019 lemo. All rights reserved.
|
||
|
//
|
||
|
|
||
|
import Foundation
|
||
|
import Moya
|
||
|
|
||
|
/// 超时时长
|
||
|
private var requestTimeOut: Double = 30
|
||
|
|
||
|
typealias successBlock = (JSON) -> ()
|
||
|
typealias failureBlock = (MoyaError) -> ()
|
||
|
|
||
|
/// 设置请求头
|
||
|
private let myEndpointClosure = { (target: APIManager) -> Endpoint in
|
||
|
var url = target.baseURL.absoluteString + target.path
|
||
|
switch target {
|
||
|
case .downLoad:
|
||
|
url = target.path
|
||
|
default: break
|
||
|
}
|
||
|
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
|
||
|
if let tempUrl = NSURL(string: url), let ip = appDelegate.httpdns?.getIpByHostAsync(tempUrl.host) {
|
||
|
// 通过HTTPDNS获取IP成功,进行URL替换和HOST头设置
|
||
|
url = url.replacingOccurrences(of: tempUrl.host ?? "", with: ip)
|
||
|
}
|
||
|
}
|
||
|
let endPoint = Endpoint(url: url,
|
||
|
sampleResponseClosure: { .networkResponse(200, target.sampleData) },
|
||
|
method: target.method,
|
||
|
task: target.task,
|
||
|
httpHeaderFields: target.headers)
|
||
|
return endPoint
|
||
|
}
|
||
|
|
||
|
/// 设置超时时常等
|
||
|
private let myRequestClosure = { (endpoint: Endpoint, closure: @escaping MoyaProvider.RequestResultClosure) -> Void in
|
||
|
do {
|
||
|
var request = try endpoint.urlRequest()
|
||
|
// 设置超时时长
|
||
|
request.timeoutInterval = requestTimeOut
|
||
|
// 打印请求的参数
|
||
|
if let requestData = request.httpBody {
|
||
|
LFLogs("[网络通信] 请求地址: \(request.url!)" + "\n" + "请求类型: \(request.httpMethod ?? "")" + "\n" + "请求参数: \(String(data: request.httpBody!, encoding: String.Encoding.utf8) ?? "")")
|
||
|
}else{
|
||
|
LFLogs("[网络通信] 请求地址: \(request.url!)" + "\n" + "请求参数: \(String(describing: request.httpMethod))")
|
||
|
}
|
||
|
closure(.success(request))
|
||
|
} catch {
|
||
|
closure(.failure(MoyaError.underlying(error, nil)))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// NetworkActivityPlugin 监听网络开始结束
|
||
|
let myNetworkActivityPlugin = NetworkActivityPlugin { (changeType, HPAPIManager) in
|
||
|
// targetType 是当前请求的基本信息
|
||
|
switch(changeType){
|
||
|
case .began:
|
||
|
LFLogs("[网络通信] 开始请求")
|
||
|
case .ended:
|
||
|
LFLogs("[网络通信] 结束请求")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/// NetworkLoggerPlugin 监听所有网络活动日志
|
||
|
let myNetworkLoggerPlugin = NetworkLoggerPlugin(verbose: true, cURL: true, output: { (separator: String, terminator: String, items: Any...) in
|
||
|
|
||
|
}, requestDataFormatter: { (requestData: Data) -> (String) in
|
||
|
return ""
|
||
|
}, responseDataFormatter: { (responseData: Data) -> (Data) in
|
||
|
return Data()
|
||
|
})
|
||
|
|
||
|
|
||
|
/// MoyaProvider请求类
|
||
|
private let provider = MoyaProvider<APIManager>(endpointClosure: myEndpointClosure, requestClosure: myRequestClosure, plugins: [myNetworkActivityPlugin, myNetworkLoggerPlugin])
|
||
|
|
||
|
private let disposeBag = DisposeBag()
|
||
|
|
||
|
/// 网络请求
|
||
|
///
|
||
|
/// - Parameter target: 请求接口类型
|
||
|
/// - Returns: 返回JSON序列
|
||
|
func ProviderRequest(_ target: APIManager) -> Observable<JSON> {
|
||
|
return Observable.create({ (observer) -> Disposable in
|
||
|
provider.rx.request(target)
|
||
|
.filterSuccessfulStatusCodes()
|
||
|
.mapJSON()
|
||
|
.subscribe(onSuccess: { (response) in
|
||
|
let json = JSON(response)
|
||
|
LFLogs("[网络通信] 回复 \(String(describing: target.path)) \(json)")
|
||
|
let code = json["code"].intValue
|
||
|
if code == 1 {
|
||
|
observer.onNext(json)
|
||
|
}else {
|
||
|
SVProgressHUD.showError(withStatus: MultiLanguageKey.networkError.localized)
|
||
|
}
|
||
|
}, onError: { (error) in
|
||
|
SVProgressHUD.showError(withStatus: MultiLanguageKey.networkError.localized)
|
||
|
observer.onError(MoyaError.underlying(error, nil))
|
||
|
})
|
||
|
.disposed(by: disposeBag)
|
||
|
return Disposables.create()
|
||
|
})
|
||
|
}
|
||
|
|
||
|
/// 网络请求下载
|
||
|
///
|
||
|
/// - Parameter target: 请求接口类型
|
||
|
/// - Returns: 返回JSON序列
|
||
|
func ProviderRequestDownload(_ target: APIManager) -> Observable<Data> {
|
||
|
return Observable.create({ (observer) -> Disposable in
|
||
|
provider.rx.request(target)
|
||
|
.filterSuccessfulStatusCodes()
|
||
|
.subscribe(onSuccess: { (response) in
|
||
|
observer.onNext(response.data)
|
||
|
}, onError: { (error) in
|
||
|
SVProgressHUD.showError(withStatus: "网络连接异常,请稍后重试".localized)
|
||
|
observer.onError(MoyaError.underlying(error, nil))
|
||
|
})
|
||
|
.disposed(by: disposeBag)
|
||
|
return Disposables.create()
|
||
|
})
|
||
|
}
|
||
|
|
||
|
|
||
|
/// 网络请求下载
|
||
|
///
|
||
|
/// - Parameter target: 请求接口类型
|
||
|
/// - Returns: 返回JSON序列
|
||
|
func ProviderRequestDownloadWithProgress(_ target: APIManager) -> Observable<ProgressResponse> {
|
||
|
return Observable.create({ (observer) -> Disposable in
|
||
|
provider.rx.requestWithProgress(target)
|
||
|
.subscribe(onNext: { (response) in
|
||
|
observer.onNext(response)
|
||
|
}, onError: { (error) in
|
||
|
SVProgressHUD.showError(withStatus: "网络连接异常,请稍后重试".localized)
|
||
|
observer.onError(MoyaError.underlying(error, nil))
|
||
|
}, onCompleted: {
|
||
|
observer.onCompleted()
|
||
|
})
|
||
|
.disposed(by: disposeBag)
|
||
|
return Disposables.create()
|
||
|
})
|
||
|
}
|