// // SpecialSaleViewController.swift // MTP2_iOS // // Created by 曹晓亮 on 2020/12/16. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit import WHToast import SDWebImage import SwiftDate import SwiftyAttributes import GTMRefresh /// 特卖商品视图容器控制类(商城) class SpecialSaleViewController: BaseViewController { // MARK: - 属性列表 /// 商品数据集合视图 @IBOutlet weak var collectionView: UICollectionView! { didSet { if collectionView.responds(to: #selector(setter: UICollectionView.isPrefetchingEnabled)) { collectionView.isPrefetchingEnabled = false } /// 设置约束 collectionView.setCollectionViewLayout(flowLayout, animated: true) } } /// 数据显示集合视图约束 lazy var flowLayout: UICollectionViewFlowLayout = { /// 最小行间距,默认是0 $0.minimumLineSpacing = 0 /// 最小左右间距,默认是10 $0.minimumInteritemSpacing = 0 /// 区域内间距,默认是 UIEdgeInsetsMake(0, 0, 0, 0) $0.sectionInset = UIEdgeInsets(top: 0.0, left: 0, bottom: 0, right: 0) /// 水平滚动 $0.scrollDirection = .vertical return $0 } (UICollectionViewFlowLayout()) /// GoodsCellIdentifier let GoodsCellIdentifier = "SpecialGoods_Cell" /// 商品数据信息 var goods: [MoGoodsInfo] = [] { didSet { /// 刷新数据 collectionView.reloadData() /// 是否隐藏按钮 self.noDataButton.isHidden = goods.count != 0 } } /// 类目 var category: MoCategory? { didSet { guard let obj = category else { return } /// 导航栏 self.navigationItem.title = obj.categoryname } } var couponHold: MoMyCouponHold? { didSet { guard let obj = couponHold else { return } /// 导航栏 self.navigationItem.title = obj.couponname } } /// 商品id 主要用于检索 var goodsid: Int? { didSet { /// 导航栏 self.navigationItem.title = "特卖商城" } } /// 市场ID var marketid: String? // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// UI界面初始化 buildView() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) /// 不为空 if goodsid != nil || couponHold != nil || category != nil { /// 隐藏导航栏 self.navigationController?.setNavigationBarHidden(false, animated: true) } } // MARK: - 数据初始化 /// 数据初始化 func initData() { if MTP2BusinessCore.shared.getLoginStatus() { if let obj = category { /// 获取该类目下的商品数据 requestQueryHsbyMarketGoodses(obj.categoryid, nil, nil) } else if let hold = couponHold { /// 获取该优惠券下的商品数据 requestQueryHsbyMarketGoodses(nil, hold.coupontypeid, nil) } else if let id = goodsid { /// 获取该goodsid下的商品数据 requestQueryHsbyMarketGoodses(nil, nil, id) } else { /// 获取所有的商品数据 requestQueryHsbyMarketGoodses(nil, nil, nil) } } else { if let obj = category { /// 获取该类目下的商品数据 requestQueryHsbyVisitorMarketGoodses(obj.categoryid, nil, nil) } else if let hold = couponHold { /// 获取该优惠券下的商品数据 requestQueryHsbyVisitorMarketGoodses(nil, hold.coupontypeid, nil) } else if let id = goodsid { /// 获取该goodsid下的商品数据 requestQueryHsbyVisitorMarketGoodses(nil, nil, id) } else { /// 获取所有的商品数据 requestQueryHsbyVisitorMarketGoodses(nil, nil, nil) } } } /// UI界面初始化 fileprivate func buildView() { /// loding...... addLoadingView() /// 添加下拉刷新控件 self.collectionView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) { /// 重置请求条件 self.initData() } } // MARK: - 接口请求 /// 查询特卖商品列表(三级商城) /// - Parameters: /// - categoryID: 类目ID /// - couponTypeID: 优惠券类别ID /// - goodsid: 商品ID fileprivate func requestQueryHsbyMarketGoodses(_ categoryID: Int?, _ couponTypeID: Int?, _ goodsid: Int?) { /// 异常 guard let goodsManager = MTP2BusinessCore.shared.goodsManager, let accountID = MTP2BusinessCore.shared.accountManager?.getCurrentTAAccountInfo()?.accountId else { return } /// startAnimating self._anim?.startAnimating() /// 发送请求 goodsManager.requestQueryHsbyMarketGoodses(marketid ?? "", accountID, categoryID, goodsid?.description, couponTypeID) { (isComplete, error, goodses) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() /// endRefreshing self.collectionView.endRefreshing() /// 数据获取成功 self.goods = goodses ?? [] /// showError if !isComplete { WHToast.showError(withMessage: "数据获取失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.0, finishHandler: {}) } } } } /// 查询游客特卖商品列表(三级商城) /// - Parameters: /// - categoryID: 类目ID /// - couponTypeID: 优惠券类别ID /// - goodsid: 商品ID fileprivate func requestQueryHsbyVisitorMarketGoodses(_ categoryID: Int?, _ couponTypeID: Int?, _ goodsid: Int?) { /// 异常 guard let goodsManager = MTP2BusinessCore.shared.goodsManager else { return } /// startAnimating self._anim?.startAnimating() /// 发送请求 goodsManager.requestQueryHsbyVisitorMarketGoodses(marketid ?? "", categoryID, goodsid?.description, couponTypeID) { (isComplete, error, goodses) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() /// endRefreshing self.collectionView.endRefreshing() /// 数据获取成功 self.goods = goodses ?? [] /// showError if !isComplete { WHToast.showError(withMessage: "数据获取失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.0, 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. if segue.identifier == "ShowGoodsDetail" { /// 商品详情 (segue.destination as? GoodsDetailViewController)?.model = (sender as? SpecialGoodsCell)?.model } } } // MARK: - UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout extension SpecialSaleViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return self.goods.count } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GoodsCellIdentifier, for: indexPath) as! SpecialGoodsCell cell.model = self.goods[indexPath.item] return cell } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return CGSize(width: collectionView.width/2, height: ((collectionView.width/2)*0.9)+(MTP2BusinessCore.shared.getLoginStatus() ? 100.0 : 75.0)) } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {} } /// 特卖商城数据Cell class SpecialGoodsCell: UICollectionViewCell { /// 背景图片 @IBOutlet weak var image: UIImageView! /// 商品名称 @IBOutlet weak var goodsName: UILabel! /// 价格 @IBOutlet weak var price: UILabel! /// 优惠券 @IBOutlet weak var score: UILabel! /// 店铺名 @IBOutlet weak var storeName: UILabel! /// 数据集合 var model: MoGoodsInfo? { didSet { /// 更新显示行情数据 guard let obj = model else { return } /// 商品名称 goodsName.text = obj.goodsname /// 抢购价 price.attributedText = (("\(obj.currencysign) ".withFont(.font_12))+("\(obj.goodsprice.toDownString(reserve: obj.decimalplace))".withFont(.font_16))).withTextColor(.red) /// 是否有优惠券 score.isHidden = !obj.hascoupon if MTP2BusinessCore.shared.getLoginStatus() { /// 发行单位 storeName.text = obj.customername } else { /// 未登录不需要显示 storeName.isHidden = true } /// 地址异常 guard let url = StringUtils.getImageUrl(obj.picurls) else { return } /// 背景图片 image.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil) } } }