123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- //
- // ShoppingCartOrderPayView.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/5/9.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- // 购物车--订单支付View
- import UIKit
- class ShoppingCartOrderPayView: BaseView {
-
- typealias CommitOrderTransBlock = (_ paraModel: OrderCreateParameterModel) -> Void
- var commitOrderTransBlock : CommitOrderTransBlock?
-
- typealias JumpNavBlock = (_ jumpType: WillJumpType) -> Void
- var jumpNavBlock : JumpNavBlock?
-
- // 全部已选总价(元)
- var totalProductPrice: Int = 0 {
- didSet {
- self.accountView.tPrice = totalProductPrice
- }
- }
-
- // 已选商品ModelArr
- var proListModelArr : Array<CartProductListModel>? {
- didSet {
- if proListModelArr != nil {
- self.proListModelArr = self.fixOriginalPayArray()
- tableView.reloadData()
- accountView.isHidden = false
- }else {
- accountView.isHidden = true
- }
-
- }
- }
-
- // 配送方式
- var deliverType: String? {
- didSet {
- if self.deliverType == "1" {
- let addr: SelfMentionAddressModel = SelfMentionAddressModel.getModel()!
- selfAddressLabel.text = "\(addr.address ?? "")"
- } else {
- selfAddressView.isHidden = true
- tableView.snp.remakeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.bottom.equalTo(accountView.snp_top)
- }
- }
- }
- }
-
- // 快递地址信息Mdl
- var expressAddressMdl: ExpressAddresModel? {
- didSet {
- if expressAddressMdl != nil && expressAddressMdl?.id != nil {
- tableView.reloadData()
- }
- }
- }
-
- // 自提联系人信息Mdl
- var selfAddressInfoMdl: SelfMentionContactsModel? {
- didSet {
- if selfAddressInfoMdl != nil {
- tableView.reloadData()
- }
- }
- }
-
- // 自提地址信息Mdl
- var selfAddrMdl: SelfAddresModel? {
- didSet {
- if selfAddrMdl != nil {
- tableView.reloadData()
- }
- }
- }
-
- override func setupViews() {
- self.backgroundColor = kf7f8faColor
- addSubview(accountView)
- addSubview(selfAddressView)
- selfAddressView.addSubview(selfAddressLabel)
- 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.equalToSuperview()
- make.bottom.equalTo(-kSafeTabBarHeight)
- make.height.equalTo(48)
- }
-
- selfAddressView.snp.makeConstraints { (make) in
- make.bottom.equalTo(accountView.snp_top)
- make.left.right.equalToSuperview()
- }
-
- selfAddressLabel.snp.remakeConstraints { (make) in
- make.left.equalToSuperview().offset(14)
- make.right.equalToSuperview().offset(-26)
- make.top.bottom.equalToSuperview()
- make.height.equalTo(40)
- }
-
- tableView.snp.makeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.bottom.equalTo(selfAddressView.snp_top)
- }
- }
-
- lazy var accountView: OrderPayAcountView = {
- let accountView = OrderPayAcountView()
- accountView.commitOrderBlock = {
- [weak self] in
- let paraMdl = OrderCreateParameterModel()
- paraMdl.money = self!.totalProductPrice
- paraMdl.selShopListArr = self?.proListModelArr
- switch self?.deliverType {
- case "1":
- // 自提
- paraMdl.name = self?.selfAddressInfoMdl?.name
- paraMdl.mobile = Int(self?.selfAddressInfoMdl?.mobile ?? "")
- paraMdl.address = "\(self?.selfAddrMdl?.address ?? "")"
- paraMdl.pickNodeId = self?.selfAddrMdl?.pickupNodeId
- paraMdl.pickNodeContact = self?.selfAddrMdl?.managerName
- case "2":
- // 快递
- paraMdl.name = self?.expressAddressMdl?.contactName
- paraMdl.mobile = Int(self?.expressAddressMdl?.contactMobile ?? "")
- paraMdl.address = self?.expressAddressMdl?.address
- paraMdl.pickNodeId = 0
- paraMdl.pickNodeContact = ""
- default:
- return
- }
-
- if let commitOrderTransBlock = self?.commitOrderTransBlock {
- commitOrderTransBlock(paraMdl)
- }
-
- }
- return accountView
- }()
-
- private lazy var selfAddressView: UIView = {
- let selfAddressView = UIView()
- selfAddressView.backgroundColor = kfff8efColor
- return selfAddressView
- }()
-
- private lazy var selfAddressLabel: UILabel = {
- let selfAddressLabel = UILabel()
- selfAddressLabel.textColor = kFFA42FColor
- selfAddressLabel.font = kRegularFont13
- selfAddressLabel.textAlignment = .left
- selfAddressLabel.numberOfLines = 0
- return selfAddressLabel
- }()
-
- 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
- tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
- return tableView
- }()
-
- }
- // MARK: - tableView dataSource && delegate
- extension ShoppingCartOrderPayView : UITableViewDelegate, UITableViewDataSource {
- func numberOfSections(in tableView: UITableView) -> Int {
-
- return proListModelArr?.isEmpty ?? true ? 0 : (proListModelArr?.count ?? 0) + 1
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- if section == 0 {
- switch deliverType {
- case "1":
- // 自提
- return 2
- case "2":
- // 快递
- return 1
- default:
- return 1
- }
- } else {
- return proListModelArr![section-1].productList?.isEmpty ?? true ? 0 : proListModelArr![section-1].productList?.count ?? 0
- }
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- if indexPath.section == 0 {
- switch deliverType {
- case "1":
- // 自提
- if selfAddressInfoMdl?.id == nil {
- if indexPath.row == 0 {
- // 自提添加收货人信息
- let cell = OrderPaySelfPickAddInfoCell.cellWith(tableView: tableView, indexPath: indexPath)
- return cell
- } else {
- // 自提地址信息
- let cell = OrderPaySelfPickAddressCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.selfAddressMdl = selfAddrMdl
- return cell
- }
- } else {
- if indexPath.row == 0 {
- // 自提个人信息
- let cell = OrderPaySelfPickInfoCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.contactMdl = selfAddressInfoMdl
- return cell
- } else {
- // 自提地址信息
- let cell = OrderPaySelfPickAddressCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.selfAddressMdl = selfAddrMdl
- return cell
- }
- }
- case "2":
- // 快递
- if expressAddressMdl == nil {
- // 快递添加地址
- let expressCell = OrderPayExpressAddInfoCell.cellWith(tableView: tableView, indexPath: indexPath)
- return expressCell
- } else {
- // 快递信息管理
- let expressCell = OrderPayExpressInfoShowCell.cellWith(tableView: tableView, indexPath: indexPath)
- expressCell.addressMdl = expressAddressMdl
- return expressCell
- }
- default:
- return UITableViewCell()
- }
-
- } else {
- // 购物车列表Item
- let cell = ShoppingCartPayOrderItemCell.cellWith(tableView: tableView, indexPath: indexPath)
- cell.productMdl = proListModelArr![indexPath.section-1].productList![indexPath.row]
- return cell
- }
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return UITableView.automaticDimension
- }
-
- func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
- if section == 0 {
- return 10
- } else {
- return 58
- }
- }
-
- func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
- if section == 0 {
- return nil
- } else {
- let headerView = ShoppingCartPayOrderHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 58))
- headerView.shopName = proListModelArr![section-1].shopName
- return headerView
-
- }
- }
-
- func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
- if section == 0 {
- return 0.000001
- } else {
- return 88
- }
- }
-
- func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
- if section == 0 {
- return nil
- } else {
- let footerView = ShoppingCartPayOrderFooter(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 88))
-
- let cartProListMdl: CartProductListModel = proListModelArr![section-1]
- let secNum: Int = cartProListMdl.productList!.count
-
- footerView.tPrice = self.calculateSectionPrice(section-1)
- footerView.tNumber = secNum
-
- footerView.buyerNoteBlock = { [weak self]
- (buyerNotes) in
- self?.proListModelArr![section-1].buyerNotes = buyerNotes
- }
-
- return footerView
- }
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- if indexPath.section != 0 {
- return
- }
-
- switch deliverType {
- case "1":
- // 自提
- if selfAddressInfoMdl?.id == nil {
- if indexPath.row == 0 {
- if let jumpNavBlock = self.jumpNavBlock {
- jumpNavBlock(WillJumpType.selfAddInfo)
- }
- } else {
- if let jumpNavBlock = self.jumpNavBlock {
- jumpNavBlock(WillJumpType.selfAddressInfo)
- }
- }
- } else {
- if indexPath.row == 0 {
- if let jumpNavBlock = self.jumpNavBlock {
- jumpNavBlock(WillJumpType.selfPersonalInfo)
- }
- } else {
- if let jumpNavBlock = self.jumpNavBlock {
- jumpNavBlock(WillJumpType.selfAddressInfo)
- }
- }
- }
- case "2":
- // 快递
- if expressAddressMdl?.id == nil {
- if let jumpNavBlock = self.jumpNavBlock {
- jumpNavBlock(WillJumpType.expressAddInfo)
- }
- } else {
- if let jumpNavBlock = self.jumpNavBlock {
- jumpNavBlock(WillJumpType.expressManageInfo)
- }
- }
- default:
- return
- }
- }
-
- }
- // MARK: - 购物车计算
- extension ShoppingCartOrderPayView {
-
- // 剔除section下商品全为下架/售罄的商家
- func fixOriginalPayArray() -> Array<CartProductListModel> {
- var fixedListMdlArr: Array<CartProductListModel> = []
- for (secIdx, proListMdl) in (proListModelArr?.enumerated())! {
- var shopAllDisabled: Bool = true
- for productMdl in proListMdl.productList ?? [] {
- if productMdl.upStatus != 0 && productMdl.stock != 0 {
- shopAllDisabled = false
- break
- }
- }
- if shopAllDisabled == false {
- fixedListMdlArr.append(proListModelArr![secIdx])
- }
- }
- return fixedListMdlArr
- }
-
- // 计算Section数据,刷新结算View
- func calculateSectionPrice(_ section: Int) -> Int {
- var totalPrice: Int = 0
-
- let cartProListMdl: CartProductListModel = proListModelArr![section]
- for productMdl in cartProListMdl.productList! {
- totalPrice += ((productMdl.skuPrice ?? 0) * (productMdl.amount ?? 0))
- }
- return totalPrice
- }
-
- }
|