NewQuoteViewController.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // NewQuoteViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2020/10/30.
  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 NewQuoteViewController: BaseViewController {
  16. // MARK: - 属性列表
  17. /// 商品展示列表
  18. @IBOutlet weak var tableView: UITableView!
  19. /// 位置按钮
  20. @IBOutlet weak var location: UIButton!
  21. /// CellIdentifier
  22. let CellIdentifier = "Goods_Cell"
  23. /// 省份信息
  24. var geographics: [MoGeographicPosition] = []
  25. /// 市区信息
  26. var city: MoGeographic? {
  27. didSet {
  28. self.location.setTitle(city?.pathname ?? "全部", for: .normal)
  29. /// 查询商品
  30. self.requestQueryHotGoods(self.city)
  31. }
  32. }
  33. /// 商品数据
  34. var preGoods: [MoGoodsInfo] = [] {
  35. didSet {
  36. /// 刷新数据
  37. tableView.reloadData()
  38. /// 是否隐藏按钮
  39. self.noDataButton.isHidden = preGoods.count != 0
  40. }
  41. }
  42. // MARK: - 生命周期
  43. override func viewDidLoad() {
  44. super.viewDidLoad()
  45. // Do any additional setup after loading the view.
  46. /// UI界面初始化
  47. buildView()
  48. }
  49. override func didMove(toParent parent: UIViewController?) {
  50. super.didMove(toParent: parent)
  51. if let _ = parent {
  52. /// 数据初始化
  53. initData()
  54. } else {
  55. noDataButton.isHidden = true
  56. }
  57. }
  58. // MARK: - 数据初始化
  59. /// 数据初始化
  60. fileprivate func initData() {
  61. /// 查询热门商品信息
  62. requestQueryHotGoods(self.city)
  63. }
  64. /// UI界面初始化
  65. fileprivate func buildView() {
  66. /// 异常
  67. guard let accountManager = MTP2BusinessCore.shared.accountManager else {
  68. return
  69. }
  70. /// loding......
  71. addLoadingView()
  72. /// 赋值省市区信息
  73. self.geographics = accountManager.geographics ?? []
  74. /// 添加下拉刷新控件
  75. self.tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  76. /// 重置请求条件
  77. self.initData()
  78. }
  79. }
  80. // MARK: - 接口请求
  81. /// 获取热门商品数据信息
  82. fileprivate func requestQueryHotGoods(_ city: MoGeographic?) {
  83. /// 异常
  84. guard let goodsManager = MTP2BusinessCore.shared.goodsManager else {
  85. return
  86. }
  87. /// 目标省份
  88. let descProvinceID = (city == nil || city?.divisionlevel != "province") ? nil : city?.autoid
  89. /// 目标城市
  90. let descCityID = (city == nil || city?.divisionlevel != "city") ? nil : city?.autoid
  91. /// startAnimating
  92. self._anim?.startAnimating()
  93. /// 发送请求
  94. goodsManager.requestQueryHsbyPreGoodses(goodsManager.getMarketIDs(.TRADEMODE_TRADEMODE_CPTRADE_LS), descProvinceID, descCityID) { (isComplete, error, preGoods) in
  95. DispatchQueue.main.async {
  96. /// stopAnimating
  97. self._anim?.stopAnimating()
  98. /// endRefreshing
  99. self.tableView.endRefreshing()
  100. if isComplete {
  101. /// 数据获取成功
  102. self.preGoods = preGoods ?? []
  103. } else {
  104. self.preGoods = []
  105. /// toast
  106. WHToast.showError(withMessage: "数据获取失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.0, finishHandler: {})
  107. }
  108. }
  109. }
  110. }
  111. // MARK: - 交互相关
  112. /// onButtonPressed
  113. /// - Parameter sender: sender
  114. @IBAction fileprivate func onButtonPressed(_ sender: UIButton) {
  115. switch sender {
  116. case location: /// 位置
  117. let geographicPickView = GeographicPickView(nibName: "GeographicPickView", bundle: nil)
  118. geographicPickView.modalTransitionStyle = .coverVertical
  119. geographicPickView.modalPresentationStyle = .overFullScreen
  120. geographicPickView.callback = { (_ takeInfo: MoGeographic?, _ isSure: Bool) in
  121. self.city = takeInfo
  122. }
  123. self.present(geographicPickView, animated: true, completion: {})
  124. default:
  125. break
  126. }
  127. }
  128. // MARK: - Navigation
  129. // In a storyboard-based application, you will often want to do a little preparation before navigation
  130. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  131. // Get the new view controller using segue.destination.
  132. // Pass the selected object to the new view controller.
  133. if segue.identifier == "ShowGoodsDetail" {
  134. /// 商品详情
  135. (segue.destination as? GoodsDetailViewController)?.model = (sender as? QuoteCell)?.model
  136. }
  137. }
  138. }
  139. // MARK: - UITableViewDelegate, UITableViewDataSource
  140. extension NewQuoteViewController: UITableViewDelegate, UITableViewDataSource {
  141. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  142. return preGoods.count
  143. }
  144. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  145. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! QuoteCell
  146. cell.model = preGoods[indexPath.row]
  147. return cell
  148. }
  149. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  150. return 210.0
  151. }
  152. }
  153. // MARK: - QuoteCell
  154. class QuoteCell: BaseTableViewCell<MoGoodsInfo> {
  155. /// 商品状态
  156. @IBOutlet weak var status: UILabel!
  157. /// 商品状态
  158. @IBOutlet weak var statusImage: UIImageView!
  159. /// 倒计时
  160. @IBOutlet weak var timeDown: UILabel!
  161. /// 抢购价
  162. @IBOutlet weak var price: UILabel!
  163. /// 商品名称
  164. @IBOutlet weak var goodsName: UILabel!
  165. /// 商品图片
  166. @IBOutlet weak var goodsImage: UIImageView!
  167. /// stackView
  168. @IBOutlet weak var stackView: UIStackView! {
  169. didSet {
  170. stackView.layer.masksToBounds = true
  171. stackView.layer.cornerRadius = 7.5
  172. }
  173. }
  174. @IBOutlet weak var priceView: UIImageView!
  175. /// 商品数据
  176. override var model: MoGoodsInfo? {
  177. didSet {
  178. guard let obj = model else { return }
  179. /// 商品状态- 1:待审核 2:未上市 3:上市 4:已注销 5:审核拒绝 6:退市 7:待退市
  180. status.text = obj.goodsstatus == 3 ? "抢购中" : "预告"
  181. /// statusImage
  182. statusImage.image = UIImage(named: obj.goodsstatus == 3 ? "goods_detail_red" : "goods_detail_blue")
  183. /// 价格视图
  184. priceView.image = UIImage(named: obj.goodsstatus == 3 ? "image_blued_grident" : "image_read_grident")
  185. if obj.goodsstatus == 3 {
  186. /// 倒计时
  187. timeDown.attributedText = " \(DateUtils.getTDateString(obj.lasttradedate)) 结束".withTextColor(.white)
  188. } else {
  189. /// 倒计时
  190. timeDown.text = "\(DateUtils.getTDateString(obj.listingdate)) 上市"
  191. }
  192. /// 商品名称
  193. goodsName.text = obj.goodsname
  194. /// 抢购价
  195. price.text = "抢购价:\(obj.currencysign)"+obj.refprice.description
  196. /// 商品图片
  197. guard let url = StringUtils.getImageUrl(obj.picurls) else { return }
  198. goodsImage.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil)
  199. }
  200. }
  201. }