酷店

KDPSupplyContentViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. //
  2. // KDPSupplyContentViewController.m
  3. // KuDianProject
  4. //
  5. // Created by admin on 2019/7/4.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "KDPSupplyContentViewController.h"
  9. #import "KDPSupplyCommentViewCell.h"
  10. #import "KDPSupplyGoodCollectionViewCell.h"
  11. #import "KDPSupplyContentReusableView.h"
  12. #import "KDPScreenViewController.h"
  13. #import <UIViewController+CWLateralSlide.h>
  14. #import "KDPSupplyListViewController.h"
  15. #import "KDPNoticeViewController.h"
  16. @interface KDPSupplyContentViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,KDPSupplyCommentViewCellDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
  17. @property (nonatomic, strong) UICollectionView *collectionView;
  18. @property (nonatomic, assign) NSInteger page;
  19. @property (nonatomic, strong) NSMutableDictionary *mutabParams;
  20. @property (nonatomic, strong) NSMutableArray *dataSource;
  21. @property (nonatomic, strong) NSMutableArray *topGoodList;
  22. @property (nonatomic, assign) BOOL shouldHiddenScreen;
  23. @end
  24. @implementation KDPSupplyContentViewController
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. // Do any additional setup after loading the view.
  28. [self setUpUI];
  29. self.shouldHiddenScreen = YES;
  30. if ([self.classModel.type isEqualToString:@"cate"]) {
  31. [self requestTopListWithGoodId:self.classModel.Id];
  32. }
  33. }
  34. - (void)setUpUI{
  35. self.navBar.hidden = YES;
  36. self.view.backgroundColor = [UIColor clearColor];
  37. [self.view addSubview:self.collectionView];
  38. }
  39. #pragma mark 网络请求
  40. - (void)requestGoodlistData{
  41. NSString *goodlistUrl = [NSString stringWithFormat:@"%@api/goods/listGoods",KDURL];
  42. [self.mutabParams setValue:@(self.page) forKey:@"page"];
  43. [KDPNetworkRequestHTTP postURL:goodlistUrl params:self.mutabParams success:^(id _Nonnull json) {
  44. [LoadingView dismiss];
  45. NSArray *array =[NSArray yy_modelArrayWithClass:[KDPGoodsModel class] json:json[@"data"]];
  46. [self.collectionView.mj_footer endRefreshing];
  47. [self.collectionView.mj_header endRefreshing];
  48. if (self.page == 1) {
  49. [self.dataSource removeAllObjects];
  50. }
  51. if (array.count > 0) {
  52. [self.dataSource addObjectsFromArray:array];
  53. }
  54. if (array.count == 0) {
  55. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  56. }
  57. [self.collectionView reloadData];
  58. } failure:^(NSError * _Nonnull error) {
  59. [self.collectionView.mj_footer endRefreshing];
  60. [self.collectionView.mj_header endRefreshing];
  61. [LoadingView dismiss];
  62. }];
  63. }
  64. - (void)requestTopListWithGoodId:(NSString *)Id{
  65. [LoadingView showInView:self.view];
  66. NSString *childListUrl = [NSString stringWithFormat:@"%@api/goods/childList",KDURL];
  67. [KDPNetworkRequestHTTP postURL:childListUrl params:@{@"pid":Id} success:^(id _Nonnull json) {
  68. [self.topGoodList removeAllObjects];
  69. NSArray *array =[NSArray yy_modelArrayWithClass:[KDPClassModel class] json:json[@"list"]];
  70. [self.topGoodList addObjectsFromArray:array];
  71. [LoadingView dismiss];
  72. [self.collectionView reloadData];
  73. } failure:^(NSError * _Nonnull error) {
  74. [LoadingView dismiss];
  75. }];
  76. }
  77. #pragma mark - lazy load
  78. - (UICollectionView *)collectionView{
  79. if (!_collectionView) {
  80. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  81. layout.minimumLineSpacing = 0;
  82. layout.sectionHeadersPinToVisibleBounds = YES;
  83. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KDTabBarHeight-KDStatusHeight-88) collectionViewLayout:layout];
  84. _collectionView.backgroundColor = [UIColor clearColor];
  85. _collectionView.showsHorizontalScrollIndicator = NO;
  86. _collectionView.showsVerticalScrollIndicator = NO;
  87. _collectionView.delegate = self;
  88. _collectionView.dataSource = self;
  89. _collectionView.emptyDataSetSource = self;
  90. _collectionView.emptyDataSetDelegate = self;
  91. [_collectionView registerClass:[KDPSupplyCommentViewCell class] forCellWithReuseIdentifier:NSStringFromClass([KDPSupplyCommentViewCell class])];
  92. [_collectionView registerClass:[KDPSupplyGoodCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([KDPSupplyGoodCollectionViewCell class])];
  93. [_collectionView registerClass:[KDPSupplyContentReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([KDPSupplyContentReusableView class])];
  94. _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  95. self.page = 1;
  96. [self requestGoodlistData];
  97. }];
  98. _collectionView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
  99. self.page ++;
  100. [self requestGoodlistData];
  101. }];
  102. [_collectionView.mj_header beginRefreshing];
  103. }
  104. return _collectionView;
  105. }
  106. - (NSMutableDictionary *)mutabParams{
  107. if (!_mutabParams) {
  108. _mutabParams = [NSMutableDictionary dictionaryWithDictionary:@{@"page":@(self.page),@"id":self.classModel.Id,@"type":self.classModel.type,@"start_price":@"",@"end_price":@"",@"start_tk_rate":@""}];
  109. }
  110. return _mutabParams;
  111. }
  112. - (NSMutableArray *)dataSource{
  113. if (!_dataSource) {
  114. _dataSource = [NSMutableArray array];
  115. }
  116. return _dataSource;
  117. }
  118. - (NSMutableArray *)topGoodList{
  119. if (!_topGoodList) {
  120. _topGoodList = [NSMutableArray array];
  121. }
  122. return _topGoodList;
  123. }
  124. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  125. if (indexPath.section == 0) {
  126. KDPSupplyCommentViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KDPSupplyCommentViewCell class]) forIndexPath:indexPath];
  127. cell.delegate = self;
  128. cell.dataArray = self.topGoodList;
  129. return cell;
  130. }
  131. if (indexPath.section == 1) {
  132. KDPSupplyGoodCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KDPSupplyGoodCollectionViewCell class]) forIndexPath:indexPath];
  133. [cell configCellWithModel:self.dataSource[indexPath.row] indexPath:indexPath];
  134. return cell;
  135. }
  136. return nil;
  137. }
  138. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  139. return 2;
  140. }
  141. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  142. KDPGoodDetailVC *detailVC = [[KDPGoodDetailVC alloc] init];
  143. detailVC.model = self.dataSource[indexPath.row];
  144. [self.navigationController pushViewController:detailVC animated:YES];
  145. }
  146. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  147. if (section == 0) {
  148. if (!self.topGoodList.count) {
  149. return 0;
  150. }
  151. return 1;
  152. }
  153. if (section == 1) {
  154. return self.dataSource.count;
  155. }
  156. return 0;
  157. }
  158. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  159. if (indexPath.section == 0) {
  160. if (self.topGoodList.count > 4) {
  161. return CGSizeMake(SCREEN_WIDTH-20, 200);
  162. } else if(self.topGoodList.count > 0 && self.topGoodList.count < 4) {
  163. return CGSizeMake(SCREEN_WIDTH-20, 110);
  164. } else{
  165. return CGSizeMake(SCREEN_WIDTH, 0.1f);
  166. }
  167. }
  168. if (indexPath.section == 1) {
  169. return CGSizeMake((SCREEN_WIDTH-25)/2, 284);
  170. }
  171. return CGSizeMake(SCREEN_WIDTH, 0.1f);
  172. }
  173. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  174. if (section == 1) {
  175. return CGSizeMake(SCREEN_WIDTH, 33);
  176. }
  177. return CGSizeZero;
  178. }
  179. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  180. if (section == 0) {
  181. if (!self.topGoodList.count) {
  182. return UIEdgeInsetsMake(0, 0, 5, 0);
  183. }
  184. return UIEdgeInsetsMake(10, 10, 10, 10);
  185. }
  186. if (section == 1) {
  187. return UIEdgeInsetsMake(7, 10, 0, 10);
  188. }
  189. return UIEdgeInsetsZero;
  190. }
  191. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  192. if (section == 1) {
  193. return 5;
  194. }
  195. return 0;
  196. }
  197. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  198. if (section == 1) {
  199. return 5;
  200. }
  201. return 0;
  202. }
  203. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  204. KDPSupplyContentReusableView *reusAbleView;
  205. if (kind == UICollectionElementKindSectionHeader) {
  206. if (indexPath.section == 1) {
  207. reusAbleView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([KDPSupplyContentReusableView class]) forIndexPath:indexPath];
  208. WeakSelf(weakSelf);
  209. reusAbleView.hidden = NO;
  210. if (self.dataSource.count ==0 && self.shouldHiddenScreen == YES) {
  211. reusAbleView.hidden = YES;
  212. }
  213. __weak KDPSupplyContentReusableView *weakReusAbleView = reusAbleView;
  214. reusAbleView.changeBlock = ^(BOOL isPrice) {
  215. weakSelf.shouldHiddenScreen = NO;
  216. KDPScreenViewController *screenVC = [[KDPScreenViewController alloc] initWithPrice:isPrice];
  217. screenVC.params = weakSelf.mutabParams;
  218. screenVC.view.frame = CGRectMake(91, 0, SCREEN_WIDTH-91, SCREEN_HEIGHT);
  219. screenVC.resetBlock = ^(BOOL isPrice) {
  220. if (isPrice == YES) {
  221. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor colorWithHex:0x333333] text:@"价格筛选"];
  222. [self.mutabParams setValue:@"" forKey:@"start_price"];
  223. [self.mutabParams setValue:@"" forKey:@"end_price"];
  224. } else{
  225. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor colorWithHex:0x333333] text:@"利润筛选"];
  226. [self.mutabParams setValue:@"" forKey:@"start_tk_rate"];
  227. }
  228. [weakSelf.collectionView.mj_header beginRefreshing];
  229. };
  230. screenVC.profitBlock = ^(NSString *baseScale) {
  231. if (!baseScale.length) {
  232. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor colorWithHex:0x333333] text:[NSString stringWithFormat:@"利润筛选"]];
  233. [self.mutabParams setValue:@"" forKey:@"start_tk_rate"];
  234. } else{
  235. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStyleProfit Color:[UIColor baseColor] text:[NSString stringWithFormat:@"佣金%@%%以上",baseScale]];
  236. [self.mutabParams setValue:baseScale forKey:@"start_tk_rate"];
  237. }
  238. [weakSelf.collectionView.mj_header beginRefreshing];
  239. };
  240. screenVC.priceBlock = ^(NSString *minPrice, NSString *maxPrice) {
  241. if (minPrice.length != 0 || maxPrice.length != 0) {
  242. if (minPrice.length == 0) {
  243. minPrice = @"0";
  244. }
  245. NSString *price=[NSString stringWithFormat:@"价格%@-%@元",minPrice,maxPrice];
  246. if (maxPrice.integerValue == 0 || [maxPrice intValue] <= [minPrice intValue]) {
  247. maxPrice = @"";
  248. price=[NSString stringWithFormat:@"价格%@元以上",minPrice];
  249. }
  250. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor baseColor] text:price];
  251. } else{
  252. [weakReusAbleView changeScreeWithStyle:KDPSupplyContentButtonStylePrice Color:[UIColor colorWithHex:0x333333] text:@"价格筛选"];
  253. }
  254. [weakSelf.mutabParams setValue:minPrice forKey:@"start_price"];
  255. [weakSelf.mutabParams setValue:maxPrice forKey:@"end_price"];
  256. [weakSelf.collectionView.mj_header beginRefreshing];
  257. };
  258. CWLateralSlideConfiguration *conf = [CWLateralSlideConfiguration configurationWithDistance:0 maskAlpha:0.7 scaleY:1 direction:CWDrawerTransitionFromRight backImage:nil];
  259. [weakSelf cw_showDrawerViewController:screenVC animationType:0 configuration:conf];
  260. };
  261. return reusAbleView;
  262. }
  263. }
  264. return nil;
  265. }
  266. #pragma DZNEmptyDataSetSource and delegate
  267. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
  268. return [UIImage imageNamed:@"no_order"];
  269. }
  270. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
  271. return YES;
  272. }
  273. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{
  274. return 50;
  275. }
  276. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  277. return [[NSAttributedString alloc] initWithString:@"还没有记录" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333],NSFontAttributeName:FONT_SYS(12)}];
  278. }
  279. - (void)commentCollectionView:(UICollectionView *)collectionView didSelectIndex:(NSInteger )index{
  280. KDPSupplyListViewController *listVC = [[KDPSupplyListViewController alloc] init];
  281. listVC.mode = self.topGoodList[index];
  282. [self.navigationController pushViewController:listVC animated:YES];
  283. }
  284. @end