Swift 5 Alamofire框架网络工具类封装
import UIKit
import Alamofire
import SwiftyJSON
class CLLNetWork: NSObject {
static func getNetWork(vc:UIViewController, url:String, type:HTTPMethod, params:[String:Any]?,success:@escaping(_ response :[String: JSON])->(),failure:@escaping(_ response :String)->() ){
CLLHUD.showAlway(view: vc.view)
Alamofire.AF.request(url, method: HTTPMethod.get, parameters: params).responseJSON { (dataResponse) in
CLLHUD.hide(view: vc.view)
switch dataResponse.result {
case .success:
let json = dataResponse.data?.toJSON().dictionaryValue
if json!["code"]?.intValue == 0{
success(json!)
}else{
CLLHUD.makeToast(string: json!["msg"]!.stringValue)
failure(json!["msg"]!.stringValue)
}
case let .failure(error):
CLLHUD.makeToast(string: error.localizedDescription)
failure(error.localizedDescription)
print(error)
}
}
}
}
extension Data {
func toJSON()-> JSON {
return JSON(self)
}
}
网友评论