123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- //
- // ShoppingCartView.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/5/8.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 购物车--首页View
- import UIKit
- class ShoppingCartView: BaseView {
-
- typealias JumpToDetailTransClosure = (_ proId: Int,_ shopId : Int) -> Void
- var jumpToDetailTransClosure : JumpToDetailTransClosure?
-
- typealias ProductDidSelectRowAt = (ProductModel) -> Void
- var productDidSelectRowAt : ProductDidSelectRowAt?
-
- var shoppingCartVCType : ShoppingCartVCType = .none {
- didSet{
- if shoppingCartVCType == .push {
- accountView.snp.makeConstraints { (make) in
- make.left.right.equalToSuperview()
- make.height.equalTo(48)
- make.bottom.equalToSuperview().offset(-kSafeTabBarHeight)
- }
- tableView.snp.makeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.bottom.equalTo(accountView.snp_top).offset(0)
- }
- }else {
- accountView.snp.makeConstraints { (make) in
- make.left.right.equalToSuperview()
- make.height.equalTo(48)
- make.bottom.equalToSuperview()
- }
- tableView.snp.makeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.bottom.equalTo(accountView.snp_top).offset(0)
- }
- }
- }
- }
-
- // 购物车列表ModelArr
- var cartListModelArr : Array<CartProductListModel>? {
- didSet {
- self.judgeAllSelectedStatus()
- self.refreshAccountView()
- self.updateAccountViewCondition()
- tableView.reloadData()
- }
- }
-
- // 热销ModelArr
- var hotSaleModelArr : Array<ProductSearchModel>? {
- didSet {
- // 预计算热销Cell行高
- let count = self.hotSaleModelArr?.count ?? 0
- let rows = count / 2 + count % 2
- let cSpace = 10 * (rows - 1) + 30
- let collectionH: CGFloat = (170 * kScaleWidth + 80) * CGFloat(rows) + CGFloat(cSpace)
- self.hotSaleHeight = collectionH + 39
-
- tableView.reloadData()
- }
- }
-
- // 热销Cell行高
- private var hotSaleHeight: CGFloat = 0
-
- // 已选商品总价
- private var totalPrice: Int = 0
- // 已选商品件数
- private var totalNum: Int = 0
- // 已选商品ModelArr
- private var selModelArr: Array<CartProductListModel> = []
-
- // 订单支付--次级block
- typealias OrderPayTransBlock = (_ selMdlArr: Array<CartProductListModel>, _ totalPrice: Int) -> Void
- var orderPayTransBlock : OrderPayTransBlock?
-
- // 刷新列表(数据)--次级block
- typealias RefreshTransBlock = () -> Void
- var refreshTransBlock : RefreshTransBlock?
-
- // 点击店铺跳转--次级block
- typealias ShopClickedTransBlock = (_ shopId: Int) -> Void
- var shopClickedTransBlock : ShopClickedTransBlock?
-
- override func setupViews() {
- self.backgroundColor = kf7f8faColor
- addSubview(accountView)
- addSubview(tableView)
- let emptyView = DIYEmptyView.empty(with: kImage(name: "default_page_data"), titleStr: nil, detailStr: "当前暂无数据")
- emptyView!.contentViewY = kScaleValue(value: 182)
- tableView.ly_emptyView = emptyView
- tableView.ly_startLoading()
-
- self.updateAccountViewCondition()
- }
-
-
- override func setupLayouts() {
-
- if shoppingCartVCType == .push {
- accountView.snp.makeConstraints { (make) in
- make.left.right.equalToSuperview()
- make.height.equalTo(48)
- make.bottom.equalToSuperview().offset(-kSafeTabBarHeight)
- }
- tableView.snp.makeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.bottom.equalTo(accountView.snp_top).offset(0)
- }
- }else {
- accountView.snp.makeConstraints { (make) in
- make.left.right.equalToSuperview()
- make.height.equalTo(48)
- make.bottom.equalToSuperview()
- }
- tableView.snp.makeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.bottom.equalTo(accountView.snp_top).offset(0)
- }
- }
- }
-
- // 处理购物车结算是否显示
- func updateAccountViewCondition() {
-
- if cartListModelArr?.isEmpty ?? true {
- accountView.isHidden = true
-
- accountView.snp.remakeConstraints { (make) in
- make.left.right.equalToSuperview()
- make.height.equalTo(0)
- if self.shoppingCartVCType == .push {
- make.bottom.equalToSuperview().offset(-kSafeTabBarHeight)
- }else {
- make.bottom.equalToSuperview()
- }
- }
- tableView.snp.remakeConstraints { (make) in
- make.top.right.left.equalToSuperview()
- make.bottom.equalTo(accountView.snp_top)
- }
- } else {
- accountView.isHidden = false
-
- accountView.snp.remakeConstraints { (make) in
- make.left.right.equalToSuperview()
- make.height.equalTo(48)
- if self.shoppingCartVCType == .push {
- make.bottom.equalToSuperview().offset(-kSafeTabBarHeight)
- }else {
- make.bottom.equalToSuperview()
- }
- }
- tableView.snp.remakeConstraints { (make) in
- make.top.right.left.equalToSuperview()
- make.bottom.equalTo(accountView.snp_top)
- }
- }
- }
-
- lazy var accountView: ShoppingCartAccountView = {
- let accountView = ShoppingCartAccountView()
-
- accountView.allSelectBlock = { [weak self]
- (isAllSelected) in
- self?.productCartAllSelApi(isSel: isAllSelected)
- }
-
- 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 = self.hotSaleHeight
- 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.hotSaleModelArr = hotSaleModelArr
- hotSaleCell.layoutIfNeeded()
- hotSaleCell.reloadData()
- hotSaleCell.refreshBlock = {
- [weak self] in
- if let refreshTransBlock = self?.refreshTransBlock {
- refreshTransBlock()
- }
- }
- hotSaleCell.jumpToDetailClosure = {
- [weak self] (proId, shopId) in
- if let jumpToDetailTransClosure = self?.jumpToDetailTransClosure {
- jumpToDetailTransClosure(proId, shopId)
- }
- }
- 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
- let productMdl: ProductModel = (self?.cartListModelArr![indexPath.section].productList![indexPath.row])!
- self?.productCartIsSelApi(pro_id: productMdl.id!, isSel: isProductSel, indexPath: indexPath)
- }
-
- cell.changeProductBlock = { [weak self]
- (productId, type) in
- // 最少只能减到1件商品
- let curAmount = self?.cartListModelArr![indexPath.section].productList![indexPath.row].amount
- if type == 2 && curAmount == 1 {
- AlertSheetView.alert(title: "确认删除商品", cancelTitle: "取消", sureTitle: "确认", cancelBlock: { (popupView, index, string) in
-
- }, confirmBlock: {
- [weak self] (popupView, index, string) in
- // 删除商品
- self?.deleteProduct(indexPath)
- })
-
- return
- }
-
- // 更改数量Api
- 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及合计View
- self?.refreshAccountView()
- UIView.performWithoutAnimation {
- tableView.reloadData()
- }
- })
- }
- return cell
-
- } else {
- // 超值热卖
- let hotSaleCell = ShoppingCartHotSaleTableViewCell.cellWith(tableView: tableView, indexPath: indexPath)
- hotSaleCell.frame = tableView.bounds
- hotSaleCell.hotSaleModelArr = hotSaleModelArr
- hotSaleCell.layoutIfNeeded()
- hotSaleCell.reloadData()
- hotSaleCell.refreshBlock = {
- [weak self] in
- if let refreshTransBlock = self?.refreshTransBlock {
- refreshTransBlock()
- }
- }
- hotSaleCell.jumpToDetailClosure = {
- [weak self] (proId, shopId) in
- if let jumpToDetailTransClosure = self?.jumpToDetailTransClosure {
- jumpToDetailTransClosure(proId, shopId)
- }
- }
- return hotSaleCell
- }
- }
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- if cartListModelArr?.isEmpty ?? true {
- switch indexPath.section {
- case 0:
- // 购物车列表为空
- return 302
- case 1:
- // 超值热卖
- return self.hotSaleHeight
- default:
- return UITableView.automaticDimension
- }
-
- } else {
- if indexPath.section < cartListModelArr!.count {
- // 购物车列表Item
- return 148
- } else {
- // 超值热卖
- return self.hotSaleHeight
- }
- }
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- if !(cartListModelArr?.isEmpty ?? true) {
- //跳转商品详情
- if indexPath.section < cartListModelArr!.count {
- let productMdl = (cartListModelArr?[indexPath.section].productList?[indexPath.row])!
-
- if let productDidSelectRowAt = productDidSelectRowAt {
- productDidSelectRowAt(productMdl)
- }
- }
- }
- }
-
- 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.isShopDisabled = judgeSectionAllDisabled(section)
- headerView.isSectionSelected = self.judgeSectionSelectedStatus(section)
- headerView.secSelectBlock = { [weak self]
- (isSectionSel) in
- let proListMdl: CartProductListModel = (self?.cartListModelArr![section])!
- self?.productCartIsSelApi(shop_id: proListMdl.shopId!, isSel: isSectionSel, section: section)
- }
- headerView.shopClickedBlock = {
- [weak self] in
- if let shopClickedTransBlock = self?.shopClickedTransBlock {
- shopClickedTransBlock(self!.cartListModelArr![section].shopId ?? 0)
- }
- }
- 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, canEditRowAt indexPath: IndexPath) -> Bool {
- // 非商品Item拦截滑动删除
- if cartListModelArr?.isEmpty ?? true {
- return false
- }
- if indexPath.section == cartListModelArr!.count {
- return false
- }
-
- return true
- }
-
- func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
- if editingStyle == .delete {
- // 删除商品
- self.deleteProduct(indexPath)
- }
- }
-
- 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! {
- // 已售罄/下架商品不改变选中状态
- if productMdl.upStatus != 0 && productMdl.stock != 0 {
- productMdl.isSelect = isSelected
- }
- }
- }
- tableView.reloadData()
- }
-
- // 店铺(全选)点击事件
- func shopSelectedAction(_ isSelected: Int, section: Int) {
- let cartProListMdl: CartProductListModel = cartListModelArr![section]
- for productMdl in cartProListMdl.productList! {
- // 已售罄/下架商品不改变选中状态
- if productMdl.upStatus != 0 && productMdl.stock != 0 {
- 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.upStatus == 0 {
- continue
- }
- if productMdl.stock == 0 {
- continue
- }
-
- 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! * productMdl.amount!
- 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
- }
-
- // 选中单个商品
- func productCartIsSelApi(pro_id:Int = 0, isSel:Int, indexPath:IndexPath) {
- SwiftMoyaNetWorkServiceProduct.shared().productCartIsSelApi(id: pro_id, shop_id: 0, isSelect: isSel) { (data) -> (Void) in
- self.cartListModelArr![indexPath.section].productList![indexPath.row].isSelect = isSel
-
- self.tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
- self.judgeAllSelectedStatus()
- self.refreshAccountView()
- }
- }
-
- // 选中section内商品
- func productCartIsSelApi(shop_id:Int = 0, isSel:Int, section:Int) {
- SwiftMoyaNetWorkServiceProduct.shared().productCartIsSelApi(id: 0, shop_id: shop_id, isSelect: isSel) { (data) -> (Void) in
- self.shopSelectedAction(isSel, section: section)
- self.judgeAllSelectedStatus()
- self.refreshAccountView()
- }
- }
-
- // 全选商品
- func productCartAllSelApi(isSel:Int) {
- SwiftMoyaNetWorkServiceProduct.shared().productCartAllSelApi(isSelect: isSel) { (data) -> (Void) in
- self.allSelectedAction(isSel)
- self.refreshAccountView()
- }
- }
-
- // 校验Section内商品是否全不可用(售罄/下架)
- func judgeSectionAllDisabled(_ section: Int) -> Bool {
- let cartProListMdl: CartProductListModel = cartListModelArr![section]
- for productMdl in cartProListMdl.productList! {
- if productMdl.stock != 0 && productMdl.upStatus != 0 {
- return false
- }
- }
- return true
- }
-
- // 删除商品条目
- func deleteProduct(_ indexPath: IndexPath) {
- 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)
- self?.tableView.deleteRows(at: [indexPath], with: .none)
- self?.refreshAccountView()
-
- if self?.cartListModelArr![indexPath.section].productList?.isEmpty ?? true {
- // 删除后商店为空,刷新table
- self?.cartListModelArr?.remove(at: indexPath.section)
- self?.tableView.reloadData()
- } else {
- // 刷新删除行所在的section(防止索引越界)
- self?.tableView.reloadSections([indexPath.section], with: UITableView.RowAnimation.none)
- }
- }
- }
-
- }
|