ShoppingCartView.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. typealias JumpToDetailTransClosure = (_ proId: Int,_ shopId : Int) -> Void
  11. var jumpToDetailTransClosure : JumpToDetailTransClosure?
  12. typealias ProductDidSelectRowAt = (ProductModel) -> Void
  13. var productDidSelectRowAt : ProductDidSelectRowAt?
  14. var shoppingCartVCType : ShoppingCartVCType = .none {
  15. didSet{
  16. if shoppingCartVCType == .push {
  17. accountView.snp.makeConstraints { (make) in
  18. make.left.right.equalToSuperview()
  19. make.height.equalTo(48)
  20. make.bottom.equalToSuperview().offset(-kSafeTabBarHeight)
  21. }
  22. tableView.snp.makeConstraints { (make) in
  23. make.top.left.right.equalToSuperview()
  24. make.bottom.equalTo(accountView.snp_top).offset(0)
  25. }
  26. }else {
  27. accountView.snp.makeConstraints { (make) in
  28. make.left.right.equalToSuperview()
  29. make.height.equalTo(48)
  30. make.bottom.equalToSuperview()
  31. }
  32. tableView.snp.makeConstraints { (make) in
  33. make.top.left.right.equalToSuperview()
  34. make.bottom.equalTo(accountView.snp_top).offset(0)
  35. }
  36. }
  37. }
  38. }
  39. // 购物车列表ModelArr
  40. var cartListModelArr : Array<CartProductListModel>? {
  41. didSet {
  42. self.judgeAllSelectedStatus()
  43. self.refreshAccountView()
  44. self.updateAccountViewCondition()
  45. tableView.reloadData()
  46. }
  47. }
  48. // 热销ModelArr
  49. var hotSaleModelArr : Array<ProductSearchModel>? {
  50. didSet {
  51. // 预计算热销Cell行高
  52. let count = self.hotSaleModelArr?.count ?? 0
  53. let rows = count / 2 + count % 2
  54. let cSpace = 10 * (rows - 1) + 30
  55. let collectionH: CGFloat = (170 * kScaleWidth + 80) * CGFloat(rows) + CGFloat(cSpace)
  56. self.hotSaleHeight = collectionH + 39
  57. tableView.reloadData()
  58. }
  59. }
  60. // 热销Cell行高
  61. private var hotSaleHeight: CGFloat = 0
  62. // 已选商品总价
  63. private var totalPrice: Int = 0
  64. // 已选商品件数
  65. private var totalNum: Int = 0
  66. // 已选商品ModelArr
  67. private var selModelArr: Array<CartProductListModel> = []
  68. // 订单支付--次级block
  69. typealias OrderPayTransBlock = (_ selMdlArr: Array<CartProductListModel>, _ totalPrice: Int) -> Void
  70. var orderPayTransBlock : OrderPayTransBlock?
  71. // 刷新列表(数据)--次级block
  72. typealias RefreshTransBlock = () -> Void
  73. var refreshTransBlock : RefreshTransBlock?
  74. // 点击店铺跳转--次级block
  75. typealias ShopClickedTransBlock = (_ shopId: Int) -> Void
  76. var shopClickedTransBlock : ShopClickedTransBlock?
  77. override func setupViews() {
  78. self.backgroundColor = kf7f8faColor
  79. addSubview(accountView)
  80. addSubview(tableView)
  81. let emptyView = DIYEmptyView.empty(with: kImage(name: "default_page_data"), titleStr: nil, detailStr: "当前暂无数据")
  82. emptyView!.contentViewY = kScaleValue(value: 182)
  83. tableView.ly_emptyView = emptyView
  84. tableView.ly_startLoading()
  85. self.updateAccountViewCondition()
  86. }
  87. override func setupLayouts() {
  88. if shoppingCartVCType == .push {
  89. accountView.snp.makeConstraints { (make) in
  90. make.left.right.equalToSuperview()
  91. make.height.equalTo(48)
  92. make.bottom.equalToSuperview().offset(-kSafeTabBarHeight)
  93. }
  94. tableView.snp.makeConstraints { (make) in
  95. make.top.left.right.equalToSuperview()
  96. make.bottom.equalTo(accountView.snp_top).offset(0)
  97. }
  98. }else {
  99. accountView.snp.makeConstraints { (make) in
  100. make.left.right.equalToSuperview()
  101. make.height.equalTo(48)
  102. make.bottom.equalToSuperview()
  103. }
  104. tableView.snp.makeConstraints { (make) in
  105. make.top.left.right.equalToSuperview()
  106. make.bottom.equalTo(accountView.snp_top).offset(0)
  107. }
  108. }
  109. }
  110. // 处理购物车结算是否显示
  111. func updateAccountViewCondition() {
  112. if cartListModelArr?.isEmpty ?? true {
  113. accountView.isHidden = true
  114. accountView.snp.remakeConstraints { (make) in
  115. make.left.right.equalToSuperview()
  116. make.height.equalTo(0)
  117. if self.shoppingCartVCType == .push {
  118. make.bottom.equalToSuperview().offset(-kSafeTabBarHeight)
  119. }else {
  120. make.bottom.equalToSuperview()
  121. }
  122. }
  123. tableView.snp.remakeConstraints { (make) in
  124. make.top.right.left.equalToSuperview()
  125. make.bottom.equalTo(accountView.snp_top)
  126. }
  127. } else {
  128. accountView.isHidden = false
  129. accountView.snp.remakeConstraints { (make) in
  130. make.left.right.equalToSuperview()
  131. make.height.equalTo(48)
  132. if self.shoppingCartVCType == .push {
  133. make.bottom.equalToSuperview().offset(-kSafeTabBarHeight)
  134. }else {
  135. make.bottom.equalToSuperview()
  136. }
  137. }
  138. tableView.snp.remakeConstraints { (make) in
  139. make.top.right.left.equalToSuperview()
  140. make.bottom.equalTo(accountView.snp_top)
  141. }
  142. }
  143. }
  144. lazy var accountView: ShoppingCartAccountView = {
  145. let accountView = ShoppingCartAccountView()
  146. accountView.allSelectBlock = { [weak self]
  147. (isAllSelected) in
  148. self?.productCartAllSelApi(isSel: isAllSelected)
  149. }
  150. accountView.orderPayBlock = {
  151. [weak self] in
  152. if let orderPayTransBlock = self?.orderPayTransBlock {
  153. orderPayTransBlock(self!.selModelArr, self!.totalPrice)
  154. }
  155. }
  156. return accountView
  157. }()
  158. lazy var tableView: UITableView = {
  159. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  160. tableView.separatorStyle = .none
  161. tableView.backgroundColor = kf7f8faColor
  162. tableView.dataSource = self
  163. tableView.delegate = self
  164. tableView.estimatedRowHeight = self.hotSaleHeight
  165. return tableView
  166. }()
  167. }
  168. // MARK: - tableView dataSource && delegate
  169. extension ShoppingCartView : UITableViewDelegate, UITableViewDataSource {
  170. func numberOfSections(in tableView: UITableView) -> Int {
  171. // 购物车列表 + 超值热卖
  172. return cartListModelArr?.isEmpty ?? true ? 2 : cartListModelArr!.count + 1
  173. }
  174. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  175. if cartListModelArr?.isEmpty ?? true {
  176. return 1
  177. } else {
  178. if section < cartListModelArr!.count {
  179. return cartListModelArr![section].productList?.isEmpty ?? true ? 0 : cartListModelArr![section].productList!.count
  180. } else {
  181. return 1
  182. }
  183. }
  184. }
  185. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  186. if cartListModelArr?.isEmpty ?? true {
  187. switch indexPath.section {
  188. case 0:
  189. // 购物车列表为空
  190. let cell = ShoppingCartListNoneItemCell.cellWith(tableView: tableView, indexPath: indexPath)
  191. return cell
  192. case 1:
  193. // 超值热卖
  194. let hotSaleCell = ShoppingCartHotSaleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  195. hotSaleCell.hotSaleModelArr = hotSaleModelArr
  196. hotSaleCell.layoutIfNeeded()
  197. hotSaleCell.reloadData()
  198. hotSaleCell.refreshBlock = {
  199. [weak self] in
  200. if let refreshTransBlock = self?.refreshTransBlock {
  201. refreshTransBlock()
  202. }
  203. }
  204. hotSaleCell.jumpToDetailClosure = {
  205. [weak self] (proId, shopId) in
  206. if let jumpToDetailTransClosure = self?.jumpToDetailTransClosure {
  207. jumpToDetailTransClosure(proId, shopId)
  208. }
  209. }
  210. return hotSaleCell
  211. default:
  212. return UITableViewCell()
  213. }
  214. } else {
  215. if indexPath.section < cartListModelArr!.count {
  216. // 购物车列表Item
  217. let cell = ShoppingCartListTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  218. cell.productMdl = cartListModelArr![indexPath.section].productList![indexPath.row]
  219. cell.productSelBlock = { [weak self]
  220. (isProductSel) in
  221. let productMdl: ProductModel = (self?.cartListModelArr![indexPath.section].productList![indexPath.row])!
  222. self?.productCartIsSelApi(pro_id: productMdl.id!, isSel: isProductSel, indexPath: indexPath)
  223. }
  224. cell.changeProductBlock = { [weak self]
  225. (productId, type) in
  226. // 最少只能减到1件商品
  227. let curAmount = self?.cartListModelArr![indexPath.section].productList![indexPath.row].amount
  228. if type == 2 && curAmount == 1 {
  229. AlertSheetView.alert(title: "确认删除商品", cancelTitle: "取消", sureTitle: "确认", cancelBlock: { (popupView, index, string) in
  230. }, confirmBlock: {
  231. [weak self] (popupView, index, string) in
  232. // 删除商品
  233. self?.deleteProduct(indexPath)
  234. })
  235. return
  236. }
  237. // 更改数量Api
  238. SwiftMoyaNetWorkServiceProduct.shared().productCartAmountApi(id: productId, type: type, completion: { [weak self] (amountData) -> (Void) in
  239. // 1.更新数据源
  240. let cartAmountMdl = amountData as? CartAmountModel
  241. let amount = cartAmountMdl?.amount
  242. self?.cartListModelArr![indexPath.section].productList![indexPath.row].amount = amount
  243. // 2.刷新cell及合计View
  244. self?.refreshAccountView()
  245. UIView.performWithoutAnimation {
  246. tableView.reloadData()
  247. }
  248. })
  249. }
  250. return cell
  251. } else {
  252. // 超值热卖
  253. let hotSaleCell = ShoppingCartHotSaleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
  254. hotSaleCell.frame = tableView.bounds
  255. hotSaleCell.hotSaleModelArr = hotSaleModelArr
  256. hotSaleCell.layoutIfNeeded()
  257. hotSaleCell.reloadData()
  258. hotSaleCell.refreshBlock = {
  259. [weak self] in
  260. if let refreshTransBlock = self?.refreshTransBlock {
  261. refreshTransBlock()
  262. }
  263. }
  264. hotSaleCell.jumpToDetailClosure = {
  265. [weak self] (proId, shopId) in
  266. if let jumpToDetailTransClosure = self?.jumpToDetailTransClosure {
  267. jumpToDetailTransClosure(proId, shopId)
  268. }
  269. }
  270. return hotSaleCell
  271. }
  272. }
  273. }
  274. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  275. if cartListModelArr?.isEmpty ?? true {
  276. switch indexPath.section {
  277. case 0:
  278. // 购物车列表为空
  279. return 302
  280. case 1:
  281. // 超值热卖
  282. return self.hotSaleHeight
  283. default:
  284. return UITableView.automaticDimension
  285. }
  286. } else {
  287. if indexPath.section < cartListModelArr!.count {
  288. // 购物车列表Item
  289. return 148
  290. } else {
  291. // 超值热卖
  292. return self.hotSaleHeight
  293. }
  294. }
  295. }
  296. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  297. if !(cartListModelArr?.isEmpty ?? true) {
  298. //跳转商品详情
  299. if indexPath.section < cartListModelArr!.count {
  300. let productMdl = (cartListModelArr?[indexPath.section].productList?[indexPath.row])!
  301. if let productDidSelectRowAt = productDidSelectRowAt {
  302. productDidSelectRowAt(productMdl)
  303. }
  304. }
  305. }
  306. }
  307. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  308. if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
  309. return 58
  310. } else {
  311. return 10
  312. }
  313. }
  314. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  315. if cartListModelArr?.count ?? 0 > 0 && section < cartListModelArr!.count {
  316. let headerView = ShoppingCartListTableViewHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 58))
  317. headerView.shopName = cartListModelArr![section].shopName
  318. headerView.isShopDisabled = judgeSectionAllDisabled(section)
  319. headerView.isSectionSelected = self.judgeSectionSelectedStatus(section)
  320. headerView.secSelectBlock = { [weak self]
  321. (isSectionSel) in
  322. let proListMdl: CartProductListModel = (self?.cartListModelArr![section])!
  323. self?.productCartIsSelApi(shop_id: proListMdl.shopId!, isSel: isSectionSel, section: section)
  324. }
  325. headerView.shopClickedBlock = {
  326. [weak self] in
  327. if let shopClickedTransBlock = self?.shopClickedTransBlock {
  328. shopClickedTransBlock(self!.cartListModelArr![section].shopId ?? 0)
  329. }
  330. }
  331. return headerView
  332. } else {
  333. return nil
  334. }
  335. }
  336. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  337. return 0.000001
  338. }
  339. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  340. return nil
  341. }
  342. func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  343. // 非商品Item拦截滑动删除
  344. if cartListModelArr?.isEmpty ?? true {
  345. return false
  346. }
  347. if indexPath.section == cartListModelArr!.count {
  348. return false
  349. }
  350. return true
  351. }
  352. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  353. if editingStyle == .delete {
  354. // 删除商品
  355. self.deleteProduct(indexPath)
  356. }
  357. }
  358. func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
  359. return "删除"
  360. }
  361. }
  362. // MARK: - 购物车逻辑处理
  363. extension ShoppingCartView {
  364. // 全选点击事件
  365. func allSelectedAction(_ isSelected: Int) {
  366. for cartProListMdl in cartListModelArr ?? [] {
  367. for productMdl in cartProListMdl.productList! {
  368. // 已售罄/下架商品不改变选中状态
  369. if productMdl.upStatus != 0 && productMdl.stock != 0 {
  370. productMdl.isSelect = isSelected
  371. }
  372. }
  373. }
  374. tableView.reloadData()
  375. }
  376. // 店铺(全选)点击事件
  377. func shopSelectedAction(_ isSelected: Int, section: Int) {
  378. let cartProListMdl: CartProductListModel = cartListModelArr![section]
  379. for productMdl in cartProListMdl.productList! {
  380. // 已售罄/下架商品不改变选中状态
  381. if productMdl.upStatus != 0 && productMdl.stock != 0 {
  382. productMdl.isSelect = isSelected
  383. }
  384. }
  385. self.tableView.reloadSections([section], with: UITableView.RowAnimation.none)
  386. self.judgeAllSelectedStatus()
  387. }
  388. // 校验Section内商品是否全选
  389. func judgeSectionSelectedStatus(_ section: Int) -> Int {
  390. let cartProListMdl: CartProductListModel = cartListModelArr![section]
  391. for productMdl in cartProListMdl.productList! {
  392. // 忽略已售罄/下架商品选中状态
  393. if productMdl.upStatus == 0 {
  394. continue
  395. }
  396. if productMdl.stock == 0 {
  397. continue
  398. }
  399. if productMdl.isSelect == 0 {
  400. return 0
  401. }
  402. }
  403. return 1
  404. }
  405. // 校验购物车内商品是否全选
  406. func judgeAllSelectedStatus() -> Void {
  407. let secCount: Int = cartListModelArr?.isEmpty ?? true ? 0 : cartListModelArr!.count
  408. for sec in 0..<secCount {
  409. let secStatus: Int = self.judgeSectionSelectedStatus(sec)
  410. if secStatus == 0 {
  411. self.accountView.isAllSelected = 0
  412. return
  413. }
  414. }
  415. self.accountView.isAllSelected = 1
  416. }
  417. // 计算结算数据,刷新结算View
  418. func refreshAccountView() {
  419. totalPrice = 0
  420. totalNum = 0
  421. selModelArr = []
  422. // 深拷贝数组
  423. for listModel in cartListModelArr! {
  424. let copyListMdl: CartProductListModel = listModel.copy() as! CartProductListModel
  425. selModelArr.append(copyListMdl)
  426. }
  427. for (secIdx, cartProListMdl) in (cartListModelArr?.enumerated())! {
  428. var subTag: Int = 0
  429. for (rowIdx, productMdl) in cartProListMdl.productList!.enumerated() {
  430. if productMdl.isSelect == 1 {
  431. totalPrice += productMdl.skuPrice! * productMdl.amount!
  432. totalNum += 1
  433. } else {
  434. if !cartListModelArr![secIdx].productList!.isEmpty {
  435. selModelArr[secIdx].productList!.remove(at: rowIdx-subTag)
  436. subTag += 1
  437. }
  438. }
  439. }
  440. }
  441. self.accountView.tPrice = totalPrice
  442. self.accountView.tNumber = totalNum
  443. }
  444. // 选中单个商品
  445. func productCartIsSelApi(pro_id:Int = 0, isSel:Int, indexPath:IndexPath) {
  446. SwiftMoyaNetWorkServiceProduct.shared().productCartIsSelApi(id: pro_id, shop_id: 0, isSelect: isSel) { (data) -> (Void) in
  447. self.cartListModelArr![indexPath.section].productList![indexPath.row].isSelect = isSel
  448. self.tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
  449. self.judgeAllSelectedStatus()
  450. self.refreshAccountView()
  451. }
  452. }
  453. // 选中section内商品
  454. func productCartIsSelApi(shop_id:Int = 0, isSel:Int, section:Int) {
  455. SwiftMoyaNetWorkServiceProduct.shared().productCartIsSelApi(id: 0, shop_id: shop_id, isSelect: isSel) { (data) -> (Void) in
  456. self.shopSelectedAction(isSel, section: section)
  457. self.judgeAllSelectedStatus()
  458. self.refreshAccountView()
  459. }
  460. }
  461. // 全选商品
  462. func productCartAllSelApi(isSel:Int) {
  463. SwiftMoyaNetWorkServiceProduct.shared().productCartAllSelApi(isSelect: isSel) { (data) -> (Void) in
  464. self.allSelectedAction(isSel)
  465. self.refreshAccountView()
  466. }
  467. }
  468. // 校验Section内商品是否全不可用(售罄/下架)
  469. func judgeSectionAllDisabled(_ section: Int) -> Bool {
  470. let cartProListMdl: CartProductListModel = cartListModelArr![section]
  471. for productMdl in cartProListMdl.productList! {
  472. if productMdl.stock != 0 && productMdl.upStatus != 0 {
  473. return false
  474. }
  475. }
  476. return true
  477. }
  478. // 删除商品条目
  479. func deleteProduct(_ indexPath: IndexPath) {
  480. let productId = cartListModelArr![indexPath.section].productList?[indexPath.row].id
  481. SwiftMoyaNetWorkServiceProduct.shared().productCartDeleteApi(id: productId ?? 0) { [weak self] (data) -> (Void) in
  482. self?.cartListModelArr![indexPath.section].productList?.remove(at: indexPath.row)
  483. self?.tableView.deleteRows(at: [indexPath], with: .none)
  484. self?.refreshAccountView()
  485. if self?.cartListModelArr![indexPath.section].productList?.isEmpty ?? true {
  486. // 删除后商店为空,刷新table
  487. self?.cartListModelArr?.remove(at: indexPath.section)
  488. self?.tableView.reloadData()
  489. } else {
  490. // 刷新删除行所在的section(防止索引越界)
  491. self?.tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
  492. }
  493. }
  494. }
  495. }