ShoppingCartOrderPayView.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 = (_ paraModel: OrderCreateParameterModel) -> 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. if proListModelArr != nil {
  24. self.proListModelArr = self.fixOriginalPayArray()
  25. tableView.reloadData()
  26. accountView.isHidden = false
  27. }else {
  28. accountView.isHidden = true
  29. }
  30. }
  31. }
  32. // 配送方式
  33. var deliverType: String? {
  34. didSet {
  35. if self.deliverType == "1" {
  36. let addr: SelfMentionAddressModel = SelfMentionAddressModel.getModel()!
  37. selfAddressLabel.text = "\(addr.address ?? "")"
  38. } else {
  39. selfAddressView.isHidden = true
  40. tableView.snp.remakeConstraints { (make) in
  41. make.top.left.right.equalToSuperview()
  42. make.bottom.equalTo(accountView.snp_top)
  43. }
  44. }
  45. }
  46. }
  47. // 快递地址信息Mdl
  48. var expressAddressMdl: ExpressAddresModel? {
  49. didSet {
  50. if expressAddressMdl != nil && expressAddressMdl?.id != nil {
  51. tableView.reloadData()
  52. }
  53. }
  54. }
  55. // 自提联系人信息Mdl
  56. var selfAddressInfoMdl: SelfMentionContactsModel? {
  57. didSet {
  58. if selfAddressInfoMdl != nil {
  59. tableView.reloadData()
  60. }
  61. }
  62. }
  63. // 自提地址信息Mdl
  64. var selfAddrMdl: SelfAddresModel? {
  65. didSet {
  66. if selfAddrMdl != nil {
  67. tableView.reloadData()
  68. }
  69. }
  70. }
  71. override func setupViews() {
  72. self.backgroundColor = kf7f8faColor
  73. addSubview(accountView)
  74. addSubview(selfAddressView)
  75. selfAddressView.addSubview(selfAddressLabel)
  76. addSubview(tableView)
  77. let emptyView = EmptyView.shared.diyCustomEmptyViewStyle2(iconStr: "page04", titleStr: "当前暂无数据")
  78. emptyView.contentViewY = kScaleValue(value: 182)
  79. tableView.ly_emptyView = emptyView
  80. tableView.ly_startLoading()
  81. }
  82. override func setupLayouts() {
  83. accountView.snp.makeConstraints { (make) in
  84. make.left.right.equalToSuperview()
  85. make.bottom.equalTo(-kSafeTabBarHeight)
  86. make.height.equalTo(48)
  87. }
  88. selfAddressView.snp.makeConstraints { (make) in
  89. make.bottom.equalTo(accountView.snp_top)
  90. make.left.right.equalToSuperview()
  91. }
  92. selfAddressLabel.snp.remakeConstraints { (make) in
  93. make.left.equalToSuperview().offset(14)
  94. make.right.equalToSuperview().offset(-26)
  95. make.top.bottom.equalToSuperview()
  96. make.height.equalTo(40)
  97. }
  98. tableView.snp.makeConstraints { (make) in
  99. make.top.left.right.equalToSuperview()
  100. make.bottom.equalTo(selfAddressView.snp_top)
  101. }
  102. }
  103. lazy var accountView: OrderPayAcountView = {
  104. let accountView = OrderPayAcountView()
  105. accountView.commitOrderBlock = {
  106. [weak self] in
  107. let paraMdl = OrderCreateParameterModel()
  108. paraMdl.money = self!.totalProductPrice
  109. paraMdl.selShopListArr = self?.proListModelArr
  110. switch self?.deliverType {
  111. case "1":
  112. // 自提
  113. paraMdl.name = self?.selfAddressInfoMdl?.name
  114. paraMdl.mobile = Int(self?.selfAddressInfoMdl?.mobile ?? "")
  115. paraMdl.address = "\(self?.selfAddrMdl?.address ?? "")"
  116. paraMdl.pickNodeId = self?.selfAddrMdl?.pickupNodeId
  117. paraMdl.pickNodeContact = self?.selfAddrMdl?.managerName
  118. case "2":
  119. // 快递
  120. paraMdl.name = self?.expressAddressMdl?.contactName
  121. paraMdl.mobile = Int(self?.expressAddressMdl?.contactMobile ?? "")
  122. paraMdl.address = self?.expressAddressMdl?.address
  123. paraMdl.pickNodeId = 0
  124. paraMdl.pickNodeContact = ""
  125. default:
  126. return
  127. }
  128. if let commitOrderTransBlock = self?.commitOrderTransBlock {
  129. commitOrderTransBlock(paraMdl)
  130. }
  131. }
  132. return accountView
  133. }()
  134. private lazy var selfAddressView: UIView = {
  135. let selfAddressView = UIView()
  136. selfAddressView.backgroundColor = kfff8efColor
  137. return selfAddressView
  138. }()
  139. private lazy var selfAddressLabel: UILabel = {
  140. let selfAddressLabel = UILabel()
  141. selfAddressLabel.textColor = kFFA42FColor
  142. selfAddressLabel.font = kRegularFont13
  143. selfAddressLabel.textAlignment = .left
  144. selfAddressLabel.numberOfLines = 0
  145. return selfAddressLabel
  146. }()
  147. lazy var tableView: UITableView = {
  148. let tableView = UITableView(frame: CGRect.zero, style: UITableView.Style.grouped)
  149. tableView.separatorStyle = .none
  150. tableView.backgroundColor = kf7f8faColor
  151. tableView.dataSource = self
  152. tableView.delegate = self
  153. tableView.estimatedRowHeight = 0.000001
  154. tableView.estimatedSectionFooterHeight = 0.000001
  155. tableView.estimatedSectionHeaderHeight = 0.000001
  156. tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
  157. return tableView
  158. }()
  159. }
  160. // MARK: - tableView dataSource && delegate
  161. extension ShoppingCartOrderPayView : UITableViewDelegate, UITableViewDataSource {
  162. func numberOfSections(in tableView: UITableView) -> Int {
  163. return proListModelArr?.isEmpty ?? true ? 0 : (proListModelArr?.count ?? 0) + 1
  164. }
  165. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  166. if section == 0 {
  167. switch deliverType {
  168. case "1":
  169. // 自提
  170. return 2
  171. case "2":
  172. // 快递
  173. return 1
  174. default:
  175. return 1
  176. }
  177. } else {
  178. return proListModelArr![section-1].productList?.isEmpty ?? true ? 0 : proListModelArr![section-1].productList?.count ?? 0
  179. }
  180. }
  181. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  182. if indexPath.section == 0 {
  183. switch deliverType {
  184. case "1":
  185. // 自提
  186. if selfAddressInfoMdl?.id == nil {
  187. if indexPath.row == 0 {
  188. // 自提添加收货人信息
  189. let cell = OrderPaySelfPickAddInfoCell.cellWith(tableView: tableView, indexPath: indexPath)
  190. return cell
  191. } else {
  192. // 自提地址信息
  193. let cell = OrderPaySelfPickAddressCell.cellWith(tableView: tableView, indexPath: indexPath)
  194. cell.selfAddressMdl = selfAddrMdl
  195. return cell
  196. }
  197. } else {
  198. if indexPath.row == 0 {
  199. // 自提个人信息
  200. let cell = OrderPaySelfPickInfoCell.cellWith(tableView: tableView, indexPath: indexPath)
  201. cell.contactMdl = selfAddressInfoMdl
  202. return cell
  203. } else {
  204. // 自提地址信息
  205. let cell = OrderPaySelfPickAddressCell.cellWith(tableView: tableView, indexPath: indexPath)
  206. cell.selfAddressMdl = selfAddrMdl
  207. return cell
  208. }
  209. }
  210. case "2":
  211. // 快递
  212. if expressAddressMdl == nil {
  213. // 快递添加地址
  214. let expressCell = OrderPayExpressAddInfoCell.cellWith(tableView: tableView, indexPath: indexPath)
  215. return expressCell
  216. } else {
  217. // 快递信息管理
  218. let expressCell = OrderPayExpressInfoShowCell.cellWith(tableView: tableView, indexPath: indexPath)
  219. expressCell.addressMdl = expressAddressMdl
  220. return expressCell
  221. }
  222. default:
  223. return UITableViewCell()
  224. }
  225. } else {
  226. // 购物车列表Item
  227. let cell = ShoppingCartPayOrderItemCell.cellWith(tableView: tableView, indexPath: indexPath)
  228. cell.productMdl = proListModelArr![indexPath.section-1].productList![indexPath.row]
  229. return cell
  230. }
  231. }
  232. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  233. return UITableView.automaticDimension
  234. }
  235. func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
  236. if section == 0 {
  237. return 10
  238. } else {
  239. return 58
  240. }
  241. }
  242. func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
  243. if section == 0 {
  244. return nil
  245. } else {
  246. let headerView = ShoppingCartPayOrderHeader(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 58))
  247. headerView.shopName = proListModelArr![section-1].shopName
  248. return headerView
  249. }
  250. }
  251. func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
  252. if section == 0 {
  253. return 0.000001
  254. } else {
  255. return 88
  256. }
  257. }
  258. func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
  259. if section == 0 {
  260. return nil
  261. } else {
  262. let footerView = ShoppingCartPayOrderFooter(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: 88))
  263. let cartProListMdl: CartProductListModel = proListModelArr![section-1]
  264. let secNum: Int = cartProListMdl.productList!.count
  265. footerView.tPrice = self.calculateSectionPrice(section-1)
  266. footerView.tNumber = secNum
  267. footerView.buyerNoteBlock = { [weak self]
  268. (buyerNotes) in
  269. self?.proListModelArr![section-1].buyerNotes = buyerNotes
  270. }
  271. return footerView
  272. }
  273. }
  274. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  275. if indexPath.section != 0 {
  276. return
  277. }
  278. switch deliverType {
  279. case "1":
  280. // 自提
  281. if selfAddressInfoMdl?.id == nil {
  282. if indexPath.row == 0 {
  283. if let jumpNavBlock = self.jumpNavBlock {
  284. jumpNavBlock(WillJumpType.selfAddInfo)
  285. }
  286. } else {
  287. if let jumpNavBlock = self.jumpNavBlock {
  288. jumpNavBlock(WillJumpType.selfAddressInfo)
  289. }
  290. }
  291. } else {
  292. if indexPath.row == 0 {
  293. if let jumpNavBlock = self.jumpNavBlock {
  294. jumpNavBlock(WillJumpType.selfPersonalInfo)
  295. }
  296. } else {
  297. if let jumpNavBlock = self.jumpNavBlock {
  298. jumpNavBlock(WillJumpType.selfAddressInfo)
  299. }
  300. }
  301. }
  302. case "2":
  303. // 快递
  304. if expressAddressMdl?.id == nil {
  305. if let jumpNavBlock = self.jumpNavBlock {
  306. jumpNavBlock(WillJumpType.expressAddInfo)
  307. }
  308. } else {
  309. if let jumpNavBlock = self.jumpNavBlock {
  310. jumpNavBlock(WillJumpType.expressManageInfo)
  311. }
  312. }
  313. default:
  314. return
  315. }
  316. }
  317. }
  318. // MARK: - 购物车计算
  319. extension ShoppingCartOrderPayView {
  320. // 剔除section下商品全为下架/售罄的商家
  321. func fixOriginalPayArray() -> Array<CartProductListModel> {
  322. var fixedListMdlArr: Array<CartProductListModel> = []
  323. for (secIdx, proListMdl) in (proListModelArr?.enumerated())! {
  324. var shopAllDisabled: Bool = true
  325. for productMdl in proListMdl.productList ?? [] {
  326. if productMdl.upStatus != 0 && productMdl.stock != 0 {
  327. shopAllDisabled = false
  328. break
  329. }
  330. }
  331. if shopAllDisabled == false {
  332. fixedListMdlArr.append(proListModelArr![secIdx])
  333. }
  334. }
  335. return fixedListMdlArr
  336. }
  337. // 计算Section数据,刷新结算View
  338. func calculateSectionPrice(_ section: Int) -> Int {
  339. var totalPrice: Int = 0
  340. let cartProListMdl: CartProductListModel = proListModelArr![section]
  341. for productMdl in cartProListMdl.productList! {
  342. totalPrice += ((productMdl.skuPrice ?? 0) * (productMdl.amount ?? 0))
  343. }
  344. return totalPrice
  345. }
  346. }