ChooseCouponViewController.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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(segmentedView.selectedIndex)
  66. }
  67. }
  68. /// 付款金额
  69. var amount: Double = 0.0
  70. /// 优惠券使用回调
  71. var usedBlock: ((_ coupon: MoMyCouponHold?, _ rowNum: Int?) -> Void)?
  72. /// 优惠券
  73. var coupon: MoMyCouponHold?
  74. // MARK: - 生命周期
  75. override func viewDidLoad() {
  76. super.viewDidLoad()
  77. // Do any additional setup after loading the view.
  78. /// UI界面初始化
  79. buildView()
  80. }
  81. // MARK: - 初始化
  82. /// UI界面初始化
  83. fileprivate func buildView() {
  84. /// loding......
  85. self.addLoadingView()
  86. }
  87. // MARK: - 交互相关
  88. /// onTapGestureRecognizer
  89. /// - Parameter sender: segmentIndex
  90. @IBAction fileprivate func onTapGestureRecognizer(_ sender: UITapGestureRecognizer) {
  91. /// 查询我的优惠券
  92. self.dismiss(animated: true, completion: {})
  93. }
  94. // MARK: - 接口请求
  95. /// 刷新数据
  96. /// - Parameter segmentIndex: segmentIndex
  97. fileprivate func reloadData(_ segmentIndex: Int) {
  98. /// 查询我的优惠券
  99. segmentIndex == 0 ? requestQueryMyCoupons() : requestQueryMyUnusedCoupons()
  100. }
  101. /// 查询我的优惠券
  102. fileprivate func requestQueryMyCoupons() {
  103. /// 异常
  104. guard let orderManager = MTP2BusinessCore.shared.orderManager,
  105. let moGoodsInfo = takeInfo as? MoGoodsInfo else { return }
  106. /// startAnimating
  107. _anim?.startAnimating()
  108. /// 查询数据
  109. orderManager.requestQueryMyCouponHolds(moGoodsInfo.goodsid, amount, moGoodsInfo.sellUserID, nil) { (isComplete, error, objs) in
  110. DispatchQueue.main.async {
  111. /// stopAnimating
  112. self._anim?.stopAnimating()
  113. /// 查询成功
  114. if isComplete {
  115. self.coupons = objs?.filter({ (obj) -> Bool in
  116. return obj.holdstatus == .executed
  117. }) ?? []
  118. } else {
  119. self.coupons = []
  120. /// showError
  121. WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
  122. }
  123. }
  124. }
  125. }
  126. /// 查询我的不可用优惠券
  127. fileprivate func requestQueryMyUnusedCoupons() {
  128. /// 异常
  129. guard let orderManager = MTP2BusinessCore.shared.orderManager,
  130. let moGoodsInfo = takeInfo as? MoGoodsInfo else { return }
  131. /// startAnimating
  132. _anim?.startAnimating()
  133. /// 查询数据
  134. orderManager.requestQueryMyUnusedCouponHolds(moGoodsInfo.goodsid, amount, moGoodsInfo.sellUserID) { (isComplete, error, objs) in
  135. DispatchQueue.main.async {
  136. /// stopAnimating
  137. self._anim?.stopAnimating()
  138. /// 查询成功
  139. if isComplete {
  140. self.coupons = objs ?? []
  141. } else {
  142. self.coupons = []
  143. /// showError
  144. WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
  145. }
  146. }
  147. }
  148. }
  149. }
  150. // MARK: - JXSegmentedViewDelegate
  151. extension ChooseCouponViewController: JXSegmentedViewDelegate {
  152. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  153. /// 刷新数据
  154. self.reloadData(index)
  155. }
  156. }
  157. // MARK: - UITableViewDelegate, UITableViewDataSource
  158. extension ChooseCouponViewController: UITableViewDelegate, UITableViewDataSource {
  159. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  160. return coupons.count
  161. }
  162. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  163. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! CouponCell
  164. if self.segmentedView.selectedIndex == 0 {
  165. cell.chooseBlock = { (_ coupon: MoMyCouponHold?, _ rowRum: Int?) in
  166. DispatchQueue.main.async {
  167. self.dismiss(animated: true) {
  168. /// 执行回调
  169. if let block = self.usedBlock,
  170. self.coupons[indexPath.row].unusedresult == "" {
  171. block(self.coupons[indexPath.row], rowRum)
  172. }
  173. }
  174. }
  175. }
  176. }
  177. cell.rowNum = indexPath.row
  178. cell.model = coupons[indexPath.row]
  179. return cell
  180. }
  181. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  182. return 130.0
  183. }
  184. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  185. if self.segmentedView.selectedIndex == 0 { /// 可用
  186. /// 获取选中的优惠券
  187. let cell = tableView.cellForRow(at: indexPath) as! CouponCell
  188. cell.choosed = !cell.choosed
  189. /// 执行回调
  190. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.2) {
  191. if let block = cell.chooseBlock,
  192. let obj = cell.model {
  193. block(obj, !cell.choosed ? nil : cell.rowNum)
  194. }
  195. }
  196. /// 先取消所有的勾选状态
  197. for (index, obj) in tableView.visibleCells.enumerated() {
  198. if index != indexPath.row {
  199. (obj as! CouponCell).choosed = false
  200. }
  201. }
  202. } else { /// 不可用
  203. DispatchQueue.main.async {
  204. self.dismiss(animated: true, completion: nil)
  205. }
  206. }
  207. }
  208. }