// // HomeViewController.swift // ADHTuanCan // // Created by 敖德亨 on 2023/9/28. // import UIKit //import MJRefresh import RxSwift import RxCocoa import RxAlamofire import Alamofire import HandyJSON import SwiftDate class HomeViewController: ADHBaseVC { var shopcartId : NSNumber? /// 是否是手动刷新 var isMJLoad = false @IBOutlet var shopCar: UIView! /// 购物车价格 @IBOutlet weak var shopCarPrice: UILabel! @IBOutlet weak var shopCarBotton: NSLayoutConstraint! let dateView = Bundle.main.loadNibNamed("HomeDateView", owner: nil, options: nil)?.last as? HomeDateView let everyBodyLove = Bundle.main.loadNibNamed("EverybodyLoveView", owner: nil, options: nil)?.last as? EverybodyLoveView let reBtnView = Bundle.main.loadNibNamed("HomeRecommendView", owner: nil, options: nil)?.last as? HomeRecommendView let searchView = Bundle.main.loadNibNamed("SearchView", owner: nil, options: nil)?.last as? SearchView //查看购物车View let checkShoppingCarView = Bundle.main.loadNibNamed("ShoppingCarDetailView", owner: nil, options: nil)?.last as? ShoppingCarDetailView //业务员必填页 let SalesmanView = Bundle.main.loadNibNamed("SalesmanFirstShowView", owner: nil, options: nil)?.last as? SalesmanFirstShowView let timeRangeView = UINib.view(withType: timeManageSheetView.self) var viewList : [Any]? /// 当前选择的时间 var nowTimeModel : HomeTimeItem? private var backImageView : UIImageView! let viewModel : HomeViewModel = HomeViewModel.init() let diposableBag = DisposeBag() /// 1为普团,2为白团 let shopType = "2" var isShowUserView = true override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) UIScrollView.appearance().isPagingEnabled = false } //去结算 @IBAction func gotoShpCar(_ sender: UIButton) { let vc = CheckOrderViewController() vc.shopType = self.shopType vc.shopcartId = shopcartId vc.shopcartTimeId = self.nowTimeModel?.shopcartTimeId vc.nowTimeModel = self.nowTimeModel self.navigationController?.pushViewController(vc, animated: true) } //查看购物车 @IBAction func checkShopCar(_ sender: UIButton) { if self.nowTimeModel.isNil{ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择日期", Etext: "Please select a date")) return } self.checkShoppingCarView?.shopType = self.shopType self.checkShoppingCarView?.shopcartId = self.shopcartId self.checkShoppingCarView?.shopcartTimeId = self.nowTimeModel?.shopcartTimeId self.checkShoppingCarView?.nowTimeModel = self.nowTimeModel self.checkShoppingCarView?.show() } override func viewDidLoad() { super.viewDidLoad() _ = NotificationCenter.default.rx.notification(Notification.Name(rawValue: "刷新首页")).takeUntil(self.rx.deallocated).subscribe(onNext:{[weak self] (notification) in self?.isMJLoad = false self?.refreshData() }) self.scrollView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[unowned self] in self.isMJLoad = true self.GetMealWithSupplyType(true) }); self.scrollView.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {[unowned self] in self.isMJLoad = true self.GetMealWithSupplyType(false) }); self.scrollView.ly_emptyView = LYEmptyView.emptyActionView(with: UIImage.init(named: "暂无数据"), titleStr: "暂无数据", detailStr: "", btnTitleStr: "点击刷新", btnClick: {[weak self] in self?.isMJLoad = true self?.GetMealWithSupplyType(true) }) self.view.backgroundColor = kTBackgroundColor self.shopCar.isHidden = true shopCarBotton.constant = 0 shopCar.bringSubviewToFront(self.scrollView) self.view.addSubview(self.scrollView) self.scrollView.mas_remakeConstraints { make in make?.top.left().right().bottom().mas_equalTo()(self.view) make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight) } // self.createUI() //获取餐品类型 self.getHomeFoodTypes() // //获取时间段范围 // self.getTimeRangDetail() } func refreshData(){ self.getTimeRangDetail() } //MARK: createUI /// <#Description#> func createUI(){ self.backImageView = UIImageView.init(image: UIImage.init(named: "首页背景")!) self.backImageView.isUserInteractionEnabled = true self.backImageView.addSubview(self.searchView!) self.searchView!.mas_remakeConstraints { make in make?.left.mas_equalTo()(self.backImageView)?.offset()(24) make?.right.mas_equalTo()(self.backImageView)?.offset()(-24) make?.top.mas_equalTo()(self.backImageView)?.offset()(self.backImageView.frame.size.height - 100) make?.height.offset()(48) } /// self.viewList = [self.backImageView!,self.dateView! ,self.everyBodyLove!,self.reBtnView!,self.collectionView] self.scrollView.addSubview(self.backImageView!) self.scrollView.addSubview(self.dateView!) self.scrollView.addSubview(self.everyBodyLove!) self.scrollView.addSubview(self.reBtnView!) self.scrollView.addSubview(self.collectionView) // self.timeRangeView?.selectCheckDateBlock = {[unowned self] in // // } self.timeRangeView?.selectCheckDateBlock = {[weak self] startTime , endTime , days in self?.viewModel.homeEditorTimeRangModel.endDate = endTime self?.viewModel.homeEditorTimeRangModel.startDate = startTime self?.refreshTimeRang() } //选择时间段 self.dateView?.timeRangeAction = {[weak self] in let monthFirstDate = NSDate.now let days : Int = monthFirstDate.day let startDate = monthFirstDate - days.days + 1.days self?.timeRangeView?.show(withStaStr: self?.viewModel.TimeRangDetail?.startDate ?? "", endStr: self?.viewModel.TimeRangDetail?.endDate ?? "",begin: startDate) } self.dateView?.selectBlock = {[weak self] model in self?.nowTimeModel = model self?.viewModel.SupplyTypeRequsetModel.shopcartTime = self?.nowTimeModel!.shopcartTime self?.isMJLoad = true self?.GetMealWithSupplyType(true) self?.reloadDateView() } //早中晚选择 self.everyBodyLove?.supplyTypeAction = {[weak self] type in self?.viewModel.SupplyTypeRequsetModel.supplyTypeId = type self?.isMJLoad = true self?.GetMealWithSupplyType(true) } //所有人都爱点击 self.everyBodyLove?.touchUpActionBlock = {[unowned self] tag in if self.nowTimeModel.isNil{ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择日期", Etext: "Please select a date")) return } let model : HomeFoodDetailModel = (self.viewModel.EveryBodyLoveList![tag]) self.checkFoodDetail(model: model) } //下方套餐选择 self.reBtnView?.runTypeAction = {[weak self] type in self?.viewModel.SupplyTypeRequsetModel.runTypeId = type self?.isMJLoad = true self?.GetMealWithSupplyType(true) } self.checkShoppingCarView?.reFreshBlock = {[weak self] in self?.isMJLoad = true self?.getTimeRangDetail() } self.checkShoppingCarView?.checkOrderBlock = {[weak self] in let vc = CheckOrderViewController() vc.shopType = self?.shopType vc.shopcartId = self?.viewModel.TimeRangDetail?.shopcartId vc.shopcartTimeId = self?.checkShoppingCarView?.nowTimeModel?.shopcartTimeId vc.nowTimeModel = self?.checkShoppingCarView?.nowTimeModel self?.navigationController?.pushViewController(vc, animated: true) } self.resetConstraint() } //MARK: 刷新加载部分 @objc func headerRefresh(){ print("下拉刷新.") self.GetMealWithSupplyType(true) } //MARK: 获取餐品类型 func getHomeFoodTypes(){ self.viewModel.GetRunAndSupply.mealType = self.shopType self.hud.showLoading() self.viewModel.signalGetHomeFoodType()?.on(success: { dd in if self.viewModel.SupplyTypeList!.runTypeList!.count > 0{ //下部套餐按钮渲染 self.reBtnView?.configTitle(model: self.viewModel.SupplyTypeList!) //早中晚默认给到第一个类型 self.viewModel.SupplyTypeRequsetModel.runTypeId = self.viewModel.SupplyTypeList!.runTypeList![0].id } if self.viewModel.SupplyTypeList!.supplyTypeList!.count > 0{ //早中晚按钮渲染 self.everyBodyLove?.configTitle(model: self.viewModel.SupplyTypeList!) //早中晚默认给到第一个类型 self.viewModel.SupplyTypeRequsetModel.supplyTypeId = self.viewModel.SupplyTypeList!.supplyTypeList![0].id self.viewModel.getEveryBodyModel.supplyTypeId = self.viewModel.SupplyTypeList!.supplyTypeList![0].id //获取餐品 self.GetMealWithSupplyType(true) } self.hud.hidden() }, error: { error in self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } /// 获取餐品类型 /// - Parameter isFirstPage: 加载第一页 func GetMealWithSupplyType(_ isFirstPage: Bool){ self.hud.showLoading() self.viewModel.SupplyTypeRequsetModel.isGeneral = self.shopType self.viewModel.signalGetHomeFoodsWithType(isFirstPage)?.on(success: { dd in self.hud.hidden() self.collectionView.reloadData() self.getEveryBodyLove(isFirstPage) self.getTimeRangDetail() self.scrollView.mj_header.endRefreshing() self.scrollView.mj_footer.endRefreshing() }, error: { error in self.getEveryBodyLove(isFirstPage) self.scrollView.mj_header.endRefreshing() self.scrollView.mj_footer.endRefreshing() self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } func getEveryBodyLove(_ isFirstPage: Bool){ self.hud.showLoading() self.viewModel.getEveryBodyModel.isGeneral = self.shopType self.viewModel.signalGetEveryBodyLove(isFirstPage)?.on(success: { dd in self.hud.hidden() self.everyBodyLove?.configMode(datList: self.viewModel.EveryBodyLoveList) }, error: { error in self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } //MARK: 获取时间范围 func getTimeRangDetail(){ self.viewModel.HomeTimeGetModel.shopType = self.shopType self.viewModel.signalGetHomeTime()?.on(success: { dd in self.hud.hidden() for item in self.viewModel.TimeRangDetail!.timeVoList! { let model : HomeTimeItem = item if model.day == self.nowTimeModel?.day{ model.isSelect = true } } self.shopcartId = self.viewModel.TimeRangDetail?.shopcartId self.viewModel.homeEditorTimeRangModel.id = self.viewModel.TimeRangDetail?.id self.viewModel.homeEditorTimeRangModel.shopType = self.shopType if !self.isMJLoad{ self.SalesmanView?.shopType = self.shopType self.SalesmanView?.showWithShopcartId(shopcartId: self.shopcartId ?? 0, vc: self) } if let _ = self.viewList{ self.resetConstraint() }else{ self.createUI() } self.getShopCarPrice() self.reloadDateView() self.resetConstraint() }, error: { error in self.hud.showFailure("获取时间范围失败") }) } /// 获取购物车价格详情 func getShopCarPrice(){ self.viewModel.getShopCarPriceRModel.id = self.shopcartId self.viewModel.signalGetShopCarPrice()?.on(success: { dd in if (self.viewModel.shopCarPriceDetail?.price ?? "") > 0{ self.scrollView.mas_remakeConstraints { make in make?.top.left().right().bottom().mas_equalTo()(self.view) make?.bottom.mas_equalTo()(self.shopCar.mas_bottom)?.offset()(-64) make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight) } self.shopCar.isHidden = false }else{ self.scrollView.mas_remakeConstraints { make in make?.top.left().right().bottom().mas_equalTo()(self.view) make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight) } self.shopCar.isHidden = true } self.shopCarPrice.text = "\(self.viewModel.shopCarPriceDetail?.price ?? "")" }, error: { error in self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } /// 刷新顶部时间范围 func refreshTimeRang(){ self.hud.showLoading() self.viewModel.signalHomeEditorTimeRang()?.on(success: { dd in self.hud.showSuccess() self.getTimeRangDetail() }, error: { error in self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error")) }) } //MARK: 刷新时间段 func reloadDateView(){ self.dateView?.configModel(dateModel: self.viewModel.TimeRangDetail!) } // 底部刷新 func footerRefresh(){ print("上拉刷新") } //MARK: 重设约束 func resetConstraint(){ // self.scrollView.addSubview(self.backImageView!) // self.scrollView.addSubview(self.dateView!) // self.scrollView.addSubview(self.everyBodyLove!) // self.scrollView.addSubview(self.collectionView) // self.scrollView.addSubview(self.reBtnView!) var contentSizeH : CGFloat = 0 for i in 0.. 0{ make?.height.offset()((206.5 + 20) * CGFloat((lin1 + lin2))) }else{ make?.height.offset()(0) } make?.top.mas_equalTo()(self.reBtnView!.mas_bottom)?.offset() make?.bottom.mas_equalTo()(self.scrollView) } } contentSizeH = contentSizeH + itme.frame.size.height } scrollView.contentSize = CGSize.init(width: 0, height: contentSizeH) } //MARK: scrollView private var scrollView : UIScrollView = { let scrollView = UIScrollView.init(frame: CGRect.zero) scrollView.alwaysBounceVertical = true scrollView.isScrollEnabled = true scrollView.showsVerticalScrollIndicator = false scrollView.backgroundColor = kTBackgroundColor scrollView.contentInsetAdjustmentBehavior = .never return scrollView }() //MARK: collectionView lazy var collectionView : UICollectionView = { //设置布局 let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init() let itemSpac = (kSCREEN_WIDTH - 323) / 3 layout.scrollDirection = .vertical //竖直 layout.itemSize = CGSize.init(width: 161.5, height: 206.5) //行距 layout.minimumInteritemSpacing = 20 layout.minimumLineSpacing = itemSpac layout.sectionInset = .init(top: 0, left: itemSpac, bottom: 0, right: itemSpac) let collectView = UICollectionView.init(frame: .zero, collectionViewLayout: layout) collectView.delegate = self collectView.dataSource = self collectView.backgroundColor = UIColor.white collectView.showsVerticalScrollIndicator = false collectView.isScrollEnabled = false collectView.register(withType: HomeFoodItem.self) return collectView }() func checkFoodDetail(model : HomeFoodDetailModel){ if model.mealType == "2"{ let vc = SetMealDetailVC() vc.mealId = model.mealId vc.mealType = model.mealType vc.runTypeId = model.runTypeId vc.supplyTypeId = model.supplyTypeId vc.shopcartTime = self.nowTimeModel?.shopcartTime vc.shopcartId = self.shopcartId vc.shopcartTimeId = self.nowTimeModel?.shopcartTimeId self.navigationController?.pushViewController(vc, animated: true) }else{ let vc = SingleMealDetailController() vc.mealId = model.mealId vc.mealType = model.mealType vc.runTypeId = model.runTypeId vc.supplyTypeId = model.supplyTypeId vc.shopcartTime = self.nowTimeModel?.shopcartTime vc.shopcartId = self.shopcartId vc.shopcartTimeId = self.nowTimeModel?.shopcartTimeId self.navigationController?.pushViewController(vc, animated: true) } } } //MARK: Delegate extension HomeViewController : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.viewModel.HomeFoodList?.count ?? 0 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell : HomeFoodItem = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeFoodItem", for: indexPath) as! HomeFoodItem cell.configData(dataModel: self.viewModel.HomeFoodList![indexPath.row]) return cell; } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { if self.nowTimeModel.isNil{ self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择日期", Etext: "Please select a date")) return } let model = self.viewModel.HomeFoodList![indexPath.row] self.checkFoodDetail(model: model) } }