From 2ebbafa4b4a2994ae979fed38e1266d9efd5657e Mon Sep 17 00:00:00 2001 From: younger_times <841720330@qq.com> Date: 星期二, 15 八月 2023 16:37:32 +0800 Subject: [PATCH] 优化 --- BrokerDriver/Network/NetworkRequest.swift | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 48 insertions(+), 4 deletions(-) diff --git a/BrokerDriver/Network/NetworkRequest.swift b/BrokerDriver/Network/NetworkRequest.swift index 41d5fc7..0d11c35 100644 --- a/BrokerDriver/Network/NetworkRequest.swift +++ b/BrokerDriver/Network/NetworkRequest.swift @@ -69,7 +69,7 @@ } func append(key: String,value: String?) -> ParamsAppender { -// if value?.isEmpty ?? true || value == "" { return self } + if value?.isEmpty ?? true || value == "" { return self } params += ["\(key)":"\(value ?? "")"] return self } @@ -109,7 +109,7 @@ func done() -> Parameters { return self.params } - + class func build(url: String) -> ParamsAppender { return ParamsAppender(url: url) } @@ -174,7 +174,7 @@ newEncoding = method == .post ? URLEncoding.httpBody : URLEncoding.queryString } - sharedSessionManager.request(params.url.absoluteString, method: method, parameters:params.done(), encoding: newEncoding, headers:headers).validate().responseData{response in + sharedSessionManager.request(params.url.absoluteString, method: method, parameters:params.done(), encoding: newEncoding, headers:headers).validate().responseData{response in LogInfo("请求地址:\(params.url)") LogInfo("请求参数:\(params.params)") hud?.hide(animated: true) @@ -209,7 +209,51 @@ return Disposables.create{} } } - + + class func requestMap<T: HandyJSON>(params: ParamsAppender, method: HTTPMethod, encoding: ParameterEncoding? = nil, progress: Bool = true) -> Observable<T>{ + return Observable<T>.create{ ob in + var hud: MBProgressHUD? + if progress { + hud = MBProgressHUD.showAdded(to: screnDelegate!.window!, animated: true) + hud?.bezelView.style = .solidColor + hud?.contentColor = .white + hud?.bezelView.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.8) + hud?.bezelView.color = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.8) + hud?.removeFromSuperViewOnHide = true + } + var newEncoding: ParameterEncoding + if encoding != nil { + newEncoding = encoding! + } else { + newEncoding = method == .post ? URLEncoding.httpBody : URLEncoding.queryString + } + + sharedSessionManager.request(params.url.absoluteString, method: method, parameters:params.done(), encoding: newEncoding).validate().responseData{response in + LogInfo("请求地址:\(params.url)") + LogInfo("请求参数:\(params.params)") + hud?.hide(animated: true) + if response.error != nil { + LogError("\(response.error!)") + alert(msg: response.error!.localizedDescription) + ob.onError(response.error!) + }else if let data = response.data,let jsonString = String(data: data, encoding: String.Encoding.utf8){ + if let next = T.deserialize(from: jsonString){ + LogInfo("返回数据:\(next.toJSON())") + ob.onNext(next) + }else{ + alert(msg: "数据解析失败") + ob.onError(NetRequestError.Other) + } + }else{ + alert(msg: "解析失败") + ob.onError(NetRequestError.Other) + } + ob.onCompleted() + } + return Disposables.create{} + } + } + class func requestData(params: ParamsAppender, method: HTTPMethod, encoding: ParameterEncoding? = nil, progress: Bool = true) -> Observable<Data>{ return Observable<Data>.create{ ob in var hud: MBProgressHUD? -- Gitblit v1.7.1