123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- //
- // ShopViewController.swift
- // RainbowPlanet
- //
- // Created by 南鑫林 on 2019/5/12.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- //
- import UIKit
- class ShopViewController: BaseViewController {
-
- var shopId : Int?
- var productSearchModelArray = Array<ProductSearchModel>()
-
- var fieldOrder : Int = 0
- var shopSectionHeaderModel = ShopSectionHeaderModel()
-
- override func viewDidLoad() {
- super.viewDidLoad()
- setupViews()
- setupLayouts()
- setupData()
- }
-
- override func setupViews() {
- navigationBar.barBackgroundColor = kFFA42FColor
- navigationBar.titleLabelColor = UIColor.white
- navigationBar.wr_setLeftButton(image: kImage(name: "navbar_back_white")!)
- navigationBar.wr_setRightButton(image: kImage(name:
- "navbar_shopping_white")!)
- statusBarStyle = .lightContent
- navigationBar.onClickRightButton = {
- [weak self] in
- let vc = ShoppingCartViewController()
- vc.shoppingCartVCType = .push
- self?.navigationController?.pushViewController(vc, animated: true)
- }
- view.addSubview(shopView)
- }
-
- override func setupLayouts() {
- shopView.snp.makeConstraints { (make) in
- make.top.equalTo(kNavBarTotalHeight)
- make.left.right.bottom.equalToSuperview()
- }
- }
-
-
- override func setupData() {
-
- shopView.tableView.addHeaderWithHeader(withBeginRefresh: true, animation: false) { [weak self] (page) in
- self?.productGetCartCountApi()
- self?.shopViewApi()
- self?.productListApi(page: page)
- }
-
- shopView.tableView.addFooterWithWithHeader(withAutomaticallyRefresh: true) { [weak self] (page) in
- self?.productListApi(page: page)
- }
-
- _ = NotificationCenter.default.rx
- .notification(NSNotification.Name("ShopVCDesc"))
- .takeUntil(self.rx.deallocated) //页面销毁自动移除通知监听
- .subscribe(onNext: {
- [weak self] notification in
- self?.shopSectionHeaderModel = notification.userInfo!["desc"] as! ShopSectionHeaderModel
- self?.fieldOrder = notification.userInfo!["field_order"] as! Int
- self?.productListApi(page: 1)
- })
- shopView.didSelectItemBlock = {
- [weak self] indexPath in
- let vc = ProductDetailViewController()
- let productSearch = self?.productSearchModelArray[indexPath.row]
- vc.productId = productSearch?.id
- vc.shopId = productSearch?.shopId
- self?.navigationController?.pushViewController(vc, animated: true)
- }
- shopView.plusCloSure = {
- [weak self] in
- self?.productGetCartCountApi()
- }
- }
-
- private lazy var shopView: ShopView = {
- let shopView = ShopView()
- return shopView
- }()
-
- /// 获取商家信息
- private func shopViewApi() {
- SwiftMoyaNetWorkServiceShop.shared().shopViewApi(shopId: shopId ?? 0) {
- [weak self] (shopModel) -> (Void) in
- let shopModel = shopModel as! ShopModel
- self?.shopView.shopModel = shopModel
- }
- }
-
- /// 获取商家商品列表
- private func productListApi(page:Int) {
-
- SwiftProgressHUD.shared().showWait()
- SwiftMoyaNetWorkServiceProduct.shared().productListApi(page: page, shopId: shopId ?? 0,desc: self.shopSectionHeaderModel.state, fieldOrder: fieldOrder) {
- [weak self] (productListModel) -> (Void) in
- let productSearchListModel = productListModel as? ProductSearchListModel
- if productSearchListModel?.pagination?.currentPage ?? 1 <= productSearchListModel?.pagination?.totalPages ?? 1 {
- if productSearchListModel?.pagination?.currentPage == 1{
- self?.productSearchModelArray.removeAll()
- self?.shopView.paginationModel = productSearchListModel?.pagination
- self?.shopView.tableView.resetNoMoreData()
- }
- self?.productSearchModelArray = (self?.productSearchModelArray)! + (productSearchListModel?.data!)!
- self?.shopView.productSearchModelArray = self?.productSearchModelArray
- self?.shopView.shopSectionHeaderModel = self?.shopSectionHeaderModel
- }else {
- self?.shopView.tableView.endFooterNoMoreData()
- }
- SwiftProgressHUD.shared().hide()
- }
- }
-
- /// 获取购物车数量
- private func productGetCartCountApi() {
- SwiftMoyaNetWorkServiceProduct.shared().productGetCartCountApi {
- [weak self] (productCartCountModel) -> (Void) in
- let productCartCountModel = productCartCountModel as! ProductCartCountModel
- if productCartCountModel.count != 0 {
- self?.navigationBar.rightButton.pp.badgeView.isUserInteractionEnabled = false
- self?.navigationBar.rightButton.pp.badgeView.offset = CGPoint(x: -13, y: 13)
- self?.navigationBar.rightButton.pp.addBadge(number: productCartCountModel.count ?? 0)
- }else {
- self?.navigationBar.rightButton.pp.badgeView.isHidden = true
- }
- }
- }
- }
|