《省钱达人》与《猎豆优选》UI相同版。域名tbk

KBNativeShopCarViewController.m 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // KBNativeShopCarViewController.m
  3. // YouHuiProject
  4. //
  5. // Created by 小花 on 2018/11/2.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "KBNativeShopCarViewController.h"
  9. #import "JsonTool.h"
  10. #import "KBNativeShopCarHeader.h"
  11. #import "KBNativeShopCarCollectionCell.h"
  12. #import "DRChildGoodModel.h"
  13. #import "DRGoodDetailViewController.h"
  14. #import "KBNoDataMsgView.h"
  15. #import "DRNativeShopCarHeader.h"
  16. static NSString *shopCarHorizontalGoodCell = @"shopCarHorizontalGoodCell";
  17. static NSString *shopCarHorizontalHeader = @"shopCarHorizontalHeader";
  18. @interface KBNativeShopCarViewController ()
  19. <
  20. UICollectionViewDelegate,
  21. UICollectionViewDataSource,
  22. UICollectionViewDelegateFlowLayout
  23. >
  24. {
  25. NSInteger _page;
  26. ActivityIndicatorView *_indicatorView;
  27. KBNativeShopCarHeader *_header;
  28. NSString *totalMoney;
  29. }
  30. @property (nonatomic, strong) UICollectionView *collectionView;
  31. @property (nonatomic, strong) NSMutableArray *dataArray;
  32. @property (nonatomic, strong) NSArray *goodsIdArr;
  33. @property (nonatomic, strong) KBNoDataMsgView *noDataView;
  34. @end
  35. @implementation KBNativeShopCarViewController
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. _page = 1;
  39. [self addWebShopCarCompleteNoti];
  40. [self initSubViews];
  41. }
  42. - (void)initSubViews {
  43. UIView *back =[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
  44. back.backgroundColor =[UIColor changeColor];
  45. [self.view addSubview:back];
  46. KBNativeShopCarHeader *header = [[KBNativeShopCarHeader alloc] initWithFrame:CGRectMake(FITSIZE(10), 0, SCREEN_WIDTH-FITSIZE(20), 27)];
  47. _header = header;
  48. [self.view addSubview:header];
  49. UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
  50. self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, header.bottom+FITSIZE(5), SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight-header.height-FITSIZE(5)) collectionViewLayout:flowLayout];
  51. flowLayout.minimumLineSpacing = 5;
  52. flowLayout.minimumInteritemSpacing = 1;
  53. self.collectionView.showsVerticalScrollIndicator=NO;
  54. self.collectionView.showsHorizontalScrollIndicator=NO;
  55. self.collectionView.backgroundColor = [UIColor yhGrayColor];
  56. self.collectionView.dataSource = self;
  57. self.collectionView.delegate = self;
  58. [self.collectionView registerClass:[KBNativeShopCarCollectionCell class] forCellWithReuseIdentifier:shopCarHorizontalGoodCell];
  59. [self.collectionView registerClass:[DRNativeShopCarHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:shopCarHorizontalHeader];
  60. [self.view addSubview:self.collectionView];
  61. MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
  62. _page = 1;
  63. [self.collectionView.mj_footer resetNoMoreData];
  64. [self requestGoods:YES];
  65. }];
  66. refreshHeader.lastUpdatedTimeLabel.hidden=YES;
  67. [refreshHeader setTitle:@"数据正在刷新" forState:MJRefreshStateRefreshing];
  68. self.collectionView.mj_header = refreshHeader;
  69. self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  70. _page++;
  71. [self requestGoods:NO];
  72. }];
  73. ActivityIndicatorView *indicatorView = [ActivityIndicatorView showInView:self.view frame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavBarHeight-TabbarHeight)];
  74. _indicatorView = indicatorView;
  75. [self.collectionView addSubview:self.noDataView];
  76. }
  77. /**
  78. 监听web购物车是否加载完成
  79. */
  80. - (void)addWebShopCarCompleteNoti {
  81. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadShopCarGoods:) name:ShopCarCompleteKey object:nil];
  82. }
  83. - (void)loadShopCarGoods:(NSNotification *)noti {
  84. NSDictionary *dic = noti.userInfo;
  85. NSArray *goodArr = dic[@"goodsIdStr"];
  86. self.goodsIdArr = goodArr;
  87. [self requestGoods:YES];
  88. }
  89. /**
  90. 加载购物车数据
  91. */
  92. - (void)requestGoods:(BOOL)refresh {
  93. NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/cartGoods",BaseURL];
  94. NSString *goodsStr = [JsonTool arrayToJSONString:self.goodsIdArr];
  95. NSDictionary *para = @{@"goodsIdStr":goodsStr,
  96. @"page":@(_page)
  97. };
  98. [DRHttp post:url params:para success:^(id json) {
  99. if (refresh) {
  100. [self.dataArray removeAllObjects];
  101. }
  102. NSArray *list = json[@"data"];
  103. totalMoney = json[@"saveTotalMoney"];
  104. _header.totalMoney = totalMoney;
  105. for (NSDictionary *dict in list) {
  106. NSArray *arr = [NSArray yy_modelArrayWithClass:[DRChildGoodModel class] json:dict[@"goodsList"]];
  107. [self.dataArray addObject:arr];
  108. }
  109. if (list.count > 0) {
  110. [self.collectionView.mj_footer endRefreshing];
  111. }else {
  112. [self.collectionView.mj_footer endRefreshingWithNoMoreData];
  113. }
  114. if (_page == 1 && list.count == 0 ) {
  115. self.noDataView.hidden = NO;
  116. }else {
  117. self.noDataView.hidden = YES;
  118. }
  119. [self.collectionView.mj_header endRefreshing];
  120. [self.collectionView reloadData];
  121. [_indicatorView stopAnimating];
  122. } failure:^(NSError *error) {
  123. [_indicatorView stopAnimating];
  124. }];
  125. }
  126. #pragma mark ---
  127. #pragma mark ============ UICollectionView Delegate && DataSource ==========
  128. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  129. {
  130. NSArray *array = self.dataArray[section];
  131. return array.count;
  132. }
  133. - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
  134. return self.dataArray.count;
  135. }
  136. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  137. return CGSizeMake(SCREEN_WIDTH-20, 110);
  138. }
  139. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  140. {
  141. return CGSizeMake(SCREEN_WIDTH-20, 35);
  142. }
  143. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
  144. {
  145. return CGSizeMake(SCREEN_WIDTH, 10);
  146. }
  147. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  148. {
  149. KBNativeShopCarCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:shopCarHorizontalGoodCell forIndexPath:indexPath];
  150. DRChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row];
  151. cell.model = model;
  152. if (indexPath.row == [self.dataArray[indexPath.section] count]-1) {
  153. // 设置圆角
  154. UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)];
  155. CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
  156. maskLayer.frame = cell.bounds;
  157. maskLayer.path = maskPath.CGPath;
  158. cell.layer.mask = maskLayer;
  159. cell.backgroundColor = [UIColor whiteColor];
  160. }
  161. return cell;
  162. }
  163. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
  164. DRNativeShopCarHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:shopCarHorizontalHeader forIndexPath:indexPath];
  165. header.layer.cornerRadius=4;
  166. header.layer.masksToBounds=YES;
  167. DRChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row];
  168. header.model = model;
  169. return header;
  170. }
  171. #pragma mark -内边距
  172. - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
  173. return UIEdgeInsetsMake(0, 5, 0, 5);
  174. }
  175. #pragma mark -行间距
  176. - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
  177. return 0;
  178. }
  179. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  180. DRChildGoodModel *model = self.dataArray[indexPath.section][indexPath.row];
  181. DREventModel *evevtModel = [[DREventModel alloc] initWithOrigin:model.origin category_id:@"0" source:ShoppingAction];
  182. DRGoodDetailViewController *detail = [[DRGoodDetailViewController alloc] init];
  183. detail.eventModel = evevtModel;
  184. DetailRequestModel *requesModel = [[DetailRequestModel alloc] initWithChildModel:model];
  185. detail.requestModel = requesModel;
  186. [self.navigationController pushViewController:detail animated:YES];
  187. }
  188. - (NSMutableArray *)dataArray {
  189. if (!_dataArray) {
  190. _dataArray = [NSMutableArray array];
  191. }
  192. return _dataArray;
  193. }
  194. - (KBNoDataMsgView *)noDataView {
  195. if (!_noDataView) {
  196. _noDataView = [[KBNoDataMsgView alloc] initWithFrame:self.collectionView.bounds title:@"没有发现购物车里的商品"];
  197. _noDataView.hidden = YES;
  198. _noDataView.refreshClick = ^{
  199. [[NSNotificationCenter defaultCenter] postNotificationName:ChangeTaoBaoAuthor object:nil];
  200. };
  201. }
  202. return _noDataView;
  203. }
  204. @end