优惠券swift版马甲包

YMBestChoiceController.swift 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. //
  2. // YMBestChoiceController.swift
  3. // MvpGoods
  4. //
  5. // Created by 小花 on 2019/4/9.
  6. // Copyright © 2019 MVP. All rights reserved.
  7. //
  8. import UIKit
  9. import ObjectMapper
  10. import MJRefresh
  11. class YMBestChoiceController: YMBaseViewController {
  12. var KBannerReuseIdentifier = "BannerReuseIdentifie"
  13. var KGoodsReuseIdentifier = "KGoodsReuseIdentifier"
  14. var KMainGoodCell = "MainGoodCell"
  15. var KGroupGoodCell = "GroupGoodCell"
  16. var KRecomCell = "KRecomCell"
  17. //外部变量
  18. var name: String?
  19. var id: Int?
  20. //私有变量
  21. var collectionView: UICollectionView?
  22. var bannerList = [YMBannerModel]()
  23. var dataArr = [YMTodayGoodModel]()
  24. var recomArr = [YMMainGoodModel]()
  25. var page = 1
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. self.view.makeLoading(offY:navBarHeight+40)
  29. setUpCollectionView()
  30. loadBannerData()
  31. loadGoodsData()
  32. loadYouLikeGoods()
  33. }
  34. //MARK: 网络相关
  35. //加载banner数据
  36. func loadBannerData() {
  37. let url = BASE_URL + "/api/v2/homeact/bannerlist"
  38. NetWorkTool.getRequest(url: url, param: [:], success: { (json) in
  39. let items = json["rst"]?["data"].arrayObject
  40. var imageList = [String]()
  41. for item in items! {
  42. let model = YMBannerModel(dict:item as! [String : AnyObject])
  43. self.bannerList.append(model)
  44. imageList.append(model.photo ?? "")
  45. }
  46. self.cycleView.setUrlsGroup(imageList)
  47. }) { (error) in
  48. }
  49. }
  50. func loadGoodsData() {
  51. let url = BASE_URL + "/api/v2/goods/goodsAndStockList"
  52. let para:[String: Any] = ["sort":"1",
  53. "page":page,
  54. "category_id":self.id ?? 0,
  55. "stype":"0"]
  56. NetWorkTool.postRequest(url: url, param: para, success: { (json) in
  57. let items = json["rst"]?.arrayObject
  58. if self.collectionView!.mj_header.isRefreshing {
  59. self.dataArr.removeAll()
  60. }
  61. for item in items! {
  62. let model = Mapper<YMTodayGoodModel>().map(JSON: item as! [String : Any])
  63. self.dataArr.append(model ?? YMTodayGoodModel())
  64. }
  65. self.collectionView?.mj_footer.endRefreshing()
  66. self.collectionView?.reloadData()
  67. }) { (error) in
  68. }
  69. }
  70. func loadYouLikeGoods() {
  71. let url = BASE_URL + "/api/v2/goods/stockTopCategoryList"
  72. let para: [String: Any] = ["page":1,
  73. "category_id":"2589",
  74. "sort":1,
  75. "is_has_coupon":1,
  76. "stype":"0"]
  77. NetWorkTool.postRequest(url: url, param: para, success: { (json) in
  78. let items = json["rst"]?["data"]["list"].arrayObject
  79. if self.collectionView!.mj_header.isRefreshing {
  80. self.recomArr.removeAll()
  81. }
  82. for item in items ?? [] {
  83. let model = Mapper<YMMainGoodModel>().map(JSON: item as! [String : Any])
  84. self.recomArr.append(model ?? YMMainGoodModel())
  85. }
  86. self.view.addSubview(self.collectionView!)
  87. self.view.dismissLoading()
  88. self.collectionView?.reloadData()
  89. self.collectionView?.mj_header.endRefreshing()
  90. }) { (error) in
  91. }
  92. }
  93. //初始化 CollectionView
  94. func setUpCollectionView() {
  95. let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: SCREENH-navHeight-tabBarHeight-41), collectionViewLayout:UICollectionViewFlowLayout())
  96. collectionView.delegate = self
  97. collectionView.dataSource = self
  98. collectionView.backgroundColor = UIColor.gloadGrayColor()
  99. collectionView.showsVerticalScrollIndicator = false
  100. collectionView.register(YMMainGoodCollectionCell.self, forCellWithReuseIdentifier: KMainGoodCell)
  101. collectionView.register(YMTodayGroupCollectionCell.self, forCellWithReuseIdentifier: KGroupGoodCell)
  102. collectionView.register(YMTodayRecomCell.self, forCellWithReuseIdentifier: KRecomCell)
  103. collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: KBannerReuseIdentifier)
  104. collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: KGoodsReuseIdentifier)
  105. let header = MJRefreshNormalHeader()
  106. header.setRefreshingTarget(self, refreshingAction: #selector(self.refreshData))
  107. collectionView.mj_header = header
  108. let footer = MJRefreshAutoNormalFooter()
  109. footer.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
  110. collectionView.mj_footer = footer
  111. self.collectionView = collectionView
  112. }
  113. lazy var cycleView: ZCycleView = {
  114. let cycleView = ZCycleView(frame: CGRect(x: 0, y: 0, width: SCREENW, height:fitSize(x: 150) ))
  115. cycleView.timeInterval = 6
  116. cycleView.backgroundColor = UIColor.init(white: 0, alpha: 0.3)
  117. cycleView.pageControlIndictorImage = UIImage(named: "page_nor")
  118. cycleView.pageControlCurrentIndictorImage = UIImage(named: "page_sel")
  119. cycleView.pageControlHeight = 18
  120. cycleView.pageControlItemSize = CGSize(width: 8, height: 3)
  121. cycleView.pageControlAlignment = .right
  122. cycleView.itemSize = CGSize(width: 240, height: 90)
  123. cycleView.itemZoomScale = 1.5
  124. cycleView.didSelectedItem = {
  125. (index: Int)->Void in
  126. let model: YMBannerModel = self.bannerList[index]
  127. let goodModel = YMMainGoodModel()
  128. goodModel.coverWith(good_ID: model.click_param, pri: model.price, discount_pri: model.discount_price, coupon_pri: model.coupon_price, start_time: model.coupon_start_time, end_time: model.coupon_end_time)
  129. let detail = YMDetailViewController()
  130. detail.goodModel = goodModel
  131. self.navigationController?.pushViewController(detail, animated: true)
  132. }
  133. return cycleView
  134. }()
  135. lazy var bannerBg: UIImageView = {
  136. let img = UIImageView(frame: cycleView.bounds)
  137. img.image = UIImage(named: "App头图BG")
  138. return img
  139. }()
  140. lazy var imgHeader: UIImageView = {
  141. let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: fitSize(x: 141), height: fitSize(x: 17)))
  142. imgView.centerX = SCREENW/2
  143. imgView.centerY = fitSize(x: 20)
  144. imgView.image = UIImage(named: "good_gods")
  145. return imgView
  146. }()
  147. @objc func refreshData() {
  148. self.page = 1
  149. loadBannerData()
  150. loadGoodsData()
  151. loadYouLikeGoods()
  152. }
  153. @objc func loadMoreData() {
  154. self.page += 1
  155. loadGoodsData()
  156. }
  157. }
  158. ///MARK: UICollectionViewDelegate
  159. extension YMBestChoiceController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
  160. func numberOfSections(in collectionView: UICollectionView) -> Int {
  161. return 2
  162. }
  163. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  164. if section == 0 {
  165. return 1
  166. }else {
  167. return self.dataArr.count
  168. }
  169. }
  170. //cell的大小
  171. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  172. if indexPath.section == 0 {
  173. if recomArr.count == 0 {
  174. return CGSize(width: SCREENW, height: fitSize(x: 0))
  175. }
  176. return CGSize(width: SCREENW, height: fitSize(x: 230))
  177. }else {
  178. let model: YMTodayGoodModel = self.dataArr[indexPath.row]
  179. switch model.type {
  180. case 1:
  181. return CGSize(width: SCREEN_WIDTH, height: fitSize(x: 135))
  182. case 2:
  183. return CGSize(width: SCREEN_WIDTH, height: fitSize(x: 215))
  184. default: break
  185. }
  186. }
  187. return CGSize(width: SCREEN_WIDTH, height: fitSize(x: 135))
  188. }
  189. //每个分区的内边距
  190. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  191. if section == 0 {
  192. return UIEdgeInsetsMake(0, 0, 0, 0)
  193. }
  194. return UIEdgeInsetsMake(0, 0, 0, 0);
  195. }
  196. //最小 item 间距
  197. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  198. return 0;
  199. }
  200. //最小行间距
  201. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  202. return 5;
  203. }
  204. //每个分区区头尺寸
  205. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  206. if section == 0 {
  207. return CGSize (width: SCREEN_WIDTH, height: fitSize(x: 150))
  208. }
  209. return CGSize(width: SCREENW, height: fitSize(x: 40))
  210. }
  211. //返回区头、区尾实例
  212. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  213. if kind == UICollectionElementKindSectionHeader {
  214. if indexPath.section == 0 {
  215. let headview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: KBannerReuseIdentifier, for: indexPath)
  216. headview.backgroundColor = UIColor.white
  217. headview.addSubview(self.bannerBg)
  218. headview.addSubview(self.cycleView)
  219. return headview;
  220. }else {
  221. let headview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: KGoodsReuseIdentifier, for: indexPath)
  222. headview.backgroundColor = UIColor.white
  223. headview.addSubview(imgHeader)
  224. return headview;
  225. }
  226. }
  227. return UICollectionReusableView();
  228. }
  229. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  230. if indexPath.section == 0 {
  231. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KRecomCell, for: indexPath) as! YMTodayRecomCell
  232. cell.setDataArray(dataArr: self.recomArr)
  233. cell.delegate = self
  234. return cell
  235. }else {
  236. let model: YMTodayGoodModel = self.dataArr[indexPath.row]
  237. switch model.type {
  238. case 1:
  239. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KMainGoodCell, for: indexPath) as! YMMainGoodCollectionCell
  240. cell.model = model.goods
  241. return cell
  242. case 2:
  243. let groupCell = collectionView.dequeueReusableCell(withReuseIdentifier: KGroupGoodCell, for: indexPath) as! YMTodayGroupCollectionCell
  244. groupCell.model = model
  245. groupCell.clickClosure = {
  246. (model: YMMainGoodModel)->Void in
  247. let detail = YMDetailViewController()
  248. detail.goodModel = model
  249. self.navigationController?.pushViewController(detail, animated: true)
  250. }
  251. return groupCell
  252. default: break
  253. }
  254. }
  255. return UICollectionViewCell()
  256. }
  257. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  258. let detail = YMDetailViewController()
  259. let todayModel = self.dataArr[indexPath.row]
  260. detail.goodModel = todayModel.goods
  261. self.navigationController?.pushViewController(detail, animated: true)
  262. }
  263. }
  264. extension YMBestChoiceController: YMTodayRecomCellDelegate {
  265. func didSelectedItem(index: Int, model: YMMainGoodModel) {
  266. let detail = YMDetailViewController()
  267. detail.goodModel = model
  268. self.navigationController?.pushViewController(detail, animated: true)
  269. }
  270. }