ShoppingCartView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // ShoppingCartView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/5/8.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. // 购物车--首页View
  8. import UIKit
  9. class ShoppingCartView: BaseView {
  10. // 购物车列表ModelArr
  11. var cartListModelArr : Array<CartProductListModel>? {
  12. didSet {
  13. tableView.reloadData()
  14. self.judgeAllSelectedStatus()
  15. self.refreshAccountView()
  16. }
  17. }
  18. // 热销ModelArr
  19. var hotSaleModelArr : Array<ProductSearchModel>? {
  20. didSet {
  21. let sectionIdx = cartListModelArr?.count ?? 1
  22. self.tableView.reloadSections([sectionIdx], with: UITableView.RowAnimation.none)
  23. }
  24. }
  25. // 已选商品总价
  26. private var totalPrice: Int = 0
  27. // 已选商品件数
  28. private var totalNum: Int = 0
  29. // 已选商品ModelArr
  30. private var selModelArr: Array<CartProductListModel> = []
  31. typealias OrderPayTransBlock = (_ selMdlArr: Array<CartProductListModel>, _ totalPrice: Int) -> Void
  32. var orderPayTransBlock : OrderPayTransBlock?
  33. override func setupViews() {
  34. self.backgroundColor = kf7f8faColor
  35. addSubview(accountView)
  36. addSubview(tableView)
  37. let emptyView = EmptyView.shared.diyCustomEmptyViewStyle2(iconStr: "page04", titleStr: "当前暂无数据")
  38. emptyView.contentViewY = kScaleValue(value: 182)
  39. tableView.ly_emptyView = emptyView
  40. tableView.ly_startLoading()
  41. }
  42. override func setupLayouts() {
  43. accountView.snp.makeConstraints { (make) in
  44. make.left.right.bottom.equalToSuperview()
  45. make.height.equalTo(48)
  46. }
  47. tableView.snp.makeConstraints { (make) in
  48. make.top.left.right.equalToSuperview()
  49. make.bottom.equalTo(accountView.snp_top).offset(0)
  50. }
  51. }
  52. lazy var accountView: ShoppingCartAccountView = {
  53. let accountView = ShoppingCartAccountView()
  54. accountView.allSelectBlock = { [weak self]
  55. (isAllSelected) in
  56. self?.allSelectedAction(isAllSelected)
  57. self?.refreshAccountView()
  58. }
  59. accountView.orderPayBlock = {
  60. [weak self] in
  61. if let orderPayTransBlock = self?.orderPayTransBlock {
  62. orderPayTransBlock(self!.selModelArr, self!.totalPrice)
  63. }
  64. }
  65. return accountView
  66. }()
  67. lazy var tableView: UITableView = {
  68. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  69. tableView.separatorStyle = .none
  70. tableView.backgroundColor = kf7f8faColor
  71. tableView.dataSource = self
  72. tableView.delegate = self
  73. tableView.estimatedRowHeight = 0.000001
  74. tableView.estimatedSectionFooterHeight = 0.000001
  75. tableView.estimatedSectionHeaderHeight = 0.000001
  76. return tableView
  77. }()
  78. }
  79. // MARK: - tableView dataSource && delegate
  80. extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
  81. func numberOfSections(in tableView: UITableView) -> Int {
  82. // 购物车列表 + 超值热卖
  83. return cartListModelArr?.isEmpty ?? true ? 2 : cartListModelArr!.count + 1
  84. }
  85. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  86. if cartListModelArr?.isEmpty ?? true {
  87. return 1
  88. } else {
  89. if section < cartListModelArr!.count {
  90. return cartListModelArr![section].productList?.isEmpty ?? true ? 0 : cartListModelArr![section].productList!.count
  91. } else {
  92. return 1
  93. }
  94. }
  95. }
  96. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  97. if cartListModelArr?.isEmpty ?? true {
  98. switch indexPath.section {
  99. case 0:
  100. // 购物车列表为空
  101. let cell = ShoppingCartListNoneItemCell.cellWith(tableView: tableView, indexPath: indexPath)
  102. return cell
  103. case 1:
  104. // 超值热卖
  105. let hotSaleCell = ShoppingCartHotSaleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  106. hotSaleCell.frame = tableView.bounds
  107. hotSaleCell.hotSaleModelArr = hotSaleModelArr
  108. hotSaleCell.layoutIfNeeded()
  109. hotSaleCell.reloadData()
  110. return hotSaleCell
  111. default:
  112. return UITableViewCell()
  113. }
  114. } else {
  115. if indexPath.section < cartListModelArr!.count {
  116. // 购物车列表Item
  117. let cell = ShoppingCartListTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  118. cell.productMdl = cartListModelArr![indexPath.section].productList![indexPath.row]
  119. cell.productSelBlock = { [weak self]
  120. (isProductSel) in
  121. self?.cartListModelArr![indexPath.section].productList![indexPath.row].isSelect = isProductSel
  122. self?.tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
  123. self?.judgeAllSelectedStatus()
  124. self?.refreshAccountView()
  125. }
  126. cell.changeProductBlock = { [weak self]
  127. (productId, type) in
  128. SwiftMoyaNetWorkServiceProduct.shared().productCartAmountApi(id: productId, type: type, completion: { [weak self] (amountData) -> (Void) in
  129. // 1.更新数据源
  130. let cartAmountMdl = amountData as? CartAmountModel
  131. let amount = cartAmountMdl?.amount
  132. self?.cartListModelArr![indexPath.section].productList![indexPath.row].amount = amount
  133. // 2.刷新cell
  134. let indexPath = IndexPath(item: indexPath.row, section: indexPath.section)
  135. self?.tableView.reloadRows(at: [indexPath], with: UITableView.RowAnimation.none)
  136. })
  137. }
  138. return cell
  139. } else {
  140. // 超值热卖
  141. let hotSaleCell = ShoppingCartHotSaleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  142. hotSaleCell.frame = tableView.bounds
  143. hotSaleCell.hotSaleModelArr = hotSaleModelArr
  144. hotSaleCell.layoutIfNeeded()
  145. hotSaleCell.reloadData()
  146. return hotSaleCell
  147. }
  148. }
  149. }
  150. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  151. return UITableView.automaticDimension
  152. }
  153. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  154. if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
  155. return 58
  156. } else {
  157. return 10
  158. }
  159. }
  160. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  161. if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
  162. let headerView = ShoppingCartListTableViewHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 58))
  163. headerView.shopName = cartListModelArr![section].shopName
  164. headerView.isSectionSelected = self.judgeSectionSelectedStatus(section)
  165. headerView.secSelectBlock = { [weak self]
  166. (isSectionSel) in
  167. self?.shopSelectedAction(isSectionSel, section: section)
  168. self?.judgeAllSelectedStatus()
  169. self?.refreshAccountView()
  170. }
  171. return headerView
  172. } else {
  173. return nil
  174. }
  175. }
  176. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  177. return 0.000001
  178. }
  179. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  180. return nil
  181. }
  182. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  183. if editingStyle == .delete {
  184. // 删除商品
  185. let productId = cartListModelArr![indexPath.section].productList?[indexPath.row].id
  186. SwiftMoyaNetWorkServiceProduct.shared().productCartDeleteApi(id: productId ?? 0) { [weak self] (data) -> (Void) in
  187. self?.cartListModelArr![indexPath.section].productList?.remove(at: indexPath.row)
  188. tableView.deleteRows(at: [indexPath], with: .none)
  189. }
  190. }
  191. }
  192. func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
  193. return "删除"
  194. }
  195. }
  196. // MARK: - 购物车逻辑处理
  197. extension ShoppingCartView {
  198. // 全选点击事件
  199. func allSelectedAction(_ isSelected: Int) {
  200. for cartProListMdl in cartListModelArr ?? [] {
  201. for productMdl in cartProListMdl.productList! {
  202. productMdl.isSelect = isSelected
  203. }
  204. }
  205. tableView.reloadData()
  206. }
  207. // 店铺(全选)点击事件
  208. func shopSelectedAction(_ isSelected: Int, section: Int) {
  209. let cartProListMdl: CartProductListModel = cartListModelArr![section]
  210. for productMdl in cartProListMdl.productList! {
  211. productMdl.isSelect = isSelected
  212. }
  213. self.tableView.reloadSections([section], with: UITableView.RowAnimation.none)
  214. self.judgeAllSelectedStatus()
  215. }
  216. // 校验Section内商品是否全选
  217. func judgeSectionSelectedStatus(_ section: Int) -> Int {
  218. let cartProListMdl: CartProductListModel = cartListModelArr![section]
  219. for productMdl in cartProListMdl.productList! {
  220. if productMdl.isSelect == 0 {
  221. return 0
  222. }
  223. }
  224. return 1
  225. }
  226. // 校验购物车内商品是否全选
  227. func judgeAllSelectedStatus() -> Void {
  228. let secCount: Int = cartListModelArr?.isEmpty ?? true ? 0 : cartListModelArr!.count
  229. for sec in 0..<secCount {
  230. let secStatus: Int = self.judgeSectionSelectedStatus(sec)
  231. if secStatus == 0 {
  232. self.accountView.isAllSelected = 0
  233. return
  234. }
  235. }
  236. self.accountView.isAllSelected = 1
  237. }
  238. // 计算结算数据,刷新结算View
  239. func refreshAccountView() {
  240. totalPrice = 0
  241. totalNum = 0
  242. selModelArr = []
  243. // 深拷贝数组
  244. for listModel in cartListModelArr! {
  245. let copyListMdl: CartProductListModel = listModel.copy() as! CartProductListModel
  246. selModelArr.append(copyListMdl)
  247. }
  248. for (secIdx, cartProListMdl) in (cartListModelArr?.enumerated())! {
  249. var subTag: Int = 0
  250. for (rowIdx, productMdl) in cartProListMdl.productList!.enumerated() {
  251. if productMdl.isSelect == 1 {
  252. totalPrice += productMdl.skuPrice ?? 0
  253. totalNum += 1
  254. } else {
  255. if !cartListModelArr![secIdx].productList!.isEmpty {
  256. selModelArr[secIdx].productList!.remove(at: rowIdx-subTag)
  257. subTag += 1
  258. }
  259. }
  260. }
  261. }
  262. self.accountView.tPrice = totalPrice
  263. self.accountView.tNumber = totalNum
  264. }
  265. }