|
@@ -8,14 +8,16 @@
|
8
|
8
|
import Foundation
|
9
|
9
|
import Moya
|
10
|
10
|
import Alamofire
|
|
11
|
+import HandyJSON
|
|
12
|
+import RxSwift
|
11
|
13
|
import SwiftyJSON
|
12
|
14
|
|
13
|
15
|
/// 超时时长
|
14
|
16
|
private var requestTimeOut:Double = 30
|
15
|
17
|
///endpointClosure
|
16
|
|
-private let myEndpointClosure = { (target: AccountService) -> Endpoint in
|
17
|
|
-///这里的endpointClosure和网上其他实现有些不太一样。
|
18
|
|
-///主要是为了解决URL带有?无法请求正确的链接地址的bug
|
|
18
|
+public let myEndpointClosure = { (target: TargetType) -> Endpoint in
|
|
19
|
+ ///这里的endpointClosure和网上其他实现有些不太一样。
|
|
20
|
+ ///主要是为了解决URL带有?无法请求正确的链接地址的bug
|
19
|
21
|
let url = target.baseURL.absoluteString + target.path
|
20
|
22
|
var endpoint = Endpoint(
|
21
|
23
|
url: url,
|
|
@@ -31,7 +33,7 @@ private let myEndpointClosure = { (target: AccountService) -> Endpoint in
|
31
|
33
|
}
|
32
|
34
|
}
|
33
|
35
|
|
34
|
|
-private let requestClosure = { (endpoint: Endpoint, done: MoyaProvider.RequestResultClosure) in
|
|
36
|
+public let requestClosure = { (endpoint: Endpoint, done: MoyaProvider.RequestResultClosure) in
|
35
|
37
|
do {
|
36
|
38
|
var request = try endpoint.urlRequest()
|
37
|
39
|
//设置请求时长
|
|
@@ -42,43 +44,19 @@ private let requestClosure = { (endpoint: Endpoint, done: MoyaProvider.RequestRe
|
42
|
44
|
}else{
|
43
|
45
|
log.info("\(request.url!)"+"\(String(describing: request.httpMethod))")
|
44
|
46
|
}
|
|
47
|
+ if let header = request.allHTTPHeaderFields {
|
|
48
|
+ log.info("请求头内容\(header)")
|
|
49
|
+ }
|
45
|
50
|
done(.success(request))
|
46
|
51
|
} catch {
|
47
|
52
|
done(.failure(MoyaError.underlying(error, nil)))
|
48
|
53
|
}
|
49
|
54
|
}
|
50
|
55
|
|
51
|
|
-/* 设置ssl
|
52
|
|
-let policies: [String: ServerTrustPolicy] = [
|
53
|
|
- "example.com": .pinPublicKeys(
|
54
|
|
- publicKeys: ServerTrustPolicy.publicKeysInBundle(),
|
55
|
|
- validateCertificateChain: true,
|
56
|
|
- validateHost: true
|
57
|
|
- )
|
58
|
|
-]
|
59
|
|
-*/
|
60
|
|
-
|
61
|
|
-// 用Moya默认的Manager还是Alamofire的Manager看实际需求。HTTPS就要手动实现Manager了
|
62
|
|
-//private public func defaultAlamofireManager() -> Manager {
|
63
|
|
-//
|
64
|
|
-// let configuration = URLSessionConfiguration.default
|
65
|
|
-//
|
66
|
|
-// configuration.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
|
67
|
|
-//
|
68
|
|
-// let policies: [String: ServerTrustPolicy] = [
|
69
|
|
-// "ap.grtstar.cn": .disableEvaluation
|
70
|
|
-// ]
|
71
|
|
-// let manager = Alamofire.SessionManager(configuration: configuration,serverTrustPolicyManager: ServerTrustPolicyManager(policies: policies))
|
72
|
|
-//
|
73
|
|
-// manager.startRequestsImmediately = false
|
74
|
|
-//
|
75
|
|
-// return manager
|
76
|
|
-//}
|
77
|
|
-
|
78
|
56
|
|
79
|
57
|
/// NetworkActivityPlugin插件用来监听网络请求
|
80
|
|
-private let networkPlugin = NetworkActivityPlugin.init { (changeType, targetType) in
|
81
|
|
-
|
|
58
|
+public let networkPlugin = NetworkActivityPlugin.init { (changeType, targetType) in
|
|
59
|
+
|
82
|
60
|
log.info("networkPlugin \(changeType)")
|
83
|
61
|
//targetType 是当前请求的基本信息
|
84
|
62
|
switch(changeType){
|
|
@@ -90,7 +68,104 @@ private let networkPlugin = NetworkActivityPlugin.init { (changeType, targetType
|
90
|
68
|
}
|
91
|
69
|
}
|
92
|
70
|
|
|
71
|
+
|
93
|
72
|
// https://github.com/Moya/Moya/blob/master/docs/Providers.md 参数使用说明
|
94
|
73
|
//stubClosure 用来延时发送网络请求
|
95
|
74
|
|
|
75
|
+final class TdwMoyaPlugin: PluginType {
|
|
76
|
+
|
|
77
|
+ public func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
|
|
78
|
+ var request = request
|
|
79
|
+ request.addValue("token值", forHTTPHeaderField: "token名字")
|
|
80
|
+ return request
|
|
81
|
+ }
|
|
82
|
+}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
|
96
|
86
|
let accountProvider = MoyaProvider<AccountService>(endpointClosure: myEndpointClosure, requestClosure: requestClosure, plugins: [networkPlugin], trackInflights: false)
|
|
87
|
+
|
|
88
|
+public class NetworkManaer{
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+ /// 使用Moya的请求封装
|
|
92
|
+ ///
|
|
93
|
+ /// - Parameters:
|
|
94
|
+ /// - target: 请求API,TargetType里的枚举值
|
|
95
|
+ /// - success: 成功的回调
|
|
96
|
+ /// - error: 连接服务器成功但是数据获取失败
|
|
97
|
+ /// - failure: 连接服务器失败
|
|
98
|
+ public class func request<T: TargetType,U: HandyJSON>(_ target: T, success: @escaping((U) -> Void), failure: ((Int?, String) ->Void)?) {
|
|
99
|
+ let provider = MoyaProvider<T>(
|
|
100
|
+ endpointClosure: myEndpointClosure,
|
|
101
|
+ requestClosure: requestClosure,
|
|
102
|
+ plugins: [networkPlugin],
|
|
103
|
+ trackInflights: false
|
|
104
|
+ // networkLoggerPlugin
|
|
105
|
+ )
|
|
106
|
+
|
|
107
|
+ provider.request(target) { result in
|
|
108
|
+ switch result {
|
|
109
|
+ case let .success(response):
|
|
110
|
+ // let json = try? response.mapString()
|
|
111
|
+ // let responseObject = try? response.mapJSON()
|
|
112
|
+ // JhLog( responseObject ?? "" );
|
|
113
|
+ do {
|
|
114
|
+ // *********** 这里可以统一处理错误码,弹出提示信息 ***********
|
|
115
|
+ let resObject = try? response.mapJSON()
|
|
116
|
+ let responseObject = JSON(resObject ?? "")
|
|
117
|
+ let code = responseObject["code"].intValue
|
|
118
|
+ let msg = String(describing: responseObject["msg"])
|
|
119
|
+ switch (code) {
|
|
120
|
+ case 0 :
|
|
121
|
+ let dictionary=responseObject["obj"].dictionaryObject
|
|
122
|
+ let responseData=U.deserialize(from: dictionary)
|
|
123
|
+ log.info(responseData?.toJSONString())
|
|
124
|
+ // 数据返回正确
|
|
125
|
+ success(responseData!)
|
|
126
|
+ case 2:
|
|
127
|
+ // 请重新登录
|
|
128
|
+ failure!(code,msg)
|
|
129
|
+ alertLogin(msg)
|
|
130
|
+ default:
|
|
131
|
+ // 其他错误
|
|
132
|
+ failureHandle(failure: failure, stateCode: code, message: msg)
|
|
133
|
+ }
|
|
134
|
+ }
|
|
135
|
+ case let .failure(error):
|
|
136
|
+ let statusCode = error.response?.statusCode ?? 1000
|
|
137
|
+ let message = "请求出错,错误码:" + String(statusCode)
|
|
138
|
+ log.error(message)
|
|
139
|
+ failureHandle(failure: failure, stateCode: statusCode, message: error.errorDescription ?? message)
|
|
140
|
+ }
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+ // 错误处理 - 弹出错误信息
|
|
144
|
+ func failureHandle(failure: ((Int?, String) ->Void)? , stateCode: Int?, message: String) {
|
|
145
|
+// Alert.show(type: .error, text: message)
|
|
146
|
+ //弹出错误信息
|
|
147
|
+ failure?(stateCode ,message)
|
|
148
|
+ }
|
|
149
|
+
|
|
150
|
+ // 登录弹窗 - 弹出是否需要登录的窗口
|
|
151
|
+ func alertLogin(_ title: String?) {
|
|
152
|
+ // TODO: 跳转到登录页的操作:
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+ // MARK: - 打印日志
|
|
158
|
+ // static let networkLoggerPlugin = NetworkLoggerPlugin(verbose: true, cURL: true, requestDataFormatter: { data -> String in
|
|
159
|
+ // return String(data: data, encoding: .utf8) ?? ""
|
|
160
|
+ // }) { data -> (Data) in
|
|
161
|
+ // do {
|
|
162
|
+ // let dataAsJSON = try JSONSerialization.jsonObject(with: data)
|
|
163
|
+ // let prettyData = try JSONSerialization.data(withJSONObject: dataAsJSON, options: .prettyPrinted)
|
|
164
|
+ // return prettyData
|
|
165
|
+ // } catch {
|
|
166
|
+ // return data
|
|
167
|
+ // }
|
|
168
|
+ // }
|
|
169
|
+}
|
|
170
|
+
|
|
171
|
+
|