| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- //
- // CouponViewController.swift
- // MTP2_iOS
- //
- // Created by 曹晓亮 on 2020/12/16.
- // Copyright © 2020 Muchinfo. All rights reserved.
- //
- import UIKit
- import JXSegmentedView
- import WHToast
- import GTMRefresh
- import IBAnimatable
- import SwiftyAttributes
- /// 优惠券视图容器控制类
- class CouponViewController: BaseViewController {
- // MARK: - 属性列表
- /// 分段控制器
- @IBOutlet weak var segmentedView: JXSegmentedView! {
- didSet {
- segmentedView.delegate = self
- segmentedView.dataSource = dataSource
- segmentedView.indicators = [self.indicator]
- }
- }
- /// segmentedDataSource
- let dataSource: JXSegmentedTitleDataSource = {
- var items: [String] = ["待使用", "已使用", "已过期"]
- $0.titles = items
- $0.titleNormalColor = UIColorFromHex(rgbValue: 0x333333)
- $0.titleNormalFont = .font_14
- $0.titleSelectedColor = UIColorFromHex(rgbValue: 0x60a1e3)
- $0.titleSelectedFont = .font_14
- $0.isTitleColorGradientEnabled = true
- return $0
- } (JXSegmentedTitleDataSource())
- /// 列表
- @IBOutlet weak var tableView: UITableView!
- /// CellIdentifier
- let CellIdentifier = "Coupon_Cell"
-
- /// 优惠券数据信息
- var coupons: [MoMyCouponHold] = [] {
- didSet {
- /// 刷新列表数据
- tableView.reloadData()
- /// 隐藏按钮
- self.noDataButton.isHidden = !(coupons.count == 0)
- }
- }
-
- // MARK: - 生命周期
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
-
- /// UI界面初始化
- buildView()
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- /// 隐藏导航栏
- self.navigationController?.setNavigationBarHidden(false, animated: true)
- }
-
- // MARK: - 初始化
- /// UI界面初始化
- fileprivate func buildView() {
- /// loding......
- self.addLoadingView()
-
- /// 添加下拉刷新控件
- self.tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
- /// 重置请求条件
- self.reloadData(self.segmentedView.selectedIndex)
- }
- /// 请求数据
- self.tableView.triggerRefreshing()
- }
-
- // MARK: - 接口请求
- /// 刷新数据
- fileprivate func reloadData(_ segmentIndex: Int) {
- if segmentIndex == 1 {
- /// 查询数据
- self.requestQueryMyUsedCoupon()
- } else {
- /// 查询数据
- self.requestQueryMyCoupons(segmentIndex == 0 ? "1,2" : "3")
- }
- }
-
- /// 查询我的优惠券
- fileprivate func requestQueryMyCoupons(_ status: String) {
- /// 异常
- guard let orderManager = MTP2BusinessCore.shared.orderManager else { return }
- /// startAnimating
- _anim?.startAnimating()
- /// 查询优惠券
- orderManager.requestQueryMyCouponHolds(nil, nil, nil, status) { (isComplete, error, objs) in
- DispatchQueue.main.async {
- /// stopAnimating
- self._anim?.stopAnimating()
- /// endRefreshing
- self.tableView.endRefreshing()
- /// 查询成功
- if isComplete {
- /// 数据过滤
- self.coupons = objs ?? []
- } else {
- self.coupons = []
- /// showError
- WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
- }
- }
- }
- }
-
- /// 查询我的已使用优惠券
- fileprivate func requestQueryMyUsedCoupon() {
- /// 异常
- guard let orderManager = MTP2BusinessCore.shared.orderManager else { return }
- /// startAnimating
- _anim?.startAnimating()
- /// 查询优惠券
- orderManager.requestQueryMyUsedCoupon { (isComplete, error, objs) in
- DispatchQueue.main.async {
- /// stopAnimating
- self._anim?.stopAnimating()
- /// endRefreshing
- self.tableView.endRefreshing()
- /// 查询成功
- if isComplete {
- /// 数据过滤
- self.coupons = objs ?? []
- } else {
- self.coupons = []
- /// showError
- WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
- }
- }
- }
- }
-
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destination.
- // Pass the selected object to the new view controller.
- }
- */
- }
- // MARK: - JXSegmentedViewDelegate
- extension CouponViewController: JXSegmentedViewDelegate {
-
- func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
- /// 刷新数据
- self.reloadData(index)
- }
- }
- // MARK: - UITableViewDelegate, UITableViewDataSource
- extension CouponViewController: UITableViewDelegate, UITableViewDataSource {
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return coupons.count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! CouponCell
- cell.usedBlock = { (_ coupon: MoMyCouponHold?) in
- let specialSaleController = self.viewController(name: "Quote", identifier: "SpecialSale") as! SpecialSaleViewController
- specialSaleController.couponHold = coupon
- self.navigationController?.pushViewController(specialSaleController, animated: true)
- }
- cell.model = coupons[indexPath.row]
- return cell
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 130.0
- }
- }
- class CouponCell: BaseTableViewCell<MoMyCouponHold> {
- /// 背景
- @IBOutlet weak var couponImage: UIImageView!
- /// 名字
- @IBOutlet weak var couponName: UILabel!
- /// 折扣
- @IBOutlet weak var discount: UILabel!
- /// 满减
- @IBOutlet weak var moneyOff: UILabel!
- /// 使用时间
- @IBOutlet weak var usedDate: UILabel!
- /// 原因
- @IBOutlet weak var result: UILabel!
- /// 数量
- @IBOutlet weak var qty: UILabel!
- /// 去使用
- @IBOutlet weak var goToUse: AnimatableButton!
- /// 选中
- @IBOutlet weak var check: AnimatableButton!
- /// model
- override var model: MoMyCouponHold? {
- didSet {
- /// 异常
- guard let obj = model else { return }
- /// 名字
- couponName.text = obj.couponname
- /// 已生效
- goToUse.isHidden = !(obj.holdstatus == .executed)
- /// 数量 未生效和已生效才显示
- qty.text = (obj.holdstatus == .invalid || obj.holdstatus == .executed) ? "\(obj.qty)张" : ""
-
- switch obj.holdstatus {
- case .invalid, .used, .expired: /// 未生效 已使用 已过期
- /// 背景
- couponImage.image = UIImage(named: "my_coupon_bg_gray")
- /// 使用时间
- usedDate.text = (obj.holdstatus == .expired || obj.holdstatus == .invalid) ? "\(DateUtils.getTDateString(obj.startdate))-\(DateUtils.getTDateString(obj.enddate))" : obj.holdstatus.description
- /// 不能使用原因
- result.text = ""
- default:
- if obj.unusedresult == "" {
- /// 背景
- couponImage.image = UIImage(named: obj.couponcategroy == 1 ? "my_coupon_bg" : "my_coupon_bg_red")
- } else {
- /// 背景
- couponImage.image = UIImage(named: "my_coupon_bg_gray")
- /// 已生效
- goToUse.isHidden = obj.unusedresult != ""
- }
- /// 使用时间
- usedDate.text = "\(DateUtils.getTDateString(obj.startdate))-\(DateUtils.getTDateString(obj.enddate))"
- }
- if obj.couponcategroy == 1 {
- discount.attributedText = ("¥".withFont(.font_12)+(obj.couponvalue.toString(reserve: 2).withFont(.font_18)))
- } else {
- discount.attributedText = ((obj.couponvalue*10.0).toString(reserve: 1).withFont(.font_18)+(" 折".withFont(.font_12)))
- }
- /// 满多少可用
- moneyOff.text = "满\(obj.conditionvalue)可用"
- /// 不能使用原因
- result.text = obj.holdstatus == .invalid ? obj.holdstatus.description : obj.unusedresult
- /// 选中
- if let vc = self.superController() as? ChooseCouponViewController,
- vc.segmentedView.selectedIndex == 0,
- model?.couponholdid == vc.coupon?.couponholdid {
- self.choosed = true
- } else {
- self.choosed = false
- }
- }
- }
- /// 优惠券使用回调
- var usedBlock: ((_ coupon: MoMyCouponHold?) -> Void)?
- /// 选中回调
- var chooseBlock: ((_ coupon: MoMyCouponHold?, _ rowrowNum: Int?) -> Void)?
- /// 是否选中
- var choosed: Bool = false {
- didSet {
- /// 只有可用才能选中
- if (self.superController() as? ChooseCouponViewController)?.segmentedView.selectedIndex == 0 {
- /// 是否选中
- check.setImage(UIImage(named: choosed ? "check_selected" : "check_normal"), for: .normal)
- } else {
- /// 是否选中
- check.setImage(UIImage(named: "check_normal"), for: .normal)
- }
- }
- }
-
- /// 按钮点击响应事件
- /// - Parameter sender: sender
- @IBAction private func onButtonPressed(_ sender: UIButton) {
- switch sender {
- case goToUse: /// 去使用
- if let block = usedBlock,
- let obj = model { block(obj) }
- case check: /// 是否选中
- if let vc = (self.superController() as? ChooseCouponViewController),
- let deletgate = vc.tableView.delegate,
- let row = self.rowNum {
- /// 执行回调
- deletgate.tableView?(vc.tableView, didSelectRowAt: IndexPath(row: row, section: 0))
- }
- default: break
- }
- }
- }
|