酷店

KDPSupplyContentViewController.m 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. self.page = 1;
  34. [self requestGoodlistData];
  35. }
  36. - (void)setUpUI{
  37. self.navBar.hidden = YES;
  38. self.view.backgroundColor = [UIColor clearColor];
  39. [self.view addSubview:self.collectionView];
  40. }
  41. #pragma mark 网络请求
  42. - (void)requestGoodlistData{
  43. NSString *goodlistUrl = [NSString stringWithFormat:@"%@api/goods/listGoods",KDURL];
  44. [self.mutabParams setValue:@(self.page) forKey:@"page"];
  45. [KDPNetworkRequestHTTP postURL:goodlistUrl params:self.mutabParams success:^(id _Nonnull json) {
  46. [LoadingView dismiss];
  47. NSArray *array =[NSArray yy_modelArrayWithClass:[KDPGoodsModel class] json:json[@"data"]];
  48. [self.collectionView.mj_footer endRefreshing];
  49. [self.collectionView.mj_header endRefreshing];
  50. if (self.page == 1) {
  51. [self.dataSource removeAllObjects];
  52. }
  53. if (array.count > 0) {
  54. [self.dataSource addObjectsFromArray:array];
  55. }
  56. if (array.count == 0) {
  57. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  58. }
  59. [self.collectionView reloadData];
  60. } failure:^(NSError * _Nonnull error) {
  61. [self.collectionView.mj_footer endRefreshing];
  62. [self.collectionView.mj_header endRefreshing];
  63. [LoadingView dismiss];
  64. }];
  65. }
  66. - (void)requestTopListWithGoodId:(NSString *)Id{
  67. [LoadingView showInView:self.view];
  68. NSString *childListUrl = [NSString stringWithFormat:@"%@api/goods/childList",KDURL];
  69. [KDPNetworkRequestHTTP postURL:childListUrl params:@{@"pid":Id} success:^(id _Nonnull json) {
  70. [self.topGoodList removeAllObjects];
  71. NSArray *array =[NSArray yy_modelArrayWithClass:[KDPClassModel class] json:json[@"list"]];
  72. [self.topGoodList addObjectsFromArray:array];
  73. [LoadingView dismiss];
  74. [self.collectionView reloadData];
  75. } failure:^(NSError * _Nonnull error) {
  76. [LoadingView dismiss];
  77. }];
  78. }
  79. #pragma mark - lazy load
  80. - (UICollectionView *)collectionView{
  81. if (!_collectionView) {
  82. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  83. layout.minimumLineSpacing = 0;
  84. layout.sectionHeadersPinToVisibleBounds = YES;
  85. _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-KDTabBarHeight-KDStatusHeight-88) collectionViewLayout:layout];
  86. _collectionView.backgroundColor = [UIColor clearColor];
  87. _collectionView.showsHorizontalScrollIndicator = NO;
  88. _collectionView.showsVerticalScrollIndicator = NO;
  89. _collectionView.delegate = self;
  90. _collectionView.dataSource = self;
  91. _collectionView.emptyDataSetSource = self;
  92. _collectionView.emptyDataSetDelegate = self;
  93. [_collectionView registerClass:[KDPSupplyCommentViewCell class] forCellWithReuseIdentifier:NSStringFromClass([KDPSupplyCommentViewCell class])];
  94. [_collectionView registerClass:[KDPSupplyGoodCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([KDPSupplyGoodCollectionViewCell class])];
  95. [_collectionView registerClass:[KDPSupplyContentReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([KDPSupplyContentReusableView class])];
  96. _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  97. self.page = 1;
  98. [self requestGoodlistData];
  99. }];
  100. _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  101. self.page ++;
  102. [self requestGoodlistData];
  103. }];
  104. }
  105. return _collectionView;
  106. }
  107. - (NSMutableDictionary *)mutabParams{
  108. if (!_mutabParams) {
  109. _mutabParams = [NSMutableDictionary dictionaryWithDictionary:@{@"page":@(self.page),@"id":self.classModel.Id,@"type":self.classModel.type,@"start_price":@"",@"end_price":@"",@"start_tk_rate":@""}];
  110. }
  111. return _mutabParams;
  112. }
  113. - (NSMutableArray *)dataSource{
  114. if (!_dataSource) {
  115. _dataSource = [NSMutableArray array];
  116. }
  117. return _dataSource;
  118. }
  119. - (NSMutableArray *)topGoodList{
  120. if (!_topGoodList) {
  121. _topGoodList = [NSMutableArray array];
  122. }
  123. return _topGoodList;
  124. }
  125. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
  126. if (indexPath.section == 0) {
  127. KDPSupplyCommentViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KDPSupplyCommentViewCell class]) forIndexPath:indexPath];
  128. cell.delegate = self;
  129. cell.dataArray = self.topGoodList;
  130. return cell;
  131. }
  132. if (indexPath.section == 1) {
  133. KDPSupplyGoodCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KDPSupplyGoodCollectionViewCell class]) forIndexPath:indexPath];
  134. [cell configCellWithModel:self.dataSource[indexPath.row] indexPath:indexPath];
  135. return cell;
  136. }
  137. return nil;
  138. }
  139. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
  140. return 2;
  141. }
  142. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  143. KDPGoodDetailVC *detailVC = [[KDPGoodDetailVC alloc] init];
  144. detailVC.model = self.dataSource[indexPath.row];
  145. [self.navigationController pushViewController:detailVC animated:YES];
  146. }
  147. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
  148. if (section == 0) {
  149. if (!self.topGoodList.count) {
  150. return 0;
  151. }
  152. return 1;
  153. }
  154. if (section == 1) {
  155. return self.dataSource.count;
  156. }
  157. return 0;
  158. }
  159. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  160. if (indexPath.section == 0) {
  161. if (self.topGoodList.count > 4) {
  162. return CGSizeMake(SCREEN_WIDTH-20, 200);
  163. } else if(self.topGoodList.count > 0 && self.topGoodList.count < 4) {
  164. return CGSizeMake(SCREEN_WIDTH-20, 110);
  165. } else{
  166. return CGSizeMake(SCREEN_WIDTH, 0.1f);
  167. }
  168. }
  169. if (indexPath.section == 1) {
  170. return CGSizeMake((SCREEN_WIDTH-25)/2, 284);
  171. }
  172. return CGSizeMake(SCREEN_WIDTH, 0.1f);
  173. }
  174. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
  175. if (section == 1) {
  176. return CGSizeMake(SCREEN_WIDTH, 33);
  177. }
  178. return CGSizeZero;
  179. }
  180. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
  181. if (section == 0) {
  182. if (!self.topGoodList.count) {
  183. return UIEdgeInsetsMake(0, 0, 5, 0);
  184. }
  185. return UIEdgeInsetsMake(10, 10, 10, 10);
  186. }
  187. if (section == 1) {
  188. return UIEdgeInsetsMake(7, 10, 0, 10);
  189. }
  190. return UIEdgeInsetsZero;
  191. }
  192. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  193. if (section == 1) {
  194. return 5;
  195. }
  196. return 0;
  197. }
  198. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  199. if (section == 1) {
  200. return 5;
  201. }
  202. return 0;
  203. }
  204. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  205. KDPSupplyContentReusableView *reusAbleView;
  206. if (kind == UICollectionElementKindSectionHeader) {
  207. if (indexPath.section == 1) {
  208. reusAbleView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:NSStringFromClass([KDPSupplyContentReusableView class]) forIndexPath:indexPath];
  209. WeakSelf(weakSelf);
  210. reusAbleView.hidden = NO;
  211. if (self.dataSource.count ==0 && self.shouldHiddenScreen == YES) {
  212. reusAbleView.hidden = YES;
  213. }
  214. __weak KDPSupplyContentReusableView *weakReusAbleView = reusAbleView;
  215. reusAbleView.changeBlock = ^(BOOL isPrice) {
  216. weakSelf.shouldHiddenScreen = NO;
  217. KDPScreenViewController *screenVC = [[KDPScreenViewController alloc] initWithPrice:isPrice];
  218. screenVC.params = weakSelf.mutabParams;
  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. conf.distance=SCREEN_WIDTH/3*2;
  260. [weakSelf cw_showDrawerViewController:screenVC animationType:0 configuration:conf];
  261. };
  262. return reusAbleView;
  263. }
  264. }
  265. return nil;
  266. }
  267. #pragma DZNEmptyDataSetSource and delegate
  268. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
  269. return [UIImage imageNamed:@"no_order"];
  270. }
  271. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
  272. return YES;
  273. }
  274. - (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{
  275. return 30;
  276. }
  277. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  278. return [[NSAttributedString alloc] initWithString:@"还没有记录" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333],NSFontAttributeName:FONT_SYS(12)}];
  279. }
  280. - (void)commentCollectionView:(UICollectionView *)collectionView didSelectIndex:(NSInteger )index{
  281. KDPSupplyListViewController *listVC = [[KDPSupplyListViewController alloc] init];
  282. listVC.mode = self.topGoodList[index];
  283. [self.navigationController pushViewController:listVC animated:YES];
  284. }
  285. @end