123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- //
- // YMBestChoiceController.swift
- // MvpGoods
- //
- // Created by 小花 on 2019/4/9.
- // Copyright © 2019 MVP. All rights reserved.
- //
- import UIKit
- import ObjectMapper
- import MJRefresh
- class YMBestChoiceController: YMBaseViewController {
-
- var KBannerReuseIdentifier = "BannerReuseIdentifie"
- var KGoodsReuseIdentifier = "KGoodsReuseIdentifier"
- var KMainGoodCell = "MainGoodCell"
- var KGroupGoodCell = "GroupGoodCell"
- var KRecomCell = "KRecomCell"
- //外部变量
- var name: String?
- var id: Int?
-
- //私有变量
- var collectionView: UICollectionView?
- var bannerList = [YMBannerModel]()
- var dataArr = [YMTodayGoodModel]()
- var recomArr = [YMMainGoodModel]()
- var page = 1
-
- override func viewDidLoad() {
- super.viewDidLoad()
- self.view.makeLoading(offY:navBarHeight+40)
- setUpCollectionView()
- loadBannerData()
- loadGoodsData()
- loadYouLikeGoods()
- }
-
-
- //MARK: 网络相关
- //加载banner数据
- func loadBannerData() {
- let url = BASE_URL + "/api/v2/homeact/bannerlist"
- NetWorkTool.getRequest(url: url, param: [:], success: { (json) in
- let items = json["rst"]?["data"].arrayObject
- var imageList = [String]()
- for item in items! {
- let model = YMBannerModel(dict:item as! [String : AnyObject])
- self.bannerList.append(model)
- imageList.append(model.photo ?? "")
- }
- self.cycleView.setUrlsGroup(imageList)
-
- }) { (error) in
-
- }
- }
-
- func loadGoodsData() {
- let url = BASE_URL + "/api/v2/goods/goodsAndStockList"
- let para:[String: Any] = ["sort":"1",
- "page":page,
- "category_id":self.id ?? 0,
- "stype":"0"]
- NetWorkTool.postRequest(url: url, param: para, success: { (json) in
- let items = json["rst"]?.arrayObject
- if self.collectionView!.mj_header.isRefreshing {
- self.dataArr.removeAll()
- }
- for item in items! {
- let model = Mapper<YMTodayGoodModel>().map(JSON: item as! [String : Any])
- self.dataArr.append(model ?? YMTodayGoodModel())
- }
- self.collectionView?.mj_footer.endRefreshing()
- self.collectionView?.reloadData()
- }) { (error) in
-
- }
- }
-
- func loadYouLikeGoods() {
- let url = BASE_URL + "/api/v2/goods/stockTopCategoryList"
- let para: [String: Any] = ["page":1,
- "category_id":"2589",
- "sort":1,
- "is_has_coupon":1,
- "stype":"0"]
-
- NetWorkTool.postRequest(url: url, param: para, success: { (json) in
- let items = json["rst"]?["data"]["list"].arrayObject
- if self.collectionView!.mj_header.isRefreshing {
- self.recomArr.removeAll()
- }
- for item in items ?? [] {
- let model = Mapper<YMMainGoodModel>().map(JSON: item as! [String : Any])
- self.recomArr.append(model ?? YMMainGoodModel())
- }
- self.view.addSubview(self.collectionView!)
- self.view.dismissLoading()
- self.collectionView?.reloadData()
- self.collectionView?.mj_header.endRefreshing()
- }) { (error) in
-
- }
-
- }
-
-
-
- //初始化 CollectionView
- func setUpCollectionView() {
- let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: SCREENH-navHeight-tabBarHeight-41), collectionViewLayout:UICollectionViewFlowLayout())
- collectionView.delegate = self
- collectionView.dataSource = self
- collectionView.backgroundColor = UIColor.gloadGrayColor()
- collectionView.showsVerticalScrollIndicator = false
- collectionView.register(YMMainGoodCollectionCell.self, forCellWithReuseIdentifier: KMainGoodCell)
- collectionView.register(YMTodayGroupCollectionCell.self, forCellWithReuseIdentifier: KGroupGoodCell)
- collectionView.register(YMTodayRecomCell.self, forCellWithReuseIdentifier: KRecomCell)
- collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: KBannerReuseIdentifier)
- collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind:UICollectionElementKindSectionHeader, withReuseIdentifier: KGoodsReuseIdentifier)
-
- let header = MJRefreshNormalHeader()
- header.setRefreshingTarget(self, refreshingAction: #selector(self.refreshData))
- collectionView.mj_header = header
-
- let footer = MJRefreshAutoNormalFooter()
- footer.setRefreshingTarget(self, refreshingAction: #selector(self.loadMoreData))
- collectionView.mj_footer = footer
-
- self.collectionView = collectionView
- }
-
-
- lazy var cycleView: ZCycleView = {
- let cycleView = ZCycleView(frame: CGRect(x: 0, y: 0, width: SCREENW, height:fitSize(x: 150) ))
- cycleView.timeInterval = 6
- cycleView.backgroundColor = UIColor.init(white: 0, alpha: 0.3)
- cycleView.pageControlIndictorImage = UIImage(named: "page_nor")
- cycleView.pageControlCurrentIndictorImage = UIImage(named: "page_sel")
- cycleView.pageControlHeight = 18
- cycleView.pageControlItemSize = CGSize(width: 8, height: 3)
- cycleView.pageControlAlignment = .right
- cycleView.itemSize = CGSize(width: 240, height: 90)
- cycleView.itemZoomScale = 1.5
- cycleView.didSelectedItem = {
- (index: Int)->Void in
- let model: YMBannerModel = self.bannerList[index]
- let goodModel = YMMainGoodModel()
- 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)
- let detail = YMDetailViewController()
- detail.goodModel = goodModel
- self.navigationController?.pushViewController(detail, animated: true)
-
- }
- return cycleView
- }()
-
- lazy var bannerBg: UIImageView = {
- let img = UIImageView(frame: cycleView.bounds)
- img.image = UIImage(named: "App头图BG")
- return img
- }()
-
- lazy var imgHeader: UIImageView = {
- let imgView = UIImageView(frame: CGRect(x: 0, y: 0, width: fitSize(x: 141), height: fitSize(x: 17)))
- imgView.centerX = SCREENW/2
- imgView.centerY = fitSize(x: 20)
- imgView.image = UIImage(named: "good_gods")
- return imgView
- }()
-
- @objc func refreshData() {
- self.page = 1
- loadBannerData()
- loadGoodsData()
- loadYouLikeGoods()
-
- }
-
- @objc func loadMoreData() {
- self.page += 1
- loadGoodsData()
- }
- }
- ///MARK: UICollectionViewDelegate
- extension YMBestChoiceController: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
-
- func numberOfSections(in collectionView: UICollectionView) -> Int {
- return 2
- }
-
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- if section == 0 {
- return 1
- }else {
- return self.dataArr.count
- }
-
- }
-
- //cell的大小
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
-
- if indexPath.section == 0 {
- if recomArr.count == 0 {
- return CGSize(width: SCREENW, height: fitSize(x: 0))
- }
- return CGSize(width: SCREENW, height: fitSize(x: 230))
- }else {
- let model: YMTodayGoodModel = self.dataArr[indexPath.row]
-
- switch model.type {
- case 1:
- return CGSize(width: SCREEN_WIDTH, height: fitSize(x: 135))
-
- case 2:
- return CGSize(width: SCREEN_WIDTH, height: fitSize(x: 215))
-
- default: break
-
- }
- }
-
-
- return CGSize(width: SCREEN_WIDTH, height: fitSize(x: 135))
- }
-
- //每个分区的内边距
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
-
- if section == 0 {
- return UIEdgeInsetsMake(0, 0, 0, 0)
- }
- return UIEdgeInsetsMake(0, 0, 0, 0);
-
- }
- //最小 item 间距
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
-
- return 0;
- }
- //最小行间距
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
-
- return 5;
-
- }
- //每个分区区头尺寸
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
- if section == 0 {
- return CGSize (width: SCREEN_WIDTH, height: fitSize(x: 150))
- }
-
- return CGSize(width: SCREENW, height: fitSize(x: 40))
- }
-
-
- //返回区头、区尾实例
- func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
-
- if kind == UICollectionElementKindSectionHeader {
-
- if indexPath.section == 0 {
- let headview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: KBannerReuseIdentifier, for: indexPath)
- headview.backgroundColor = UIColor.white
- headview.addSubview(self.bannerBg)
- headview.addSubview(self.cycleView)
- return headview;
- }else {
-
- let headview = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: KGoodsReuseIdentifier, for: indexPath)
- headview.backgroundColor = UIColor.white
- headview.addSubview(imgHeader)
- return headview;
- }
- }
- return UICollectionReusableView();
-
- }
-
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
-
- if indexPath.section == 0 {
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KRecomCell, for: indexPath) as! YMTodayRecomCell
- cell.setDataArray(dataArr: self.recomArr)
- cell.delegate = self
- return cell
- }else {
- let model: YMTodayGoodModel = self.dataArr[indexPath.row]
-
- switch model.type {
- case 1:
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: KMainGoodCell, for: indexPath) as! YMMainGoodCollectionCell
- cell.model = model.goods
- return cell
-
- case 2:
- let groupCell = collectionView.dequeueReusableCell(withReuseIdentifier: KGroupGoodCell, for: indexPath) as! YMTodayGroupCollectionCell
- groupCell.model = model
- groupCell.clickClosure = {
- (model: YMMainGoodModel)->Void in
- let detail = YMDetailViewController()
- detail.goodModel = model
- self.navigationController?.pushViewController(detail, animated: true)
- }
- return groupCell
-
- default: break
-
- }
- }
-
- return UICollectionViewCell()
- }
-
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- let detail = YMDetailViewController()
- let todayModel = self.dataArr[indexPath.row]
- detail.goodModel = todayModel.goods
- self.navigationController?.pushViewController(detail, animated: true)
- }
-
- }
- extension YMBestChoiceController: YMTodayRecomCellDelegate {
- func didSelectedItem(index: Int, model: YMMainGoodModel) {
- let detail = YMDetailViewController()
- detail.goodModel = model
- self.navigationController?.pushViewController(detail, animated: true)
- }
- }
|