CouponViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // CouponViewController.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 GTMRefresh
  12. import IBAnimatable
  13. import SwiftyAttributes
  14. /// 优惠券视图容器控制类
  15. class CouponViewController: BaseViewController {
  16. // MARK: - 属性列表
  17. /// 分段控制器
  18. @IBOutlet weak var segmentedView: JXSegmentedView! {
  19. didSet {
  20. segmentedView.delegate = self
  21. segmentedView.dataSource = dataSource
  22. segmentedView.indicators = [self.indicator]
  23. }
  24. }
  25. /// segmentedDataSource
  26. let dataSource: JXSegmentedTitleDataSource = {
  27. var items: [String] = ["待使用", "已使用", "已过期"]
  28. $0.titles = items
  29. $0.titleNormalColor = UIColorFromHex(rgbValue: 0x333333)
  30. $0.titleNormalFont = .font_14
  31. $0.titleSelectedColor = UIColorFromHex(rgbValue: 0x60a1e3)
  32. $0.titleSelectedFont = .font_14
  33. $0.isTitleColorGradientEnabled = true
  34. return $0
  35. } (JXSegmentedTitleDataSource())
  36. /// 列表
  37. @IBOutlet weak var tableView: UITableView!
  38. /// CellIdentifier
  39. let CellIdentifier = "Coupon_Cell"
  40. /// 优惠券数据信息
  41. var coupons: [MoMyCouponHold] = [] {
  42. didSet {
  43. /// 刷新列表数据
  44. tableView.reloadData()
  45. /// 隐藏按钮
  46. self.noDataButton.isHidden = !(coupons.count == 0)
  47. }
  48. }
  49. // MARK: - 生命周期
  50. override func viewDidLoad() {
  51. super.viewDidLoad()
  52. // Do any additional setup after loading the view.
  53. /// UI界面初始化
  54. buildView()
  55. }
  56. override func viewWillAppear(_ animated: Bool) {
  57. super.viewWillAppear(animated)
  58. /// 隐藏导航栏
  59. self.navigationController?.setNavigationBarHidden(false, animated: true)
  60. }
  61. // MARK: - 初始化
  62. /// UI界面初始化
  63. fileprivate func buildView() {
  64. /// loding......
  65. self.addLoadingView()
  66. /// 添加下拉刷新控件
  67. self.tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  68. /// 重置请求条件
  69. self.reloadData(self.segmentedView.selectedIndex)
  70. }
  71. /// 请求数据
  72. self.tableView.triggerRefreshing()
  73. }
  74. // MARK: - 接口请求
  75. /// 刷新数据
  76. fileprivate func reloadData(_ segmentIndex: Int) {
  77. if segmentIndex == 1 {
  78. /// 查询数据
  79. self.requestQueryMyUsedCoupon()
  80. } else {
  81. /// 查询数据
  82. self.requestQueryMyCoupons(segmentIndex == 0 ? "1,2" : "3")
  83. }
  84. }
  85. /// 查询我的优惠券
  86. fileprivate func requestQueryMyCoupons(_ status: String) {
  87. /// 异常
  88. guard let orderManager = MTP2BusinessCore.shared.orderManager else { return }
  89. /// startAnimating
  90. _anim?.startAnimating()
  91. /// 查询优惠券
  92. orderManager.requestQueryMyCouponHolds(nil, nil, nil, status) { (isComplete, error, objs) in
  93. DispatchQueue.main.async {
  94. /// stopAnimating
  95. self._anim?.stopAnimating()
  96. /// endRefreshing
  97. self.tableView.endRefreshing()
  98. /// 查询成功
  99. if isComplete {
  100. /// 数据过滤
  101. self.coupons = objs ?? []
  102. } else {
  103. self.coupons = []
  104. /// showError
  105. WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
  106. }
  107. }
  108. }
  109. }
  110. /// 查询我的已使用优惠券
  111. fileprivate func requestQueryMyUsedCoupon() {
  112. /// 异常
  113. guard let orderManager = MTP2BusinessCore.shared.orderManager else { return }
  114. /// startAnimating
  115. _anim?.startAnimating()
  116. /// 查询优惠券
  117. orderManager.requestQueryMyUsedCoupon { (isComplete, error, objs) in
  118. DispatchQueue.main.async {
  119. /// stopAnimating
  120. self._anim?.stopAnimating()
  121. /// endRefreshing
  122. self.tableView.endRefreshing()
  123. /// 查询成功
  124. if isComplete {
  125. /// 数据过滤
  126. self.coupons = objs ?? []
  127. } else {
  128. self.coupons = []
  129. /// showError
  130. WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
  131. }
  132. }
  133. }
  134. }
  135. /*
  136. // MARK: - Navigation
  137. // In a storyboard-based application, you will often want to do a little preparation before navigation
  138. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  139. // Get the new view controller using segue.destination.
  140. // Pass the selected object to the new view controller.
  141. }
  142. */
  143. }
  144. // MARK: - JXSegmentedViewDelegate
  145. extension CouponViewController: JXSegmentedViewDelegate {
  146. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  147. /// 刷新数据
  148. self.reloadData(index)
  149. }
  150. }
  151. // MARK: - UITableViewDelegate, UITableViewDataSource
  152. extension CouponViewController: UITableViewDelegate, UITableViewDataSource {
  153. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  154. return coupons.count
  155. }
  156. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  157. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! CouponCell
  158. cell.usedBlock = { (_ coupon: MoMyCouponHold?) in
  159. let specialSaleController = self.viewController(name: "Quote", identifier: "SpecialSale") as! SpecialSaleViewController
  160. specialSaleController.couponHold = coupon
  161. self.navigationController?.pushViewController(specialSaleController, animated: true)
  162. }
  163. cell.model = coupons[indexPath.row]
  164. return cell
  165. }
  166. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  167. return 130.0
  168. }
  169. }
  170. class CouponCell: BaseTableViewCell<MoMyCouponHold> {
  171. /// 背景
  172. @IBOutlet weak var couponImage: UIImageView!
  173. /// 名字
  174. @IBOutlet weak var couponName: UILabel!
  175. /// 折扣
  176. @IBOutlet weak var discount: UILabel!
  177. /// 满减
  178. @IBOutlet weak var moneyOff: UILabel!
  179. /// 使用时间
  180. @IBOutlet weak var usedDate: UILabel!
  181. /// 原因
  182. @IBOutlet weak var result: UILabel!
  183. /// 数量
  184. @IBOutlet weak var qty: UILabel!
  185. /// 去使用
  186. @IBOutlet weak var goToUse: AnimatableButton!
  187. /// 选中
  188. @IBOutlet weak var check: AnimatableButton!
  189. /// model
  190. override var model: MoMyCouponHold? {
  191. didSet {
  192. /// 异常
  193. guard let obj = model else { return }
  194. /// 名字
  195. couponName.text = obj.couponname
  196. /// 已生效
  197. goToUse.isHidden = !(obj.holdstatus == .executed)
  198. /// 数量 未生效和已生效才显示
  199. qty.text = (obj.holdstatus == .invalid || obj.holdstatus == .executed) ? "\(obj.qty)张" : ""
  200. switch obj.holdstatus {
  201. case .invalid, .used, .expired: /// 未生效 已使用 已过期
  202. /// 背景
  203. couponImage.image = UIImage(named: "my_coupon_bg_gray")
  204. /// 使用时间
  205. usedDate.text = (obj.holdstatus == .expired || obj.holdstatus == .invalid) ? "\(DateUtils.getTDateString(obj.startdate))-\(DateUtils.getTDateString(obj.enddate))" : obj.holdstatus.description
  206. /// 不能使用原因
  207. result.text = ""
  208. default:
  209. if obj.unusedresult == "" {
  210. /// 背景
  211. couponImage.image = UIImage(named: obj.couponcategroy == 1 ? "my_coupon_bg" : "my_coupon_bg_red")
  212. } else {
  213. /// 背景
  214. couponImage.image = UIImage(named: "my_coupon_bg_gray")
  215. /// 已生效
  216. goToUse.isHidden = obj.unusedresult != ""
  217. }
  218. /// 使用时间
  219. usedDate.text = "\(DateUtils.getTDateString(obj.startdate))-\(DateUtils.getTDateString(obj.enddate))"
  220. }
  221. if obj.couponcategroy == 1 {
  222. discount.attributedText = ("¥".withFont(.font_12)+(obj.couponvalue.toString(reserve: 2).withFont(.font_18)))
  223. } else {
  224. discount.attributedText = ((obj.couponvalue*10.0).toString(reserve: 1).withFont(.font_18)+(" 折".withFont(.font_12)))
  225. }
  226. /// 满多少可用
  227. moneyOff.text = "满\(obj.conditionvalue)可用"
  228. /// 不能使用原因
  229. result.text = obj.holdstatus == .invalid ? obj.holdstatus.description : obj.unusedresult
  230. /// 选中
  231. if let vc = self.superController() as? ChooseCouponViewController,
  232. vc.segmentedView.selectedIndex == 0,
  233. model?.couponholdid == vc.coupon?.couponholdid {
  234. self.choosed = true
  235. } else {
  236. self.choosed = false
  237. }
  238. }
  239. }
  240. /// 优惠券使用回调
  241. var usedBlock: ((_ coupon: MoMyCouponHold?) -> Void)?
  242. /// 选中回调
  243. var chooseBlock: ((_ coupon: MoMyCouponHold?, _ rowrowNum: Int?) -> Void)?
  244. /// 是否选中
  245. var choosed: Bool = false {
  246. didSet {
  247. /// 只有可用才能选中
  248. if (self.superController() as? ChooseCouponViewController)?.segmentedView.selectedIndex == 0 {
  249. /// 是否选中
  250. check.setImage(UIImage(named: choosed ? "check_selected" : "check_normal"), for: .normal)
  251. } else {
  252. /// 是否选中
  253. check.setImage(UIImage(named: "check_normal"), for: .normal)
  254. }
  255. }
  256. }
  257. /// 按钮点击响应事件
  258. /// - Parameter sender: sender
  259. @IBAction private func onButtonPressed(_ sender: UIButton) {
  260. switch sender {
  261. case goToUse: /// 去使用
  262. if let block = usedBlock,
  263. let obj = model { block(obj) }
  264. case check: /// 是否选中
  265. if let vc = (self.superController() as? ChooseCouponViewController),
  266. let deletgate = vc.tableView.delegate,
  267. let row = self.rowNum {
  268. /// 执行回调
  269. deletgate.tableView?(vc.tableView, didSelectRowAt: IndexPath(row: row, section: 0))
  270. }
  271. default: break
  272. }
  273. }
  274. }