// // KDPTopViewController.m // KuDianProject // // Created by 学丽 on 2019/7/4. // Copyright © 2019 KDP. All rights reserved. // #import "KDPTopViewController.h" #define liveEarningCell @"liveEarningCell" #define bannerCell @"bannerCell" #define recommendCell @"recommendCell" #define recommendHead @"recommendHead" @interface KDPTopViewController () { NSTimer *order_timer; NSInteger addNum; } @property(nonatomic,strong)NSMutableArray *dataArray; @property(nonatomic,strong)NSArray *bannerArray; @property(nonatomic,copy)NSString *orderNumber; @property(nonatomic,copy)NSString *order_rebate_money; @property(nonatomic,strong)UIButton *headImgBtn;//左侧头像按钮 @property(nonatomic,strong)UIButton *messageButton;//右侧消息按钮 @property(nonatomic,strong)UICollectionView *dataCollectionView; @property(nonatomic,strong)KDPServiceView *serviceView; //服务端传参 @property(nonatomic,strong)NSString *pageNum; @property(nonatomic,strong)NSString *selection_page; @property(nonatomic,strong)NSString *selection_id; @property(nonatomic,strong)NSString *selection_id_count; @property(nonatomic,strong)NSString *selection_id_page; @end @implementation KDPTopViewController -(void)initialieValues { self.pageNum=@"1"; self.selection_id=@"0"; self.selection_id_count=@"0"; self.selection_id_page=@"0"; self.selection_page=@"0"; } - (void)viewDidLoad { [super viewDidLoad]; [self initialieValues]; [self initUI]; } #pragma makr---首页收益 -(void)getUserTodayIncom { [KDPNetworkRequestHTTP postURL:topIncomeUrl params:nil success:^(id _Nonnull json) { //订单数量,返回的是总数量,减去之前的数量 //相差的金额,需要减去上次显示的数量 NSInteger newnum=[json[@"num"] integerValue]; addNum= newnum-self.orderNumber.integerValue; self.orderNumber =json[@"num"];//总数量; self.order_rebate_money = json[@"total_rebate"]; [self.dataCollectionView reloadData]; } failure:^(NSError * _Nonnull error) { }]; } #pragma makr---首页banner -(void)getBanner { // [LoadingView show]; [KDPNetworkRequestHTTP getURL:bannerURL params:nil success:^(id _Nonnull json) { [LoadingView dismiss]; self.bannerArray=[NSArray yy_modelArrayWithClass:[KDPBannerModel class] json:json[@"data"]]; [self.dataCollectionView reloadData]; } failure:^(NSError * _Nonnull error) { [LoadingView dismiss]; }]; } #pragma makr---获取首页数据 -(void)getRecommendGood { // [LoadingView show]; 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}; [KDPNetworkRequestHTTP postURL:topGoodRecommURL params:dic success:^(id _Nonnull json) { [LoadingView dismiss]; self.selection_id_page = json[@"selection_id_count"]; self.selection_id = json[@"selection_id"]; self.selection_page = json[@"selection_page"]; self.selection_id_count = json[@"selection_id_count"]; if (self.pageNum.integerValue == 1) { [self.dataArray removeAllObjects]; } NSArray *currentA=[NSArray yy_modelArrayWithClass:[KDPGoodsModel class] json:json[@"data"]]; [self.dataCollectionView.mj_header endRefreshing]; [self.dataCollectionView.mj_footer endRefreshing]; if (currentA.count == 0) { [self.dataCollectionView.mj_footer endRefreshingWithNoMoreData]; } [self.dataArray addObjectsFromArray:currentA]; [self.dataCollectionView reloadData]; } failure:^(NSError * _Nonnull error) { [LoadingView dismiss]; [self.dataCollectionView.mj_header endRefreshing]; [self.dataCollectionView.mj_footer endRefreshing]; }]; } -(void)initUI { self.navBar.navTitleLabel.text=@"酷店"; [UIApplication sharedApplication].statusBarStyle=UIStatusBarStyleLightContent; [self.navBar addSubview:self.headImgBtn]; [self.navBar addSubview:self.messageButton]; [self.headImgBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(10); make.top.mas_equalTo(KDStatusHeight+4); make.height.width.mas_equalTo(32); }]; // UIImageView *imgV=[[UIImageView alloc]init]; // imgV.image=[UIImage imageNamed:@"LIVE"]; // [self.navBar addSubview:imgV]; // [imgV mas_makeConstraints:^(MASConstraintMaker *make) { // make.left.mas_equalTo(self.headImgBtn.mas_left).offset(5); // make.width.mas_equalTo(22); // make.height.mas_equalTo(9); // make.top.mas_equalTo(self.headImgBtn.mas_bottom).offset(-7); // }]; [self.messageButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(-10); make.top.mas_equalTo(KDStatusHeight+10); make.width.height.mas_equalTo(22); }]; [_messageButton ba_moveBadgeWithX:22 Y:2]; [self.view addSubview:self.dataCollectionView]; [self.dataCollectionView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.bottom.mas_equalTo(0); make.top.mas_equalTo(KDNavBarHeight); }]; } #pragma mark---collectionDelegate&&DataSource -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 1) { KDPBannerCell *bannerC=[collectionView dequeueReusableCellWithReuseIdentifier:bannerCell forIndexPath:indexPath]; bannerC.bannerArr = self.bannerArray; bannerC.bannerBlock = ^(KDPBannerModel * _Nonnull model) { if (model.click_type.integerValue == 1) {//h5 KDPWebInteractionVC *web=[[KDPWebInteractionVC alloc]init]; web.url=model.click_param; [self.navigationController pushViewController:web animated:YES]; }else if (model.click_type.integerValue == 2){//商品详情 KDPGoodDetailVC *goodD=[[KDPGoodDetailVC alloc]init]; KDPGoodsModel *models=[[KDPGoodsModel alloc]init]; models.goods_id=model.click_param; goodD.model=models; [self.navigationController pushViewController:goodD animated:YES]; }else if (model.click_type.integerValue == 3) {//x专题列表 KDPProjectListVC *listV=[[KDPProjectListVC alloc]init]; listV.navBar.navTitleLabel.text=model.title; listV.projectID=model.click_param; if (model.type.length == 0) { listV.type = @"module"; }else{ listV.type = model.type; } [self.navigationController pushViewController:listV animated:YES]; } }; return bannerC; } if (indexPath.section == 2) { KDPRecommendGoodCell *goodC=[collectionView dequeueReusableCellWithReuseIdentifier:recommendCell forIndexPath:indexPath]; goodC.model=self.dataArray[indexPath.row]; return goodC; } KDPLiveEarningCell *earinCell =[collectionView dequeueReusableCellWithReuseIdentifier:liveEarningCell forIndexPath:indexPath]; if (addNum>0) { earinCell.addNumL.text=[NSString stringWithFormat:@"+%ld",addNum]; } earinCell.orderNumgL.text=self.orderNumber; earinCell.rebet_money=self.order_rebate_money; return earinCell; } -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 3; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { if (section == 2) { return self.dataArray.count; } if (section == 1&& self.bannerArray.count==1) { return 0; } return 1; } -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 1) { if (self.bannerArray.count==0 ) { return CGSizeMake(SCREEN_WIDTH, 0.1); } return CGSizeMake(SCREEN_WIDTH, 113); } if (indexPath.section == 2) { return CGSizeMake(SCREEN_WIDTH, 121); } return CGSizeMake(SCREEN_WIDTH, 116); } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { KDPTodayLiveDataVC *todayV=[[KDPTodayLiveDataVC alloc]init]; [self.navigationController pushViewController:todayV animated:YES]; } if (indexPath.section == 2) { KDPGoodDetailVC *goodDetail=[[KDPGoodDetailVC alloc]init]; goodDetail.model=self.dataArray[indexPath.row]; [self.navigationController pushViewController:goodDetail animated:YES]; } } -(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (section == 2) { return CGSizeMake(SCREEN_WIDTH, 60); } return CGSizeMake(0, 0 ); } -(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 2) { KDPRecommendHeadView *headV=[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:recommendHead forIndexPath:indexPath]; return headV; } return nil; } -(NSMutableArray *)dataArray { if (!_dataArray) { _dataArray=[NSMutableArray array]; } return _dataArray; } #pragma mark---页面布局 -(UIButton *)headImgBtn { if (!_headImgBtn) { _headImgBtn=[[UIButton alloc]init]; _headImgBtn.backgroundColor=[UIColor colorWithHexString:ThemeColor]; _headImgBtn.layer.cornerRadius=16; _headImgBtn.layer.masksToBounds=YES; _headImgBtn.layer.borderColor=[UIColor colorWithHexString:@"#FFB444"].CGColor; UIImageView *imgv=[[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 28, 28)]; imgv.layer.cornerRadius=14; imgv.layer.masksToBounds=YES; imgv.tag=1000; [_headImgBtn addSubview:imgv]; _headImgBtn.layer.borderWidth=1; [_headImgBtn addTarget:self action:@selector(drawerClickButton) forControlEvents:UIControlEventTouchUpInside]; _headImgBtn.adjustsImageWhenHighlighted=NO; } return _headImgBtn; } -(UIButton *)messageButton { if (!_messageButton) { _messageButton=[[UIButton alloc]init]; [_messageButton setImage:[UIImage imageNamed:@"message_icon"] forState:UIControlStateNormal]; _messageButton.adjustsImageWhenHighlighted=NO; [_messageButton addTarget:self action:@selector(messageclickButton) forControlEvents:UIControlEventTouchUpInside]; [_messageButton ba_setBadgeLabelAttributes:^(BABadgeLabel *badgeLabel) { UILabel *label = (UILabel *)badgeLabel; label.width=10; label.textColor = [UIColor baseColor]; label.backgroundColor = [UIColor redColor]; label.height = label.width; label.font = [UIFont systemFontOfSize:8]; }]; [_messageButton ba_hiddenBadge]; } return _messageButton; } -(UICollectionView *)dataCollectionView { if (!_dataCollectionView) { UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init]; layout.minimumLineSpacing=0; layout.minimumInteritemSpacing=0; _dataCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, KDNavBarHeight, SCREEN_WIDTH, SCREEN_HEIGHT-KDNavBarHeight-KDTabBarHeight) collectionViewLayout:layout]; _dataCollectionView.backgroundColor=[UIColor colorWithHexString:LineColor]; [_dataCollectionView registerClass:[KDPLiveEarningCell class] forCellWithReuseIdentifier:liveEarningCell]; [_dataCollectionView registerClass:[KDPBannerCell class] forCellWithReuseIdentifier:bannerCell]; [_dataCollectionView registerClass:[KDPRecommendGoodCell class] forCellWithReuseIdentifier:recommendCell]; [_dataCollectionView registerClass:[KDPRecommendHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:recommendHead]; [_dataCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"]; _dataCollectionView.showsVerticalScrollIndicator=NO; _dataCollectionView.delegate=self; _dataCollectionView.dataSource=self; _dataCollectionView.mj_header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{ [self getBanner]; [self initialieValues]; [self getRecommendGood]; }]; _dataCollectionView.emptyDataSetDelegate = self; _dataCollectionView.emptyDataSetSource = self; if (@available(iOS 11.0, *)) { _dataCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { self.automaticallyAdjustsScrollViewInsets = NO; } _dataCollectionView.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ self.pageNum=[NSString stringWithFormat:@"%ld",self.pageNum.integerValue+1]; [self getRecommendGood]; }]; } return _dataCollectionView; } #pragma mark---侧滑 -(void)drawerClickButton { KDPDrawerVC *vcFrame =[[KDPDrawerVC alloc]init]; CWLateralSlideConfiguration *con =[CWLateralSlideConfiguration defaultConfiguration]; con.distance=SCREEN_WIDTH/3*2; [self cw_showDrawerViewController:vcFrame animationType:0 configuration:con]; } #pragma mark---消息中心 -(void)messageclickButton { KDPNoticeViewController *noticeV=[[KDPNoticeViewController alloc]init]; [self.navigationController pushViewController:noticeV animated:YES]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.tabBarController.tabBar.hidden=NO; [self updateData]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [order_timer invalidate]; } #pragma mark---更新数据 -(void)updateData { if ([KDPAccountTool isLogin]) { [self getUserTodayIncom]; [self isOpenService]; [self initialieValues]; [self getRecommendGood]; [self getBanner]; [self messageUnreadNumData]; order_timer = [NSTimer timerWithTimeInterval:30 target:self selector:@selector(order_timerAction) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:order_timer forMode:NSDefaultRunLoopMode]; [self setUserHeadImg]; } } -(void)order_timerAction { if ([KDPAccountTool isLogin]) { [self getUserTodayIncom]; } } #pragma mark----设置头像 -(void)setUserHeadImg { KDPAccountModel *model=[KDPAccountTool account]; UIImageView *imgv=[self.headImgBtn viewWithTag:1000]; [imgv sd_setImageWithURL:[NSURL URLWithString:model.img]placeholderImage:[UIImage imageNamed:placholderImg]]; } #pragma mark---打开服务提示页面 -(KDPServiceView *)serviceView { if (!_serviceView) { _serviceView=[[KDPServiceView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; __weak KDPTopViewController *weakself=self; _serviceView.serviceBlock = ^{ KDPWebInteractionVC *pidV=[[KDPWebInteractionVC alloc]init]; pidV.url=custSerURL; [weakself cw_pushViewController:pidV]; [weakself.serviceView removeFromSuperview]; }; } return _serviceView; } #pragma mark---获取未读消息个数 -(void)messageUnreadNumData { if ([KDPAccountTool isLogin]) { [KDPNetworkRequestHTTP postURL:message_numURL params:nil success:^(id _Nonnull json) { if ([json[@"data"][@"message_new"] integerValue] == 1) {//是否有新消息 0:无 1:有 [self.messageButton ba_showBadge]; }else{ [self.messageButton ba_hiddenBadge]; } } failure:^(NSError * _Nonnull error) { }]; } } #pragma mark---判断是否打开客服页面 -(void)isOpenService { //存储第一次打开APP,是否显示客服页面, NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults]; NSString *oneStep =[defaults objectForKey:@"step"]; if (oneStep.integerValue == 0) { [UIView animateWithDuration:0.3 animations:^{ [[UIApplication sharedApplication].keyWindow addSubview:self.serviceView]; }]; [defaults setValue:@"1" forKey:@"step"]; [defaults synchronize]; } } - (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{ return [UIImage imageNamed:@"no_order"]; } - (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView{ return YES; } - (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{ return [[NSAttributedString alloc] initWithString:@"还没有记录" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithHex:0x333333],NSFontAttributeName:FONT_SYS(12)}]; } - (CGFloat )spaceHeightForEmptyDataSet:(UIScrollView *)scrollView{ return 30; } @end