// // HCMineMainViewController.m // hc // // Created by xiaoxi on 2018/1/16. // Copyright © 2018年 hc. All rights reserved. // #import "HCMineMainViewController.h" #import "HFStretchableTableHeaderView.h" #import "HCLoginViewController.h" #import "HCCollectionViewController.h" #import "HCSettingViewController.h" #import "HCMyTicketViewController.h" #import "HCMyOrderViewController.h" #import #import "HCBrowserHistoryController.h" #import "HCFeedbackController.h" #import #import "HCFindBookWebViewController.h" #import "UITableViewCell+Cc_CornRadio.h" #import "HCFourButtonView.h" #import "HCShopCarViewController.h" #import "CCActionSheet.h" #import "HCMineBlanceView.h" #import "HCMineOrderView.h" #import "HCMoreMenuView.h" #import "HCOrderMainViewController.h" #import "HCOrderMainViewController.h" #import "HCCommissionMainViewController.h" #import "HCMyFansViewController.h" #import "HCMyCollectionViewController.h" #import "HCInviteFansViewController.h" #import "HCMessageListController.h" #import "HCUserInfo.h" #import "HCUserInfoView.h" #import "HCCollectionMainViewController.h" #import "HCLinkFansController.h" #import "HCPrivilegeReferralViewController.h" #import "HCUserInfo.h" #import "HCMonthMainViewController.h" #import "HCMineAdView.h" #import "HCAccountDetailController.h" #import "AdPopModel.h" #import "AdJumpManager.h" #import "UIView+BABadgeView.h" #import "HCBuyLimitMainController.h" #import "HCHistoryViewController.h" #import "HCUserView.h" #import "HCWebDetailController.h" #import "AdJumpManager.h" #import "HCGoodDetailViewController.h" #import "HCGoodCollectionCell.h" #import "CCAlertShowView.h" #import "HCPushAlertView.h" #import "HCFiveStartAlertView.h" #import "HCUpdateAlertView.h" #import "HCTitleMoreMeunView.h" #import "HCMineRecomGoodsView.h" #import "HCMyCoupleViewController.h" #import "HCGoodCollectionCell.h" #import "HCSignInViewController.h" #import "HCShopCarViewController.h" #import "UIView+SDAutoLayout.h" #import "HCMyOrderViewController.h" #import #import #import #import "HCMineHeadView.h" #import "HCGoodHorzitolCollectionCell.h" #import "HCFindRequestViewModel.h" static NSString *KHorizontalCellId = @"KHorizontalCellId"; static NSString *KYHGoodCollectionCell = @"KYHGoodCollectionCell"; @interface HCMineMainViewController () < CCActionSheetDelegate, YHMineFourButtonViewDelegate, YHMoreMenuViewDelegate, KBTitleMoreMenuViewDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout >{ UIView *_saveView; NSInteger _youLikePage; BOOL isAdd; } @property (nonatomic, strong) UIView *header; @property (nonatomic, strong) UIImageView *headerImg; @property (nonatomic, strong) HCMoreMenuView *topMenuView; @property (nonatomic, strong) HCTitleMoreMeunView *bottomMenuView; @property (nonatomic, strong) HCMineRecomGoodsView *recomGoodsView; @property (nonatomic, strong) HCMineAdView *adView; @property (nonatomic, strong) UIButton *messageBtn; @property (nonatomic, strong) HCUserInfo *userInfo; @property (nonatomic, strong) HCUserView *userView; @property (nonatomic, strong) YYLabel *totalSaveMoney; /*dataSource**/ @property (nonatomic, strong) NSArray *cellData; @property (nonatomic, strong) NSArray *sexArr; @property(nonatomic,strong)UICollectionView *collections; @property (nonatomic, strong) UIView *leftLine; @property (nonatomic, strong) UIView *rightLine; @property (nonatomic, strong) UILabel *titleLabel; @property (nonatomic, strong) NSMutableArray *youLikeArr; @end @implementation HCMineMainViewController - (void)requestYouLikeData { [HCFindRequestViewModel requestGuessYouLikeParamPage:_youLikePage success:^(NSArray *array) { [self.youLikeArr addObjectsFromArray:array]; [self.collections reloadData]; [self noMoreDataWithArray:array]; [self endRefreshing]; if (self.youLikeArr.count != 0) { [self initSubViews]; } } failure:^(NSError *error) { [MBProgressHUD showMessage:@"网络错误"]; [SVProgressHUD dismiss]; [self endRefreshing]; }]; } - (void)noMoreDataWithArray:(NSArray *)array { if (array.count <= 0) { MJRefreshAutoNormalFooter *foot = (MJRefreshAutoNormalFooter *)self.collections.mj_footer; [foot setTitle:@"到底啦" forState:MJRefreshStateIdle]; } } - (void)endRefreshing { [self.collections.mj_header endRefreshing]; [self.collections.mj_footer endRefreshing]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent]; [self loadUserInfo]; if ([AccountTool isLogin]) { [self checkForNewMessage]; } } - (void)viewDidLoad { [super viewDidLoad]; _youLikePage = 1; isAdd = YES; [self configNavightion]; [self configTableView]; [self loadAdData]; [self requestYouLikeData]; } - (void)configNavightion { self.navigationBar.backgroundColor = [UIColor clearColor]; self.navigationBar.hidden = YES; } - (void)configTableView { [self.view addSubview:self.collections]; __weak typeof(self) weakSelf = self; //头部背景图 self.headerImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,KStatusBarHeight+Fitsize(130))]; self.headerImg.userInteractionEnabled = YES; self.headerImg.clipsToBounds = YES; self.header.layer.masksToBounds = YES; self.headerImg.contentMode = UIViewContentModeScaleAspectFill; //头视图 self.header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, Fitsize(260))]; self.headerImg.image = [UIImage imageNamed:@"mine_top_bg"]; [self.header addSubview:self.headerImg]; //上边的多菜单 NSArray *buttons = @[@{@"title":@"我的优惠券",@"image":@"youhuiquan"}, @{@"title":@"我的收藏",@"image":@"shoucang"}, @{@"title":@"我的订单",@"image":@"dingdan"}, @{@"title":@"浏览记录",@"image":@"liulanjilu"}]; self.topMenuView = [[HCMoreMenuView alloc] initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, (SCREEN_WIDTH-20)/4) buttonInfoArray:buttons]; self.topMenuView.delelgate = self; [self.header addSubview:self.topMenuView]; [self.topMenuView mas_updateConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.headerImg.height-30); make.left.mas_equalTo(10); make.right.mas_equalTo(-10); make.height.mas_equalTo((SCREEN_WIDTH-20)/4); }]; //用户信息 self.userView = [[HCUserView alloc] initWithFrame:CGRectMake(25, 0, SCREEN_WIDTH-3, 95)]; self.userView.centerY = KStatusBarHeight + (self.headerImg.height-KStatusBarHeight)/2+10; self.userView.tapActionBlock = ^{ if ([AccountTool isLogin]) { [weakSelf settingAction]; }else { [weakSelf goToLoginPage]; } }; [self.headerImg addSubview:self.userView]; //消息按钮 UIButton *msgButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-50, KStatusBarHeight, 40, 40)]; [msgButton setImage:[UIImage imageNamed:@"xiaoxi"] forState:UIControlStateNormal]; [msgButton addTarget:self action:@selector(gotoMyMessagesPage) forControlEvents:UIControlEventTouchUpInside]; self.messageBtn = msgButton; [msgButton ba_addDotWithColor:[UIColor whiteColor]]; [msgButton ba_moveBadgeWithX:-12 Y:15]; [self.headerImg addSubview:msgButton]; //消息按钮 UIButton *setButton = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-100, KStatusBarHeight, 40, 40)]; [setButton setImage:[UIImage imageNamed:@"shezhi"] forState:UIControlStateNormal]; [setButton addTarget:self action:@selector(settingAction) forControlEvents:UIControlEventTouchUpInside]; [self.headerImg addSubview:setButton]; if (@available(iOS 11.0, *)) { self.collections.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;//UIScrollView也适用 }else { self.automaticallyAdjustsScrollViewInsets = NO; } } - (void)scrollViewDidScroll:(UIScrollView *)scrollView { // // CGFloat yOffset = scrollView.contentOffset.y; // // if(yOffset < 0) // { // self.headerImg.y = yOffset; // self.headerImg.height = 170+ABS(yOffset); // } } #pragma mark ============ UICollectionView Delegate && DataSource ========== - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.youLikeArr.count; } - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section { if (self.youLikeArr.count ==0) { return CGSizeMake(SCREEN_WIDTH, FITSIZE(220)); } return CGSizeMake(SCREEN_WIDTH, FITSIZE(260)); } - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section { return CGSizeMake(0, 0); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { HCChildGoodModel *model = self.youLikeArr[indexPath.item]; HCGoodCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:KYHGoodCollectionCell forIndexPath:indexPath]; cell.backgroundColor =[UIColor whiteColor]; cell.model = model; return cell; } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{ UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headner" forIndexPath:indexPath]; if (isAdd) { [header addSubview:self.header]; isAdd = NO; } return header; } /** 加载用户信息 */ - (void)loadUserInfo { if (![AccountTool isLogin]) { [self noLoginView]; return; } [HCHttp post:UserInfo params:nil success:^(id json) { self.userInfo = [HCUserInfo yy_modelWithJSON:json]; [self.userView setLoginView:self.userInfo];//设置用户信息 NSString *saveStr = [NSString stringWithFormat:@"已累计为你省钱%.2f元",[self.userInfo.allSaveMoney floatValue]]; NSMutableAttributedString *disAttr = [[NSMutableAttributedString alloc] initWithString:saveStr]; [disAttr yy_setFont:[UIFont systemFontOfSize:14] range:NSMakeRange(0, saveStr.length)]; [disAttr yy_setFont:[UIFont systemFontOfSize:16] range:NSMakeRange(7, saveStr.length-8)]; [disAttr yy_setColor:[UIColor homeRedColor] range:NSMakeRange(7, saveStr.length-8)]; self.totalSaveMoney.attributedText = disAttr; } failure:^(NSError *error) { if (![AccountTool isLogin]) { [self noLoginView]; } }]; } - (void)noLoginView { [self.userView setUnLoginView]; } /** 检查是否有新消息 */ - (void)checkForNewMessage { if ([AccountTool isLogin]) { NSString *url = [NSString stringWithFormat:@"%@/api/v2/message_push/messageNew",BaseURL]; [HCHttp post:url params:nil success:^(id json) { NSNumber *new = json[@"data"][@"message_new"]; if ([new boolValue]) { [self.messageBtn ba_showBadge]; }else { [self.messageBtn ba_hiddenBadge]; } [self.collections.mj_header endRefreshing]; } failure:^(NSError *error) { }]; } } /** 加载广告位数据 */ - (void)loadAdData { NSString *url = [NSString stringWithFormat:@"%@/api/v2/adv/advByType",BaseURL]; NSDictionary *para = @{@"adv_type":@"4"}; [HCHttp get:url params:para success:^(id json) { NSArray *list = [NSArray yy_modelArrayWithClass:[AdPopModel class] json:json[@"data"]]; if (list.count > 0) { [self showAdViewWithAdModel:list.firstObject]; } } failure:^(NSError *error) { }]; } /** 加载我的页面推荐商品 */ - (void)loadMineRecomGood { NSString *url = [NSString stringWithFormat:@"%@/api/v2/goods/pocketMyCenter",BaseURL]; [HCHttp post:url params:nil success:^(id json) { HCChildGoodModel *model = [HCChildGoodModel yy_modelWithJSON:json]; self.recomGoodsView.model = model; } failure:^(NSError *error) { }]; } - (void)showAdViewWithAdModel:(AdPopModel *)model { [self.adView mas_updateConstraints:^(MASConstraintMaker *make) { make.height.mas_equalTo(74+10); }]; self.adView.model = model; } #pragma mark ------------ - (void)settingAction { HCSettingViewController *setting = [[HCSettingViewController alloc] init]; [self.navigationController pushViewController:setting animated:YES]; } #pragma mark ---- YHMoreMenuViewDelegate - (void)morenMenu:(HCMoreMenuView *)menuView Button:(UIButton *)button clickIndex:(NSInteger)index { switch (index) { case 0: //我的优惠券 [self gotoMycouplePage]; break; case 1: //我的收藏 [self goToMyCollectionPage]; break; case 2: //我的订单 [self goToMyOrderPage]; break; case 3:{ //浏览记录 [self goToMyBrowserHistory]; } break; default: break; } } #pragma mark ---------------- private --------- /** 用户登录 */ - (void)goToLoginPage { HCLoginViewController *login = [[HCLoginViewController alloc] init]; [self presentViewController:login animated:YES completion:nil]; } /** 我的订单 */ - (void)goToMyOrderPage { [self openMyOrderWebView]; } - (void)openMyOrderWebView { if ([[ALBBSession sharedInstance] isLogin]){ HCMyOrderViewController *order = [[HCMyOrderViewController alloc] init]; [self.navigationController pushViewController:order animated:YES]; }else { //淘宝授权 ALBBSDK *albbSDK = [ALBBSDK sharedInstance]; [albbSDK setAppkey:ALBC_APP_KEY]; [albbSDK setAuthOption:NormalAuth]; [albbSDK auth:self successCallback:^(ALBBSession *session){ HCMyOrderViewController *order = [[HCMyOrderViewController alloc] init]; [self.navigationController pushViewController:order animated:YES]; } failureCallback:^(ALBBSession *session,NSError *error){ }]; } } /** 我的优惠券 */ - (void)gotoMycouplePage { if (![AccountTool isLogin]) { [self goToLoginPage]; return; } HCMyCoupleViewController *couple = [[HCMyCoupleViewController alloc] init]; [self.navigationController pushViewController:couple animated:YES]; } /** 购物车 */ - (void)goToMyshopCarPage { HCShopCarViewController *shopCar = [[HCShopCarViewController alloc] init]; [self.navigationController pushViewController:shopCar animated:YES]; } /** 我的收藏 */ - (void)goToMyCollectionPage { if (![AccountTool isLogin]) { [self goToLoginPage]; return; } HCMyCollectionViewController *collection = [[HCMyCollectionViewController alloc] init]; [self.navigationController pushViewController:collection animated:YES]; } /** 我的消息 */ - (void)gotoMyMessagesPage { if (![AccountTool isLogin]) { [self goToLoginPage]; return; } HCMessageListController *messages = [[HCMessageListController alloc] init]; [self.navigationController pushViewController:messages animated:YES]; } /** 已领优惠券 */ - (void)goToMyTicketPage { if (![AccountTool isLogin]) { [self goToLoginPage]; return; } HCMyTicketViewController *ticket = [[HCMyTicketViewController alloc] init]; [self.navigationController pushViewController:ticket animated:YES]; } /** 浏览历史 */ - (void)goToMyBrowserHistory { if (![AccountTool isLogin]) { [self goToLoginPage]; return; } HCHistoryViewController *history = [[HCHistoryViewController alloc] init]; [self.navigationController pushViewController:history animated:YES]; } #pragma mark -------------------- YHMineFourButtonViewDelegate --------- - (void)YHMineFourButtonViewDidClickButtonIndex:(NSInteger)index { switch (index) { case 0: [self goToMyOrderPage]; break; case 1: [self goToMyTicketPage]; break; case 2: [self goToMyshopCarPage]; break; case 3: [self goToMyBrowserHistory]; break; default: break; } } #pragma mark ===================== layezer ============== -(UICollectionView *)collections { if(!_collections) { CGRect collectionViewFrame= CGRectMake(0, 0,SCREEN_WIDTH, SCREEN_HEIGHT-TabbarHeight); UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init]; flowLayout.sectionInset=UIEdgeInsetsMake(5, 7.5, 10, 7.5); CGFloat width = (SCREEN_WIDTH)/2; CGFloat height = width + 102; flowLayout.itemSize = CGSizeMake(width-FITSIZE(12.5f), height);// 该行代码就算不写,item也会有默认尺寸 UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:collectionViewFrame collectionViewLayout:flowLayout]; collectionView.backgroundColor = [UIColor clearColor]; collectionView.dataSource = self; collectionView.delegate = self; _collections = collectionView; _collections.showsVerticalScrollIndicator=NO; _collections.showsHorizontalScrollIndicator=NO; _collections.layer.masksToBounds=YES; [_collections registerClass:[HCGoodCollectionCell class] forCellWithReuseIdentifier:KYHGoodCollectionCell]; [_collections registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headner"]; _collections.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{ [self requestYouLikeData]; }]; [self.collections.mj_footer beginRefreshing]; } return _collections; } - (void)initSubViews { [self.header addSubview:self.leftLine]; [self.header addSubview:self.titleLabel]; [self.header addSubview:self.rightLine]; [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(SCREEN_WIDTH/2-FITSIZE(30)); make.top.mas_equalTo(self.headerImg.height+(SCREEN_WIDTH-20)/4-20); make.height.mas_equalTo(FITSIZE(40)); }]; [self.leftLine mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(self.titleLabel.mas_left).mas_offset(-5); make.centerY.mas_equalTo(self.titleLabel.mas_centerY); make.width.mas_equalTo(60); make.height.mas_equalTo(1); }]; [self.rightLine mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(self.titleLabel.mas_right).mas_offset(5); make.centerY.mas_equalTo(self.titleLabel.mas_centerY); make.width.mas_equalTo(60); make.height.mas_equalTo(1); }]; } - (NSMutableArray *)youLikeArr { if (!_youLikeArr) { _youLikeArr = [NSMutableArray array]; } return _youLikeArr; } #pragma mark ------ - (UIView *)leftLine { if (!_leftLine) { _leftLine = [[UIView alloc] init]; _leftLine.backgroundColor = [UIColor YHColorWithHex:0xdddddd]; } return _leftLine; } - (UIView *)rightLine { if (!_rightLine) { _rightLine = [[UIView alloc] init]; _rightLine.backgroundColor = [UIColor YHColorWithHex:0xdddddd]; } return _rightLine; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] init]; _titleLabel.textColor = [UIColor YHColorWithHex:0x666666]; _titleLabel.font = [UIFont systemFontOfSize:14]; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.text = @"猜你喜欢"; } return _titleLabel; } @end