酷店

KDPSearchResultViewController.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. if (@available(iOS 11.0, *)) {
  118. _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  119. } else {
  120. self.automaticallyAdjustsScrollViewInsets = NO;
  121. }
  122. _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  123. self.page ++;
  124. [self requestGoodData];
  125. }];
  126. }
  127. return _collectionView;
  128. }
  129. - (NSMutableArray *)dataSource{
  130. if (!_dataSource) {
  131. _dataSource = [NSMutableArray array];
  132. }
  133. return _dataSource;
  134. }
  135. - (NSMutableDictionary *)params{
  136. if (!_params) {
  137. _params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"",@"page",@"",@"sort",self.keyWordStr,@"keyword",@"",@"start_price",@"",@"end_price",@"",@"start_tk_rate",@"0",@"is_has_coupon", nil];
  138. }
  139. return _params;
  140. }
  141. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  142. KDPSupplyContentReusableView *reusAbleView;
  143. if (kind == UICollectionElementKindSectionHeader) {
  144. reusAbleView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([KDPSupplyContentReusableView class]) forIndexPath:indexPath];
  145. reusAbleView.layer.borderColor = [UIColor colorWithHex:0xf4f4f4].CGColor;
  146. reusAbleView.layer.borderWidth = 0.5;
  147. WeakSelf(weakSelf);
  148. __weak KDPSupplyContentReusableView *weakReusAbleView = reusAbleView;
  149. reusAbleView.changeBlock = ^(BOOL isPrice) {
  150. KDPScreenViewController *screenVC = [[KDPScreenViewController alloc] initWithPrice:isPrice];
  151. screenVC.params = weakSelf.params;
  152. screenVC.resetBlock = ^(BOOL isPrice) {
  153. if (isPrice == YES) {
  154. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor colorWithHex:0x333333] text:@"价格筛选"];
  155. [self.params setValue:@"" forKey:@"start_price"];
  156. [self.params setValue:@"" forKey:@"end_price"];
  157. } else{
  158. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor colorWithHex:0x333333] text:@"利润筛选"];
  159. [self.params setValue:@"" forKey:@"start_tk_rate"];
  160. }
  161. [weakSelf.collectionView.mj_header beginRefreshing];
  162. };
  163. screenVC.profitBlock = ^(NSString *baseScale) {
  164. if (!baseScale.length) {
  165. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor colorWithHex:0x333333] text:[NSString stringWithFormat:@"利润筛选"]];
  166. [self.params setValue:@"" forKey:@"start_tk_rate"];
  167. } else{
  168. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor baseColor] text:[NSString stringWithFormat:@"佣金%@%%以上",baseScale]];
  169. [self.params setValue:baseScale forKey:@"start_tk_rate"];
  170. }
  171. [weakSelf.collectionView.mj_header beginRefreshing];
  172. };
  173. screenVC.priceBlock = ^(NSString *minPrice, NSString *maxPrice) {
  174. if (minPrice.length != 0 || maxPrice.length != 0) {
  175. if (minPrice.length == 0) {
  176. minPrice = @"0";
  177. }
  178. NSString *price=[NSString stringWithFormat:@"价格%@-%@元",minPrice,maxPrice];
  179. if (maxPrice.integerValue == 0 || [maxPrice intValue] <= [minPrice intValue]) {
  180. price=[NSString stringWithFormat:@"价格%@元以上",minPrice];
  181. maxPrice = @"";
  182. }
  183. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor baseColor] text:price];
  184. } else{
  185. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor colorWithHex:0x333333] text:@"价格筛选"];
  186. }
  187. [weakSelf.params setValue:minPrice forKey:@"start_price"];
  188. [weakSelf.params setValue:maxPrice forKey:@"end_price"];
  189. [weakSelf.collectionView.mj_header beginRefreshing];
  190. };
  191. CWLateralSlideConfiguration *conf = [CWLateralSlideConfiguration configurationWithDistance:0 maskAlpha:0.7 scaleY:1 direction:CWDrawerTransitionFromRight backImage:nil];
  192. conf.distance=SCREEN_WIDTH/3*2;
  193. [weakSelf cw_showDrawerViewController:screenVC animationType:0 configuration:conf];
  194. };
  195. return reusAbleView;
  196. }
  197. return nil;
  198. }
  199. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  200. if (section == 0) {
  201. return CGSizeMake(SCREEN_WIDTH, 33);
  202. }
  203. return CGSizeZero;
  204. }
  205. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  206. return 1;
  207. }
  208. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  209. return self.dataSource.count;
  210. }
  211. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  212. KDPRecommendGoodCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KDPRecommendGoodCell class]) forIndexPath:indexPath];
  213. cell.model = self.dataSource[indexPath.row];
  214. return cell;
  215. }
  216. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
  217. return [UIImage imageNamed:@"no_order"];
  218. }
  219. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
  220. return YES;
  221. }
  222. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{
  223. return 50;
  224. }
  225. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  226. return [[NSAttributedString alloc] initWithString:@"还没有记录" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333],NSFontAttributeName:FONT_SYS(12)}];
  227. }
  228. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  229. KDPGoodDetailVC *detailVC = [[KDPGoodDetailVC alloc] init];
  230. detailVC.model = self.dataSource[indexPath.row];
  231. [self.navigationController pushViewController:detailVC animated:YES];
  232. }
  233. @end