酷店

KDPTopViewController.m 17KB

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