1. 在类的模型之中或类的结构体 里面 实现下面方法
/// 添加预约数据源模型 - rootModel class DataModelForAddNewBespeakModel: NSObject { /// 返回新增预约界面 cell数量 目前就3个 var cellCount = 3 /// 顾客模型 var customerModel = Mapper<JYCustomerListItemModel>().map(JSONObject: "默认是空的") /// 时间和发型师 的组合 var timeModelAndHairdresserModel = TimeModelAndHairdresserModel() /// 选择的项目 - 传给后台的数据 var itemsModel = ItemsModel() /// phz 传给我的原生的数据 JYBespekSelectItemModel var originalDataForItemsModel = [JYBespekSelectItemModel]() /// 新增预约需要的参数 这是关键 的一个 func paramsForNewAddBespeak() -> [String: Any] { return ["crm_customer_ID": self.customerModel?.customerId ?? "", "BookingDate" : self.timeModelAndHairdresserModel.timeModel.selectedDateModel.yearMonthDay , "StartTime" : self.timeModelAndHairdresserModel.timeModel.bookTime , "ServiceEmployee" : self.timeModelAndHairdresserModel.hairdresserModel?.employeeId ?? "", "ServiceItemGroupID" : self.itemsModel.groupId , "Remark" : "备注", "itemIds" : self.itemsModel.projectIdArr] as [String: Any] } }
2. 协议的实现
//MARK: 纯功能性协议:BespeakRequestProtocol(保证控制器拥有这个方法), 必须实现 业务处理协议:JYServiceNetworkProtocol(请求失败和请求成功的业务处理) protocol BespeakRequestProtocol{} extension BespeakRequestProtocol where Self: UIViewController { /// 新增预约请求 func requestForNewAddBespeak(params:[String:Any], type:JYServiceNetworkProtocol) { JYRequestAlamofire.request(method: JYHTTPMethod.post, url: JY_API_ADD_BESPEAK, params: params, handleSuccess: { (result) in let model = Mapper<QueryBespeakModel>().map(JSONObject: result) if model?.ret == "0"{ type.operationSuccess(data: [:], identifier: "func requestForNewAddBespeak") }else{ type.serverShowErr(err: model?.msg, identifier: "func requestForNewAddBespeak") } }) { (err) in type.serverShowErr(err: "暂无网络", identifier: "func requestForNewAddBespeak") } } }
3. 控制器调用
class JYAddNewBespeakVC: JYBaseVC {
/// 页面所有数据模型
var dataModel = DataModelForAddNewBespeakModel()
}
3.1 //点击确定按钮 @IBAction func clickSureBespeakBtn(_ sender: Any) { view.showHud() self.requestForNewAddBespeak(params: self.dataModel.paramsForNewAddBespeak(), type: self) } 3.2 实现协议 extension JYAddNewBespeakVC:BespeakRequestProtocol, JYServiceNetworkProtocol{ func serverShowErr(err: String?, identifier: String?) { view.showTipsInfo(tips: err) } func operationSuccess(data: [String : Any]?, identifier: String?) { view.hiddenHUd() "请求成功做的事情" } }
4.因为本身是controller 遵循的协议, 可以获得view的属性
/// 取消预约请求 func requestForCancleBespeak(bespeakId : String, ProtocolFollower:JYServiceNetworkProtocol){ view.showHud() let parameters = ["ID" : bespeakId] weak var weakSelf = self JYRequestAlamofire.request(method: .put, url:JY_API_CANCLE_BESPEAK , params: parameters, handleSuccess: { [weak self] (res) in }) { [weak self] (_) in self?.view.loseNetWorkDismissToast() } }