HomeViewController.swift 21 KB

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