PacketViewController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. //
  2. // PacketViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2020/10/28.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import WHToast
  10. import SDWebImage
  11. import GTMRefresh
  12. import IBAnimatable
  13. import JXSegmentedView
  14. /// 我的包裹视图容器控制类
  15. class PacketViewController: BaseViewController {
  16. // MARK: - 属性列表
  17. /// 分段控制器
  18. @IBOutlet weak var segmentedView: JXSegmentedView! {
  19. didSet {
  20. segmentedView.delegate = self
  21. segmentedView.dataSource = dataSource
  22. segmentedView.indicators = [self.indicator]
  23. }
  24. }
  25. /// segmentedDataSource
  26. let dataSource: JXSegmentedTitleDataSource = {
  27. var items: [String] = ["全部", "待发货", "待收货", "已完成"]
  28. $0.titles = items
  29. $0.titleNormalColor = UIColorFromHex(rgbValue: 0x333333)
  30. $0.titleNormalFont = .font_14
  31. $0.titleSelectedColor = UIColorFromHex(rgbValue: 0x60a1e3)
  32. $0.titleSelectedFont = .font_14
  33. $0.isTitleColorGradientEnabled = true
  34. return $0
  35. } (JXSegmentedTitleDataSource())
  36. /// 数据展示列表
  37. @IBOutlet weak var tableView: UITableView!
  38. // CellIdentifier
  39. let CellIdentifier = "Packet_Cell"
  40. /// 我的包裹单据数据信息
  41. var myPackages: [MoHsbyMyPackages] = [] {
  42. didSet {
  43. /// 刷新列表数据
  44. tableView.reloadData()
  45. /// 隐藏按钮
  46. self.noDataButton.isHidden = !(myPackages.count == 0)
  47. }
  48. }
  49. // MARK: - 生命周期
  50. override func viewDidLoad() {
  51. super.viewDidLoad()
  52. // Do any additional setup after loading the view.
  53. /// UI界面初始化
  54. buildView()
  55. }
  56. override func viewWillAppear(_ animated: Bool) {
  57. super.viewWillAppear(animated)
  58. /// 隐藏导航栏
  59. self.navigationController?.setNavigationBarHidden(false, animated: true)
  60. }
  61. override func viewDidAppear(_ animated: Bool) {
  62. super.viewDidAppear(animated)
  63. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.2) {
  64. /// 默认滚动
  65. self.segmentedView.defaultSelectedIndex = self.selectedIndex
  66. /// 刷新数据
  67. self.segmentedView.reloadData()
  68. /// 刷新数据
  69. self.tableView.triggerRefreshing()
  70. }
  71. }
  72. // MARK: - 初始化
  73. /// UI界面初始化
  74. fileprivate func buildView() {
  75. /// loding......
  76. self.addLoadingView()
  77. /// 添加下拉刷新控件
  78. self.tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  79. /// 重置请求条件
  80. self.reloadData(self.segmentedView.selectedIndex)
  81. }
  82. }
  83. // MARK: - 接口请求
  84. /// 刷新数据
  85. fileprivate func reloadData(_ segmentIndex: Int) {
  86. var status: Int?
  87. switch segmentIndex {
  88. case 1, 2, 3: /// 抢购中
  89. status = self.segmentedView.selectedIndex
  90. default:
  91. break
  92. }
  93. /// 查询我的包裹信息
  94. self.requestQueryHsbyMyPackages(status)
  95. }
  96. /// 查询我的包裹信息
  97. /// - Parameter status: status
  98. fileprivate func requestQueryHsbyMyPackages(_ status: Int?) {
  99. /// 异常
  100. guard let orderManager = MTP2BusinessCore.shared.orderManager else { return }
  101. /// startAnimating
  102. _anim?.startAnimating()
  103. /// 发送请求
  104. orderManager.requestQueryHsbyMyPackages(status) { (isComplete, error, myPackages) in
  105. DispatchQueue.main.async {
  106. /// stopAnimating
  107. self._anim?.stopAnimating()
  108. /// endRefreshing
  109. self.tableView.endRefreshing(isSuccess: true)
  110. /// 数据赋值
  111. if isComplete {
  112. self.myPackages = myPackages ?? []
  113. } else { /// 查询失败
  114. self.myPackages = []
  115. WHToast.showError(withMessage: "数据查询失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {})
  116. }
  117. }
  118. }
  119. }
  120. /// 设置我的包裹已收货状态
  121. /// - Parameter takeOrderID: 提货单号
  122. fileprivate func requestSetHsbyMyPackagesStatus(_ takeOrderID: String?) {
  123. /// 异常
  124. guard let orderManager = MTP2BusinessCore.shared.orderManager else { return }
  125. /// startAnimating
  126. _anim?.startAnimating()
  127. /// 发送请求
  128. orderManager.requestSetHsbyMyPackagesStatus(takeOrderID ?? "") { (isComplete, error) in
  129. DispatchQueue.main.async {
  130. /// stopAnimating
  131. self._anim?.stopAnimating()
  132. if isComplete {
  133. WHToast.showSuccess(withMessage: "确认收货成功!", duration: 1.5, finishHandler: {
  134. /// 查询我的包裹信息
  135. self.requestQueryHsbyMyPackages(self.segmentedView.selectedIndex)
  136. })
  137. } else { /// 查询失败
  138. WHToast.showError(withMessage: "确认收货失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {})
  139. }
  140. }
  141. }
  142. }
  143. // MARK: - Navigation
  144. // In a storyboard-based application, you will often want to do a little preparation before navigation
  145. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  146. // Get the new view controller using segue.destination.
  147. // Pass the selected object to the new view controller.
  148. if segue.identifier == "PacketDetail" {
  149. (segue.destination as? PacketDetailViewController)?.obj = (sender as? PacketCell)?.model
  150. }
  151. }
  152. }
  153. // MARK: - UITableViewDelegate, UITableViewDataSource
  154. extension PacketViewController: UITableViewDelegate, UITableViewDataSource {
  155. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  156. return self.myPackages.count
  157. }
  158. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  159. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! PacketCell
  160. cell.model = self.myPackages[indexPath.row]
  161. cell.takeOrderBlock = { (_ takeOrderID: String?) in
  162. guard let id = takeOrderID else {
  163. return
  164. }
  165. /// show alert
  166. self.showAlertController(title: "提示", message: "确定要收货吗?", cancelTitle: "取消", sureTitle: "确认收货") {} sureBlock: {
  167. /// 确认提货
  168. self.requestSetHsbyMyPackagesStatus(id)
  169. }
  170. }
  171. return cell
  172. }
  173. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  174. return self.myPackages[indexPath.row].takeorderstatus == .UNRECEIVED ? 210.0 : 175.0
  175. }
  176. }
  177. // MARK: - JXSegmentedViewDelegate
  178. extension PacketViewController: JXSegmentedViewDelegate {
  179. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  180. /// 重置默认状态
  181. self.selectedIndex = index
  182. /// 刷新数据
  183. self.reloadData(index)
  184. }
  185. }
  186. // MARK: - PacketCell
  187. class PacketCell: BaseTableViewCell<MoHsbyMyPackages> {
  188. /// 公司名称
  189. @IBOutlet weak var company: UILabel!
  190. /// 状态
  191. @IBOutlet weak var status: UILabel!
  192. /// 单号
  193. @IBOutlet weak var orderNo: UILabel!
  194. /// 商品图片
  195. @IBOutlet weak var goodsImage: UIImageView!
  196. /// 价格
  197. @IBOutlet weak var price: UILabel!
  198. /// 金额
  199. @IBOutlet weak var amount: UILabel!
  200. /// 标题
  201. @IBOutlet weak var title: UILabel!
  202. /// 数量
  203. @IBOutlet weak var qty: UILabel!
  204. /// 物流单号
  205. @IBOutlet weak var trackingNo: UILabel!
  206. /// 物流单号
  207. @IBOutlet weak var trackingView: UIStackView!
  208. /// 复制
  209. @IBOutlet weak var copyButton: AnimatableButton!
  210. /// 确认提货
  211. @IBOutlet weak var takeOrder: AnimatableButton!
  212. /// 数据模型
  213. override var model: MoHsbyMyPackages? {
  214. didSet {
  215. guard let obj = model else { return }
  216. /// 单据状态
  217. status.text = obj.takeorderstatus.description
  218. /// 确认收货按钮
  219. takeOrder.isHidden = !(obj.takeorderstatus == .UNRECEIVED)
  220. /// 已完成
  221. trackingView.isHidden = (obj.takeorderstatus == .COMPLETE)
  222. /// 赋值按钮
  223. copyButton.isHidden = (obj.takeorderstatus == .UNRECEIVED) ? false : true
  224. /// 公司名称
  225. company.text = obj.vendorname
  226. /// 标题
  227. title.text = obj.goodsname
  228. /// 商品代码
  229. orderNo.text = obj.goodscode
  230. /// 价格
  231. price.text = "\(obj.currencysign) \(obj.averageprice.toDownString(reserve: obj.decimalplace))"
  232. /// 数量
  233. qty.text = "×\(obj.qty)"
  234. /// 金额
  235. amount.text = "金额:\(obj.currencysign) \(obj.amount)"
  236. /// 物流单号
  237. trackingNo.text = obj.takeorderstatus == .UNSEND ? "" : "物流单号:\(model?.takeremark ?? "")"
  238. /// 商品图片
  239. guard let url = StringUtils.getImageUrl(obj.picurls) else { return }
  240. goodsImage.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil)
  241. }
  242. }
  243. /// 确认提货回调块
  244. var takeOrderBlock: ((_ takeOrderID: String?) -> Void)?
  245. /// 按钮点击响应事件
  246. /// - Parameter sender: sender
  247. @IBAction private func onButtonPressed(_ sender: AnimatableButton) {
  248. switch sender {
  249. case copyButton: /// 赋值按钮
  250. let paste = UIPasteboard.general
  251. paste.string = model?.takeremark
  252. WHToast.showSuccess(withMessage: "复制成功", duration: 1.5, finishHandler: {})
  253. default:
  254. guard let block = takeOrderBlock else { return }
  255. /// 确认提货
  256. block(model?.takeorderid)
  257. }
  258. }
  259. }