口袋优选

KBChildPageViewController.m 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. //
  2. // KBChildPageViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/1/16.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBChildPageViewController.h"
  9. #import "KBChildPageViewController.h"
  10. #import "KBChildHeaderView.h"
  11. #import "KBTypeButtonHeader.h"
  12. #import "XLPlainFlowLayout.h"
  13. #import "KBHeaderReusableView.h"
  14. #import "KBGoodCollectionCell.h"
  15. #import "KBChildCategoryModel.h"
  16. #import "KBChildGoodModel.h"
  17. #import "KBItemListViewController.h"
  18. #import "KBGoodListViewController.h"
  19. #import "KBGoodDetailViewController.h"
  20. #import "SelectView.h"
  21. #import "KBOtherFilterView.h"
  22. #import "KBTodayModelView.h"
  23. #import "KBClassifyListController.h"
  24. #import "KBOtherTopCollectionCell.h"
  25. #import "AdJumpManager.h"
  26. #import "KBChildPageAdView.h"
  27. static NSString *headerID = @"headerID";
  28. static NSString *cellID = @"KBGoodCollectionCell";
  29. static NSString *KSectionHeader = @"SectionHeader";
  30. static NSString *KLDOtherTopCollectionCell = @"KBOtherTopCollectionCell";
  31. static CGFloat selectedViewHeight = 40.f;
  32. @interface KBChildPageViewController ()
  33. <
  34. UICollectionViewDelegate,
  35. UICollectionViewDataSource,
  36. XHCollectionViewDelegateFlowLayout,
  37. ClassifySelectViewDelegate,
  38. YHChildHeaderViewDelegate
  39. >
  40. {
  41. NSInteger _page;
  42. NSInteger _type;
  43. NSInteger _is_has_coupon;
  44. BOOL _changeType;
  45. UIView *topRecomView;
  46. }
  47. @property (nonatomic, strong) UICollectionView *collectionView;
  48. @property (nonatomic, strong) NSArray *categoryArr;
  49. @property (nonatomic, strong) NSMutableArray *goodsArr;
  50. @property (nonatomic, strong) NSArray *adDataArr;
  51. @property (nonatomic, strong) KBOtherFilterView *selectedView;
  52. @property (nonatomic, strong) NSArray *filtrateArray;
  53. @property (nonatomic, strong) KBTodayModelView *recommonView;
  54. @property (nonatomic, strong) KBChildHeaderView *channelHeader;
  55. @property (nonatomic, strong) KBChildPageAdView *headAdView;
  56. @property (nonatomic, strong) KBOtherTopCollectionCell *topCell;
  57. @end
  58. @implementation KBChildPageViewController
  59. - (void)viewDidLoad {
  60. [super viewDidLoad];
  61. [self configParam];
  62. [self configCollectionView];
  63. [self requesFiltrateParam];
  64. [self loadChannelHeader];
  65. [self loadAdData];
  66. }
  67. - (void)configParam {
  68. _page = 0;
  69. _type = 1; //默认请求推荐的数据
  70. }
  71. - (void)configCollectionView {
  72. [self.collectionView removeFromSuperview];
  73. XLPlainFlowLayout *flowLayout = [[XLPlainFlowLayout alloc]init];
  74. flowLayout.naviHeight = 0;
  75. flowLayout.minimumLineSpacing = 5;
  76. flowLayout.minimumInteritemSpacing = 0;
  77. flowLayout.headerReferenceSize = CGSizeMake(0, 0);
  78. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
  79. [self.collectionView registerClass:[KBGoodCollectionCell class] forCellWithReuseIdentifier:cellID];
  80. [self.collectionView registerClass:[KBOtherTopCollectionCell class] forCellWithReuseIdentifier:KLDOtherTopCollectionCell];
  81. [self.collectionView registerClass:[KBHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerID];
  82. [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeader];
  83. self.collectionView.backgroundColor = [UIColor clearColor];
  84. self.collectionView.showsVerticalScrollIndicator = NO;
  85. self.collectionView.delegate = self;
  86. self.collectionView.dataSource = self;
  87. MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  88. [self refreshData];
  89. }];
  90. header.activityIndicatorViewStyle=UIActivityIndicatorViewStyleWhite;
  91. header.stateLabel.textColor=[UIColor whiteColor];
  92. header.lastUpdatedTimeLabel.hidden=YES;
  93. [header setTitle:@"数据正在刷新" forState:MJRefreshStateRefreshing];
  94. // header.lastUpdatedTimeLabel.hidden = YES;
  95. // header.stateLabel.hidden = YES;
  96. //正在刷新的图片
  97. // NSArray *imgArr = @[[UIImage imageNamed:@"ant1"],[UIImage imageNamed:@"ant2"]];
  98. // [header setImages:imgArr duration:0.3 forState:MJRefreshStateRefreshing];
  99. // [header setImages:@[[UIImage imageNamed:@"ant1"]] duration:1 forState:MJRefreshStateWillRefresh];
  100. // [header setImages:@[[UIImage imageNamed:@"ant1"]] duration:1 forState:MJRefreshStatePulling];
  101. self.collectionView.mj_header = header;
  102. self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
  103. [self loadMoreData];
  104. }];
  105. [self.collectionView.mj_footer beginRefreshing];
  106. [self.view addSubview: self.collectionView];
  107. [self configCollectionHeader];
  108. }
  109. - (void)configCollectionHeader {
  110. UIView *recomView = [[UIView alloc] initWithFrame:CGRectMake(Fitsize(10), 0, SCREEN_WIDTH-Fitsize(20), Fitsize(208))];
  111. topRecomView = recomView;
  112. recomView.layer.cornerRadius = Fitsize(8);
  113. recomView.backgroundColor = [UIColor whiteColor];
  114. UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, Fitsize(65), Fitsize(40))];
  115. title.font = [UIFont systemFontOfSize:Fitsize(14)];//[UIColor homeRedColor]
  116. title.centerX = SCREEN_WIDTH/2;
  117. title.text = @"热门商品";
  118. [title sizeToFit];
  119. title.textColor =[UIColor gradientWidthFromColor:[UIColor YHColorWithHex:0x53F3FD] toColor:[UIColor YHColorWithHex:0x726BFF] withWidth:title.width];
  120. [recomView addSubview:title];
  121. UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(title.left-20, 0, Fitsize(13), Fitsize(15))];
  122. icon.centerY = title.centerY;
  123. icon.image = [UIImage imageNamed:@"hotRecom_left"];
  124. [recomView addSubview:icon];
  125. UIImageView *icon2 = [[UIImageView alloc] initWithFrame:CGRectMake(title.right+5, 0, Fitsize(13), Fitsize(15))];
  126. icon2.centerY = title.centerY;
  127. icon2.image = [UIImage imageNamed:@"hotRecom_right"];
  128. [recomView addSubview:icon2];
  129. KBTodayModelView *modelView = [[KBTodayModelView alloc] initWithFrame:CGRectMake(Fitsize(0), Fitsize(40), SCREEN_WIDTH-Fitsize(20), Fitsize(168)) titleNumOfLine:1];
  130. modelView.isOhterPage = YES;
  131. modelView.name = self.model.name;
  132. modelView.Id = self.model.Id;
  133. self.recommonView = modelView;
  134. [recomView addSubview:modelView];
  135. self.headAdView = [[KBChildPageAdView alloc] initWithFrame:CGRectMake(Fitsize(10), 0, SCREEN_WIDTH-Fitsize(20), 0)];
  136. __weak typeof(self) weakSelf = self;
  137. self.headAdView.tapAdBlock = ^(AdPopModel * _Nonnull model) {
  138. if (model) {
  139. [AdJumpManager jumpToPageWithModel:model ParentViewControll:weakSelf];
  140. }
  141. };
  142. self.channelHeader = [[KBChildHeaderView alloc] initWithFrame:CGRectMake(Fitsize(10), 0, SCREEN_WIDTH-Fitsize(20), 0) delegete:self];
  143. }
  144. #pragma mark ====================== Load Data ==========
  145. - (void)loadAdData {
  146. NSString *url = [NSString stringWithFormat:@"%@/api/v2/adv/advByType",BaseURL];
  147. NSDictionary *para = @{@"adv_type":@"7",
  148. @"category_id":self.model.Id};
  149. [KBHttp get:url params:para success:^(id json) {
  150. self.adDataArr = [NSArray yy_modelArrayWithClass:[AdPopModel class] json:json[@"data"]];
  151. if (self.adDataArr.count > 0) {
  152. //展示广告
  153. self.headAdView.height = Fitsize(111);
  154. self.headAdView.y = self.channelHeader.height;
  155. topRecomView.y = self.headAdView.bottom+5;
  156. AdPopModel *model = self.adDataArr.firstObject;
  157. [self.headAdView setAdModel:model];
  158. }else {
  159. //隐藏广告
  160. self.headAdView.height = 0;
  161. topRecomView.y = self.channelHeader.height;
  162. }
  163. [self.collectionView reloadData];
  164. } failure:^(NSError *error) {
  165. }];
  166. }
  167. /**
  168. 加载筛选范围
  169. */
  170. - (void)requesFiltrateParam {
  171. NSString *urlString = [NSString stringWithFormat:@"%@/api/categoryNew/priceSection", BaseURL];
  172. [KBHttp post:urlString params:nil success:^(id json) {
  173. self.filtrateArray = json[@"data"];
  174. // [self.view addSubview:self.selectedView];
  175. } failure:^(NSError *error) {
  176. }];
  177. }
  178. /**
  179. 加载下部商品列表
  180. */
  181. - (void)loadCategoryGoodsList:(BOOL)refresh switchButton:(UIButton *)switchButton{
  182. NSDictionary *param = @{@"page":@(_page),
  183. @"category_id":self.model.Id,
  184. @"sort":@(_type),
  185. @"is_has_coupon":@(_is_has_coupon),
  186. @"stype":@"0"
  187. };
  188. NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/stockTopCategoryList",BaseURL];
  189. [KBHttp post:url params:param success:^(id json) {
  190. if (refresh) [self.goodsArr removeAllObjects];
  191. if ([self.collectionView.mj_header isRefreshing])[self.goodsArr removeAllObjects];
  192. NSArray *topList = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"][@"top"]];
  193. [self.recommonView setGoodData:topList];
  194. NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildGoodModel class] json:json[@"data"][@"list"]];
  195. [self.goodsArr addObjectsFromArray:list];
  196. [self noMoreDataWithArray:list];
  197. [self.collectionView reloadData];
  198. [self.collectionView.mj_header endRefreshing];
  199. [self.collectionView.mj_footer endRefreshing];
  200. [SVProgressHUD dismiss];
  201. switchButton.selected = !switchButton.selected;
  202. } failure:^(NSError *error) {
  203. [self.collectionView.mj_header endRefreshing];
  204. [self.collectionView.mj_footer endRefreshing];
  205. [SVProgressHUD dismiss];
  206. }];
  207. }
  208. - (void)noMoreDataWithArray:(NSArray *)array {
  209. if (array > 0) {
  210. self.collectionView.footRefreshState = MJFooterRefreshStateLoadMore;
  211. }else {
  212. self.collectionView.footRefreshState = MJFooterRefreshStateNoMore;
  213. }
  214. }
  215. /**
  216. 加载头部分类
  217. */
  218. - (void)loadChannelHeader {
  219. NSString *url = [NSString stringWithFormat:@"%@/api/v2/channel/getSonGroup",BaseURL];
  220. NSDictionary *para = @{@"category_id":self.model.Id};
  221. [KBHttp post:url params:para success:^(id json) {
  222. NSArray *list = [NSArray yy_modelArrayWithClass:[KBChildCategoryModel class] json:json[@"data"]];
  223. [self.channelHeader setChannelDataArr:list];
  224. if (self.adDataArr.count > 0) {
  225. self.headAdView.y = self.channelHeader.bottom;
  226. topRecomView.y = self.headAdView.bottom+5;
  227. }else {
  228. topRecomView.y = self.channelHeader.height;
  229. }
  230. [self.collectionView reloadData];
  231. [self loadCategoryGoodsList:NO switchButton:nil];
  232. } failure:^(NSError *error) {
  233. [self.collectionView.mj_header endRefreshing];
  234. [self.collectionView.mj_footer endRefreshing];
  235. }];
  236. }
  237. /**
  238. 上拉加载
  239. */
  240. - (void)loadMoreData {
  241. _page++;
  242. [self loadCategoryGoodsList:NO switchButton:nil];
  243. }
  244. - (void)refreshData {
  245. _page = 1;
  246. [self loadChannelHeader];
  247. // [self loadCategoryGoodsList:YES switchButton:nil];
  248. }
  249. #pragma mark ------ YHChildHeaderViewDelegate --------
  250. - (void)YHChildHeaderViewDidSelectedIndex:(NSInteger)index model:(KBChildCategoryModel *)model {
  251. KBClassifyListController *list = [[KBClassifyListController alloc] init];
  252. list.firstCategoryId = self.model.Id;
  253. list.cate_id = model.Id;
  254. list.name = model.name;
  255. list.isOtherPage = YES;
  256. [self.navigationController pushViewController:list animated:YES];
  257. NSString *tag = [NSString stringWithFormat:@"%@-%@",self.model.name,model.name];
  258. [MobClick event:CategoryClassify label:tag];
  259. }
  260. #pragma mark -------JC_SelectViewDelegate
  261. ////仅显示优惠券
  262. //- (void)ClassifyselectItme:(KBOtherFilterView *)selectView onlyTicketClick:(UIButton *)switchBtn {
  263. // [SVProgressHUD show];
  264. // [self.collectionView scrollToTop];
  265. // _is_has_coupon = switchBtn.selected ? 0 : 1;
  266. // [self loadCategoryGoodsList:YES switchButton:switchBtn];
  267. //}
  268. //上方条件选择
  269. - (void)ClassifyselectTopButton:(KBOtherFilterView *)selectView withIndex:(NSInteger)index withButtonType:(JCButtonClickType )type1 button:(UIButton *)button{
  270. [SVProgressHUD show];
  271. //价格
  272. if (index == 2&&type1) {
  273. switch (type1) {
  274. case JCButtonClickTypeNormal:
  275. //正常价格
  276. {
  277. NSLog(@"上边按钮的正常价格");
  278. }
  279. break;
  280. case JCButtonClickTypeUp:
  281. //价格升序排列
  282. {
  283. NSLog(@"上边按钮的价格升序排列");
  284. _type = 3;
  285. }
  286. break;
  287. case JCButtonClickTypeDown:
  288. //价格降序排列
  289. {
  290. NSLog(@"上边按钮的价格降序排列");
  291. _type = 4;
  292. }
  293. break;
  294. default:
  295. break;
  296. }
  297. }else if (index == 0){//综合
  298. NSLog(@"上边按钮的综合");
  299. _type = 1;
  300. }else if (index == 1){//销量
  301. NSLog(@"上边按钮的销量");
  302. _type = 2;
  303. }else{//筛选
  304. _is_has_coupon = button.selected ? 0 : 1;
  305. [self loadCategoryGoodsList:YES switchButton:button];
  306. return;
  307. NSLog(@"上边有券按钮");
  308. }
  309. [self loadCategoryGoodsList:YES switchButton:nil];
  310. }
  311. #pragma mark ------
  312. #pragma mark - scrollView
  313. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  314. {
  315. //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
  316. UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
  317. //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
  318. CGFloat velocity = [pan velocityInView:scrollView].y;
  319. if (velocity <- 10) {
  320. //向上拖动,隐藏导航栏
  321. self.collectionView.height = ChildTableViewHeight+NavBarHeight-KStatusBarHeight;
  322. }else if (velocity > 10) {
  323. //向下拖动,显示导航栏
  324. self.collectionView.height = ChildTableViewHeight;
  325. }else if(velocity == 0){
  326. //停止拖拽
  327. }
  328. NSNotification *noti = [[NSNotification alloc] initWithName:ScrollDirectorNoti object:nil userInfo:@{@"director":@(velocity)}];
  329. [[NSNotificationCenter defaultCenter] postNotification:noti];
  330. }
  331. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  332. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  333. return 2;
  334. }
  335. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  336. {
  337. if (section == 0) {
  338. return 1;
  339. }else {
  340. return self.goodsArr.count;
  341. }
  342. }
  343. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  344. if (indexPath.section == 0) {
  345. CGFloat height = topRecomView.bottom+5;
  346. return CGSizeMake(SCREEN_WIDTH, height);
  347. }else {
  348. CGFloat width = (SCREEN_WIDTH)/2;
  349. CGFloat height = width + 102;
  350. return CGSizeMake(width-FITSIZE(12.5f), height);
  351. }
  352. }
  353. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  354. {
  355. if (indexPath.section == 0) {
  356. self.topCell = [collectionView dequeueReusableCellWithReuseIdentifier:KLDOtherTopCollectionCell forIndexPath:indexPath];
  357. [self.topCell addSubview:self.channelHeader];
  358. [self.topCell addSubview:self.headAdView];
  359. [self.topCell addSubview:topRecomView];
  360. return self.topCell;
  361. }else {
  362. KBGoodCollectionCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
  363. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  364. cell.model = model;
  365. return cell;
  366. }
  367. }
  368. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
  369. UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:KSectionHeader forIndexPath:indexPath];
  370. [header addSubview:self.selectedView];
  371. return header;
  372. }
  373. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  374. KBChildGoodModel *model = self.goodsArr[indexPath.row];
  375. if ([model.type isEqualToString:@"1"]) {
  376. //专场
  377. KBGoodListViewController *list = [[KBGoodListViewController alloc] init];
  378. list.cate_id = model.goods_id;
  379. [self.navigationController pushViewController:list animated:YES];
  380. }else {
  381. //详情
  382. KBGoodDetailViewController *detail = [[KBGoodDetailViewController alloc] init];
  383. DetailRequestModel *requestModel = [[DetailRequestModel alloc] initWithChildModel:model];
  384. detail.requestModel = requestModel;
  385. KBEventModel *evevtModel = [[KBEventModel alloc] initWithOrigin:model.origin category_id:self.model.Id source:homeAction];
  386. detail.eventModel = evevtModel;
  387. [self.navigationController pushViewController:detail animated:YES];
  388. }
  389. [MobClick event:category_goods label:self.model.name];
  390. }
  391. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
  392. if (section == 0) {
  393. return CGSizeMake(0, 0);
  394. }else {
  395. return CGSizeMake(SCREEN_WIDTH, selectedViewHeight+5);
  396. }
  397. }
  398. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  399. if (section == 0) {
  400. return UIEdgeInsetsMake(0, 0, 0, 0);
  401. }else{
  402. return UIEdgeInsetsMake(0, FITSIZE(10), 0, FITSIZE(10));
  403. }
  404. }
  405. #pragma mark - JHCollectionViewDelegateFlowLayout
  406. - (UIColor *)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout backgroundColorForSection:(NSInteger)section
  407. {
  408. return [@[
  409. [UIColor clearColor],
  410. [UIColor YHColorWithHex:0xf6f6f6],
  411. [UIColor yellowColor]
  412. ] objectAtIndex:section];
  413. }
  414. #pragma mark -------------
  415. - (NSMutableArray *)goodsArr {
  416. if (!_goodsArr) {
  417. _goodsArr = [NSMutableArray array];
  418. }
  419. return _goodsArr;
  420. }
  421. - (KBOtherFilterView *)selectedView {
  422. if (!_selectedView) {
  423. _selectedView = [[KBOtherFilterView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, selectedViewHeight) withArr:self.filtrateArray];
  424. _selectedView.delegate = self;
  425. }
  426. return _selectedView;
  427. }
  428. - (void)didReceiveMemoryWarning {
  429. [super didReceiveMemoryWarning];
  430. // Dispose of any resources that can be recreated.
  431. }
  432. /*
  433. #pragma mark - Navigation
  434. // In a storyboard-based application, you will often want to do a little preparation before navigation
  435. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  436. // Get the new view controller using [segue destinationViewController].
  437. // Pass the selected object to the new view controller.
  438. }
  439. */
  440. @end