import UIKit
import Alamofire
enum MethodType{
case GET
case POST
}
class NetworkTool: NSObject {
class func request(type : MethodType ,urlString : String , paramters: [String :Any]? = nil,finishedCallback : @escaping (_ result : Any) -> ()) {
//判断是什么类型的请求
let method = type == .GET ? HTTPMethod.get : HTTPMethod.post
// 发送网络请求
Alamofire.request(urlString , method: method, parameters: paramters).responseJSON { (response) in
guard let result = response.result.value else{
print(response.result.value)
return
}
// 回调
finishedCallback(result as AnyObject)
}
}
}
//---
import UIKit
class BaseVM: NSObject {
lazy var anchorGroups: [AnchorGroup] = [AnchorGroup]()
func loadAnchorGroupData(isLiveData : Bool, URLString : String, parameters : [String : Any]? = nil, finishedCallback : @escaping () -> ()) {
NetworkTool.request(type: .GET, urlString: URLString, paramters: parameters) { (result) in
guard let dict = result as? [String : Any] else { return }
if isLiveData {
guard let dictionary = dict["data"] as? [String : Any] else { return }
self.anchorGroups.append(AnchorGroup(dict: dictionary))
} else {
guard let arr = dict["data"] as? [[String : Any]] else { return }
for dict in arr {
self.anchorGroups.append(AnchorGroup(dict: dict))
}
}
finishedCallback()
}
}
// MARK: - items, total
lazy var searchGroup: [SearchModel] = [SearchModel]()
func loadSearchData(URLString : String, parameters : [String : Any]? = nil, finishedCallback : @escaping () -> ()) {
NetworkTool.request(type: .GET, urlString: URLString, paramters: parameters) { (result) in
guard let dict = result as? [String : Any] else { return }
guard let dictionary = dict["data"] as? [String : Any] else { return }
self.searchGroup.append(SearchModel(dict: dictionary))
finishedCallback()
}
}
}