SpecialSaleViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. // MARK: - 生命周期
  77. override func viewDidLoad() {
  78. super.viewDidLoad()
  79. // Do any additional setup after loading the view.
  80. /// UI界面初始化
  81. buildView()
  82. }
  83. override func viewWillAppear(_ animated: Bool) {
  84. super.viewWillAppear(animated)
  85. /// 不为空
  86. if goodsid != nil || couponHold != nil || category != nil {
  87. /// 隐藏导航栏
  88. self.navigationController?.setNavigationBarHidden(false, animated: true)
  89. }
  90. }
  91. override func didMove(toParent parent: UIViewController?) {
  92. super.didMove(toParent: parent)
  93. if let _ = parent {
  94. /// 数据初始化
  95. initData()
  96. } else {
  97. noDataButton.isHidden = true
  98. }
  99. }
  100. // MARK: - 数据初始化
  101. /// 数据初始化
  102. fileprivate func initData() {
  103. if MTP2BusinessCore.shared.getLoginStatus() {
  104. if let obj = category {
  105. /// 获取该类目下的商品数据
  106. requestQueryHsbyMarketGoodses(obj.categoryid, nil, nil)
  107. } else if let hold = couponHold {
  108. /// 获取该优惠券下的商品数据
  109. requestQueryHsbyMarketGoodses(nil, hold.coupontypeid, nil)
  110. } else if let id = goodsid {
  111. /// 获取该goodsid下的商品数据
  112. requestQueryHsbyMarketGoodses(nil, nil, id)
  113. } else {
  114. /// 获取所有的商品数据
  115. requestQueryHsbyMarketGoodses(nil, nil, nil)
  116. }
  117. } else {
  118. if let obj = category {
  119. /// 获取该类目下的商品数据
  120. requestQueryHsbyVisitorMarketGoodses(obj.categoryid, nil, nil)
  121. } else if let hold = couponHold {
  122. /// 获取该优惠券下的商品数据
  123. requestQueryHsbyVisitorMarketGoodses(nil, hold.coupontypeid, nil)
  124. } else if let id = goodsid {
  125. /// 获取该goodsid下的商品数据
  126. requestQueryHsbyVisitorMarketGoodses(nil, nil, id)
  127. } else {
  128. /// 获取所有的商品数据
  129. requestQueryHsbyVisitorMarketGoodses(nil, nil, nil)
  130. }
  131. }
  132. }
  133. /// UI界面初始化
  134. fileprivate func buildView() {
  135. /// loding......
  136. addLoadingView()
  137. /// 添加下拉刷新控件
  138. self.collectionView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  139. /// 重置请求条件
  140. self.initData()
  141. }
  142. }
  143. // MARK: - 接口请求
  144. /// 查询特卖商品列表(三级商城)
  145. /// - Parameters:
  146. /// - categoryID: 类目ID
  147. /// - couponTypeID: 优惠券类别ID
  148. /// - goodsid: 商品ID
  149. fileprivate func requestQueryHsbyMarketGoodses(_ categoryID: Int?,
  150. _ couponTypeID: Int?,
  151. _ goodsid: Int?) {
  152. /// 异常
  153. guard let goodsManager = MTP2BusinessCore.shared.goodsManager,
  154. let accountID = MTP2BusinessCore.shared.accountManager?.getCurrentTAAccountInfo()?.accountId else {
  155. return
  156. }
  157. /// startAnimating
  158. self._anim?.startAnimating()
  159. /// 发送请求
  160. goodsManager.requestQueryHsbyMarketGoodses(goodsManager.getMarketIDs(.TRADEMODE_TRADEMODE_HSBY_SHOP), accountID, categoryID, goodsid?.description, couponTypeID) { (isComplete, error, goodses) in
  161. DispatchQueue.main.async {
  162. /// stopAnimating
  163. self._anim?.stopAnimating()
  164. /// endRefreshing
  165. self.collectionView.endRefreshing()
  166. /// 数据获取成功
  167. self.goods = goodses ?? []
  168. /// showError
  169. if !isComplete {
  170. WHToast.showError(withMessage: "数据获取失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.0, finishHandler: {})
  171. }
  172. }
  173. }
  174. }
  175. /// 查询游客特卖商品列表(三级商城)
  176. /// - Parameters:
  177. /// - categoryID: 类目ID
  178. /// - couponTypeID: 优惠券类别ID
  179. /// - goodsid: 商品ID
  180. fileprivate func requestQueryHsbyVisitorMarketGoodses(_ categoryID: Int?,
  181. _ couponTypeID: Int?,
  182. _ goodsid: Int?) {
  183. /// 异常
  184. guard let goodsManager = MTP2BusinessCore.shared.goodsManager else {
  185. return
  186. }
  187. /// startAnimating
  188. self._anim?.startAnimating()
  189. /// 发送请求
  190. goodsManager.requestQueryHsbyVisitorMarketGoodses(goodsManager.getMarketIDs(.TRADEMODE_TRADEMODE_HSBY_SHOP), categoryID, goodsid?.description, couponTypeID) { (isComplete, error, goodses) in
  191. DispatchQueue.main.async {
  192. /// stopAnimating
  193. self._anim?.stopAnimating()
  194. /// endRefreshing
  195. self.collectionView.endRefreshing()
  196. /// 数据获取成功
  197. self.goods = goodses ?? []
  198. /// showError
  199. if !isComplete {
  200. WHToast.showError(withMessage: "数据获取失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.0, finishHandler: {})
  201. }
  202. }
  203. }
  204. }
  205. // MARK: - Navigation
  206. // In a storyboard-based application, you will often want to do a little preparation before navigation
  207. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  208. // Get the new view controller using segue.destination.
  209. // Pass the selected object to the new view controller.
  210. if segue.identifier == "ShowGoodsDetail" {
  211. /// 商品详情
  212. (segue.destination as? GoodsDetailViewController)?.model = (sender as? SpecialGoodsCell)?.model
  213. }
  214. }
  215. }
  216. // MARK: - UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
  217. extension SpecialSaleViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  218. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  219. return self.goods.count
  220. }
  221. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  222. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GoodsCellIdentifier, for: indexPath) as! SpecialGoodsCell
  223. cell.model = self.goods[indexPath.item]
  224. return cell
  225. }
  226. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  227. return CGSize(width: collectionView.width/2, height: ((collectionView.width/2)*0.9)+(MTP2BusinessCore.shared.getLoginStatus() ? 100.0 : 75.0))
  228. }
  229. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {}
  230. }
  231. /// 特卖商城数据Cell
  232. class SpecialGoodsCell: UICollectionViewCell {
  233. /// 背景图片
  234. @IBOutlet weak var image: UIImageView!
  235. /// 商品名称
  236. @IBOutlet weak var goodsName: UILabel!
  237. /// 价格
  238. @IBOutlet weak var price: UILabel!
  239. /// 优惠券
  240. @IBOutlet weak var score: UILabel!
  241. /// 店铺名
  242. @IBOutlet weak var storeName: UILabel!
  243. /// 数据集合
  244. var model: MoGoodsInfo? {
  245. didSet {
  246. /// 更新显示行情数据
  247. guard let obj = model else { return }
  248. /// 商品名称
  249. goodsName.text = obj.goodsname
  250. /// 抢购价
  251. price.attributedText = (("\(obj.currencysign) ".withFont(.font_12))+("\(obj.goodsprice.toDownString(reserve: obj.decimalplace))".withFont(.font_16))).withTextColor(.red)
  252. /// 是否有优惠券
  253. score.isHidden = !obj.hascoupon
  254. if MTP2BusinessCore.shared.getLoginStatus() {
  255. /// 发行单位
  256. storeName.text = obj.customername
  257. } else {
  258. /// 未登录不需要显示
  259. storeName.isHidden = true
  260. }
  261. /// 地址异常
  262. guard let url = StringUtils.getImageUrl(obj.picurls) else { return }
  263. /// 背景图片
  264. image.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil)
  265. }
  266. }
  267. }