MyGoodsViewController.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // MyGoodsViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2020/11/12.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import WHToast
  10. import GTMRefresh
  11. /// 我的商品视图容器控制类
  12. class MyGoodsViewController: BaseViewController {
  13. // MARK: - 属性列表
  14. /// 我的商品
  15. @IBOutlet weak var tableView: UITableView!
  16. /// MyOrdersCellIdentifier
  17. let MyOrdersCellIdentifier = "MyOrders_Cell"
  18. /// 我的商品数据信息
  19. var myGoods: [MoHsbyMyGoods] = [] {
  20. didSet {
  21. /// 刷新列表数据
  22. tableView.reloadData()
  23. /// 隐藏按钮
  24. self.noDataButton.isHidden = !(myGoods.count == 0)
  25. }
  26. }
  27. /// 执行回调块
  28. var block: ((_ model: MoHsbyMyGoods?) -> Void)?
  29. // MARK: - 生命周期
  30. override func viewDidLoad() {
  31. super.viewDidLoad()
  32. // Do any additional setup after loading the view.
  33. /// UI界面初始化
  34. self.buildView()
  35. /// 数据初始化
  36. self.initData()
  37. }
  38. override func viewWillAppear(_ animated: Bool) {
  39. super.viewWillAppear(animated)
  40. /// 隐藏导航栏
  41. self.navigationController?.setNavigationBarHidden(false, animated: true)
  42. }
  43. // MARK: - 初始化
  44. /// UI界面初始化
  45. fileprivate func buildView() {
  46. /// addLoadingView
  47. self.addLoadingView()
  48. /// 添加下拉刷新控件
  49. self.tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  50. /// 查询我的商品数据请求
  51. self.requestQueryMyGoods()
  52. }
  53. }
  54. /// 数据初始化
  55. fileprivate func initData() {
  56. /// 查询我的商品数据请求
  57. self.tableView.triggerRefreshing()
  58. }
  59. // MARK: - 接口请求
  60. /// 查询我的商品数据请求
  61. func requestQueryMyGoods() {
  62. /// 异常
  63. guard let orderManager = MTP2BusinessCore.shared.orderManager else {
  64. return
  65. }
  66. /// startAnimating
  67. self._anim?.startAnimating()
  68. /// 发送请求
  69. orderManager.requestQueryHsbyMyGoods { (isComplete, error, goods) in
  70. DispatchQueue.main.async {
  71. /// stopAnimating
  72. self._anim?.stopAnimating()
  73. /// endRefreshing
  74. self.tableView.endRefreshing(isSuccess: true)
  75. /// 数据赋值
  76. if isComplete {
  77. self.myGoods = goods ?? []
  78. } else {
  79. self.myGoods = []
  80. WHToast.showError(withMessage: "数据查询失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {})
  81. }
  82. }
  83. }
  84. }
  85. /*
  86. // MARK: - Navigation
  87. // In a storyboard-based application, you will often want to do a little preparation before navigation
  88. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  89. // Get the new view controller using segue.destination.
  90. // Pass the selected object to the new view controller.
  91. }
  92. */
  93. }
  94. // MARK: - 初始化相关
  95. extension MyGoodsViewController: UITableViewDelegate, UITableViewDataSource {
  96. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  97. return myGoods.count
  98. }
  99. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  100. let cell = tableView.dequeueReusableCell(withIdentifier: MyOrdersCellIdentifier, for: indexPath) as! MyOrdersCell
  101. cell.model = myGoods[indexPath.row]
  102. return cell
  103. }
  104. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  105. return 115.0
  106. }
  107. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  108. /// 执行回调
  109. if let _ = block {
  110. block!(myGoods[indexPath.row])
  111. /// 返回上层视图
  112. self.navigationController?.popViewController(animated: true)
  113. } else {
  114. /// 商品详情
  115. guard let goods = MTP2BusinessCore.shared.goodsManager?.goodsInfos.first(where: {$0.goodsid == myGoods[indexPath.row].goodsid}) else { return }
  116. let goodsDetailController = UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "GoodsDetail") as! GoodsDetailViewController
  117. goodsDetailController.model = goods
  118. self.navigationController?.pushViewController(goodsDetailController, animated: true)
  119. }
  120. }
  121. }