ShopViewController.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // ShopViewController.swift
  3. // RainbowPlanet
  4. //
  5. // Created by 南鑫林 on 2019/5/12.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. class ShopViewController: BaseViewController {
  10. var shopId : Int?
  11. var productSearchModelArray = Array<ProductSearchModel>()
  12. var fieldOrder : Int = 0
  13. var shopSectionHeaderModel = ShopSectionHeaderModel()
  14. override func viewDidLoad() {
  15. super.viewDidLoad()
  16. setupViews()
  17. setupLayouts()
  18. setupData()
  19. }
  20. override func setupViews() {
  21. navigationBar.barBackgroundColor = kFFA42FColor
  22. navigationBar.titleLabelColor = UIColor.white
  23. navigationBar.wr_setLeftButton(image: kImage(name: "navbar_back_white")!)
  24. navigationBar.wr_setRightButton(image: kImage(name:
  25. "navbar_shopping_white")!)
  26. statusBarStyle = .lightContent
  27. navigationBar.onClickRightButton = {
  28. [weak self] in
  29. let vc = ShoppingCartViewController()
  30. vc.shoppingCartVCType = .push
  31. self?.navigationController?.pushViewController(vc, animated: true)
  32. }
  33. view.addSubview(shopView)
  34. }
  35. override func setupLayouts() {
  36. shopView.snp.makeConstraints { (make) in
  37. make.top.equalTo(kNavBarTotalHeight)
  38. make.left.right.bottom.equalToSuperview()
  39. }
  40. }
  41. override func setupData() {
  42. shopView.tableView.addHeaderWithHeader(withBeginRefresh: true, animation: false) { [weak self] (page) in
  43. self?.productGetCartCountApi()
  44. self?.shopViewApi()
  45. self?.productListApi(page: page)
  46. }
  47. shopView.tableView.addFooterWithWithHeader(withAutomaticallyRefresh: true) { [weak self] (page) in
  48. self?.productListApi(page: page)
  49. }
  50. _ = NotificationCenter.default.rx
  51. .notification(NSNotification.Name("ShopVCDesc"))
  52. .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
  53. .subscribe(onNext: {
  54. [weak self] notification in
  55. self?.shopSectionHeaderModel = notification.userInfo!["desc"] as! ShopSectionHeaderModel
  56. self?.fieldOrder = notification.userInfo!["field_order"] as! Int
  57. self?.productListApi(page: 1)
  58. })
  59. shopView.didSelectItemBlock = {
  60. [weak self] indexPath in
  61. let vc = ProductDetailViewController()
  62. let productSearch = self?.productSearchModelArray[indexPath.row]
  63. vc.productId = productSearch?.id
  64. vc.shopId = productSearch?.shopId
  65. self?.navigationController?.pushViewController(vc, animated: true)
  66. }
  67. shopView.plusCloSure = {
  68. [weak self] in
  69. self?.productGetCartCountApi()
  70. }
  71. }
  72. private lazy var shopView: ShopView = {
  73. let shopView = ShopView()
  74. return shopView
  75. }()
  76. /// 获取商家信息
  77. private func shopViewApi() {
  78. SwiftMoyaNetWorkServiceShop.shared().shopViewApi(shopId: shopId ?? 0) {
  79. [weak self] (shopModel) -> (Void) in
  80. let shopModel = shopModel as! ShopModel
  81. self?.shopView.shopModel = shopModel
  82. }
  83. }
  84. /// 获取商家商品列表
  85. private func productListApi(page:Int) {
  86. SwiftProgressHUD.shared().showWait()
  87. SwiftMoyaNetWorkServiceProduct.shared().productListApi(page: page, shopId: shopId ?? 0,desc: self.shopSectionHeaderModel.state, fieldOrder: fieldOrder) {
  88. [weak self] (productListModel) -> (Void) in
  89. let productSearchListModel = productListModel as? ProductSearchListModel
  90. if productSearchListModel?.pagination?.currentPage ?? 1 <= productSearchListModel?.pagination?.totalPages ?? 1 {
  91. if productSearchListModel?.pagination?.currentPage == 1{
  92. self?.productSearchModelArray.removeAll()
  93. self?.shopView.paginationModel = productSearchListModel?.pagination
  94. self?.shopView.tableView.resetNoMoreData()
  95. }
  96. self?.productSearchModelArray = (self?.productSearchModelArray)! + (productSearchListModel?.data!)!
  97. self?.shopView.productSearchModelArray = self?.productSearchModelArray
  98. self?.shopView.shopSectionHeaderModel = self?.shopSectionHeaderModel
  99. }else {
  100. self?.shopView.tableView.endFooterNoMoreData()
  101. }
  102. SwiftProgressHUD.shared().hide()
  103. }
  104. }
  105. /// 获取购物车数量
  106. private func productGetCartCountApi() {
  107. SwiftMoyaNetWorkServiceProduct.shared().productGetCartCountApi {
  108. [weak self] (productCartCountModel) -> (Void) in
  109. let productCartCountModel = productCartCountModel as! ProductCartCountModel
  110. if productCartCountModel.count != 0 {
  111. self?.navigationBar.rightButton.pp.badgeView.isUserInteractionEnabled = false
  112. self?.navigationBar.rightButton.pp.badgeView.offset = CGPoint(x: -13, y: 13)
  113. self?.navigationBar.rightButton.pp.addBadge(number: productCartCountModel.count ?? 0)
  114. }else {
  115. self?.navigationBar.rightButton.pp.badgeView.isHidden = true
  116. }
  117. }
  118. }
  119. }