SpecialSaleViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //
  2. // SpecialSaleViewController.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 WHToast
  10. import SDWebImage
  11. import SwiftDate
  12. import SwiftyAttributes
  13. import GTMRefresh
  14. /// 特卖商品视图容器控制类(商城)
  15. class SpecialSaleViewController: BaseViewController {
  16. // MARK: - 属性列表
  17. /// 商品数据集合视图
  18. @IBOutlet weak var collectionView: UICollectionView! {
  19. didSet {
  20. if collectionView.responds(to: #selector(setter: UICollectionView.isPrefetchingEnabled)) {
  21. collectionView.isPrefetchingEnabled = false
  22. }
  23. /// 设置约束
  24. collectionView.setCollectionViewLayout(flowLayout, animated: true)
  25. }
  26. }
  27. /// 数据显示集合视图约束
  28. lazy var flowLayout: UICollectionViewFlowLayout = {
  29. /// 最小行间距,默认是0
  30. $0.minimumLineSpacing = 0
  31. /// 最小左右间距,默认是10
  32. $0.minimumInteritemSpacing = 0
  33. /// 区域内间距,默认是 UIEdgeInsetsMake(0, 0, 0, 0)
  34. $0.sectionInset = UIEdgeInsets(top: 0.0, left: 0, bottom: 0, right: 0)
  35. /// 水平滚动
  36. $0.scrollDirection = .vertical
  37. return $0
  38. } (UICollectionViewFlowLayout())
  39. /// GoodsCellIdentifier
  40. let GoodsCellIdentifier = "SpecialGoods_Cell"
  41. /// 商品数据信息
  42. var goods: [MoGoodsInfo] = [] {
  43. didSet {
  44. /// 刷新数据
  45. collectionView.reloadData()
  46. /// 是否隐藏按钮
  47. self.noDataButton.isHidden = goods.count != 0
  48. }
  49. }
  50. /// 类目
  51. var category: MoCategory? {
  52. didSet {
  53. guard let obj = category else {
  54. return
  55. }
  56. /// 导航栏
  57. self.navigationItem.title = obj.categoryname
  58. }
  59. }
  60. var couponHold: MoMyCouponHold? {
  61. didSet {
  62. guard let obj = couponHold else {
  63. return
  64. }
  65. /// 导航栏
  66. self.navigationItem.title = obj.couponname
  67. }
  68. }
  69. /// 商品id 主要用于检索
  70. var goodsid: Int? {
  71. didSet {
  72. /// 导航栏
  73. self.navigationItem.title = "特卖商城"
  74. }
  75. }
  76. /// 市场ID
  77. var marketid: String?
  78. // MARK: - 生命周期
  79. override func viewDidLoad() {
  80. super.viewDidLoad()
  81. // Do any additional setup after loading the view.
  82. /// UI界面初始化
  83. buildView()
  84. }
  85. override func viewWillAppear(_ animated: Bool) {
  86. super.viewWillAppear(animated)
  87. /// 不为空
  88. if goodsid != nil || couponHold != nil || category != nil {
  89. /// 隐藏导航栏
  90. self.navigationController?.setNavigationBarHidden(false, animated: true)
  91. }
  92. }
  93. // MARK: - 数据初始化
  94. /// 数据初始化
  95. func initData() {
  96. if MTP2BusinessCore.shared.getLoginStatus() {
  97. if let obj = category {
  98. /// 获取该类目下的商品数据
  99. requestQueryHsbyMarketGoodses(obj.categoryid, nil, nil)
  100. } else if let hold = couponHold {
  101. /// 获取该优惠券下的商品数据
  102. requestQueryHsbyMarketGoodses(nil, hold.coupontypeid, nil)
  103. } else if let id = goodsid {
  104. /// 获取该goodsid下的商品数据
  105. requestQueryHsbyMarketGoodses(nil, nil, id)
  106. } else {
  107. /// 获取所有的商品数据
  108. requestQueryHsbyMarketGoodses(nil, nil, nil)
  109. }
  110. } else {
  111. if let obj = category {
  112. /// 获取该类目下的商品数据
  113. requestQueryHsbyVisitorMarketGoodses(obj.categoryid, nil, nil)
  114. } else if let hold = couponHold {
  115. /// 获取该优惠券下的商品数据
  116. requestQueryHsbyVisitorMarketGoodses(nil, hold.coupontypeid, nil)
  117. } else if let id = goodsid {
  118. /// 获取该goodsid下的商品数据
  119. requestQueryHsbyVisitorMarketGoodses(nil, nil, id)
  120. } else {
  121. /// 获取所有的商品数据
  122. requestQueryHsbyVisitorMarketGoodses(nil, nil, nil)
  123. }
  124. }
  125. }
  126. /// UI界面初始化
  127. fileprivate func buildView() {
  128. /// loding......
  129. addLoadingView()
  130. /// 添加下拉刷新控件
  131. self.collectionView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  132. /// 重置请求条件
  133. self.initData()
  134. }
  135. }
  136. // MARK: - 接口请求
  137. /// 查询特卖商品列表(三级商城)
  138. /// - Parameters:
  139. /// - categoryID: 类目ID
  140. /// - couponTypeID: 优惠券类别ID
  141. /// - goodsid: 商品ID
  142. fileprivate func requestQueryHsbyMarketGoodses(_ categoryID: Int?,
  143. _ couponTypeID: Int?,
  144. _ goodsid: Int?) {
  145. /// 异常
  146. guard let goodsManager = MTP2BusinessCore.shared.goodsManager,
  147. let accountID = MTP2BusinessCore.shared.accountManager?.getCurrentTAAccountInfo()?.accountId else {
  148. return
  149. }
  150. /// startAnimating
  151. self._anim?.startAnimating()
  152. /// 发送请求
  153. goodsManager.requestQueryHsbyMarketGoodses(marketid ?? "", accountID, categoryID, goodsid?.description, couponTypeID) { (isComplete, error, goodses) in
  154. DispatchQueue.main.async {
  155. /// stopAnimating
  156. self._anim?.stopAnimating()
  157. /// endRefreshing
  158. self.collectionView.endRefreshing()
  159. /// 数据获取成功
  160. self.goods = goodses ?? []
  161. /// showError
  162. if !isComplete {
  163. WHToast.showError(withMessage: "数据获取失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.0, finishHandler: {})
  164. }
  165. }
  166. }
  167. }
  168. /// 查询游客特卖商品列表(三级商城)
  169. /// - Parameters:
  170. /// - categoryID: 类目ID
  171. /// - couponTypeID: 优惠券类别ID
  172. /// - goodsid: 商品ID
  173. fileprivate func requestQueryHsbyVisitorMarketGoodses(_ categoryID: Int?,
  174. _ couponTypeID: Int?,
  175. _ goodsid: Int?) {
  176. /// 异常
  177. guard let goodsManager = MTP2BusinessCore.shared.goodsManager else {
  178. return
  179. }
  180. /// startAnimating
  181. self._anim?.startAnimating()
  182. /// 发送请求
  183. goodsManager.requestQueryHsbyVisitorMarketGoodses(marketid ?? "", categoryID, goodsid?.description, couponTypeID) { (isComplete, error, goodses) in
  184. DispatchQueue.main.async {
  185. /// stopAnimating
  186. self._anim?.stopAnimating()
  187. /// endRefreshing
  188. self.collectionView.endRefreshing()
  189. /// 数据获取成功
  190. self.goods = goodses ?? []
  191. /// showError
  192. if !isComplete {
  193. WHToast.showError(withMessage: "数据获取失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.0, finishHandler: {})
  194. }
  195. }
  196. }
  197. }
  198. // MARK: - Navigation
  199. // In a storyboard-based application, you will often want to do a little preparation before navigation
  200. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  201. // Get the new view controller using segue.destination.
  202. // Pass the selected object to the new view controller.
  203. if segue.identifier == "ShowGoodsDetail" {
  204. /// 商品详情
  205. (segue.destination as? GoodsDetailViewController)?.model = (sender as? SpecialGoodsCell)?.model
  206. }
  207. }
  208. }
  209. // MARK: - UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
  210. extension SpecialSaleViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  211. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  212. return self.goods.count
  213. }
  214. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  215. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GoodsCellIdentifier, for: indexPath) as! SpecialGoodsCell
  216. cell.model = self.goods[indexPath.item]
  217. return cell
  218. }
  219. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  220. return CGSize(width: collectionView.width/2, height: ((collectionView.width/2)*0.9)+(MTP2BusinessCore.shared.getLoginStatus() ? 100.0 : 75.0))
  221. }
  222. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {}
  223. }
  224. /// 特卖商城数据Cell
  225. class SpecialGoodsCell: UICollectionViewCell {
  226. /// 背景图片
  227. @IBOutlet weak var image: UIImageView!
  228. /// 商品名称
  229. @IBOutlet weak var goodsName: UILabel!
  230. /// 价格
  231. @IBOutlet weak var price: UILabel!
  232. /// 优惠券
  233. @IBOutlet weak var score: UILabel!
  234. /// 店铺名
  235. @IBOutlet weak var storeName: UILabel!
  236. /// 数据集合
  237. var model: MoGoodsInfo? {
  238. didSet {
  239. /// 更新显示行情数据
  240. guard let obj = model else { return }
  241. /// 商品名称
  242. goodsName.text = obj.goodsname
  243. /// 抢购价
  244. price.attributedText = (("\(obj.currencysign) ".withFont(.font_12))+("\(obj.goodsprice.toDownString(reserve: obj.decimalplace))".withFont(.font_16))).withTextColor(.red)
  245. /// 是否有优惠券
  246. score.isHidden = !obj.hascoupon
  247. if MTP2BusinessCore.shared.getLoginStatus() {
  248. /// 发行单位
  249. storeName.text = obj.customername
  250. } else {
  251. /// 未登录不需要显示
  252. storeName.isHidden = true
  253. }
  254. /// 地址异常
  255. guard let url = StringUtils.getImageUrl(obj.picurls) else { return }
  256. /// 背景图片
  257. image.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil)
  258. }
  259. }
  260. }