// // PacketViewController.swift // MTP2_iOS // // Created by Handy_Cao on 2020/10/28. // Copyright © 2020 Muchinfo. All rights reserved. // import UIKit import WHToast import SDWebImage import GTMRefresh import IBAnimatable import JXSegmentedView /// 我的包裹视图容器控制类 class PacketViewController: BaseViewController { // MARK: - 属性列表 /// 分段控制器 @IBOutlet weak var segmentedView: JXSegmentedView! { didSet { segmentedView.delegate = self segmentedView.dataSource = dataSource segmentedView.indicators = [self.indicator] } } /// segmentedDataSource let dataSource: JXSegmentedTitleDataSource = { var items: [String] = ["全部", "待发货", "待收货", "已完成"] $0.titles = items $0.titleNormalColor = UIColorFromHex(rgbValue: 0x333333) $0.titleNormalFont = .font_14 $0.titleSelectedColor = UIColorFromHex(rgbValue: 0x60a1e3) $0.titleSelectedFont = .font_14 $0.isTitleColorGradientEnabled = true return $0 } (JXSegmentedTitleDataSource()) /// 数据展示列表 @IBOutlet weak var tableView: UITableView! // CellIdentifier let CellIdentifier = "Packet_Cell" /// 我的包裹单据数据信息 var myPackages: [MoHsbyMyPackages] = [] { didSet { /// 刷新列表数据 tableView.reloadData() /// 隐藏按钮 self.noDataButton.isHidden = !(myPackages.count == 0) } } // MARK: - 生命周期 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. /// UI界面初始化 buildView() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) /// 隐藏导航栏 self.navigationController?.setNavigationBarHidden(false, animated: true) } override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.2) { /// 默认滚动 self.segmentedView.defaultSelectedIndex = self.selectedIndex /// 刷新数据 self.segmentedView.reloadData() /// 刷新数据 self.tableView.triggerRefreshing() } } // MARK: - 初始化 /// UI界面初始化 fileprivate func buildView() { /// loding...... self.addLoadingView() /// 添加下拉刷新控件 self.tableView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) { /// 重置请求条件 self.reloadData(self.segmentedView.selectedIndex) } } // MARK: - 接口请求 /// 刷新数据 fileprivate func reloadData(_ segmentIndex: Int) { var status: Int? switch segmentIndex { case 1, 2, 3: /// 抢购中 status = self.segmentedView.selectedIndex default: break } /// 查询我的包裹信息 self.requestQueryHsbyMyPackages(status) } /// 查询我的包裹信息 /// - Parameter status: status fileprivate func requestQueryHsbyMyPackages(_ status: Int?) { /// 异常 guard let orderManager = MTP2BusinessCore.shared.orderManager else { return } /// startAnimating _anim?.startAnimating() /// 发送请求 orderManager.requestQueryHsbyMyPackages(status) { (isComplete, error, myPackages) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() /// endRefreshing self.tableView.endRefreshing(isSuccess: true) /// 数据赋值 if isComplete { self.myPackages = myPackages ?? [] } else { /// 查询失败 self.myPackages = [] WHToast.showError(withMessage: "数据查询失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {}) } } } } /// 设置我的包裹已收货状态 /// - Parameter takeOrderID: 提货单号 fileprivate func requestSetHsbyMyPackagesStatus(_ takeOrderID: String?) { /// 异常 guard let orderManager = MTP2BusinessCore.shared.orderManager else { return } /// startAnimating _anim?.startAnimating() /// 发送请求 orderManager.requestSetHsbyMyPackagesStatus(takeOrderID ?? "") { (isComplete, error) in DispatchQueue.main.async { /// stopAnimating self._anim?.stopAnimating() if isComplete { WHToast.showSuccess(withMessage: "确认收货成功!", duration: 1.5, finishHandler: { /// 查询我的包裹信息 self.requestQueryHsbyMyPackages(self.segmentedView.selectedIndex) }) } else { /// 查询失败 WHToast.showError(withMessage: "确认收货失败,原因:\(error?.retMsg ?? "未知错误")", duration: 1.5, finishHandler: {}) } } } } // MARK: - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation override func prepare(for segue: UIStoryboardSegue, sender: Any?) { // Get the new view controller using segue.destination. // Pass the selected object to the new view controller. if segue.identifier == "PacketDetail" { (segue.destination as? PacketDetailViewController)?.obj = (sender as? PacketCell)?.model } } } // MARK: - UITableViewDelegate, UITableViewDataSource extension PacketViewController: UITableViewDelegate, UITableViewDataSource { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.myPackages.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath) as! PacketCell cell.model = self.myPackages[indexPath.row] cell.takeOrderBlock = { (_ takeOrderID: String?) in guard let id = takeOrderID else { return } /// show alert self.showAlertController(title: "提示", message: "确定要收货吗?", cancelTitle: "取消", sureTitle: "确认收货") {} sureBlock: { /// 确认提货 self.requestSetHsbyMyPackagesStatus(id) } } return cell } func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { return self.myPackages[indexPath.row].takeorderstatus == .UNRECEIVED ? 210.0 : 175.0 } } // MARK: - JXSegmentedViewDelegate extension PacketViewController: JXSegmentedViewDelegate { func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) { /// 重置默认状态 self.selectedIndex = index /// 刷新数据 self.reloadData(index) } } // MARK: - PacketCell class PacketCell: BaseTableViewCell { /// 公司名称 @IBOutlet weak var company: UILabel! /// 状态 @IBOutlet weak var status: UILabel! /// 单号 @IBOutlet weak var orderNo: UILabel! /// 商品图片 @IBOutlet weak var goodsImage: UIImageView! /// 价格 @IBOutlet weak var price: UILabel! /// 金额 @IBOutlet weak var amount: UILabel! /// 标题 @IBOutlet weak var title: UILabel! /// 数量 @IBOutlet weak var qty: UILabel! /// 物流单号 @IBOutlet weak var trackingNo: UILabel! /// 物流单号 @IBOutlet weak var trackingView: UIStackView! /// 复制 @IBOutlet weak var copyButton: AnimatableButton! /// 确认提货 @IBOutlet weak var takeOrder: AnimatableButton! /// 数据模型 override var model: MoHsbyMyPackages? { didSet { guard let obj = model else { return } /// 单据状态 status.text = obj.takeorderstatus.description /// 确认收货按钮 takeOrder.isHidden = !(obj.takeorderstatus == .UNRECEIVED) /// 已完成 trackingView.isHidden = (obj.takeorderstatus == .COMPLETE) /// 赋值按钮 copyButton.isHidden = (obj.takeorderstatus == .UNRECEIVED) ? false : true /// 公司名称 company.text = obj.vendorname /// 标题 title.text = obj.goodsname /// 商品代码 orderNo.text = obj.goodscode /// 价格 price.text = "\(obj.currencysign) \(obj.averageprice.toDownString(reserve: obj.decimalplace))" /// 数量 qty.text = "×\(obj.qty)" /// 金额 amount.text = "金额:\(obj.currencysign) \(obj.amount)" /// 物流单号 trackingNo.text = obj.takeorderstatus == .UNSEND ? "" : "物流单号:\(model?.takeremark ?? "")" /// 商品图片 guard let url = StringUtils.getImageUrl(obj.picurls) else { return } goodsImage.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil) } } /// 确认提货回调块 var takeOrderBlock: ((_ takeOrderID: String?) -> Void)? /// 按钮点击响应事件 /// - Parameter sender: sender @IBAction private func onButtonPressed(_ sender: AnimatableButton) { switch sender { case copyButton: /// 赋值按钮 let paste = UIPasteboard.general paste.string = model?.takeremark WHToast.showSuccess(withMessage: "复制成功", duration: 1.5, finishHandler: {}) default: guard let block = takeOrderBlock else { return } /// 确认提货 block(model?.takeorderid) } } }