ChooseCouponViewController.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. }
  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, "1,2") { (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. /// 更新数据
  119. if self.coupons.count != 0 {
  120. self.dataSource.titles[0] = "可用优惠券 (\(self.coupons.count))"
  121. self.segmentedView.reloadData()
  122. }
  123. } else {
  124. self.coupons = []
  125. /// showError
  126. WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
  127. }
  128. }
  129. }
  130. }
  131. /// 查询我的不可用优惠券
  132. fileprivate func requestQueryMyUnusedCoupons() {
  133. /// 异常
  134. guard let orderManager = MTP2BusinessCore.shared.orderManager,
  135. let moGoodsInfo = takeInfo as? MoGoodsInfo else { return }
  136. /// startAnimating
  137. _anim?.startAnimating()
  138. /// 查询数据
  139. orderManager.requestQueryMyUnusedCouponHolds(moGoodsInfo.goodsid, amount, moGoodsInfo.sellUserID) { (isComplete, error, objs) in
  140. DispatchQueue.main.async {
  141. /// stopAnimating
  142. self._anim?.stopAnimating()
  143. /// 查询成功
  144. if isComplete {
  145. self.coupons = objs ?? []
  146. /// 更新数据
  147. if self.coupons.count != 0 {
  148. self.dataSource.titles[1] = "不可用优惠券 (\(self.coupons.count))"
  149. self.segmentedView.reloadData()
  150. }
  151. } else {
  152. self.coupons = []
  153. /// showError
  154. WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
  155. }
  156. }
  157. }
  158. }
  159. }
  160. // MARK: - JXSegmentedViewDelegate
  161. extension ChooseCouponViewController: JXSegmentedViewDelegate {
  162. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  163. /// 刷新数据
  164. self.reloadData(index)
  165. }
  166. }
  167. // MARK: - UITableViewDelegate, UITableViewDataSource
  168. extension ChooseCouponViewController: UITableViewDelegate, UITableViewDataSource {
  169. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  170. return coupons.count
  171. }
  172. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  173. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! CouponCell
  174. if self.segmentedView.selectedIndex == 0 {
  175. cell.chooseBlock = { (_ coupon: MoMyCouponHold?, _ rowRum: Int?) in
  176. DispatchQueue.main.async {
  177. self.dismiss(animated: true) {
  178. /// 执行回调
  179. if let block = self.usedBlock,
  180. self.coupons[indexPath.row].unusedresult == "" {
  181. block(self.coupons[indexPath.row], rowRum)
  182. }
  183. }
  184. }
  185. }
  186. }
  187. cell.rowNum = indexPath.row
  188. cell.model = coupons[indexPath.row]
  189. return cell
  190. }
  191. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  192. return 130.0
  193. }
  194. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  195. if self.segmentedView.selectedIndex == 0 { /// 可用
  196. /// 获取选中的优惠券
  197. let cell = tableView.cellForRow(at: indexPath) as! CouponCell
  198. cell.choosed = !cell.choosed
  199. /// 执行回调
  200. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.2) {
  201. if let block = cell.chooseBlock,
  202. let obj = cell.model {
  203. block(obj, !cell.choosed ? nil : cell.rowNum)
  204. }
  205. }
  206. /// 先取消所有的勾选状态
  207. for (index, obj) in tableView.visibleCells.enumerated() {
  208. if index != indexPath.row {
  209. (obj as! CouponCell).choosed = false
  210. }
  211. }
  212. } else { /// 不可用
  213. DispatchQueue.main.async {
  214. self.dismiss(animated: true, completion: nil)
  215. }
  216. }
  217. }
  218. }