|
@@ -8,8 +8,9 @@
|
|
|
import Alamofire
|
|
|
import UIKit
|
|
|
|
|
|
+private var bf_abInfoData: [String: Any] = Dictionary<String, Any>.init() // 实验数据
|
|
|
public class BFNetRequestAdaptor: NSObject {
|
|
|
- public typealias completeHander = (_ jsonObject: Any?, _ extData: [String: Any]?, _ error: Error?, _ duration: TimeInterval?) -> Void
|
|
|
+ public typealias completeHander = (_ jsonObject: Any?, _ extData: [String: Any]?, _ error: PQError?, _ duration: TimeInterval?) -> Void
|
|
|
/// get请求
|
|
|
public class func getRequestData(url: String, parames: [String: Any]?, commonParams: [String: Any]? = nil, encoding: ParameterEncoding = URLEncoding.default, timeoutInterval: TimeInterval = 60, response: @escaping completeHander) {
|
|
|
SWNetRequest.getRequestData(url: url, parames: requestParams(encoding: encoding, parames: parames, commonParams: commonParams), encoding: encoding, timeoutInterval: timeoutInterval) { jsonObject, error, duration in
|
|
@@ -65,22 +66,24 @@ public class BFNetRequestAdaptor: NSObject {
|
|
|
/// - commonParams: <#commonParams description#>
|
|
|
/// - Returns: <#description#>
|
|
|
public class func requestParams(encoding: ParameterEncoding, parames: [String: Any]?, commonParams: [String: Any]?) -> [String: Any]? {
|
|
|
- if commonParams == nil {
|
|
|
+ if parames == nil || (parames?.keys.count ?? 0) <= 0 {
|
|
|
return commonParams
|
|
|
}
|
|
|
+ if commonParams == nil || (commonParams?.keys.count ?? 0) <= 0 {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
var requestParams: [String: Any] = Dictionary<String, Any>.init()
|
|
|
+ var tempCommonParams: [String: Any] = commonParams ?? Dictionary<String, Any>.init()
|
|
|
+ var tempParames: [String: Any] = parames ?? Dictionary<String, Any>.init()
|
|
|
+ tempCommonParams["abInfoData"] = bf_dictionaryToJsonString(dic: bf_abInfoData) ?? ""
|
|
|
if encoding is JSONEncoding {
|
|
|
requestParams["baseInfo"] = commonParams
|
|
|
requestParams["params"] = parames
|
|
|
} else {
|
|
|
- requestParams = commonParams!
|
|
|
- if parames != nil, parames?.count ?? 0 > 0 {
|
|
|
- if parames?.keys.contains("abInfoData") ?? false, "\(parames?["abInfoData"] ?? "")".count > 0, "\(parames?["abInfoData"] ?? "")" != "{}" {
|
|
|
- requestParams.removeValue(forKey: "abInfoData")
|
|
|
- }
|
|
|
- for (key, value) in parames!.reversed() {
|
|
|
- requestParams[key] = value
|
|
|
- }
|
|
|
+ requestParams = tempCommonParams
|
|
|
+ tempParames.removeValue(forKey: "abInfoData")
|
|
|
+ for (key, value) in tempParames.reversed() {
|
|
|
+ requestParams[key] = value
|
|
|
}
|
|
|
}
|
|
|
return requestParams
|
|
@@ -92,7 +95,9 @@ public class BFNetRequestAdaptor: NSObject {
|
|
|
public class func paraseJsonObject(respondDict: [String: Any]) -> (Any?, [String: Any]?, error: PQError?) {
|
|
|
if respondDict.keys.contains("code") && "\(respondDict["code"] ?? "")" == "0" && respondDict.keys.contains("data") {
|
|
|
let extData = respondDict["extData"] as? [String: Any]
|
|
|
- if extData != nil, extData?.keys.contains("abInfoData") ?? false {}
|
|
|
+ if extData != nil, extData?.keys.contains("abInfoData") ?? false {
|
|
|
+ bf_parasABinfoData(abInfo: extData?["abInfoData"] as? String)
|
|
|
+ }
|
|
|
return (respondDict["data"], extData, nil)
|
|
|
} else if (respondDict.keys.contains("msg") && "\(respondDict["msg"] ?? "")".count > 0) || (respondDict.keys.contains("message") && "\(respondDict["message"] ?? "")".count > 0) {
|
|
|
return (respondDict["data"], nil, PQError(msg: "\(respondDict["msg"] ?? "")", code: Int("\(respondDict["code"] ?? "")") ?? 10001))
|
|
@@ -100,4 +105,38 @@ public class BFNetRequestAdaptor: NSObject {
|
|
|
return (nil, nil, PQError(msg: "\(respondDict["msg"] ?? "")", code: Int("\(respondDict["code"] ?? "")") ?? 10001))
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ /// 解析abInfoData
|
|
|
+ /// - Parameter abInfo: <#abInfo description#>
|
|
|
+ /// - Returns: description
|
|
|
+ private static func bf_parasABinfoData(abInfo: String?) {
|
|
|
+ if abInfo != nil, (abInfo?.count ?? 0) > 0, abInfo != "{}" {
|
|
|
+ guard let infoDic = bf_jsonStringToDictionary(abInfo!) else { return }
|
|
|
+ bf_abInfoData.merge(infoDic, uniquingKeysWith: { (key, _) -> Any in
|
|
|
+ key
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static func bf_jsonStringToDictionary(_ str: String) -> [String: Any]? {
|
|
|
+ let data = str.data(using: String.Encoding.utf8)
|
|
|
+ if data == nil {
|
|
|
+ return [:]
|
|
|
+ }
|
|
|
+ if let dict = try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? [String: Any] {
|
|
|
+ return dict
|
|
|
+ }
|
|
|
+ return [:]
|
|
|
+ }
|
|
|
+
|
|
|
+ private static func bf_dictionaryToJsonString(dic: [String: Any]) -> String? {
|
|
|
+ if !JSONSerialization.isValidJSONObject(dic) {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ guard let data = try? JSONSerialization.data(withJSONObject: dic, options: []) else {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
+ let str = String(data: data, encoding: String.Encoding.utf8)
|
|
|
+ return str
|
|
|
+ }
|
|
|
}
|