ChooseCouponViewController.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // ChooseCouponViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by 曹晓亮 on 2020/12/16.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import JXSegmentedView
  10. import WHToast
  11. import DeviceKit
  12. /// 优惠券选择视图容器控制类
  13. class ChooseCouponViewController: BaseViewController {
  14. // MARK: - 属性列表
  15. /// 分段控制器
  16. @IBOutlet weak var segmentedView: JXSegmentedView! {
  17. didSet {
  18. segmentedView.delegate = self
  19. segmentedView.dataSource = dataSource
  20. segmentedView.indicators = [self.indicator]
  21. }
  22. }
  23. /// segmentedDataSource
  24. let dataSource: JXSegmentedTitleDataSource = {
  25. var items: [String] = ["可用优惠券", "不可用优惠券"]
  26. $0.titles = items
  27. $0.titleNormalColor = UIColorFromHex(rgbValue: 0x333333)
  28. $0.titleNormalFont = .font_14
  29. $0.titleSelectedColor = UIColorFromHex(rgbValue: 0x60a1e3)
  30. $0.titleSelectedFont = .font_14
  31. $0.isTitleColorGradientEnabled = true
  32. return $0
  33. } (JXSegmentedTitleDataSource())
  34. /// 列表
  35. @IBOutlet weak var tableView: UITableView!
  36. /// 高度约束
  37. @IBOutlet weak var heightLayoutConstraint: NSLayoutConstraint! {
  38. didSet {
  39. heightLayoutConstraint.constant = 0.0
  40. }
  41. }
  42. /// CellIdentifier
  43. let CellIdentifier = "Coupon_Cell"
  44. /// 优惠券数据信息
  45. var coupons: [MoMyCouponHold] = [] {
  46. didSet {
  47. /// 刷新列表数据
  48. tableView.reloadData()
  49. /// 设备类型
  50. if Device.current == .iPhone6 || Device.current == .iPhone6s || Device.current == .iPhone7 || Device.current == .iPhone8 {
  51. /// 高度约束
  52. heightLayoutConstraint.constant = coupons.count == 0 ? 200.0 : (coupons.count <= 3 ? CGFloat(coupons.count)*130.0 : 400.0)
  53. } else {
  54. /// 高度约束
  55. heightLayoutConstraint.constant = coupons.count == 0 ? 200.0 : (coupons.count <= 4 ? CGFloat(coupons.count)*130.0 : 400.0)
  56. }
  57. }
  58. }
  59. /// 商品数据
  60. override var takeInfo: Any? {
  61. didSet {
  62. /// 异常
  63. guard let _ = takeInfo as? MoGoodsInfo else { return }
  64. /// 查询数据
  65. reloadData(0)
  66. reloadData(1)
  67. }
  68. }
  69. /// 付款金额
  70. var amount: Double = 0.0
  71. /// 优惠券使用回调
  72. var usedBlock: ((_ coupon: MoMyCouponHold?, _ rowNum: Int?) -> Void)?
  73. /// 优惠券
  74. var coupon: MoMyCouponHold?
  75. // MARK: - 生命周期
  76. override func viewDidLoad() {
  77. super.viewDidLoad()
  78. // Do any additional setup after loading the view.
  79. /// UI界面初始化
  80. buildView()
  81. }
  82. // MARK: - 初始化
  83. /// UI界面初始化
  84. fileprivate func buildView() {
  85. /// loding......
  86. self.addLoadingView()
  87. }
  88. // MARK: - 交互相关
  89. /// onTapGestureRecognizer
  90. /// - Parameter sender: segmentIndex
  91. @IBAction fileprivate func onTapGestureRecognizer(_ sender: UITapGestureRecognizer) {
  92. /// 查询我的优惠券
  93. self.dismiss(animated: true, completion: {})
  94. }
  95. // MARK: - 接口请求
  96. /// 刷新数据
  97. /// - Parameter segmentIndex: segmentIndex
  98. fileprivate func reloadData(_ segmentIndex: Int) {
  99. /// 查询我的优惠券
  100. segmentIndex == 0 ? requestQueryMyCoupons() : requestQueryMyUnusedCoupons()
  101. }
  102. /// 查询我的优惠券
  103. fileprivate func requestQueryMyCoupons() {
  104. /// 异常
  105. guard let orderManager = MTP2BusinessCore.shared.orderManager,
  106. let moGoodsInfo = takeInfo as? MoGoodsInfo else { return }
  107. /// startAnimating
  108. _anim?.startAnimating()
  109. /// 查询数据
  110. orderManager.requestQueryMyCouponHolds(moGoodsInfo.goodsid, amount, moGoodsInfo.sellUserID, "1,2") { (isComplete, error, objs) in
  111. DispatchQueue.main.async {
  112. /// stopAnimating
  113. self._anim?.stopAnimating()
  114. /// 查询成功
  115. if isComplete {
  116. self.coupons = objs?.filter({ (obj) -> Bool in
  117. return obj.holdstatus == .executed
  118. }) ?? []
  119. /// 更新数据
  120. if self.coupons.count != 0 {
  121. self.dataSource.titles[0] = "可用优惠券 (\(self.coupons.count))"
  122. self.segmentedView.reloadData()
  123. }
  124. } else {
  125. self.coupons = []
  126. /// showError
  127. WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
  128. }
  129. }
  130. }
  131. }
  132. /// 查询我的不可用优惠券
  133. fileprivate func requestQueryMyUnusedCoupons() {
  134. /// 异常
  135. guard let orderManager = MTP2BusinessCore.shared.orderManager,
  136. let moGoodsInfo = takeInfo as? MoGoodsInfo else { return }
  137. /// startAnimating
  138. _anim?.startAnimating()
  139. /// 查询数据
  140. orderManager.requestQueryMyUnusedCouponHolds(moGoodsInfo.goodsid, amount, moGoodsInfo.sellUserID) { (isComplete, error, objs) in
  141. DispatchQueue.main.async {
  142. /// stopAnimating
  143. self._anim?.stopAnimating()
  144. /// 查询成功
  145. if isComplete {
  146. self.coupons = objs ?? []
  147. /// 更新数据
  148. if self.coupons.count != 0 {
  149. self.dataSource.titles[1] = "不可用优惠券 (\(self.coupons.count))"
  150. self.segmentedView.reloadData()
  151. }
  152. } else {
  153. self.coupons = []
  154. /// showError
  155. WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
  156. }
  157. }
  158. }
  159. }
  160. }
  161. // MARK: - JXSegmentedViewDelegate
  162. extension ChooseCouponViewController: JXSegmentedViewDelegate {
  163. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  164. /// 刷新数据
  165. self.reloadData(index)
  166. }
  167. }
  168. // MARK: - UITableViewDelegate, UITableViewDataSource
  169. extension ChooseCouponViewController: UITableViewDelegate, UITableViewDataSource {
  170. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  171. return coupons.count
  172. }
  173. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  174. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! CouponCell
  175. if self.segmentedView.selectedIndex == 0 {
  176. cell.chooseBlock = { (_ coupon: MoMyCouponHold?, _ rowRum: Int?) in
  177. DispatchQueue.main.async {
  178. self.dismiss(animated: true) {
  179. /// 执行回调
  180. if let block = self.usedBlock,
  181. self.coupons[indexPath.row].unusedresult == "" {
  182. block(self.coupons[indexPath.row], rowRum)
  183. }
  184. }
  185. }
  186. }
  187. }
  188. cell.rowNum = indexPath.row
  189. cell.model = coupons[indexPath.row]
  190. return cell
  191. }
  192. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  193. return 130.0
  194. }
  195. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  196. if self.segmentedView.selectedIndex == 0 { /// 可用
  197. /// 获取选中的优惠券
  198. let cell = tableView.cellForRow(at: indexPath) as! CouponCell
  199. cell.choosed = !cell.choosed
  200. /// 执行回调
  201. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.2) {
  202. if let block = cell.chooseBlock,
  203. let obj = cell.model {
  204. block(obj, !cell.choosed ? nil : cell.rowNum)
  205. }
  206. }
  207. /// 先取消所有的勾选状态
  208. for (index, obj) in tableView.visibleCells.enumerated() {
  209. if index != indexPath.row {
  210. (obj as! CouponCell).choosed = false
  211. }
  212. }
  213. } else { /// 不可用
  214. DispatchQueue.main.async {
  215. self.dismiss(animated: true, completion: nil)
  216. }
  217. }
  218. }
  219. }