SearchViewController.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // SearchViewController.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. /// 商品检索视图容器控制类
  10. class SearchViewController: BaseViewController {
  11. // MARK: - 属性列表
  12. /// 数据展示列表
  13. @IBOutlet weak var tableView: UITableView!
  14. /// 历史检索视图
  15. @IBOutlet weak var collectionView: UICollectionView! {
  16. didSet {
  17. /// 设置约束
  18. collectionView.setCollectionViewLayout(flowLayout, animated: true)
  19. }
  20. }
  21. /// 高度约束
  22. @IBOutlet weak var heightLayoutConstraint: NSLayoutConstraint!
  23. /// 删除按钮
  24. @IBOutlet weak var delete: UIButton!
  25. /// 检索视图
  26. lazy var searchBar: UISearchBar = {
  27. $0.layer.cornerRadius = 10.0
  28. if #available(iOS 13.0, *) {
  29. $0.searchTextField.backgroundColor = UIColorFromHex(rgbValue: 0xf0f0f0)
  30. } else {
  31. // Fallback on earlier versions
  32. $0.barTintColor = UIColorFromHex(rgbValue: 0xf0f0f0)
  33. }
  34. $0.delegate = self
  35. $0.showsCancelButton = false
  36. $0.tintColor = .white
  37. $0.placeholder = "请输入商品名称或者代码"
  38. return $0
  39. }(UISearchBar())
  40. /// 数据显示集合视图约束
  41. lazy var flowLayout: UICollectionViewFlowLayout = {
  42. /// 最小行间距,默认是0
  43. $0.minimumLineSpacing = 10
  44. /// 最小左右间距,默认是10
  45. $0.minimumInteritemSpacing = 10
  46. /// 区域内间距,默认是 UIEdgeInsetsMake(0, 0, 0, 0)
  47. $0.sectionInset = UIEdgeInsets(top: 5.0, left: 5, bottom: 5, right: 5)
  48. /// 水平滚动
  49. $0.scrollDirection = .vertical
  50. return $0
  51. } (UICollectionViewFlowLayout())
  52. /// CellIdentifier
  53. let CellIdentifier: String = "SearchGoods_Cell"
  54. /// HisCellIdentifier
  55. let HisCellIdentifier: String = "HisSearchGoods_Cell"
  56. /// 检索商品信息
  57. var searchGoods: [MoSearchGoods] = [] {
  58. didSet {
  59. /// 刷新列表数据
  60. tableView.reloadData()
  61. }
  62. }
  63. /// 历史记录
  64. var hisResults: [String] {
  65. get {
  66. UserDefaultsUtils.getResearchGoodsInfo()
  67. }
  68. }
  69. /// 交易模式
  70. var tradeMode: ETradeMode?
  71. // MARK: - 生命周期
  72. override func viewDidLoad() {
  73. super.viewDidLoad()
  74. // Do any additional setup after loading the view.
  75. /// 检索视图
  76. self.navigationItem.titleView = self.searchBar
  77. /// 数据初始化
  78. self.initData()
  79. }
  80. override func viewWillAppear(_ animated: Bool) {
  81. super.viewWillAppear(animated)
  82. /// 显示导航栏
  83. self.navigationController?.setNavigationBarHidden(false, animated: true)
  84. }
  85. // MARK: - 数据初始化
  86. /// 数据初始化
  87. fileprivate func initData() {
  88. /// 刷新数据
  89. self.collectionView.reloadData()
  90. /// 设置动态高度约束
  91. self.heightLayoutConstraint.constant = hisResults.count > 0 ? 90.0 : 0.0
  92. }
  93. // MARK: - 交互相关
  94. @IBAction fileprivate func onBarButtonItemPressed(_ sender: UIBarButtonItem) {
  95. /// 失去第一次响应者
  96. self.searchBar.resignFirstResponder()
  97. self.searchBar.text = ""
  98. /// 赋值历史
  99. self.collectionView.reloadData()
  100. /// 设置动态高度约束
  101. self.heightLayoutConstraint.constant = hisResults.count > 0 ? 90.0 : 0.0
  102. }
  103. /// onButtonPressed
  104. /// - Parameter sender: sender
  105. @IBAction fileprivate func onButtonPressed(_ sender: UIButton) {
  106. switch sender {
  107. case delete: /// 删除
  108. DispatchQueue.main.async {
  109. UserDefaultsUtils.deleteAllResearchGoodsInfo()
  110. /// 数据初始化
  111. self.initData()
  112. }
  113. default:
  114. break
  115. }
  116. }
  117. // MARK: - 接口请求
  118. /// 商品检索数据信息
  119. /// - Parameter string: 检索内容
  120. func requestSearchGoods(_ string: String) {
  121. /// 异常
  122. guard let goodsManager = MTP2BusinessCore.shared.goodsManager else { return }
  123. /// 商品检索
  124. goodsManager.requestSearchGoodses(string, "16,70,71") { (isComplete, error, goods) in
  125. DispatchQueue.main.async { self.searchGoods = goods ?? [] }
  126. }
  127. }
  128. /*
  129. // MARK: - Navigation
  130. // In a storyboard-based application, you will often want to do a little preparation before navigation
  131. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  132. // Get the new view controller using segue.destination.
  133. // Pass the selected object to the new view controller.
  134. }
  135. */
  136. }
  137. // MARK: - UITableViewDelegate, UITableViewDataSource
  138. extension SearchViewController: UITableViewDelegate, UITableViewDataSource {
  139. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  140. return self.searchGoods.count
  141. }
  142. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  143. let cell = tableView.dequeueReusableCell(withIdentifier: CellIdentifier, for: indexPath)
  144. cell.textLabel?.text = self.searchGoods[indexPath.row].goodsname
  145. cell.detailTextLabel?.text = self.searchGoods[indexPath.row].goodscode
  146. return cell
  147. }
  148. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  149. /// 添加检索商品信息
  150. UserDefaultsUtils.setResearchGoodsInfo(searchBar.text ?? "")
  151. searchBar.resignFirstResponder()
  152. /// 商品详情
  153. guard let goods = MTP2BusinessCore.shared.goodsManager?.goodsInfos.first(where: {$0.goodsid == self.searchGoods[indexPath.row].goodsid}) else { return }
  154. /// 商城商品
  155. if goods.trademode == .TRADEMODE_TRADEMODE_HSBY_SHOP {
  156. let specialSaleController = viewController(name: "Quote", identifier: "SpecialSale") as! SpecialSaleViewController
  157. specialSaleController.goodsid = goods.goodsid
  158. self.navigationController?.pushViewController(specialSaleController, animated: true)
  159. } else {
  160. let goodsDetailController = UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "GoodsDetail") as! GoodsDetailViewController
  161. goodsDetailController.model = goods
  162. self.navigationController?.pushViewController(goodsDetailController, animated: true)
  163. }
  164. }
  165. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  166. return 44.0
  167. }
  168. }
  169. // MARK: - UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
  170. extension SearchViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  171. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  172. return hisResults.count
  173. }
  174. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  175. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HisCellIdentifier, for: indexPath) as! SearchCell
  176. cell.textLabel.text = hisResults[indexPath.row]
  177. return cell
  178. }
  179. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  180. self.searchBar.text = hisResults[indexPath.row]
  181. UIView.animate(withDuration: 0.5) {
  182. self.heightLayoutConstraint.constant = 0.0
  183. } completion: { (finished) in
  184. if finished {
  185. /// 查询数据
  186. self.requestSearchGoods(self.hisResults[indexPath.row])
  187. }
  188. }
  189. }
  190. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  191. let width = hisResults[indexPath.row].getTextSize(font: .font_12, size: .zero).width+10.0
  192. return CGSize(width: width > 60.0 ? width : 60.0, height: 30.0)
  193. }
  194. }
  195. // MARK: - UITableViewDelegate, UISearchBarDelegate
  196. extension SearchViewController: UISearchBarDelegate {
  197. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  198. /// 根据结果检索信息
  199. self.requestSearchGoods(searchBar.text ?? "")
  200. }
  201. }
  202. // MARK: - SearchCell
  203. class SearchCell: UICollectionViewCell {
  204. /// 标题
  205. @IBOutlet weak var textLabel: UILabel! {
  206. didSet {
  207. textLabel.layer.cornerRadius = 10.0
  208. textLabel.layer.masksToBounds = true
  209. }
  210. }
  211. }