ShoppingCartOrderPayView.swift 13 KB

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