class RecommendViewModel: NSObject {
//懒加载 lazy var anthorGroups:[AnthorGroup] = [AnthorGroup]()
//热门
private lazy var hotGroup : AnthorGroup = AnthorGroup()
//颜值
private lazy var prettyGroup : AnthorGroup = AnthorGroup()
//定义参数
let prameters = ["limit":"4","offset":"0","time":NSDate.getCurrentTime()]
func requestData(finishedCallBack:@escaping () -> ()) {
let dGroup = DispatchGroup()
dGroup.enter()
AF.request("http://capi.douyucdn.cn/api/v1/getBigDataRoom", method: .get, parameters: ["time":NSDate.getCurrentTime()]).responseJSON { response in
switch response.result{
case .success(let json):
let dict = json as! Dictionary<String, Any>
let arrayData = dict["data"] as! Array<AnyObject>
self.hotGroup.tag_name = "热门"
self.hotGroup.icon_url = "home_header_hot"
//3.2 获取主播数据
if let anthorModels = [AnthorModel].deserialize(from: arrayData){
for anthorModel in anthorModels{
self.hotGroup.anthorModels.append(anthorModel!)
}
}
//3.4 离开组
dGroup.leave()
break
case .failure(let error):
print("error:\(error)")
break
}
}
//请求颜值数据
dGroup.enter()
AF.request("http://capi.douyucdn.cn/api/v1/getHotCate", method: .get, parameters: prameters).responseJSON { response in
switch response.result{
case .success(let json):
let dict = json as! Dictionary<String, Any>
let arrayData = dict["data"] as! Array<AnyObject>
//3.遍历数组,获取字典,将字典转成模型
if let groups = [AnthorGroup].deserialize(from: arrayData){
for group in groups{
let anthorModels:[AnthorModel]? = group?.room_list
for anthorModel in anthorModels!{
group?.anthorModels.append(anthorModel)
}
self.anthorGroups.append(group!)
}
}
//3.4 离开组
dGroup.leave()
break
case .failure(let error):
print("error:\(error)")
break
}
}
dGroup.notify(queue: DispatchQueue.main) {
self.anthorGroups.insert(self.prettyGroup, at: 0)
self.anthorGroups.insert(self.hotGroup, at: 0)
finishedCallBack()
}
}
}