| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- //
- // ChooseCouponViewController.swift
- // MTP2_iOS
- //
- // Created by 曹晓亮 on 2020/12/16.
- // Copyright © 2020 Muchinfo. All rights reserved.
- //
- import UIKit
- import JXSegmentedView
- import WHToast
- import DeviceKit
- /// 优惠券选择视图容器控制类
- class ChooseCouponViewController: 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!
- /// 高度约束
- @IBOutlet weak var heightLayoutConstraint: NSLayoutConstraint! {
- didSet {
- heightLayoutConstraint.constant = 0.0
- }
- }
- /// CellIdentifier
- let CellIdentifier = "Coupon_Cell"
- /// 优惠券数据信息
- var coupons: [MoMyCouponHold] = [] {
- didSet {
- /// 刷新列表数据
- tableView.reloadData()
- /// 设备类型
- if Device.current == .iPhone6 || Device.current == .iPhone6s || Device.current == .iPhone7 || Device.current == .iPhone8 {
- /// 高度约束
- heightLayoutConstraint.constant = coupons.count == 0 ? 200.0 : (coupons.count <= 3 ? CGFloat(coupons.count)*130.0 : 400.0)
- } else {
- /// 高度约束
- heightLayoutConstraint.constant = coupons.count == 0 ? 200.0 : (coupons.count <= 4 ? CGFloat(coupons.count)*130.0 : 400.0)
- }
- }
- }
- /// 商品数据
- override var takeInfo: Any? {
- didSet {
- /// 异常
- guard let _ = takeInfo as? MoGoodsInfo else { return }
- /// 查询数据
- reloadData(0)
- }
- }
- /// 付款金额
- var amount: Double = 0.0
- /// 优惠券使用回调
- var usedBlock: ((_ coupon: MoMyCouponHold?, _ rowNum: Int?) -> Void)?
- /// 优惠券
- var coupon: MoMyCouponHold?
-
- // MARK: - 生命周期
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- /// UI界面初始化
- buildView()
- }
-
- // MARK: - 初始化
- /// UI界面初始化
- fileprivate func buildView() {
- /// loding......
- self.addLoadingView()
- }
-
- // MARK: - 交互相关
- /// onTapGestureRecognizer
- /// - Parameter sender: segmentIndex
- @IBAction fileprivate func onTapGestureRecognizer(_ sender: UITapGestureRecognizer) {
- /// 查询我的优惠券
- self.dismiss(animated: true, completion: {})
- }
-
- // MARK: - 接口请求
- /// 刷新数据
- /// - Parameter segmentIndex: segmentIndex
- fileprivate func reloadData(_ segmentIndex: Int) {
- /// 查询我的优惠券
- segmentIndex == 0 ? requestQueryMyCoupons() : requestQueryMyUnusedCoupons()
- }
-
- /// 查询我的优惠券
- fileprivate func requestQueryMyCoupons() {
- /// 异常
- guard let orderManager = MTP2BusinessCore.shared.orderManager,
- let moGoodsInfo = takeInfo as? MoGoodsInfo else { return }
-
- /// startAnimating
- _anim?.startAnimating()
- /// 查询数据
- orderManager.requestQueryMyCouponHolds(moGoodsInfo.goodsid, amount, moGoodsInfo.sellUserID, "1,2") { (isComplete, error, objs) in
- DispatchQueue.main.async {
- /// stopAnimating
- self._anim?.stopAnimating()
- /// 查询成功
- if isComplete {
- self.coupons = objs?.filter({ (obj) -> Bool in
- return obj.holdstatus == .executed
- }) ?? []
- /// 更新数据
- if self.coupons.count != 0 {
- self.dataSource.titles[0] = "可用优惠券 (\(self.coupons.count))"
- self.segmentedView.reloadData()
- }
- } else {
- self.coupons = []
- /// showError
- WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
- }
- }
- }
- }
-
- /// 查询我的不可用优惠券
- fileprivate func requestQueryMyUnusedCoupons() {
- /// 异常
- guard let orderManager = MTP2BusinessCore.shared.orderManager,
- let moGoodsInfo = takeInfo as? MoGoodsInfo else { return }
-
- /// startAnimating
- _anim?.startAnimating()
- /// 查询数据
- orderManager.requestQueryMyUnusedCouponHolds(moGoodsInfo.goodsid, amount, moGoodsInfo.sellUserID) { (isComplete, error, objs) in
- DispatchQueue.main.async {
- /// stopAnimating
- self._anim?.stopAnimating()
- /// 查询成功
- if isComplete {
- self.coupons = objs ?? []
- /// 更新数据
- if self.coupons.count != 0 {
- self.dataSource.titles[1] = "不可用优惠券 (\(self.coupons.count))"
- self.segmentedView.reloadData()
- }
- } else {
- self.coupons = []
- /// showError
- WHToast.showError(withMessage: "查询失败,请稍候再试!", duration: 1.5, finishHandler: {})
- }
- }
- }
- }
- }
- // MARK: - JXSegmentedViewDelegate
- extension ChooseCouponViewController: JXSegmentedViewDelegate {
-
- func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
- /// 刷新数据
- self.reloadData(index)
- }
- }
- // MARK: - UITableViewDelegate, UITableViewDataSource
- extension ChooseCouponViewController: 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
- if self.segmentedView.selectedIndex == 0 {
- cell.chooseBlock = { (_ coupon: MoMyCouponHold?, _ rowRum: Int?) in
- DispatchQueue.main.async {
- self.dismiss(animated: true) {
- /// 执行回调
- if let block = self.usedBlock,
- self.coupons[indexPath.row].unusedresult == "" {
- block(self.coupons[indexPath.row], rowRum)
- }
- }
- }
- }
- }
- cell.rowNum = indexPath.row
- cell.model = coupons[indexPath.row]
- return cell
- }
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 130.0
- }
-
- func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- if self.segmentedView.selectedIndex == 0 { /// 可用
- /// 获取选中的优惠券
- let cell = tableView.cellForRow(at: indexPath) as! CouponCell
- cell.choosed = !cell.choosed
- /// 执行回调
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.2) {
- if let block = cell.chooseBlock,
- let obj = cell.model {
- block(obj, !cell.choosed ? nil : cell.rowNum)
- }
- }
-
- /// 先取消所有的勾选状态
- for (index, obj) in tableView.visibleCells.enumerated() {
- if index != indexPath.row {
- (obj as! CouponCell).choosed = false
- }
- }
- } else { /// 不可用
- DispatchQueue.main.async {
- self.dismiss(animated: true, completion: nil)
- }
- }
- }
- }
|