酷店

KDPTopViewController.m 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. //
  2. // KDPTopViewController.m
  3. // KuDianProject
  4. //
  5. // Created by 学丽 on 2019/7/4.
  6. // Copyright © 2019 KDP. All rights reserved.
  7. //
  8. #import "KDPTopViewController.h"
  9. #define liveEarningCell @"liveEarningCell"
  10. #define bannerCell @"bannerCell"
  11. #define recommendCell @"recommendCell"
  12. #define recommendHead @"recommendHead"
  13. @interface KDPTopViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,DZNEmptyDataSetDelegate,DZNEmptyDataSetSource>
  14. {
  15. NSTimer *order_timer;
  16. NSInteger addNum;
  17. }
  18. @property(nonatomic,strong)NSMutableArray *dataArray;
  19. @property(nonatomic,strong)NSArray *bannerArray;
  20. @property(nonatomic,copy)NSString *orderNumber;
  21. @property(nonatomic,copy)NSString *order_rebate_money;
  22. @property(nonatomic,strong)UIButton *headImgBtn;//左侧头像按钮
  23. @property(nonatomic,strong)UIButton *messageButton;//右侧消息按钮
  24. @property(nonatomic,strong)UICollectionView *dataCollectionView;
  25. @property(nonatomic,strong)KDPServiceView *serviceView;
  26. //服务端传参
  27. @property(nonatomic,strong)NSString *pageNum;
  28. @property(nonatomic,strong)NSString *selection_page;
  29. @property(nonatomic,strong)NSString *selection_id;
  30. @property(nonatomic,strong)NSString *selection_id_count;
  31. @property(nonatomic,strong)NSString *selection_id_page;
  32. @end
  33. @implementation KDPTopViewController
  34. -(void)initialieValues
  35. {
  36. self.pageNum=@"1";
  37. self.selection_id=@"0";
  38. self.selection_id_count=@"0";
  39. self.selection_id_page=@"0";
  40. self.selection_page=@"0";
  41. }
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. [self initialieValues];
  45. [self initUI];
  46. [self getBanner];
  47. }
  48. #pragma makr---首页收益
  49. -(void)getUserTodayIncom
  50. {
  51. [KDPNetworkRequestHTTP postURL:topIncomeUrl params:nil success:^(id _Nonnull json) {
  52. //订单数量,返回的是总数量,减去之前的数量
  53. //相差的金额,需要减去上次显示的数量
  54. NSInteger newnum=[json[@"num"] integerValue];
  55. addNum= newnum-self.orderNumber.integerValue;
  56. self.orderNumber =json[@"num"];//总数量;
  57. self.order_rebate_money = json[@"total_rebate"];
  58. [self.dataCollectionView reloadData];
  59. } failure:^(NSError * _Nonnull error) {
  60. }];
  61. }
  62. #pragma makr---首页banner
  63. -(void)getBanner
  64. {
  65. [LoadingView show];
  66. [KDPNetworkRequestHTTP getURL:bannerURL params:nil success:^(id _Nonnull json) {
  67. [LoadingView dismiss];
  68. self.bannerArray=[NSArray yy_modelArrayWithClass:[KDPBannerModel class] json:json[@"data"]];
  69. [self.dataCollectionView reloadData];
  70. } failure:^(NSError * _Nonnull error) {
  71. [LoadingView dismiss];
  72. }];
  73. }
  74. #pragma makr---获取首页数据
  75. -(void)getRecommendGood
  76. {
  77. // [LoadingView show];
  78. NSDictionary *dic=@{@"page":self.pageNum,@"selection_page":self.selection_page,@"selection_id":self.selection_id,@"selection_id_page":self.selection_id_page,@"selection_id_count":self.selection_id_count};
  79. [KDPNetworkRequestHTTP postURL:topGoodRecommURL params:dic success:^(id _Nonnull json) {
  80. [LoadingView dismiss];
  81. self.selection_id_page = json[@"selection_id_count"];
  82. self.selection_id = json[@"selection_id"];
  83. self.selection_page = json[@"selection_page"];
  84. self.selection_id_count = json[@"selection_id_count"];
  85. if (self.pageNum.integerValue == 1) {
  86. [self.dataArray removeAllObjects];
  87. }
  88. NSArray *currentA=[NSArray yy_modelArrayWithClass:[KDPGoodsModel class] json:json[@"data"]];
  89. [self.dataCollectionView.mj_header endRefreshing];
  90. [self.dataCollectionView.mj_footer endRefreshing];
  91. if (currentA.count == 0) {
  92. [self.dataCollectionView.mj_footer endRefreshingWithNoMoreData];
  93. }
  94. [self.dataArray addObjectsFromArray:currentA];
  95. [self.dataCollectionView reloadData];
  96. } failure:^(NSError * _Nonnull error) {
  97. [LoadingView dismiss];
  98. [self.dataCollectionView.mj_header endRefreshing];
  99. [self.dataCollectionView.mj_footer endRefreshing];
  100. }];
  101. }
  102. -(void)initUI
  103. {
  104. self.navBar.navTitleLabel.text=@"酷店";
  105. [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent;
  106. [self.navBar addSubview:self.headImgBtn];
  107. [self.navBar addSubview:self.messageButton];
  108. [self.headImgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  109. make.left.mas_equalTo(10);
  110. make.top.mas_equalTo(KDStatusHeight+4);
  111. make.height.width.mas_equalTo(32);
  112. }];
  113. UIImageView *imgV=[[UIImageView alloc]init];
  114. imgV.image=[UIImage imageNamed:@"LIVE"];
  115. [self.navBar addSubview:imgV];
  116. [imgV mas_makeConstraints:^(MASConstraintMaker *make) {
  117. make.left.mas_equalTo(self.headImgBtn.mas_left).offset(5);
  118. make.width.mas_equalTo(22);
  119. make.height.mas_equalTo(9);
  120. make.top.mas_equalTo(self.headImgBtn.mas_bottom).offset(-7);
  121. }];
  122. [self.messageButton mas_makeConstraints:^(MASConstraintMaker *make) {
  123. make.right.mas_equalTo(-10);
  124. make.top.mas_equalTo(KDStatusHeight+10);
  125. make.width.height.mas_equalTo(22);
  126. }];
  127. [_messageButton ba_moveBadgeWithX:22 Y:2];
  128. [self.view addSubview:self.dataCollectionView];
  129. [self.dataCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
  130. make.left.right.bottom.mas_equalTo(0);
  131. make.top.mas_equalTo(KDNavBarHeight);
  132. }];
  133. }
  134. #pragma mark---collectionDelegate&&DataSource
  135. -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  136. {
  137. if (indexPath.section == 1) {
  138. KDPBannerCell *bannerC=[collectionView dequeueReusableCellWithReuseIdentifier:bannerCell forIndexPath:indexPath];
  139. bannerC.bannerArr = self.bannerArray;
  140. bannerC.bannerBlock = ^(KDPBannerModel * _Nonnull model) {
  141. if (model.click_type.integerValue == 1) {//h5
  142. KDPWebInteractionVC *web=[[KDPWebInteractionVC alloc]init];
  143. web.url=model.click_param;
  144. [self.navigationController pushViewController:web animated:YES];
  145. }else if (model.click_type.integerValue == 2){//商品详情
  146. KDPGoodDetailVC *goodD=[[KDPGoodDetailVC alloc]init];
  147. KDPGoodsModel *models=[[KDPGoodsModel alloc]init];
  148. models.goods_id=model.click_param;
  149. goodD.model=models;
  150. [self.navigationController pushViewController:goodD animated:YES];
  151. }else if (model.click_type.integerValue == 3)
  152. {//x专题列表
  153. KDPProjectListVC *listV=[[KDPProjectListVC alloc]init];
  154. listV.navBar.navTitleLabel.text=model.title;
  155. listV.projectID=model.click_param;
  156. if (model.type.length == 0) {
  157. listV.type = @"module";
  158. }else{
  159. listV.type = model.type;
  160. }
  161. [self.navigationController pushViewController:listV animated:YES];
  162. }
  163. };
  164. return bannerC;
  165. }
  166. if (indexPath.section == 2) {
  167. KDPRecommendGoodCell *goodC=[collectionView dequeueReusableCellWithReuseIdentifier:recommendCell forIndexPath:indexPath];
  168. goodC.model=self.dataArray[indexPath.row];
  169. return goodC;
  170. }
  171. KDPLiveEarningCell *earinCell =[collectionView dequeueReusableCellWithReuseIdentifier:liveEarningCell forIndexPath:indexPath];
  172. if (addNum>0) {
  173. earinCell.addNumL.text=[NSString stringWithFormat:@"+%ld",addNum];
  174. }
  175. earinCell.orderNumgL.text=self.orderNumber;
  176. earinCell.rebet_money=self.order_rebate_money;
  177. // earinCell.liveDataBlock = ^{
  178. // self.tabBarController.selectedIndex=2;
  179. // };
  180. return earinCell;
  181. }
  182. -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
  183. {
  184. return 3;
  185. }
  186. -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  187. {
  188. if (section == 2) {
  189. return self.dataArray.count;
  190. }
  191. if (section == 1&& self.bannerArray.count==1) {
  192. return 0;
  193. }
  194. return 1;
  195. }
  196. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
  197. {
  198. if (indexPath.section == 1) {
  199. if (self.bannerArray.count==0 ) {
  200. return CGSizeMake(SCREEN_WIDTH, 0.1);
  201. }
  202. return CGSizeMake(SCREEN_WIDTH, 113);
  203. }
  204. if (indexPath.section == 2) {
  205. return CGSizeMake(SCREEN_WIDTH, 121);
  206. }
  207. return CGSizeMake(SCREEN_WIDTH, 116);
  208. }
  209. -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  210. {
  211. if (indexPath.section == 0) {
  212. KDPTodayLiveDataVC *todayV=[[KDPTodayLiveDataVC alloc]init];
  213. [self.navigationController pushViewController:todayV animated:YES];
  214. }
  215. if (indexPath.section == 2) {
  216. KDPGoodDetailVC *goodDetail=[[KDPGoodDetailVC alloc]init];
  217. goodDetail.model=self.dataArray[indexPath.row];
  218. [self.navigationController pushViewController:goodDetail animated:YES];
  219. }
  220. }
  221. -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
  222. {
  223. if (section == 2) {
  224. return CGSizeMake(SCREEN_WIDTH, 60);
  225. }
  226. return CGSizeMake(0, 0 );
  227. }
  228. -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
  229. {
  230. if (indexPath.section == 2) {
  231. KDPRecommendHeadView *headV=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:recommendHead forIndexPath:indexPath];
  232. return headV;
  233. }
  234. return nil;
  235. }
  236. -(NSMutableArray *)dataArray
  237. {
  238. if (!_dataArray) {
  239. _dataArray=[NSMutableArray array];
  240. }
  241. return _dataArray;
  242. }
  243. #pragma mark---页面布局
  244. -(UIButton *)headImgBtn
  245. {
  246. if (!_headImgBtn) {
  247. _headImgBtn=[[UIButton alloc]init];
  248. _headImgBtn.backgroundColor=[UIColor colorWithHexString:ThemeColor];
  249. _headImgBtn.layer.cornerRadius=16;
  250. _headImgBtn.layer.masksToBounds=YES;
  251. _headImgBtn.layer.borderColor=[UIColor colorWithHexString:@"#FFB444"].CGColor;
  252. UIImageView *imgv=[[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 28, 28)];
  253. imgv.layer.cornerRadius=14;
  254. imgv.layer.masksToBounds=YES;
  255. imgv.tag=1000;
  256. [_headImgBtn addSubview:imgv];
  257. _headImgBtn.layer.borderWidth=1;
  258. [_headImgBtn addTarget:self action:@selector(drawerClickButton) forControlEvents:UIControlEventTouchUpInside];
  259. _headImgBtn.adjustsImageWhenHighlighted=NO;
  260. }
  261. return _headImgBtn;
  262. }
  263. -(UIButton *)messageButton
  264. {
  265. if (!_messageButton) {
  266. _messageButton=[[UIButton alloc]init];
  267. [_messageButton setImage:[UIImage imageNamed:@"message_icon"] forState:UIControlStateNormal];
  268. _messageButton.adjustsImageWhenHighlighted=NO;
  269. [_messageButton addTarget:self action:@selector(messageclickButton) forControlEvents:UIControlEventTouchUpInside];
  270. [_messageButton ba_setBadgeLabelAttributes:^(BABadgeLabel *badgeLabel) {
  271. UILabel *label = (UILabel *)badgeLabel;
  272. label.width=10;
  273. label.textColor = [UIColor baseColor];
  274. label.backgroundColor = [UIColor redColor];
  275. label.height = label.width;
  276. label.font = [UIFont systemFontOfSize:8];
  277. }];
  278. [_messageButton ba_hiddenBadge];
  279. }
  280. return _messageButton;
  281. }
  282. -(UICollectionView *)dataCollectionView
  283. {
  284. if (!_dataCollectionView) {
  285. UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
  286. layout.minimumLineSpacing=0;
  287. layout.minimumInteritemSpacing=0;
  288. _dataCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight-KDTabBarHeight) collectionViewLayout:layout];
  289. _dataCollectionView.backgroundColor=[UIColor colorWithHexString:LineColor];
  290. [_dataCollectionView registerClass:[KDPLiveEarningCell class] forCellWithReuseIdentifier:liveEarningCell];
  291. [_dataCollectionView registerClass:[KDPBannerCell class] forCellWithReuseIdentifier:bannerCell];
  292. [_dataCollectionView registerClass:[KDPRecommendGoodCell class] forCellWithReuseIdentifier:recommendCell];
  293. [_dataCollectionView registerClass:[KDPRecommendHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:recommendHead];
  294. [_dataCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
  295. _dataCollectionView.showsVerticalScrollIndicator=NO;
  296. _dataCollectionView.delegate=self;
  297. _dataCollectionView.dataSource=self;
  298. _dataCollectionView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
  299. [self getBanner];
  300. [self initialieValues];
  301. [self getRecommendGood];
  302. }];
  303. _dataCollectionView.emptyDataSetDelegate = self;
  304. _dataCollectionView.emptyDataSetSource = self;
  305. if (@available(iOS 11.0, *)) {
  306. _dataCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  307. } else {
  308. self.automaticallyAdjustsScrollViewInsets = NO;
  309. } _dataCollectionView.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
  310. self.pageNum=[NSString stringWithFormat:@"%ld",self.pageNum.integerValue+1];
  311. [self getRecommendGood];
  312. }];
  313. }
  314. return _dataCollectionView;
  315. }
  316. #pragma mark---侧滑
  317. -(void)drawerClickButton
  318. {
  319. KDPDrawerVC *vcFrame =[[KDPDrawerVC alloc]init];
  320. CWLateralSlideConfiguration *con =[CWLateralSlideConfiguration defaultConfiguration];
  321. con.distance=SCREEN_WIDTH/3*2;
  322. [self cw_showDrawerViewController:vcFrame animationType:0 configuration:con];
  323. }
  324. #pragma mark---消息中心
  325. -(void)messageclickButton
  326. {
  327. KDPNoticeViewController *noticeV=[[KDPNoticeViewController alloc]init];
  328. [self.navigationController pushViewController:noticeV animated:YES];
  329. }
  330. -(void)viewWillAppear:(BOOL)animated
  331. {
  332. [super viewWillAppear:animated];
  333. self.tabBarController.tabBar.hidden=NO;
  334. [self updateData];
  335. }
  336. -(void)viewWillDisappear:(BOOL)animated
  337. {
  338. [super viewWillDisappear:animated];
  339. [order_timer invalidate];
  340. }
  341. #pragma mark---更新数据
  342. -(void)updateData
  343. {
  344. if ([KDPAccountTool isLogin]) {
  345. [self getUserTodayIncom];
  346. [self isOpenService];
  347. [self initialieValues];
  348. [self getRecommendGood];
  349. [self messageUnreadNumData];
  350. order_timer = [NSTimer timerWithTimeInterval:30 target:self selector:@selector(order_timerAction) userInfo:nil repeats:YES];
  351. [[NSRunLoop mainRunLoop] addTimer:order_timer forMode:NSDefaultRunLoopMode];
  352. [self setUserHeadImg];
  353. }
  354. }
  355. -(void)order_timerAction
  356. {
  357. if ([KDPAccountTool isLogin]) {
  358. [self getUserTodayIncom];
  359. }
  360. }
  361. #pragma mark----设置头像
  362. -(void)setUserHeadImg
  363. {
  364. KDPAccountModel *model=[KDPAccountTool account];
  365. UIImageView *imgv=[self.headImgBtn viewWithTag:1000];
  366. [imgv sd_setImageWithURL:[NSURL URLWithString:model.img]placeholderImage:[UIImage imageNamed:placholderImg]];
  367. }
  368. #pragma mark---打开服务提示页面
  369. -(KDPServiceView *)serviceView
  370. {
  371. if (!_serviceView) {
  372. _serviceView=[[KDPServiceView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  373. __weak KDPTopViewController *weakself=self;
  374. _serviceView.serviceBlock = ^{
  375. KDPWebInteractionVC *pidV=[[KDPWebInteractionVC alloc]init];
  376. pidV.url=custSerURL;
  377. [weakself cw_pushViewController:pidV];
  378. [weakself.serviceView removeFromSuperview];
  379. };
  380. }
  381. return _serviceView;
  382. }
  383. #pragma mark---获取未读消息个数
  384. -(void)messageUnreadNumData
  385. {
  386. if ([KDPAccountTool isLogin]) {
  387. [KDPNetworkRequestHTTP postURL:message_numURL params:nil success:^(id _Nonnull json) {
  388. if ([json[@"data"][@"message_new"] integerValue] == 1) {//是否有新消息 0:无 1:有
  389. [self.messageButton ba_showBadge];
  390. }else{
  391. [self.messageButton ba_hiddenBadge];
  392. }
  393. } failure:^(NSError * _Nonnull error) {
  394. }];
  395. }
  396. }
  397. #pragma mark---判断是否打开客服页面
  398. -(void)isOpenService
  399. {
  400. //存储第一次打开APP,是否显示客服页面,
  401. NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
  402. NSString *oneStep =[defaults objectForKey:@"step"];
  403. if (oneStep.integerValue == 0) {
  404. [UIView animateWithDuration:0.3 animations:^{
  405. [[UIApplication sharedApplication].keyWindow addSubview:self.serviceView];
  406. }];
  407. [defaults setValue:@"1" forKey:@"step"];
  408. [defaults synchronize];
  409. }
  410. }
  411. - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
  412. return [UIImage imageNamed:@"no_order"];
  413. }
  414. - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{
  415. return YES;
  416. }
  417. - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
  418. return [[NSAttributedString alloc] initWithString:@"还没有记录" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333],NSFontAttributeName:FONT_SYS(12)}];
  419. }
  420. - (CGFloat )spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{
  421. return 30;
  422. }
  423. @end