dkahgld

ZBProductListViewController.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. //
  2. // ZBProductListViewController.m
  3. // ZBProject
  4. //
  5. // Created by 学丽 on 2019/4/9.
  6. // Copyright © 2019 ZB. All rights reserved.
  7. //
  8. #import "ZBProductListViewController.h"
  9. #import "ZBProductListCell.h"
  10. @interface ZBProductListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
  11. {
  12. CGFloat _lastContentOffset;
  13. CGFloat _oldY;
  14. }
  15. @property(nonatomic,strong)UICollectionView *collectionV;
  16. @property(nonatomic,strong)NSMutableArray *dataArray;
  17. @property(nonatomic,assign)NSInteger Page;
  18. @property(nonatomic,strong)NSMutableArray *listArray;
  19. //侧滑筛选
  20. @property(nonatomic,strong)ZBFilterView *filterView;
  21. @property(nonatomic,copy)NSString *lowestPrice;//最低价
  22. @property(nonatomic,copy)NSString *highPrice;//最低价
  23. @property(nonatomic,copy)NSString *start_rate;//佣金比例下限
  24. @property(nonatomic,assign)NSInteger counts;//判断是不是首次打开
  25. @end
  26. @implementation ZBProductListViewController
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. [self initNav];
  30. self.counts = 1;
  31. [self getProductData];
  32. if ([self.model.type isEqualToString:@"cate"]) {
  33. [self requestWithId:self.model.Id];
  34. }else{
  35. self.collectionV.backgroundColor=[UIColor lineColor];
  36. }
  37. }
  38. -(void)requestWithId:(NSString *)Id
  39. {
  40. if (![AccountTool isLogin]) {
  41. return;
  42. }
  43. [LoadingView showInView:self.view];
  44. [ZBHTTP post:[NSString stringWithFormat:@"%@api/goods/childList",BASEURL] params:@{@"pid":Id} success:^(id _Nonnull json) {
  45. [self.listArray removeAllObjects];
  46. NSArray *array =[NSArray yy_modelArrayWithClass:[ClassModel class] json:json[@"list"]];
  47. [self.listArray addObjectsFromArray:array];
  48. [LoadingView dismiss];
  49. if (self.listArray.count ==0) {
  50. self.collectionV.backgroundColor=[UIColor lineColor];
  51. }else{
  52. self.collectionV.backgroundColor=[UIColor clearColor];
  53. }
  54. [self.collectionV reloadData];
  55. } failure:^(NSError * _Nonnull error) {
  56. [LoadingView dismiss];
  57. }];
  58. }
  59. -(void)getProductData
  60. {
  61. if (![AccountTool isLogin]) {
  62. return;
  63. }
  64. [LoadingView showInView:self.view];
  65. //type为cate有
  66. NSDictionary *dics =@{@"page":@(self.Page),@"id":self.model.Id,@"type":self.model.type,@"start_price":self.lowestPrice,@"end_price":self.highPrice,@"start_tk_rate":self.start_rate};
  67. [ZBHTTP post:[NSString stringWithFormat:@"%@api/goods/listGoods",BASEURL] params:dics success:^(id _Nonnull json) {
  68. [LoadingView dismiss];
  69. NSArray *array =[NSArray yy_modelArrayWithClass:[ZBGoodModel class] json:json[@"data"]];
  70. [self.collectionV.mj_footer endRefreshing];
  71. [self.collectionV.mj_header endRefreshing];
  72. if (self.Page == 1) {
  73. self.dataArray=[NSMutableArray array];
  74. [self.dataArray removeAllObjects];
  75. }
  76. self.counts++;
  77. if (array.count > 0) {
  78. [self.dataArray addObjectsFromArray:array];
  79. }
  80. if (array.count == 0) {
  81. [self.collectionV.mj_footer endRefreshingWithNoMoreData];
  82. }
  83. [self.collectionV reloadData];
  84. } failure:^(NSError * _Nonnull error) {
  85. [LoadingView dismiss];
  86. }];
  87. }
  88. -(void)initNav
  89. {
  90. self.Page = 1;
  91. self.lowestPrice=@"";
  92. self.start_rate=@"";
  93. self.highPrice=@"";
  94. [self.view addSubview:self.collectionV];
  95. [self.collectionV mas_makeConstraints:^(MASConstraintMaker *make) {
  96. make.left.right.mas_equalTo(0);
  97. make.top.mas_equalTo(10);
  98. make.bottom.mas_equalTo(self.view.mas_bottom);
  99. }];
  100. [[UIApplication sharedApplication].keyWindow addSubview:self.filterView];
  101. self.filterView.frame=CGRectMake(SCREEN_WIDTH, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  102. }
  103. #pragma mark---CollectionDelegate&&DataSource
  104. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. if (indexPath.section == 1) {
  107. if (self.dataArray.count == 0 && self.counts!=1) {
  108. NoDataCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"nodata" forIndexPath:indexPath];
  109. return cell;
  110. }
  111. ZBProductGoodListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"list" forIndexPath:indexPath];
  112. cell.model=self.dataArray[indexPath.row];
  113. return cell;
  114. }else{
  115. ZBProductClassCell *classC=[collectionView dequeueReusableCellWithReuseIdentifier:@"class" forIndexPath:indexPath];
  116. classC.listArray=self.listArray;
  117. classC.clickListBlock = ^(ClassModel * _Nonnull model) {//列表跳转
  118. ZBSecondListVC *listV =[[ZBSecondListVC alloc]init];
  119. listV.Id = model.Id;
  120. if (model.type.length == 0) {
  121. listV.type = @"module";
  122. }else{
  123. listV.type = model.type;
  124. }
  125. listV.click_type=@"1";
  126. listV.navigationItem.title=model.name;
  127. [self.navigationController pushViewController:listV animated:YES];
  128. } ;
  129. return classC;
  130. }
  131. }
  132. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  133. {
  134. return 2;
  135. }
  136. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  137. {
  138. if (section == 0) {
  139. return 1;
  140. }else{
  141. if (self.dataArray.count == 0 && self.counts!=1) {
  142. return 1;
  143. }
  144. return self.dataArray.count;
  145. }
  146. }
  147. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  148. {
  149. if (section == 0) {
  150. return CGSizeMake(0, 0);
  151. }else{
  152. return CGSizeMake(SCREEN_WIDTH, 40);
  153. }
  154. }
  155. -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  156. {
  157. if (indexPath.section == 1 && kind==UICollectionElementKindSectionHeader ) {
  158. ZBProductHeadView *head=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
  159. head.backgroundColor=[UIColor lineColor];
  160. if (self.lowestPrice.length!=0 || self.highPrice.length !=0) {
  161. NSString *price=[NSString stringWithFormat:@"价格%ld-%ld元",self.lowestPrice.integerValue,self.highPrice.integerValue];
  162. if (self.highPrice.integerValue == 0) {
  163. price=[NSString stringWithFormat:@"价格%ld元以上",self.lowestPrice.integerValue];
  164. }
  165. [head.priceBtn setTitle:price forState:UIControlStateNormal];
  166. [head.priceBtn setTitleColor:[UIColor baseColor] forState:UIControlStateNormal];
  167. [head.priceBtn setImage:nil forState:UIControlStateNormal];
  168. [head.priceBtn setButtonImageTitleStyle:ButtonImageTitleStyleDefault padding:0];
  169. }else{
  170. [head.priceBtn setTitle:@"价格筛选" forState:UIControlStateNormal];
  171. [head.priceBtn setTitleColor:[UIColor YHColorWithHex:0x333333] forState:UIControlStateNormal];
  172. [head.priceBtn setImage:[UIImage imageNamed:@"shaixuan_icon"] forState:UIControlStateNormal];
  173. [head.priceBtn setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:5];
  174. }
  175. if (self.start_rate.length!=0) {//佣金比例搜索
  176. [head.profitsBtn setTitle:[NSString stringWithFormat:@"佣金%@%%以上",self.start_rate] forState:UIControlStateNormal];
  177. [head.profitsBtn setTitleColor:[UIColor baseColor] forState:UIControlStateNormal];
  178. [head.profitsBtn setImage:nil forState:UIControlStateNormal];
  179. [head.profitsBtn setButtonImageTitleStyle:ButtonImageTitleStyleDefault padding:0];
  180. }else{
  181. [head.profitsBtn setTitle:@"利润筛选" forState:UIControlStateNormal];
  182. [head.profitsBtn setTitleColor:[UIColor YHColorWithHex:0x333333] forState:UIControlStateNormal];
  183. [head.profitsBtn setImage:[UIImage imageNamed:@"shaixuan_icon"] forState:UIControlStateNormal];
  184. [head.profitsBtn setButtonImageTitleStyle:ButtonImageTitleStyleRight padding:5];
  185. }
  186. head.choiceBlock = ^(UIButton * _Nonnull btn) {
  187. [UIView animateWithDuration:0.5 animations:^{
  188. self.filterView.frame=CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  189. [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleDefault;
  190. }];
  191. if (btn.tag == 1111) {//价格筛选
  192. [self.filterView settitle:@"价格区间(元)" LeftPlachoder:@"最低价" rightISHidden:NO RightPlacholder:@"最高价" Array:@[@"10元以下",@"10-50元",@"50-100元",@"100元以上"]rate:self.start_rate lowPrice:self.lowestPrice highprice:self.highPrice];
  193. }else{
  194. [self.filterView settitle:@"利润区间" LeftPlachoder:@"最低利润" rightISHidden:YES RightPlacholder:@"" Array:@[@"20%以上",@"30%以上",@"40%以上",@"50%以上"]rate:self.start_rate lowPrice:self.lowestPrice highprice:self.highPrice];
  195. }
  196. };
  197. return head;
  198. }else{
  199. UICollectionReusableView *head=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"view" forIndexPath:indexPath];
  200. return head;
  201. }
  202. }
  203. -(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
  204. {
  205. if (self.listArray.count >0) {
  206. return UIEdgeInsetsMake(10, 10, 0, 10);
  207. }else{
  208. return UIEdgeInsetsMake(0.1, 10, 0, 9.5);
  209. }
  210. }
  211. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  212. {
  213. if (indexPath.section==1) {
  214. if (self.dataArray.count == 0 && self.counts !=1) {
  215. return CGSizeMake(SCREEN_WIDTH, 200);
  216. }
  217. return CGSizeMake((SCREEN_WIDTH-30)/2, 277);
  218. }else{
  219. if (self.listArray.count==0) {
  220. return CGSizeMake(0, 0.1);
  221. }else if (self.listArray.count>4 ) {
  222. return CGSizeMake(SCREEN_WIDTH-20, 180);
  223. }else{
  224. return CGSizeMake(SCREEN_WIDTH-20, 90);
  225. }
  226. }
  227. }
  228. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  229. {
  230. if (indexPath.section == 0 || self.dataArray.count == 0) {
  231. return;
  232. }
  233. ZBGoodDetailVC *detilV =[[ZBGoodDetailVC alloc]init];
  234. detilV.model=self.dataArray[indexPath.row];
  235. detilV.detailModel=self.dataArray[indexPath.section];
  236. [self.navigationController pushViewController:detilV animated:YES];
  237. }
  238. -(UICollectionView *)collectionV
  239. {
  240. if (!_collectionV) {
  241. XLPlainFlowLayout *flowLayout =[[XLPlainFlowLayout alloc]init];
  242. flowLayout.naviHeight = 0;
  243. flowLayout.minimumLineSpacing = 10;
  244. flowLayout.minimumInteritemSpacing = 0;
  245. flowLayout.headerReferenceSize = CGSizeMake(0, 0);
  246. _collectionV =[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
  247. _collectionV.delegate=self;
  248. _collectionV.dataSource=self;
  249. _collectionV.showsVerticalScrollIndicator=NO;
  250. _collectionV.showsHorizontalScrollIndicator=NO;
  251. _collectionV.backgroundColor=[UIColor clearColor];
  252. [_collectionV registerClass:[ZBProductGoodListCell class] forCellWithReuseIdentifier:@"list"];
  253. [_collectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"view"];
  254. [_collectionV registerClass:[ZBProductHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
  255. [_collectionV registerClass:[NoDataCell class] forCellWithReuseIdentifier:@"nodata"];
  256. [_collectionV registerClass:[ZBProductClassCell class] forCellWithReuseIdentifier:@"class"];
  257. self.collectionV.mj_header =[MJRefreshNormalHeader headerWithRefreshingBlock:^{
  258. self.Page = 1;
  259. [self getProductData];
  260. }];
  261. self.collectionV.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  262. self.Page ++;
  263. [self getProductData];
  264. }];
  265. }
  266. return _collectionV;
  267. }
  268. -(void)scrollViewDidScroll:(UIScrollView *)scrollView
  269. {
  270. if (scrollView.contentOffset.y < 0 || [self.collectionV.mj_footer isRefreshing]) {
  271. return;
  272. }
  273. //scrollView已经有拖拽手势,直接拿到scrollView的拖拽手势
  274. UIPanGestureRecognizer *pan = scrollView.panGestureRecognizer;
  275. //获取到拖拽的速度 >0 向下拖动 <0 向上拖动
  276. CGFloat velocity = [pan velocityInView:scrollView].y;
  277. // CGFloat offY = scrollView.contentOffset.y - _lastContentOffset;
  278. // NSNotification *noti = [[NSNotification alloc] initWithName:@"scrollDirectorNoti" object:nil userInfo:@{@"director":@(velocity),@"offY":@(offY)}];
  279. // [[NSNotificationCenter defaultCenter] postNotification:noti];
  280. _lastContentOffset = scrollView.contentOffset.y;
  281. if(self.listArray.count>0 )
  282. {
  283. if ([scrollView isEqual: self.collectionV]) {
  284. if (self.collectionV.contentOffset.y > _oldY) {
  285. // 上滑
  286. [UIView animateWithDuration:0.1 animations:^{
  287. self.collectionV.backgroundColor=[UIColor lineColor];
  288. }];
  289. }
  290. else if(self.collectionV.contentOffset.y<=50 ){
  291. // 下滑
  292. [UIView animateWithDuration:0.1 animations:^{
  293. self.collectionV.backgroundColor=[UIColor clearColor];
  294. }];
  295. }
  296. }
  297. }
  298. }
  299. - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
  300. // 获取开始拖拽时tableview偏移量
  301. _oldY = self.collectionV.contentOffset.y;
  302. }
  303. -(NSMutableArray *)dataArray
  304. {
  305. if (!_dataArray) {
  306. _dataArray =[NSMutableArray array];
  307. }
  308. return _dataArray;
  309. }
  310. -(NSMutableArray *)listArray
  311. {
  312. if (!_listArray) {
  313. _listArray=[NSMutableArray array];
  314. }
  315. return _listArray;
  316. }
  317. -(ZBFilterView *)filterView
  318. {
  319. if (!_filterView) {
  320. _filterView=[[ZBFilterView alloc]init];
  321. __weak typeof(self) weakself = self;
  322. _filterView.changeruleBlock = ^(NSString * _Nonnull rate, NSString * _Nonnull lowStr, NSString * _Nonnull highStr, BOOL isshow) {
  323. //isshow为true时是佣金显示
  324. //需要判断价格筛选还是佣金筛选
  325. if (isshow) {
  326. weakself.start_rate=rate;
  327. }else{
  328. weakself.lowestPrice = lowStr;
  329. weakself.highPrice=highStr;
  330. }
  331. weakself.Page = 1;
  332. [weakself getProductData];
  333. [weakself.collectionV reloadData];
  334. };
  335. }
  336. return _filterView;
  337. }
  338. @end