123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- //
- // ShoppingCartView.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/5/8.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 购物车--首页View
- import UIKit
- class ShoppingCartView: BaseView {
-
- // 购物车列表ModelArr
- var cartListModelArr : Array<CartProductListModel>? {
- didSet {
- tableView.reloadData()
- self.judgeAllSelectedStatus()
- self.refreshAccountView()
- }
- }
-
- // 热销ModelArr
- var hotSaleModelArr : Array<ProductSearchModel>? {
- didSet {
- let sectionIdx = cartListModelArr?.count ?? 1
- self.tableView.reloadSections([sectionIdx], with: UITableView.RowAnimation.none)
- }
- }
-
- // 已选商品总价
- private var totalPrice: Int = 0
- // 已选商品件数
- private var totalNum: Int = 0
- // 已选商品ModelArr
- private var selModelArr: Array<CartProductListModel> = []
-
- typealias OrderPayTransBlock = (_ selMdlArr: Array<CartProductListModel>, _ totalPrice: Int) -> Void
- var orderPayTransBlock : OrderPayTransBlock?
-
- override func setupViews() {
- self.backgroundColor = kf7f8faColor
- addSubview(accountView)
- addSubview(tableView)
- let emptyView = EmptyView.shared.diyCustomEmptyViewStyle2(iconStr: "page04", titleStr: "当前暂无数据")
- emptyView.contentViewY = kScaleValue(value: 182)
- tableView.ly_emptyView = emptyView
- tableView.ly_startLoading()
- }
-
- override func setupLayouts() {
- accountView.snp.makeConstraints { (make) in
- make.left.right.bottom.equalToSuperview()
- make.height.equalTo(48)
- }
- tableView.snp.makeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.bottom.equalTo(accountView.snp_top).offset(0)
- }
- }
-
- lazy var accountView: ShoppingCartAccountView = {
- let accountView = ShoppingCartAccountView()
-
- accountView.allSelectBlock = { [weak self]
- (isAllSelected) in
- self?.allSelectedAction(isAllSelected)
- self?.refreshAccountView()
- }
-
- accountView.orderPayBlock = {
- [weak self] in
- if let orderPayTransBlock = self?.orderPayTransBlock {
- orderPayTransBlock(self!.selModelArr, self!.totalPrice)
- }
- }
-
- return accountView
- }()
-
- lazy var tableView: UITableView = {
- let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
- tableView.separatorStyle = .none
- tableView.backgroundColor = kf7f8faColor
- tableView.dataSource = self
- tableView.delegate = self
- tableView.estimatedRowHeight = 0.000001
- tableView.estimatedSectionFooterHeight = 0.000001
- tableView.estimatedSectionHeaderHeight = 0.000001
- return tableView
- }()
- }
- // MARK: - tableView dataSource && delegate
- extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
- func numberOfSections(in tableView: UITableView) -> Int {
- // 购物车列表 + 超值热卖
- return cartListModelArr?.isEmpty ?? true ? 2 : cartListModelArr!.count + 1
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- if cartListModelArr?.isEmpty ?? true {
- return 1
-
- } else {
- if section < cartListModelArr!.count {
- return cartListModelArr![section].productList?.isEmpty ?? true ? 0 : cartListModelArr![section].productList!.count
- } else {
- return 1
- }
- }
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- if cartListModelArr?.isEmpty ?? true {
- switch indexPath.section {
- case 0:
- // 购物车列表为空
- let cell = ShoppingCartListNoneItemCell.cellWith(tableView: tableView, indexPath: indexPath)
- return cell
- case 1:
- // 超值热卖
- let hotSaleCell = ShoppingCartHotSaleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
- hotSaleCell.frame = tableView.bounds
- hotSaleCell.hotSaleModelArr = hotSaleModelArr
- hotSaleCell.layoutIfNeeded()
- hotSaleCell.reloadData()
- return hotSaleCell
- default:
- return UITableViewCell()
- }
-
- } else {
- if indexPath.section < cartListModelArr!.count {
- // 购物车列表Item
- let cell = ShoppingCartListTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.productMdl = cartListModelArr![indexPath.section].productList![indexPath.row]
-
- cell.productSelBlock = { [weak self]
- (isProductSel) in
- self?.cartListModelArr![indexPath.section].productList![indexPath.row].isSelect = isProductSel
-
- self?.tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
- self?.judgeAllSelectedStatus()
- self?.refreshAccountView()
- }
-
- cell.changeProductBlock = { [weak self]
- (productId, type) in
- SwiftMoyaNetWorkServiceProduct.shared().productCartAmountApi(id: productId, type: type, completion: { [weak self] (amountData) -> (Void) in
- // 1.更新数据源
- let cartAmountMdl = amountData as? CartAmountModel
- let amount = cartAmountMdl?.amount
- self?.cartListModelArr![indexPath.section].productList![indexPath.row].amount = amount
- // 2.刷新cell
- let indexPath = IndexPath(item: indexPath.row, section: indexPath.section)
- self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
- })
- }
- return cell
-
- } else {
- // 超值热卖
- let hotSaleCell = ShoppingCartHotSaleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
- hotSaleCell.frame = tableView.bounds
- hotSaleCell.hotSaleModelArr = hotSaleModelArr
- hotSaleCell.layoutIfNeeded()
- hotSaleCell.reloadData()
- return hotSaleCell
- }
- }
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return UITableView.automaticDimension
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
- return 58
- } else {
- return 10
- }
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
- let headerView = ShoppingCartListTableViewHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 58))
- headerView.shopName = cartListModelArr![section].shopName
- headerView.isSectionSelected = self.judgeSectionSelectedStatus(section)
- headerView.secSelectBlock = { [weak self]
- (isSectionSel) in
- self?.shopSelectedAction(isSectionSel, section: section)
- self?.judgeAllSelectedStatus()
- self?.refreshAccountView()
- }
- return headerView
-
- } else {
- return nil
- }
- }
-
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- return 0.000001
- }
- func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
- return nil
- }
-
- func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
-
- if editingStyle == .delete {
- // 删除商品
- let productId = cartListModelArr![indexPath.section].productList?[indexPath.row].id
- SwiftMoyaNetWorkServiceProduct.shared().productCartDeleteApi(id: productId ?? 0) { [weak self] (data) -> (Void) in
- self?.cartListModelArr![indexPath.section].productList?.remove(at: indexPath.row)
- tableView.deleteRows(at: [indexPath], with: .none)
- }
- }
- }
-
- func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
- return "删除"
- }
- }
- // MARK: - 购物车逻辑处理
- extension ShoppingCartView {
-
- // 全选点击事件
- func allSelectedAction(_ isSelected: Int) {
- for cartProListMdl in cartListModelArr ?? [] {
- for productMdl in cartProListMdl.productList! {
- productMdl.isSelect = isSelected
- }
- }
- tableView.reloadData()
- }
-
- // 店铺(全选)点击事件
- func shopSelectedAction(_ isSelected: Int, section: Int) {
- let cartProListMdl: CartProductListModel = cartListModelArr![section]
- for productMdl in cartProListMdl.productList! {
- productMdl.isSelect = isSelected
- }
- self.tableView.reloadSections([section], with: UITableView.RowAnimation.none)
- self.judgeAllSelectedStatus()
- }
-
- // 校验Section内商品是否全选
- func judgeSectionSelectedStatus(_ section: Int) -> Int {
- let cartProListMdl: CartProductListModel = cartListModelArr![section]
- for productMdl in cartProListMdl.productList! {
- if productMdl.isSelect == 0 {
- return 0
- }
- }
- return 1
- }
-
- // 校验购物车内商品是否全选
- func judgeAllSelectedStatus() -> Void {
- let secCount: Int = cartListModelArr?.isEmpty ?? true ? 0 : cartListModelArr!.count
- for sec in 0..<secCount {
- let secStatus: Int = self.judgeSectionSelectedStatus(sec)
- if secStatus == 0 {
- self.accountView.isAllSelected = 0
- return
- }
- }
- self.accountView.isAllSelected = 1
- }
-
- // 计算结算数据,刷新结算View
- func refreshAccountView() {
- totalPrice = 0
- totalNum = 0
- selModelArr = []
-
- // 深拷贝数组
- for listModel in cartListModelArr! {
- let copyListMdl: CartProductListModel = listModel.copy() as! CartProductListModel
- selModelArr.append(copyListMdl)
- }
-
- for (secIdx, cartProListMdl) in (cartListModelArr?.enumerated())! {
- var subTag: Int = 0
- for (rowIdx, productMdl) in cartProListMdl.productList!.enumerated() {
- if productMdl.isSelect == 1 {
- totalPrice += productMdl.skuPrice ?? 0
- totalNum += 1
-
- } else {
- if !cartListModelArr![secIdx].productList!.isEmpty {
- selModelArr[secIdx].productList!.remove(at: rowIdx-subTag)
- subTag += 1
- }
- }
- }
- }
-
- self.accountView.tPrice = totalPrice
- self.accountView.tNumber = totalNum
- }
-
- }
|