问题 :封装方法时编码统一写死了 写成 ->JSONEncoding.default (post请求用),get请求应该是 :URLEncoding.default。
贴一下代码
NetworksSessionManager.shared.request(
requestUrl,
method: method,
parameters: parameters,
encoding: method == .get ? URLEncoding.default : JSONEncoding.default,
headers:headers)
.validate({ (request, response, data) -> Request.ValidationResult in
return .success
})
.responseJSON { (response) in
switch response.result {
case .success(let data):
let code = (data as! [String:Any])["code"] as! Int
let message = (data as! [String:Any])["message"] as! String
if code == 2000 {
completion(data)
} else {
failure(code,message)
}
ErrorHandling.shared.systemError(code: code, msg: message)
GlobalTools.Dprint("请求数据成功\(data)")
case .failure(let error):
if response.response != nil {
failure(response.response!.statusCode,error.localizedDescription)
GlobalTools.Dprint("处理错误\(error)")
ErrorHandling.shared.defaultError(
code: response.response!.statusCode,
msg: error.localizedDescription)
}
}
}
有好的封装结构,请大家不吝赐教!
网友评论