最近在重写之前的一个项目,使用swift3.0的环境,网络请求的框架使用Alamofire,所以对Alamofire进行了一个简单的分装,可以支持rxswift,模仿moya
由于我们的项目接口,大量的接口都是传递的xml文本的参数,所以有一个直接传递参数问xml文本的方法,不需要的话,可以直接删除掉
-
targetType协议
//接口相关配置
public protocol TargetType {
// 接口地址,不包括域名
var url: String {get}
// 接口参数--xml文本参数
var xml: String {get}
// 接口参数--字典参数
var param: [String:Any] {get}
var method: Method {get}
// 是否显示hud提示框
var hud: Bool {get}
}
// MARK: - 接口的扩展,默认post,显示hud
extension TargetType
public var method: Method {
return Method.post
}
public var hud: Bool {
return true
}
}
-
RequestClient协议
/// 域名
var host: String { get }
/// rx---Almofire封装--传递xml字符串
///
/// - Parameters:
/// - r: 接口需要的相关信息
/// - Success: 成功的回调
/// - Failure: 失败的回调
func rx_RequestXML(_ r: T) -> Observable
/// rx---Almofire封装--传递json
///
/// - Parameters:
/// - r: 接口需要的相关信息
/// - Success: 成功的回调
/// - Failure: 失败的回调
func rx_RequestParam(_ r: T) -> Observable
/// Almofire封装--传递xml字符串
///
/// - Parameters:
/// - r: 接口需要的相关信息
/// - Success: 成功的回调
/// - Failure: 失败的回调
func requestXML(_ r: T,
Success: @escaping(_ response: Any) -> (),
Failure: @escaping(_ error: Error) -> ())
/// Almofire封装--传递json
///
/// - Parameters:
/// - r: 接口需要的相关信息
/// - Success: 成功的回调
/// - Failure: 失败的回调
func requestParam(_ r: T,
Success: @escaping(_ response: Any) -> (),
Failure: @escaping(_ error: Error) -> ())
-
ALTools 实现RequestClient协议,这里给出rx_xml参数和字典参数的方法,方法里面的jsonformat是自己根据项目需要封装的一个格式化json格式的方法
// Almofire封装类
public struct ALTools: RequestClient {
var host: String {
return SERVER
}
/// rx---Almofire封装--传递json
/// - Parameter r: 接口需要的相关信息
/// - Returns: 二进制data
internal func rx_RequestParam(_ r: T) -> Observable {
....
}
/// rx---Almofire封装方法---传递xml字符串
/// - Parameter r: 接口需要的相关信息
/// - Returns: 二进制data
internal func rx_RequestXML(_ r: T) -> Observable {
if r.hud {
SVProgressHUD.show()
}
return Observable.create({ (observer) -> Disposable in
var request = URLRequest(url: URL(string: "\(self.host)\(r.url)")!)
request.httpMethod = r.method.rawValue
let data = r.xml.data(using: String.Encoding.utf8)
request.httpBody = data
request.setValue("text/xml", forHTTPHeaderField: "Content-Type")
Alamofire.request(request).responseData { (response) in
if let data = response.data {
if r.hud {
SVProgressHUD.dismiss()
}
if data.jsonFormat != nil {
observer.onNext(data)
observer.onCompleted()
}else {
SVProgressHUD.showError(withStatus: "服务器获取数据失败,请稍后再试")
SVProgressHUD.dismiss(withDelay: DISMISSTIME)
observer.onCompleted()
}
}else {
SVProgressHUD.showError(withStatus: response.error?.localizedDescription)
SVProgressHUD.dismiss(withDelay: DISMISSTIME)
observer.onError(response.error!)
}
}
return Disposables.create {
}
})
}
/// Almofire封装--传递json
///
/// - Parameters:
/// - r: 接口需要的相关信息
/// - Success: 成功的回调
/// - Failure: 失败的回调
internal func requestParam(_ r: T, Success: @escaping (Any) -> (), Failure: @escaping (Error) -> ()) {
if r.hud {
SVProgressHUD.show()
}
Alamofire.request(URL(string: "\(self.host)\(r.url)")!, method: r.method, parameters: r.param).responseData {
(response) in
if let data = response.data, let result = String(data: data, encoding: .utf8) {
SLLog(message: result)
SVProgressHUD.dismiss()
Success(data)
}else {
SVProgressHUD.showError(withStatus: response.error?.localizedDescription)
Failure(response.error!)
}
}
}
/// Almofire封装--传递xml字符串
///
/// - Parameters:
/// - r: 接口需要的相关信息
/// - Success: 成功的回调
/// - Failure: 失败的回调
internal func requestXML(_ r: T, Success: @escaping (Any) -> (), Failure: @escaping (Error) -> ()) {
....
}
}
- 使用
struct loginApi: TargetType {
var url: String {
return loginASHX
}
var param: [String:Any] {
switch self {
case .login(let phone, let pwd):
return ["t":2, "tel":phone, "pwd":pwd]
}
}
var xml: String {
//一个字典转xml的方法
return XMLTool.shareInstance.getXML(withParam: param, isChannel: true)
}
}
//调用
ALTools().rx_RequestXML(loginApi)....