ShoppingCartOrderPayView.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // ShoppingCartOrderPayView.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/5/9.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. // 购物车--订单支付View
  8. import UIKit
  9. class ShoppingCartOrderPayView: BaseView {
  10. typealias CommitOrderTransBlock = () -> Void
  11. var commitOrderTransBlock : CommitOrderTransBlock?
  12. typealias JumpNavBlock = (_ jumpType: WillJumpType) -> Void
  13. var jumpNavBlock : JumpNavBlock?
  14. // 全部已选总价
  15. var totalProductPrice: Int = 0 {
  16. didSet {
  17. self.accountView.tPrice = totalProductPrice
  18. }
  19. }
  20. // 已选商品ModelArr
  21. var proListModelArr : Array<CartProductListModel>? {
  22. didSet {
  23. tableView.reloadData()
  24. }
  25. }
  26. // 全部已选总价
  27. var deliverType: String? {
  28. didSet {
  29. }
  30. }
  31. // 快递地址信息Mdl
  32. var expressAddressMdl: ExpressAddresModel? {
  33. didSet {
  34. self.tableView.reloadSections([0], with: UITableView.RowAnimation.none)
  35. }
  36. }
  37. // 自提地址信息Mdl
  38. var selfAddressArrMdl: Array<SelfAddresModel>? {
  39. didSet {
  40. self.tableView.reloadSections([0], with: UITableView.RowAnimation.none)
  41. }
  42. }
  43. override func setupViews() {
  44. self.backgroundColor = kf7f8faColor
  45. addSubview(accountView)
  46. addSubview(tableView)
  47. let emptyView = EmptyView.shared.diyCustomEmptyViewStyle2(iconStr: "page04", titleStr: "当前暂无数据")
  48. emptyView.contentViewY = kScaleValue(value: 182)
  49. tableView.ly_emptyView = emptyView
  50. tableView.ly_startLoading()
  51. }
  52. override func setupLayouts() {
  53. accountView.snp.makeConstraints { (make) in
  54. make.left.right.equalToSuperview()
  55. make.bottom.equalTo(-kSafeTabBarHeight)
  56. make.height.equalTo(48)
  57. }
  58. tableView.snp.makeConstraints { (make) in
  59. make.edges.equalToSuperview()
  60. make.bottom.equalTo(accountView.snp_top).offset(-20)
  61. }
  62. }
  63. lazy var accountView: OrderPayAcountView = {
  64. let accountView = OrderPayAcountView()
  65. accountView.commitOrderBlock = {
  66. [weak self] in
  67. if let commitOrderTransBlock = self?.commitOrderTransBlock {
  68. commitOrderTransBlock()
  69. }
  70. }
  71. return accountView
  72. }()
  73. lazy var tableView: UITableView = {
  74. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  75. tableView.separatorStyle = .none
  76. tableView.backgroundColor = kf7f8faColor
  77. tableView.dataSource = self
  78. tableView.delegate = self
  79. tableView.estimatedRowHeight = 0.000001
  80. tableView.estimatedSectionFooterHeight = 0.000001
  81. tableView.estimatedSectionHeaderHeight = 0.000001
  82. return tableView
  83. }()
  84. }
  85. // MARK: - tableView dataSource && delegate
  86. extension ShoppingCartOrderPayView : UITableViewDelegate, UITableViewDataSource {
  87. func numberOfSections(in tableView: UITableView) -> Int {
  88. return proListModelArr!.count + 1
  89. }
  90. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  91. if section == 0 {
  92. switch deliverType {
  93. case "1":
  94. // 自提
  95. return 2
  96. case "2":
  97. // 快递
  98. return 1
  99. default:
  100. return 1
  101. }
  102. } else {
  103. return proListModelArr![section-1].productList!.count
  104. }
  105. }
  106. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  107. if indexPath.section == 0 {
  108. switch deliverType {
  109. case "1":
  110. // 自提
  111. if selfAddressArrMdl?.isEmpty ?? true {
  112. if indexPath.row == 0 {
  113. // 自提添加收货人信息
  114. let cell = OrderPaySelfPickAddInfoCell.cellWith(tableView: tableView, indexPath: indexPath)
  115. return cell
  116. } else {
  117. // 自提地址信息
  118. let cell = OrderPaySelfPickAddressCell.cellWith(tableView: tableView, indexPath: indexPath)
  119. return cell
  120. }
  121. } else {
  122. if indexPath.row == 0 {
  123. // 自提个人信息
  124. let cell = OrderPaySelfPickInfoCell.cellWith(tableView: tableView, indexPath: indexPath)
  125. return cell
  126. } else {
  127. // 自提地址信息
  128. let cell = OrderPaySelfPickAddressCell.cellWith(tableView: tableView, indexPath: indexPath)
  129. return cell
  130. }
  131. }
  132. case "2":
  133. // 快递
  134. if expressAddressMdl == nil {
  135. // 快递添加地址
  136. let expressCell = OrderPayExpressAddInfoCell.cellWith(tableView: tableView, indexPath: indexPath)
  137. return expressCell
  138. } else {
  139. // 快递信息管理
  140. let expressCell = OrderPayExpressInfoShowCell.cellWith(tableView: tableView, indexPath: indexPath)
  141. expressCell.addressMdl = expressAddressMdl
  142. return expressCell
  143. }
  144. default:
  145. return UITableViewCell()
  146. }
  147. } else {
  148. // 购物车列表Item
  149. let cell = ShoppingCartPayOrderItemCell.cellWith(tableView: tableView, indexPath: indexPath)
  150. cell.productMdl = proListModelArr![indexPath.section-1].productList![indexPath.row]
  151. return cell
  152. }
  153. }
  154. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  155. return UITableView.automaticDimension
  156. }
  157. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  158. if section == 0 {
  159. return 10
  160. } else {
  161. return 58
  162. }
  163. }
  164. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  165. if section == 0 {
  166. return nil
  167. } else {
  168. let headerView = ShoppingCartPayOrderHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 58))
  169. headerView.shopName = proListModelArr![section-1].shopName
  170. return headerView
  171. }
  172. }
  173. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  174. if section == 0 {
  175. return 0.000001
  176. } else {
  177. return 88
  178. }
  179. }
  180. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  181. if section == 0 {
  182. return nil
  183. } else {
  184. let footerView = ShoppingCartPayOrderFooter(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 88))
  185. let cartProListMdl: CartProductListModel = proListModelArr![section-1]
  186. let secNum: Int = cartProListMdl.productList!.count
  187. footerView.tPrice = self.calculateSectionPrice(section-1)
  188. footerView.tNumber = secNum
  189. footerView.buyerNoteBlock = { [weak self]
  190. (buyerNotes) in
  191. self?.proListModelArr![section-1].buyerNotes = buyerNotes
  192. }
  193. return footerView
  194. }
  195. }
  196. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  197. if indexPath.section != 0 {
  198. return
  199. }
  200. switch deliverType {
  201. case "1":
  202. // 自提
  203. if selfAddressArrMdl?.isEmpty ?? true {
  204. if indexPath.row == 0 {
  205. if let jumpNavBlock = self.jumpNavBlock {
  206. jumpNavBlock(WillJumpType.selfAddInfo)
  207. }
  208. } else {
  209. if let jumpNavBlock = self.jumpNavBlock {
  210. jumpNavBlock(WillJumpType.selfAddressInfo)
  211. }
  212. }
  213. } else {
  214. if indexPath.row == 0 {
  215. if let jumpNavBlock = self.jumpNavBlock {
  216. jumpNavBlock(WillJumpType.selfPersonalInfo)
  217. }
  218. } else {
  219. if let jumpNavBlock = self.jumpNavBlock {
  220. jumpNavBlock(WillJumpType.selfAddressInfo)
  221. }
  222. }
  223. }
  224. case "2":
  225. // 快递
  226. if expressAddressMdl == nil {
  227. if let jumpNavBlock = self.jumpNavBlock {
  228. jumpNavBlock(WillJumpType.expressAddInfo)
  229. }
  230. } else {
  231. if let jumpNavBlock = self.jumpNavBlock {
  232. jumpNavBlock(WillJumpType.expressManageInfo)
  233. }
  234. }
  235. default:
  236. return
  237. }
  238. }
  239. }
  240. // MARK: - 购物车计算
  241. extension ShoppingCartOrderPayView {
  242. // 计算Section数据,刷新结算View
  243. func calculateSectionPrice(_ section: Int) -> Int {
  244. var totalPrice: Int = 0
  245. let cartProListMdl: CartProductListModel = proListModelArr![section]
  246. for productMdl in cartProListMdl.productList! {
  247. totalPrice += productMdl.skuPrice! * productMdl.amount!
  248. }
  249. return totalPrice
  250. }
  251. }