Alamofire基本使用
//
// NetworkTool.swift
// huakaMall
//
// Created by lizongyao on 2019/1/21.
// Copyright © 2019 lzy. All rights reserved.
//
import UIKit
import Alamofire
class NetworkTool {
static func request(api:String,params: [String:Any]?, successBlock:@escaping(_rep: [String:Any]) ->Void,failedBlock:@escaping(_rep: [String:Any]) ->Void) {
var paramDic = [String:Any]()
paramDic["baseDevice"] = Singleton.shareInstance.baseDevice
if let p = params {
for e in p {
paramDic[e.key] = p[e.key]
}
}
let headers: HTTPHeaders= [
"token": Singleton.shareInstance.token,
"sign": paramDic.md5()]
leturl =URL.init(string:DOMAINURL+"/api/"+APIVERSION+"/"+ api)
print(url as Any)
print(paramDic)
Alamofire.request(url!, method:HTTPMethod.post, parameters: paramDic, encoding:JSONEncoding.default, headers: headers).responseJSON{ (rep)in
varresultDic = rep.result.valueas! [String:Any]
ifresultDic["code"]as!String=="0000"{
letjson = resultDic["data"]as!String
successBlock(json.toDictionary()as! [String:Any])
}else{
failedBlock(resultDic)
}
}
}
}