酷店

KDPSearchResultViewController.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //
  2. // KDPSearchResultViewController.m
  3. // KuDianProject
  4. //
  5. // Created by admin on 2019/7/10.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "KDPSearchResultViewController.h"
  9. #import "KDPSupplyContentReusableView.h"
  10. #import "KDPRecommendGoodCell.h"
  11. #import "KDPScreenViewController.h"
  12. @interface KDPSearchResultViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
  13. @property (nonatomic, strong) UICollectionView *collectionView;
  14. @property (nonatomic, strong) NSMutableArray *dataSource;
  15. @property (nonatomic, assign) NSInteger page;
  16. @property (nonatomic, strong) NSMutableDictionary *params;
  17. @end
  18. @implementation KDPSearchResultViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. // Do any additional setup after loading the view.
  22. [self setupNav];
  23. [self.view addSubview:self.collectionView];
  24. [self requestGoodData];
  25. }
  26. - (void)setupNav{
  27. self.navBar.hidden = YES;
  28. UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, KDNavBarHeight)];
  29. navView.backgroundColor = [UIColor whiteColor];
  30. [self.view addSubview:navView];
  31. self.view.backgroundColor = [UIColor colorWithHex:0xf4f4f4];
  32. UIButton *returnBtn =[[UIButton alloc]initWithFrame:CGRectMake(15, KDNavBarHeight - 13-18, 11, 18)];
  33. [returnBtn addTarget:self action:@selector(returnClickBtn) forControlEvents:UIControlEventTouchUpInside];
  34. [returnBtn setImage:[UIImage imageNamed:@"return_black"] forState:UIControlStateNormal];
  35. returnBtn.adjustsImageWhenHighlighted = NO;
  36. [navView addSubview:returnBtn];
  37. UIButton *searchBtn =[[UIButton alloc]initWithFrame:CGRectMake(41, KDStatusHeight+5, SCREEN_WIDTH-55, 31)];
  38. searchBtn.layer.cornerRadius=15.5;
  39. searchBtn.backgroundColor=[UIColor colorWithHex:0xEEEEEE];
  40. [searchBtn addTarget:self action:@selector(returnClickBtn) forControlEvents:UIControlEventTouchUpInside];
  41. [navView addSubview:searchBtn];
  42. UIImageView *iconimg=[[UIImageView alloc]initWithFrame:CGRectMake(12, 8, 15, 15)];
  43. iconimg.image=[UIImage imageNamed:@"search_icon"];
  44. [searchBtn addSubview:iconimg];
  45. UITextField *textField=[[UITextField alloc]init];
  46. textField.textColor=[UIColor whiteColor];
  47. textField.backgroundColor=[UIColor colorWithHex:0x929292];
  48. textField.font=[UIFont systemFontOfSize:14];
  49. textField.userInteractionEnabled=NO;
  50. [searchBtn addSubview:textField];
  51. textField.layer.cornerRadius=2;
  52. textField.layer.masksToBounds=YES;
  53. [textField mas_makeConstraints:^(MASConstraintMaker *make) {
  54. make.left.mas_equalTo(iconimg.right+5);
  55. make.top.mas_equalTo(3);
  56. make.height.mas_equalTo(24);
  57. }];
  58. textField.text = [NSString stringWithFormat:@" %@ × ",self.keyWordStr];
  59. }
  60. - (void)requestGoodData{
  61. if (![KDPAccountTool isLogin]) {
  62. return;
  63. }
  64. [self.params setValue:[NSString stringWithFormat:@"%ld",(long)self.page] forKey:@"page"];
  65. [LoadingView showInView:self.view];
  66. NSString *getGoodUrl = [NSString stringWithFormat:@"%@api/goods/search",KDURL];
  67. [KDPNetworkRequestHTTP postURL:getGoodUrl params:self.params success:^(id _Nonnull json) {
  68. [LoadingView dismiss];
  69. NSArray *dataArr = [NSArray yy_modelArrayWithClass:[KDPGoodsModel class] json:json[@"data"]];
  70. if (self.page == 1) {
  71. [self.dataSource removeAllObjects];
  72. }
  73. if (dataArr.count > 0) {
  74. [self.dataSource addObjectsFromArray:dataArr];
  75. } else{
  76. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  77. }
  78. [self.collectionView.mj_footer endRefreshing];
  79. [self.collectionView.mj_header endRefreshing];
  80. [self.collectionView reloadData];
  81. } failure:^(NSError * _Nonnull error) {
  82. [LoadingView dismiss];
  83. }];
  84. }
  85. - (void)returnClickBtn{
  86. [self.navigationController popViewControllerAnimated:NO];
  87. }
  88. - (void)viewWillAppear:(BOOL)animated{
  89. [super viewWillAppear:animated];
  90. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
  91. }
  92. - (void)viewWillDisappear:(BOOL)animated{
  93. [super viewWillDisappear:animated];
  94. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  95. }
  96. - (UICollectionView *)collectionView{
  97. if (!_collectionView) {
  98. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
  99. flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH, 130);
  100. flowLayout.minimumLineSpacing = 0;
  101. flowLayout.minimumInteritemSpacing = 0;
  102. flowLayout.sectionHeadersPinToVisibleBounds = YES;
  103. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight) collectionViewLayout:flowLayout];
  104. _collectionView.showsVerticalScrollIndicator = NO;
  105. _collectionView.showsHorizontalScrollIndicator = NO;
  106. _collectionView.emptyDataSetSource = self;
  107. _collectionView.emptyDataSetDelegate = self;
  108. [_collectionView registerClass:[KDPRecommendGoodCell class] forCellWithReuseIdentifier:NSStringFromClass([KDPRecommendGoodCell class])];
  109. [_collectionView registerClass:[KDPSupplyContentReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([KDPSupplyContentReusableView class])];
  110. _collectionView.backgroundColor = [UIColor whiteColor];
  111. _collectionView.delegate = self;
  112. _collectionView.dataSource = self;
  113. _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  114. self.page = 1;
  115. [self requestGoodData];
  116. }];
  117. _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  118. self.page ++;
  119. [self requestGoodData];
  120. }];
  121. }
  122. return _collectionView;
  123. }
  124. - (NSMutableArray *)dataSource{
  125. if (!_dataSource) {
  126. _dataSource = [NSMutableArray array];
  127. }
  128. return _dataSource;
  129. }
  130. - (NSMutableDictionary *)params{
  131. if (!_params) {
  132. _params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"",@"page",@"",@"sort",self.keyWordStr,@"keyword",@"",@"start_price",@"",@"end_price",@"",@"start_tk_rate",@"0",@"is_has_coupon", nil];
  133. }
  134. return _params;
  135. }
  136. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  137. KDPSupplyContentReusableView *reusAbleView;
  138. if (kind == UICollectionElementKindSectionHeader) {
  139. reusAbleView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([KDPSupplyContentReusableView class]) forIndexPath:indexPath];
  140. reusAbleView.layer.borderColor = [UIColor colorWithHex:0xf4f4f4].CGColor;
  141. reusAbleView.layer.borderWidth = 0.5;
  142. WeakSelf(weakSelf);
  143. __weak KDPSupplyContentReusableView *weakReusAbleView = reusAbleView;
  144. reusAbleView.changeBlock = ^(BOOL isPrice) {
  145. KDPScreenViewController *screenVC = [[KDPScreenViewController alloc] initWithPrice:isPrice];
  146. screenVC.params = weakSelf.params;
  147. screenVC.resetBlock = ^(BOOL isPrice) {
  148. if (isPrice == YES) {
  149. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor colorWithHex:0x333333] text:@"价格筛选"];
  150. [self.params setValue:@"" forKey:@"start_price"];
  151. [self.params setValue:@"" forKey:@"end_price"];
  152. } else{
  153. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor colorWithHex:0x333333] text:@"利润筛选"];
  154. [self.params setValue:@"" forKey:@"start_tk_rate"];
  155. }
  156. [weakSelf.collectionView.mj_header beginRefreshing];
  157. };
  158. screenVC.profitBlock = ^(NSString *baseScale) {
  159. if (!baseScale.length) {
  160. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor colorWithHex:0x333333] text:[NSString stringWithFormat:@"利润筛选"]];
  161. [self.params setValue:@"" forKey:@"start_tk_rate"];
  162. } else{
  163. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor baseColor] text:[NSString stringWithFormat:@"佣金%@%%以上",baseScale]];
  164. [self.params setValue:baseScale forKey:@"start_tk_rate"];
  165. }
  166. [weakSelf.collectionView.mj_header beginRefreshing];
  167. };
  168. screenVC.priceBlock = ^(NSString *minPrice, NSString *maxPrice) {
  169. if (minPrice.length != 0 || maxPrice.length != 0) {
  170. if (minPrice.length == 0) {
  171. minPrice = @"0";
  172. }
  173. NSString *price=[NSString stringWithFormat:@"价格%@-%@元",minPrice,maxPrice];
  174. if (maxPrice.integerValue == 0 || [maxPrice intValue] <= [minPrice intValue]) {
  175. price=[NSString stringWithFormat:@"价格%@元以上",minPrice];
  176. maxPrice = @"";
  177. }
  178. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor baseColor] text:price];
  179. } else{
  180. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor colorWithHex:0x333333] text:@"价格筛选"];
  181. }
  182. [weakSelf.params setValue:minPrice forKey:@"start_price"];
  183. [weakSelf.params setValue:maxPrice forKey:@"end_price"];
  184. [weakSelf.collectionView.mj_header beginRefreshing];
  185. };
  186. CWLateralSlideConfiguration *conf = [CWLateralSlideConfiguration configurationWithDistance:0 maskAlpha:0.7 scaleY:1 direction:CWDrawerTransitionFromRight backImage:nil];
  187. conf.distance=SCREEN_WIDTH/3*2;
  188. [weakSelf cw_showDrawerViewController:screenVC animationType:0 configuration:conf];
  189. };
  190. return reusAbleView;
  191. }
  192. return nil;
  193. }
  194. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  195. if (section == 0) {
  196. return CGSizeMake(SCREEN_WIDTH, 33);
  197. }
  198. return CGSizeZero;
  199. }
  200. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  201. return 1;
  202. }
  203. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  204. return self.dataSource.count;
  205. }
  206. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  207. KDPRecommendGoodCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KDPRecommendGoodCell class]) forIndexPath:indexPath];
  208. cell.model = self.dataSource[indexPath.row];
  209. return cell;
  210. }
  211. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
  212. return [UIImage imageNamed:@"no_order"];
  213. }
  214. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
  215. return YES;
  216. }
  217. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{
  218. return 50;
  219. }
  220. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  221. return [[NSAttributedString alloc] initWithString:@"还没有记录" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333],NSFontAttributeName:FONT_SYS(12)}];
  222. }
  223. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  224. KDPGoodDetailVC *detailVC = [[KDPGoodDetailVC alloc] init];
  225. detailVC.model = self.dataSource[indexPath.row];
  226. [self.navigationController pushViewController:detailVC animated:YES];
  227. }
  228. @end