HomeSecondViewController.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. //
  2. // HomeSecondViewController.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/11/12.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import RxCocoa
  10. import SwiftDate
  11. class HomeSecondViewController: ADHBaseVC {
  12. @IBOutlet var shopCar: UIView!
  13. /// 购物车价格
  14. @IBOutlet weak var shopCarPrice: UILabel!
  15. @IBOutlet weak var shopCarBotton: NSLayoutConstraint!
  16. let dateView = Bundle.main.loadNibNamed("HomeDateView", owner: nil, options: nil)?.last as? HomeDateView
  17. let everyBodyLove = Bundle.main.loadNibNamed("EverybodyLoveView", owner: nil, options: nil)?.last as? EverybodyLoveView
  18. let reBtnView = Bundle.main.loadNibNamed("HomeRecommendView", owner: nil, options: nil)?.last as? HomeRecommendView
  19. let searchView = Bundle.main.loadNibNamed("SearchView", owner: nil, options: nil)?.last as? SearchView
  20. //查看购物车View
  21. let checkShoppingCarView = Bundle.main.loadNibNamed("ShoppingCarDetailView", owner: nil, options: nil)?.last as? ShoppingCarDetailView
  22. let timeRangeView = UINib.view(withType: timeManageSheetView.self)
  23. var viewList : [Any]?
  24. /// 当前选择的时间
  25. var nowTimeModel : HomeTimeItem?
  26. private var backImageView : UIImageView!
  27. let viewModel : HomeViewModel = HomeViewModel.init()
  28. let diposableBag = DisposeBag()
  29. /// 1为普团,2为白团
  30. let shopType = "1"
  31. /// 购物车ID
  32. var shopcartId : NSNumber?
  33. override func viewWillAppear(_ animated: Bool) {
  34. super.viewWillAppear(animated)
  35. UIScrollView.appearance().isPagingEnabled = false
  36. }
  37. //去结算
  38. @IBAction func gotoShpCar(_ sender: UIButton) {
  39. let vc = CheckOrderViewController()
  40. vc.shopType = self.shopType
  41. vc.shopcartId = shopcartId
  42. vc.shopcartTimeId = self.nowTimeModel?.shopcartTimeId
  43. vc.nowTimeModel = self.nowTimeModel
  44. self.navigationController?.pushViewController(vc, animated: true)
  45. }
  46. //查看购物车
  47. @IBAction func checkShopCar(_ sender: UIButton) {
  48. if self.nowTimeModel.isNil{
  49. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择日期", Etext: "Please select a date"))
  50. return
  51. }
  52. self.checkShoppingCarView?.shopType = self.shopType
  53. self.checkShoppingCarView?.shopcartId = self.shopcartId
  54. self.checkShoppingCarView?.shopcartTimeId = self.nowTimeModel?.shopcartTimeId
  55. self.checkShoppingCarView?.nowTimeModel = self.nowTimeModel
  56. self.checkShoppingCarView?.show()
  57. }
  58. override func viewDidLoad() {
  59. super.viewDidLoad()
  60. _ = NotificationCenter.default.rx.notification(Notification.Name(rawValue: "刷新首页")).takeUntil(self.rx.deallocated).subscribe(onNext:{[weak self] (notification) in
  61. self?.refreshData()
  62. })
  63. self.scrollView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[unowned self] in
  64. self.GetMealWithSupplyType(true)
  65. });
  66. self.scrollView.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {[unowned self] in
  67. self.GetMealWithSupplyType(false)
  68. });
  69. self.scrollView.ly_emptyView = LYEmptyView.emptyActionView(with: UIImage.init(named: "暂无数据"), titleStr: "暂无数据", detailStr: "", btnTitleStr: "点击刷新", btnClick: {[weak self] in
  70. self?.GetMealWithSupplyType(true)
  71. })
  72. self.view.backgroundColor = kTBackgroundColor
  73. self.shopCar.isHidden = true
  74. shopCarBotton.constant = 0
  75. shopCar.bringSubviewToFront(self.scrollView)
  76. self.view.addSubview(self.scrollView)
  77. self.scrollView.mas_remakeConstraints { make in
  78. make?.top.left().right().bottom().mas_equalTo()(self.view)
  79. make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight)
  80. }
  81. // self.createUI()
  82. //获取餐品类型
  83. self.getHomeFoodTypes()
  84. //获取时间段范围
  85. // self./*getTimeRangDetail*/()
  86. }
  87. func refreshData(){
  88. self.getTimeRangDetail()
  89. }
  90. //MARK: createUI
  91. func createUI(){
  92. self.backImageView = UIImageView.init(image: UIImage.init(named: "首页背景")!)
  93. self.backImageView.isUserInteractionEnabled = true
  94. self.backImageView.addSubview(self.searchView!)
  95. self.searchView!.mas_remakeConstraints { make in
  96. make?.left.mas_equalTo()(self.backImageView)?.offset()(24)
  97. make?.right.mas_equalTo()(self.backImageView)?.offset()(-24)
  98. make?.top.mas_equalTo()(self.backImageView)?.offset()(self.backImageView.frame.size.height - 100)
  99. make?.height.offset()(48)
  100. }
  101. //
  102. self.viewList = [self.backImageView!,self.dateView! ,self.everyBodyLove!,self.reBtnView!,self.collectionView]
  103. self.scrollView.addSubview(self.backImageView!)
  104. self.scrollView.addSubview(self.dateView!)
  105. self.scrollView.addSubview(self.everyBodyLove!)
  106. self.scrollView.addSubview(self.reBtnView!)
  107. self.scrollView.addSubview(self.collectionView)
  108. self.timeRangeView?.selectCheckDateBlock = {[weak self] startTime , endTime , days in
  109. self?.viewModel.homeEditorTimeRangModel.endDate = endTime
  110. self?.viewModel.homeEditorTimeRangModel.startDate = startTime
  111. self?.refreshTimeRang()
  112. }
  113. //选择时间段
  114. self.dateView?.timeRangeAction = {[weak self] in
  115. let monthFirstDate = NSDate.now
  116. let days : Int = monthFirstDate.day
  117. let startDate = monthFirstDate - days.days + 1.days
  118. self?.timeRangeView?.show(withStaStr: self?.viewModel.TimeRangDetail?.startDate ?? "", endStr: self?.viewModel.TimeRangDetail?.endDate ?? "",begin: startDate)
  119. }
  120. self.dateView?.selectBlock = {[weak self] model in
  121. self?.nowTimeModel = model
  122. self?.viewModel.SupplyTypeRequsetModel.shopcartTime = self?.nowTimeModel!.shopcartTime
  123. self?.GetMealWithSupplyType(true)
  124. self?.reloadDateView()
  125. }
  126. //早中晚选择
  127. self.everyBodyLove?.supplyTypeAction = {[weak self] type in
  128. self?.viewModel.SupplyTypeRequsetModel.supplyTypeId = type
  129. self?.GetMealWithSupplyType(true)
  130. }
  131. //所有人都爱点击
  132. self.everyBodyLove?.touchUpActionBlock = {[unowned self] tag in
  133. if self.nowTimeModel.isNil{
  134. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择日期", Etext: "Please select a date"))
  135. return
  136. }
  137. let model : HomeFoodDetailModel = (self.viewModel.EveryBodyLoveList![tag])
  138. self.checkFoodDetail(model: model)
  139. }
  140. //下方套餐选择
  141. self.reBtnView?.runTypeAction = {[weak self] type in
  142. self?.viewModel.SupplyTypeRequsetModel.runTypeId = type
  143. self?.GetMealWithSupplyType(true)
  144. }
  145. self.checkShoppingCarView?.reFreshBlock = {[weak self] in
  146. self?.getTimeRangDetail()
  147. }
  148. self.checkShoppingCarView?.checkOrderBlock = {[weak self] in
  149. let vc = CheckOrderViewController()
  150. vc.shopType = self?.shopType
  151. vc.shopcartId = self?.viewModel.TimeRangDetail?.shopcartId
  152. vc.shopcartTimeId = self?.checkShoppingCarView?.nowTimeModel?.shopcartTimeId
  153. vc.nowTimeModel = self?.checkShoppingCarView?.nowTimeModel
  154. self?.navigationController?.pushViewController(vc, animated: true)
  155. }
  156. self.resetConstraint()
  157. }
  158. //MARK: 刷新加载部分
  159. @objc func headerRefresh(){
  160. print("下拉刷新.")
  161. self.GetMealWithSupplyType(true)
  162. }
  163. //MARK: 获取餐品类型
  164. func getHomeFoodTypes(){
  165. self.viewModel.GetRunAndSupply.mealType = self.shopType
  166. self.hud.showLoading()
  167. self.viewModel.signalGetHomeFoodType()?.on(success: { dd in
  168. if self.viewModel.SupplyTypeList!.runTypeList!.count > 0{
  169. //下部套餐按钮渲染
  170. self.reBtnView?.configTitle(model: self.viewModel.SupplyTypeList!)
  171. //早中晚默认给到第一个类型
  172. self.viewModel.SupplyTypeRequsetModel.runTypeId = self.viewModel.SupplyTypeList!.runTypeList![0].id
  173. }
  174. if self.viewModel.SupplyTypeList!.supplyTypeList!.count > 0{
  175. //早中晚按钮渲染
  176. self.everyBodyLove?.configTitle(model: self.viewModel.SupplyTypeList!)
  177. //早中晚默认给到第一个类型
  178. self.viewModel.SupplyTypeRequsetModel.supplyTypeId = self.viewModel.SupplyTypeList!.supplyTypeList![0].id
  179. self.viewModel.getEveryBodyModel.supplyTypeId = self.viewModel.SupplyTypeList!.supplyTypeList![0].id
  180. //获取餐品
  181. self.GetMealWithSupplyType(true)
  182. }
  183. self.hud.hidden()
  184. }, error: { error in
  185. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  186. })
  187. }
  188. /// 获取餐品类型
  189. /// - Parameter isFirstPage: 加载第一页
  190. func GetMealWithSupplyType(_ isFirstPage: Bool){
  191. self.hud.showLoading()
  192. self.viewModel.SupplyTypeRequsetModel.isGeneral = self.shopType
  193. self.viewModel.signalGetHomeFoodsWithType(isFirstPage)?.on(success: { dd in
  194. self.hud.hidden()
  195. self.collectionView.reloadData()
  196. self.getEveryBodyLove(isFirstPage)
  197. self.getTimeRangDetail()
  198. self.scrollView.mj_header.endRefreshing()
  199. self.scrollView.mj_footer.endRefreshing()
  200. }, error: { error in
  201. self.getEveryBodyLove(isFirstPage)
  202. self.scrollView.mj_header.endRefreshing()
  203. self.scrollView.mj_footer.endRefreshing()
  204. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  205. })
  206. }
  207. func getEveryBodyLove(_ isFirstPage: Bool){
  208. self.hud.showLoading()
  209. self.viewModel.getEveryBodyModel.isGeneral = self.shopType
  210. self.viewModel.signalGetEveryBodyLove(isFirstPage)?.on(success: { dd in
  211. self.hud.hidden()
  212. self.everyBodyLove?.configMode(datList: self.viewModel.EveryBodyLoveList)
  213. if let _ = self.viewList{
  214. self.resetConstraint()
  215. }else{
  216. self.createUI()
  217. }
  218. self.scrollView.mj_header.endRefreshing()
  219. self.scrollView.mj_footer.endRefreshing()
  220. }, error: { error in
  221. self.scrollView.mj_header.endRefreshing()
  222. self.scrollView.mj_footer.endRefreshing()
  223. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  224. })
  225. }
  226. //MARK: 获取时间范围
  227. func getTimeRangDetail(){
  228. self.viewModel.HomeTimeGetModel.shopType = self.shopType
  229. self.viewModel.signalGetHomeTime()?.on(success: { dd in
  230. self.hud.hidden()
  231. for item in self.viewModel.TimeRangDetail!.timeVoList! {
  232. let model : HomeTimeItem = item
  233. if model.day == self.nowTimeModel?.day{
  234. model.isSelect = true
  235. }
  236. }
  237. self.shopcartId = self.viewModel.TimeRangDetail?.shopcartId
  238. self.viewModel.homeEditorTimeRangModel.id = self.viewModel.TimeRangDetail?.id
  239. self.viewModel.homeEditorTimeRangModel.shopType = self.shopType
  240. if let _ = self.viewList{
  241. self.resetConstraint()
  242. }else{
  243. self.createUI()
  244. }
  245. self.getShopCarPrice()
  246. self.reloadDateView()
  247. self.resetConstraint()
  248. }, error: { error in
  249. self.hud.showFailure("获取时间范围失败")
  250. })
  251. }
  252. /// 获取购物车价格详情
  253. func getShopCarPrice(){
  254. self.viewModel.getShopCarPriceRModel.id = self.shopcartId
  255. self.viewModel.signalGetShopCarPrice()?.on(success: { dd in
  256. if (self.viewModel.shopCarPriceDetail?.price ?? "") > 0{
  257. self.scrollView.mas_remakeConstraints { make in
  258. make?.top.left().right().bottom().mas_equalTo()(self.view)
  259. make?.bottom.mas_equalTo()(self.shopCar.mas_bottom)?.offset()(-64)
  260. make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight)
  261. }
  262. self.shopCar.isHidden = false
  263. }else{
  264. self.scrollView.mas_remakeConstraints { make in
  265. make?.top.left().right().bottom().mas_equalTo()(self.view)
  266. make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight)
  267. }
  268. self.shopCar.isHidden = true
  269. }
  270. self.shopCarPrice.text = "\(self.viewModel.shopCarPriceDetail?.price ?? "")"
  271. }, error: { error in
  272. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  273. })
  274. }
  275. /// 刷新顶部时间范围
  276. func refreshTimeRang(){
  277. self.hud.showLoading()
  278. self.viewModel.signalHomeEditorTimeRang()?.on(success: { dd in
  279. self.hud.showSuccess()
  280. self.getTimeRangDetail()
  281. }, error: { error in
  282. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  283. })
  284. }
  285. //MARK: 刷新时间段
  286. func reloadDateView(){
  287. self.dateView?.configModel(dateModel: self.viewModel.TimeRangDetail!)
  288. }
  289. // 底部刷新
  290. func footerRefresh(){
  291. print("上拉刷新")
  292. }
  293. //MARK: 重设约束
  294. func resetConstraint(){
  295. // self.scrollView.addSubview(self.backImageView!)
  296. // self.scrollView.addSubview(self.dateView!)
  297. // self.scrollView.addSubview(self.everyBodyLove!)
  298. // self.scrollView.addSubview(self.collectionView)
  299. // self.scrollView.addSubview(self.reBtnView!)
  300. var contentSizeH : CGFloat = 0
  301. for i in 0..<viewList!.count{
  302. let itme : UIView = viewList![i] as! UIView
  303. itme.mas_remakeConstraints { make in
  304. make?.left.right().mas_equalTo()(self.scrollView)
  305. if i == 0{
  306. make?.width.offset()(kSCREEN_WIDTH)
  307. make?.top.mas_equalTo()(self.scrollView)
  308. }
  309. if i == 1{
  310. if self.viewModel.TimeRangDetail != nil{
  311. make?.height.offset()(178)
  312. }else{
  313. make?.height.offset()(0)
  314. }
  315. make?.top.mas_equalTo()(self.backImageView.mas_bottom)?.offset()(-24)
  316. }
  317. if i == 2{
  318. make?.top.mas_equalTo()(self.dateView!.mas_bottom)?.offset()
  319. // if (self.viewModel.EveryBodyLoveList?.count ?? 0) == 0{
  320. // make?.height.offset()(47)
  321. // }else{
  322. // make?.height.offset()(320+47+20)
  323. // }
  324. make?.height.offset()(320+47+20)
  325. }
  326. if i == 3{
  327. if self.viewModel.SupplyTypeList != nil{
  328. make?.height.offset()(76)
  329. }else{
  330. make?.height.offset()(0)
  331. }
  332. make?.top.mas_equalTo()(self.everyBodyLove!.mas_bottom)?.offset()
  333. }
  334. if i == 4{
  335. //self.viewModel.HomeFoodList?.count date数量 2两列 取整数取余数
  336. let lin1 = (self.viewModel.HomeFoodList?.count ?? 0)/2
  337. let lin2 = (self.viewModel.HomeFoodList?.count ?? 0)%2
  338. if (self.viewModel.HomeFoodList?.count ?? 0) > 0{
  339. make?.height.offset()((206.5 + 20) * CGFloat((lin1 + lin2)))
  340. }else{
  341. make?.height.offset()(0)
  342. }
  343. make?.top.mas_equalTo()(self.reBtnView!.mas_bottom)?.offset()
  344. make?.bottom.mas_equalTo()(self.scrollView)
  345. }
  346. }
  347. contentSizeH = contentSizeH + itme.frame.size.height
  348. }
  349. scrollView.contentSize = CGSize.init(width: 0, height: contentSizeH)
  350. }
  351. //MARK: scrollView
  352. private var scrollView : UIScrollView = {
  353. let scrollView = UIScrollView.init(frame: CGRect.zero)
  354. scrollView.alwaysBounceVertical = true
  355. scrollView.isScrollEnabled = true
  356. scrollView.showsVerticalScrollIndicator = false
  357. scrollView.backgroundColor = kTBackgroundColor
  358. scrollView.contentInsetAdjustmentBehavior = .never
  359. return scrollView
  360. }()
  361. //MARK: collectionView
  362. lazy var collectionView : UICollectionView = {
  363. //设置布局
  364. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
  365. let itemSpac = (kSCREEN_WIDTH - 323) / 3
  366. layout.scrollDirection = .vertical //竖直
  367. layout.itemSize = CGSize.init(width: 161.5, height: 206.5)
  368. //行距
  369. layout.minimumInteritemSpacing = 20
  370. layout.minimumLineSpacing = itemSpac
  371. layout.sectionInset = .init(top: 0, left: itemSpac, bottom: 0, right: itemSpac)
  372. let collectView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
  373. collectView.delegate = self
  374. collectView.dataSource = self
  375. collectView.backgroundColor = UIColor.white
  376. collectView.showsVerticalScrollIndicator = false
  377. collectView.isScrollEnabled = false
  378. collectView.register(withType: HomeFoodItem.self)
  379. return collectView
  380. }()
  381. func checkFoodDetail(model : HomeFoodDetailModel){
  382. if model.mealType == "2"{
  383. let vc = SetMealDetailVC()
  384. vc.mealId = model.mealId
  385. vc.mealType = model.mealType
  386. vc.runTypeId = model.runTypeId
  387. vc.supplyTypeId = model.supplyTypeId
  388. vc.shopcartTime = self.nowTimeModel?.shopcartTime
  389. vc.shopcartId = self.shopcartId
  390. vc.shopcartTimeId = self.nowTimeModel?.shopcartTimeId
  391. self.navigationController?.pushViewController(vc, animated: true)
  392. }else{
  393. let vc = SingleMealDetailController()
  394. vc.mealId = model.mealId
  395. vc.mealType = model.mealType
  396. vc.runTypeId = model.runTypeId
  397. vc.supplyTypeId = model.supplyTypeId
  398. vc.shopcartTime = self.nowTimeModel?.shopcartTime
  399. vc.shopcartId = self.shopcartId
  400. vc.shopcartTimeId = self.nowTimeModel?.shopcartTimeId
  401. self.navigationController?.pushViewController(vc, animated: true)
  402. }
  403. }
  404. }
  405. //MARK: Delegate
  406. extension HomeSecondViewController : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
  407. {
  408. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  409. return self.viewModel.HomeFoodList?.count ?? 0
  410. }
  411. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  412. let cell : HomeFoodItem = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeFoodItem", for: indexPath) as! HomeFoodItem
  413. cell.configData(dataModel: self.viewModel.HomeFoodList![indexPath.row])
  414. return cell;
  415. }
  416. func numberOfSections(in collectionView: UICollectionView) -> Int {
  417. return 1
  418. }
  419. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  420. if self.nowTimeModel.isNil{
  421. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择日期", Etext: "Please select a date"))
  422. return
  423. }
  424. let model = self.viewModel.HomeFoodList![indexPath.row]
  425. self.checkFoodDetail(model: model)
  426. }
  427. }