HomeViewController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. //
  2. // HomeViewController.swift
  3. // MTP2_iOS
  4. //
  5. // Created by Handy_Cao on 2020/10/23.
  6. // Copyright © 2020 Muchinfo. All rights reserved.
  7. //
  8. import UIKit
  9. import SDCycleScrollView
  10. import SafariServices
  11. import SDWebImage
  12. import SwiftyAttributes
  13. import WHToast
  14. import GTMRefresh
  15. import WebKit
  16. /// 首页视图容器管理类
  17. class HomeViewController: BaseViewController {
  18. // MARK: - 属性列表
  19. /// 系统设置
  20. @IBOutlet weak var settings: UIButton!
  21. /// 新卡抢购
  22. @IBOutlet weak var xkqg: UIButton!
  23. /// 热卖专区
  24. @IBOutlet weak var rmzq: UIButton!
  25. /// 我的订单
  26. @IBOutlet weak var wddd: UIButton!
  27. /// 海南网警
  28. @IBOutlet weak var police: UIButton!
  29. /// 海拔资讯
  30. @IBOutlet weak var hbzx: UIButton!
  31. /// 海拔资讯
  32. @IBOutlet weak var hbzxStackView: UIStackView!
  33. /// 我的卡包
  34. @IBOutlet weak var wdkb: UIButton!
  35. /// 主滚动视图
  36. @IBOutlet weak var scrollView: UIScrollView!
  37. /// 操作视图
  38. @IBOutlet weak var operationView: UIView!
  39. /// 图片轮播图
  40. @IBOutlet weak var bannerView: SDCycleScrollView! {
  41. didSet {
  42. bannerView.bannerImageViewContentMode = .scaleAspectFill
  43. bannerView.autoScrollTimeInterval = 5.0
  44. bannerView.autoScroll = true
  45. bannerView.infiniteLoop = true
  46. bannerView.scrollDirection = .horizontal
  47. bannerView.layer.masksToBounds = true
  48. bannerView.layer.cornerRadius = 10.0
  49. bannerView.delegate = self
  50. }
  51. }
  52. /// 商品数据集合视图
  53. @IBOutlet weak var collectionView: UICollectionView! {
  54. didSet {
  55. if collectionView.responds(to: #selector(setter: UICollectionView.isPrefetchingEnabled)) {
  56. collectionView.isPrefetchingEnabled = false
  57. }
  58. /// 设置约束
  59. collectionView.setCollectionViewLayout(flowLayout, animated: true)
  60. }
  61. }
  62. /// 商品数据列表高度
  63. @IBOutlet weak var collectionViewHeightConstraint: NSLayoutConstraint!
  64. /// 搜索框
  65. @IBOutlet weak var searchBar: UIButton!
  66. /// 数据显示集合视图约束
  67. lazy var flowLayout: UICollectionViewFlowLayout = {
  68. /// 最小行间距,默认是0
  69. $0.minimumLineSpacing = 0
  70. /// 最小左右间距,默认是10
  71. $0.minimumInteritemSpacing = 0
  72. /// 区域内间距,默认是 UIEdgeInsetsMake(0, 0, 0, 0)
  73. $0.sectionInset = UIEdgeInsets(top: 0.0, left: 0, bottom: 0, right: 0)
  74. /// 水平滚动
  75. $0.scrollDirection = .vertical
  76. return $0
  77. } (UICollectionViewFlowLayout())
  78. /// 商品数据信息
  79. var goods: [MoGoodsInfo] = [] {
  80. didSet {
  81. /// 刷新数据
  82. self.collectionView.reloadData()
  83. /// 设置约束
  84. collectionViewHeightConstraint.constant = CGFloat((goods.count%2 == 0) ? (goods.count/2) : (goods.count/2+1))*(((collectionView.width/2)*0.9)+80.0)
  85. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.5, execute: {
  86. if self.goods.count > 0 {
  87. /// 等待UI操作完成,也就是tableView刷新完之后执行
  88. DispatchQueue.main.async { [weak self] in
  89. if self?.visibleGoodsCodes.count != 0 { self?.shouldSubscriptGoodsCodes = self?.visibleGoodsCodes }
  90. }
  91. }
  92. })
  93. }
  94. }
  95. /// GoodsCellIdentifier
  96. let GoodsCellIdentifier = "Goods_Cell"
  97. // MARK: - 生命周期
  98. required init?(coder aDecoder: NSCoder) {
  99. super.init(coder: aDecoder)
  100. /// 设置TabBar图片
  101. self.tabBarItem.image = UIImage(named: "home_normal")?.withRenderingMode(.alwaysOriginal)
  102. self.tabBarItem.selectedImage = UIImage(named: "home_selected")?.withRenderingMode(.alwaysOriginal)
  103. }
  104. override func viewWillAppear(_ animated: Bool) {
  105. super.viewWillAppear(animated)
  106. /// 隐藏导航栏
  107. self.navigationController?.setNavigationBarHidden(true, animated: true)
  108. /// 行情侦听
  109. initListen()
  110. }
  111. override func viewDidDisappear(_ animated: Bool) {
  112. super.viewDidDisappear(animated)
  113. /// 清除广播侦听
  114. MTP2BusinessCore.shared.broadcastManager?.removeBroadcastListener(owner: self, forName: .ReceiveTradeQuote)
  115. }
  116. override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
  117. super.viewWillTransition(to: size, with: coordinator)
  118. }
  119. override func viewDidLoad() {
  120. super.viewDidLoad()
  121. // Do any additional setup after loading the view.
  122. /// 不需要返回按钮
  123. addBackBarButtonItem(true)
  124. /// UI界面初始化
  125. buildView()
  126. /// 数据初始化
  127. initData()
  128. }
  129. // MARK: - 行情初始化
  130. /// 行情监听
  131. fileprivate func initListen() {
  132. if goods.count > 0 {
  133. /// 等待UI操作完成,也就是tableView刷新完之后执行
  134. DispatchQueue.main.async { [weak self] in
  135. if self?.visibleGoodsCodes.count != 0 { self?.shouldSubscriptGoodsCodes = self?.visibleGoodsCodes }
  136. }
  137. }
  138. /// 侦听行情推送广播
  139. MTP2BusinessCore.shared.broadcastManager?.addBroadcastListener(owner: self, action: .ReceiveTradeQuote) { [weak self] in
  140. guard let quoteGoodsInfos = $0.object as? [(goodsHqCode: String, exchHqCode: String)],
  141. let weakSelf = self else { return }
  142. /// 有数据
  143. if quoteGoodsInfos.count != 0 {
  144. /// 待刷新行
  145. var refreshIndexPaths = [IndexPath]()
  146. DispatchQueue.main.async {
  147. for (index, cell) in weakSelf.collectionView.visibleCells.enumerated() {
  148. if quoteGoodsInfos.first(where: { $0.goodsHqCode == (cell as? GoodsCell)?.model?.goodscode }) != nil {
  149. let indexPath = IndexPath(row: index, section: 0)
  150. if refreshIndexPaths.firstIndex(of: indexPath) == nil {
  151. /// 记录待刷新行
  152. /// 判断是否在可见区域
  153. if weakSelf.collectionView.indexPathsForVisibleItems.firstIndex(of: indexPath) != nil {
  154. refreshIndexPaths.append(indexPath)
  155. }
  156. }
  157. }
  158. }
  159. }
  160. /// 刷新行情列表
  161. if refreshIndexPaths.count > 0 {
  162. DispatchQueue.main.async {
  163. weakSelf.collectionView.reloadItems(at: refreshIndexPaths)
  164. }
  165. }
  166. }
  167. }
  168. }
  169. // MARK: - 初始化
  170. /// UI界面初始化
  171. fileprivate func buildView() {
  172. /// addLoadingView
  173. self.addLoadingView()
  174. /// 添加下拉刷新控件
  175. self.scrollView.gtm_addRefreshHeaderView(refreshHeader: DefaultGTMRefreshHeader()) {
  176. if let _ = UserDefaultsUtils.addressInfo() {
  177. /// 重置请求条件
  178. self.requestHomePageDatas()
  179. } else {
  180. /// 游客登录
  181. /// startAnimating
  182. self._anim?.startAnimating()
  183. /// 查询未登录前的相关信息
  184. MTP2BusinessCore.shared.requestCommonDatas { (_) in
  185. DispatchQueue.main.async {
  186. /// stopAnimating
  187. self._anim?.stopAnimating()
  188. /// 获取首页相关数据
  189. self.requestHomePageDatas()
  190. }
  191. }
  192. }
  193. }
  194. }
  195. /// 数据初始化
  196. fileprivate func initData() {
  197. /// 当前登录了
  198. if let _ = MTP2BusinessCore.shared.accountManager?.loginRsp {
  199. /// 获取首页相关数据
  200. self.requestHomePageDatas()
  201. } else {
  202. /// 游客登录
  203. /// startAnimating
  204. self._anim?.startAnimating()
  205. /// 查询未登录前的相关信息
  206. MTP2BusinessCore.shared.requestCommonDatas { (_) in
  207. DispatchQueue.main.async {
  208. /// stopAnimating
  209. self._anim?.stopAnimating()
  210. /// 获取首页相关数据
  211. self.requestHomePageDatas()
  212. }
  213. }
  214. }
  215. }
  216. // MARK: - 按钮交互
  217. /// 按钮相应点击事件方法
  218. /// - Parameter sender: sender
  219. @IBAction fileprivate func onButtonPressed(_ sender: UIButton) {
  220. switch sender {
  221. case settings: /// 系统设置
  222. self.push(storyboardName: "Settings", storyboardId: "Settings", checkLogin: false)
  223. case rmzq,
  224. xkqg: /// 热卖专区 新卡抢购
  225. guard let main = self.appDelegate.window?.rootViewController as? MainViewController,
  226. let quoteController = (main.viewControllers?[1] as? BaseNavigationController)?.viewControllers[0] as? QuoteViewController else { return }
  227. main.selectedIndex = 1
  228. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+0.4) {
  229. /// 选中
  230. quoteController.segmentedView.defaultSelectedIndex = (sender == self.rmzq ? 0 : 1)
  231. quoteController.segmentedView.reloadData()
  232. quoteController.segmentedView.delegate?.segmentedView(quoteController.segmentedView, didSelectedItemAt: sender == self.rmzq ? 0 : 1)
  233. }
  234. case hbzx: /// 海拔资讯
  235. let auth = UIStoryboard(name: "Settings", bundle: nil).instantiateViewController(withIdentifier: "Auth") as! AuthAddressViewController
  236. auth.title = "海拔资讯"
  237. auth.url = MTP2BusinessCore.shared.address?.newsUrl ?? ""
  238. self.navigationController?.pushViewController(auth, animated: true)
  239. case wddd: /// 我的订单
  240. self.push(storyboardName: "Order", storyboardId: "Order")
  241. case wdkb: /// 我的商品
  242. self.push(storyboardName: "Quote", storyboardId: "MyGoods")
  243. case police: /// 海南网警
  244. push(storyboardName: "Feedback", storyboardId: "Feedback")
  245. case searchBar: /// 跳转到检索页面
  246. push(storyboardName: "Quote", storyboardId: "Search", checkLogin: false)
  247. default:
  248. break
  249. }
  250. }
  251. // MARK: - 接口请求
  252. /// 查询首页数据信息
  253. fileprivate func requestHomePageDatas() {
  254. /// 异常
  255. guard let goodsManager = MTP2BusinessCore.shared.goodsManager,
  256. let noticeManager = MTP2BusinessCore.shared.noticeManager else {
  257. return
  258. }
  259. let group = DispatchGroup()
  260. /// startAnimating
  261. self._anim?.startAnimating()
  262. var hotGoods: [MoGoodsInfo] = []
  263. /// 已登录
  264. if MTP2BusinessCore.shared.getLoginStatus() {
  265. /// 资金账号
  266. let accountID = MTP2BusinessCore.shared.accountManager?.getCurrentTAAccountInfo()?.accountId ?? 0
  267. /// 获取商城商品数据
  268. group.enter()
  269. goodsManager.requestQueryHsbyMarketGoodses(goodsManager.getMarketIDs(.TRADEMODE_TRADEMODE_HSBY_SHOP), accountID, nil, nil, nil) { (isComplete, error, marketGoodses) in
  270. hotGoods.append(contentsOf: marketGoodses ?? [])
  271. group.leave()
  272. }
  273. } else {
  274. /// 获取商城商品数据
  275. group.enter()
  276. goodsManager.requestQueryHsbyVisitorMarketGoodses(goodsManager.getMarketIDs(.TRADEMODE_TRADEMODE_HSBY_SHOP), nil, nil, nil) { (isComplete, error, marketGoodses) in
  277. hotGoods.append(contentsOf: marketGoodses ?? [])
  278. group.leave()
  279. }
  280. }
  281. /// 获取热门商品
  282. group.enter()
  283. goodsManager.requestQueryTopGoods(goodsManager.getMarketIDs(.TRADEMODE_LISTING_SELECT), nil, nil) { (isComplete, error, topGoods) in
  284. hotGoods.append(contentsOf: topGoods ?? [])
  285. group.leave()
  286. }
  287. /// 发送请求
  288. group.enter()
  289. var configs: [MoImageConfigs] = []
  290. /// 查询轮播图
  291. noticeManager.requestQueryImageConfigs { (isComplete, error, imageConfigs) in
  292. configs = imageConfigs ?? []
  293. group.leave()
  294. }
  295. var notices: [MoNotice] = []
  296. group.enter()
  297. /// 查询未读公告消息
  298. noticeManager.requestNotices(nil, nil, nil, true) { (isComplete, error, messages) in
  299. notices = messages ?? []
  300. group.leave()
  301. }
  302. group.notify(queue: DispatchQueue.main) {
  303. /// stopAnimating
  304. self._anim?.stopAnimating()
  305. /// endRefreshing
  306. self.scrollView.endRefreshing()
  307. /// 热门商品数据
  308. self.goods = hotGoods.sorted(by: { (obj1, obj2) -> Bool in
  309. return obj1.hotindex>obj2.hotindex
  310. })
  311. /// 轮播图相关
  312. if configs.count != 0 {
  313. let paths = configs.map({ (model) -> String in
  314. let managerUrl = MTP2BusinessCore.shared.address?.openApiUrl ?? ""
  315. let url = managerUrl+NSString(string: model.imagepath).replacingOccurrences(of: "./", with: "/")
  316. return url
  317. })
  318. self.bannerView.imageURLStringsGroup = paths
  319. }
  320. /// 更新成功
  321. if notices.count != 0 {
  322. self.showAlertController(title: "提示", message: "有新的未读消息公告,是否前去查看", cancelTitle: "取消", sureTitle: "去查看", cancelBlock: {}) {
  323. /// 跳转到公告消息页面
  324. (self.appDelegate.window?.rootViewController as? MainViewController)?.selectedIndex = 2
  325. }
  326. }
  327. /// 新闻地址信息
  328. if let url = MTP2BusinessCore.shared.address?.newsUrl,
  329. url != "" {
  330. self.hbzxStackView.isHidden = false
  331. } else {
  332. self.hbzxStackView.isHidden = true
  333. }
  334. }
  335. }
  336. // MARK: - 订阅行情相关
  337. var uuid: String = UUID().uuidString
  338. var shouldSubscriptGoodsCodes: Set<String>? {
  339. didSet {
  340. guard let quoteSubscriptManager = MTP2BusinessCore.shared.quoteSubscriptManager else { return }
  341. quoteSubscriptManager.updateQuoteSubcriptGoods(uuid: uuid, goodsCode: shouldSubscriptGoodsCodes ?? [])
  342. }
  343. }
  344. var visibleGoodsCodes: Set<String> {
  345. var goodsCodes = Set<String>()
  346. self.collectionView.visibleCells.forEach { (cell) in
  347. if let quoteCell = cell as? GoodsCell,
  348. let goodsCode = quoteCell.model?.goodscode {
  349. goodsCodes.insert(goodsCode)
  350. }
  351. }
  352. return goodsCodes
  353. }
  354. /*
  355. // MARK: - Navigation
  356. // In a storyboard-based application, you will often want to do a little preparation before navigation
  357. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  358. // Get the new view controller using segue.destination.
  359. // Pass the selected object to the new view controller.
  360. }
  361. */
  362. }
  363. // MARK: - SFSafariViewControllerDelegate
  364. extension HomeViewController: SFSafariViewControllerDelegate {
  365. func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
  366. controller.dismiss(animated: true, completion: {})
  367. }
  368. }
  369. // MARK: - UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
  370. extension HomeViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
  371. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  372. return goods.count
  373. }
  374. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  375. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: GoodsCellIdentifier, for: indexPath) as! GoodsCell
  376. cell.model = goods[indexPath.row]
  377. return cell
  378. }
  379. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  380. return CGSize(width: collectionView.width/2, height: ((collectionView.width/2)*0.9)+80.0)
  381. }
  382. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  383. /// 商品详情
  384. let goodsDetailController = UIStoryboard(name: "Quote", bundle: nil).instantiateViewController(withIdentifier: "GoodsDetail") as! GoodsDetailViewController
  385. goodsDetailController.model = self.goods[indexPath.row]
  386. self.navigationController?.pushViewController(goodsDetailController, animated: true)
  387. }
  388. }
  389. // MARK: - SDCycleScrollViewDelegate
  390. extension HomeViewController: SDCycleScrollViewDelegate {
  391. func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didScrollTo index: Int) {}
  392. func cycleScrollView(_ cycleScrollView: SDCycleScrollView!, didSelectItemAt index: Int) {}
  393. }
  394. /// 商品数据Cell
  395. class GoodsCell: UICollectionViewCell {
  396. /// 背景图片
  397. @IBOutlet weak var image: UIImageView!
  398. /// 商品名称
  399. @IBOutlet weak var goodsName: UILabel!
  400. /// 价格
  401. @IBOutlet weak var price: UILabel!
  402. /// 数据集合
  403. var model: MoGoodsInfo? {
  404. didSet {
  405. /// 更新显示行情数据
  406. guard let obj = MTP2BusinessCore.shared.goodsManager?.goodsInfos.first(where: { $0.goodscode == model?.goodscode }) else { return }
  407. /// 商品名称
  408. goodsName.text = obj.goodsname
  409. /// 价格
  410. if obj.trademode == .TRADEMODE_LISTING_SELECT {
  411. price.attributedText = (obj.currencysign.withFont(.font_12)+" \((obj.getLast() == 0.0 ? obj.last : obj.getLast()).toDownString(reserve: obj.decimalplace))".withFont(.font_16)).withTextColor(.red)
  412. } else if obj.trademode == .TRADEMODE_TRADEMODE_HSBY_SHOP { /// 商城商品
  413. price.attributedText = (("\(obj.currencysign)".withFont(.font_12))+("\(obj.goodsprice)".withFont(.font_16))).withTextColor(.red)
  414. } else { /// 新品抢购
  415. price.attributedText = (("抢购价:\(obj.currencysign)".withFont(.font_12))+(obj.refprice.description.withFont(.font_16))).withTextColor(.red)
  416. }
  417. /// 地址异常
  418. if let url = StringUtils.getImageUrl(obj.picurls) {
  419. /// 背景图片
  420. image.sd_setImage(with: url, placeholderImage: UIImage(named: "placeholder_image"), options: .queryDiskDataSync, context: nil)
  421. }
  422. }
  423. }
  424. }