// // KXMainPageViewController.m // QBCS // // Created by kuxuan on 2017/6/6. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXMainPageViewController.h" #import "KXMainHorizontalCycleView.h" #import "KXCycleModel.h" #import "KXLoanButton.h" #import "KXMainListTableViewCell.h" #import "KXWebDetailViewController.h" #import "KXLoanAllDetailViewController.h" #import "KXLoanNewViewController.h" #import "KXMainDetailViewController.h" #import "KXLetterViewController.h" #import "KXMainDetailModel.h" #import "KXMainPageViewModel.h" #import "KXLoanAllModel.h" #import "KXDismissViewController.h" #import "KXMainVerticleCycleView.h" #import "KXVerticleModel.h" #import "KXMarkView.h" #import "KXThirdMarkView.h" //3.0的弹出框 #import "KXBaseLinkViewController.h" #import "KXLaunchModel.h" #import @interface KXMainPageViewController () { KXMainHorizontalCycleView *_cycleView; KXMainVerticleCycleView *_verticleView; UIView *_headerView; UIView *_alertWindow; KXMarkView *_markView; KXLaunchModel *_launchModel; KXThirdMarkView *_thirdMarkView; } @property (nonatomic,strong)UITableView *tableView; @property (nonatomic,strong)NSMutableArray *dataSource; @property (nonatomic,assign)NSInteger page; @property (nonatomic,strong)UIButton *rightBtn; @property (nonatomic,strong)UILabel *redLabel; @property (nonatomic,strong)NSArray *otherSource; //新品区数据,暂时写死 @property (nonatomic,strong)NSMutableArray *noticeArray; //公告数组 @property (nonatomic,strong)NSMutableArray *allArray; @end @implementation KXMainPageViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.page=1; self.navigationController.delegate = self; [self setupUI]; [self createTabletableView]; [self setAdAlert]; [self setNavgation]; [self requestCycleViewData]; [MobClick event:@"HomeTab"]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES]; // [self loadFirst]; [self requestMoreLetterAction]; } - (void)viewWillDisappear:(BOOL)animated { [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES]; } - (void)setAdAlert { NSDate *date = [[NSUserDefaults standardUserDefaults]valueForKey:KXADALERTKEY]; if (!date||[KXTime isToday:date]==NO) { _alertWindow = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds]; _alertWindow.layer.cornerRadius = 8; _alertWindow.layer.masksToBounds = YES; _alertWindow.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3]; UIImageView *adImageView = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2.0-150*SCREEN_MUTI, 106*SCREEN_MUTI, 300*SCREEN_MUTI, 430*SCREEN_MUTI)]; [_alertWindow addSubview:adImageView]; adImageView.userInteractionEnabled = YES; [KXMainPageViewModel getAdAlertUrl:^(KXLaunchModel *model) { _launchModel = model; [adImageView sd_setImageWithURL:[NSURL URLWithString:model.image_url]]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(adClick:)]; [adImageView addGestureRecognizer:tap]; [self.view addSubview:_alertWindow]; }]; UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom]; closeButton.frame = CGRectMake(SCREEN_WIDTH/2.0-15, 560*SCREEN_MUTI, 30, 30); [closeButton setImage:[UIImage imageNamed:@"main_alert_close"] forState:UIControlStateNormal]; [closeButton addTarget:self action:@selector(closeAction) forControlEvents:UIControlEventTouchUpInside]; [_alertWindow addSubview:closeButton]; } NSDate *USDate = [NSDate date]; NSTimeZone *zone = [NSTimeZone systemTimeZone]; NSInteger seconds = [zone secondsFromGMTForDate:USDate]; NSDate *nowDate = [USDate dateByAddingTimeInterval:seconds]; [[NSUserDefaults standardUserDefaults]setValue:nowDate forKey:KXADALERTKEY]; [[NSUserDefaults standardUserDefaults]synchronize]; } - (void)closeAction { [MobClick event:@"advertisementHomeClose"]; [_alertWindow.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [obj removeFromSuperview]; }]; [_alertWindow removeFromSuperview]; _alertWindow = nil; } - (void)adClick:(UITapGestureRecognizer *)tap { [MobClick event:@"advertisementHomeClick"]; [_alertWindow.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { [obj removeFromSuperview]; }]; [_alertWindow removeFromSuperview]; _alertWindow = nil; if ([_launchModel.skip_type isEqualToString:@"0"]) { if ([_launchModel.product_id isEqualToString:@"0"]) { KXBaseLinkViewController *baselink = [[KXBaseLinkViewController alloc]init]; baselink.linkString = _launchModel.link; KXDismissViewController *detailNav=[[KXDismissViewController alloc]initWithRootViewController:baselink]; [self.navigationController presentViewController:detailNav animated:YES completion:nil]; }else{ KXMainDetailViewController *detail=[[KXMainDetailViewController alloc]init]; KXDismissViewController *detailNav=[[KXDismissViewController alloc]initWithRootViewController:detail]; detail.detailID=_launchModel.product_id; detail.enterSource = @"homeAd"; [self.navigationController presentViewController:detailNav animated:YES completion:nil]; } }else{ KXWebDetailViewController *web=[[KXWebDetailViewController alloc]init]; KXDismissViewController *detailNav=[[KXDismissViewController alloc]initWithRootViewController:web]; web.product_apply_id = @"0"; web.product_id = _launchModel.product_id; web.webStr=_launchModel.link; [self.navigationController presentViewController:detailNav animated:YES completion:nil]; } } -(void)requestMoreLetterAction { NSString *urlString=[NSString stringWithFormat:@"%@/news/getMaxIdSecond",URL]; [KXHTTP post:urlString params:nil success:^(id json) { NSString *max=[[NSUserDefaults standardUserDefaults]objectForKey:MAX_ID]; if (self.rightBtn) { if (!max||max.intValue<[json[@"max_id"] intValue]) { dispatch_async(dispatch_get_main_queue(), ^{ self.redLabel.hidden=NO; }); }else{ dispatch_async(dispatch_get_main_queue(), ^{ self.redLabel.hidden=YES; }); } } } failure:^(NSError *error) { }]; } -(void)setNavgation { self.rightBtn=[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 30, 22, 20, 20)]; [self.rightBtn setImage:[UIImage imageNamed:@"main_letter_white"] forState:UIControlStateNormal]; [self.rightBtn addTarget:self action:@selector(letterAction) forControlEvents:UIControlEventTouchUpInside]; [_headerView addSubview:self.rightBtn]; [self.rightBtn setBadgeFrame:CGRectMake(14, 0, 6,6)]; [self.rightBtn showBadge]; } -(void)letterAction { [MobClick event:@"newsClick"]; KXLetterViewController *letter=[[KXLetterViewController alloc]init]; letter.hideblock = ^{ [self.rightBtn clearBadge]; }; [self.navigationController pushViewController:letter animated:YES]; } -(void)setupUI { _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 370*SCREEN_MUTI)]; _headerView.backgroundColor=[UIColor KXColorWithHex:0xF4F4F4]; // _headerView.layer.borderColor=[UIColor lineColor].CGColor; // _headerView.layer.borderWidth=0.3; _cycleView=[[KXMainHorizontalCycleView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120*SCREEN_MUTI)]; _cycleView.delegate=self; [_headerView addSubview:_cycleView]; // UILabel *peapleLabel=[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, 22, 100, 20)]; // peapleLabel.text=[NSString stringWithFormat:@"在线人数:%d人",15000+arc4random()%1000]; // peapleLabel.textColor=[UIColor whiteColor]; // peapleLabel.layer.cornerRadius=10; // peapleLabel.layer.masksToBounds=YES; // peapleLabel.font=FONT_SYS(10); // peapleLabel.textAlignment=NSTextAlignmentCenter; // peapleLabel.layer.borderColor=[UIColor whiteColor].CGColor; // peapleLabel.layer.borderWidth=1; // peapleLabel.backgroundColor=[UIColor colorWithWhite:0 alpha:0.6]; // [_headerView addSubview:peapleLabel]; _verticleView = [[KXMainVerticleCycleView alloc]initWithFrame:CGRectMake(0, 120*SCREEN_MUTI, SCREEN_WIDTH, 46*SCREEN_MUTI)]; _verticleView.backgroundColor = [UIColor whiteColor]; _verticleView.delegate = self; [_headerView addSubview:_verticleView]; NSMutableArray *contentArray = [NSMutableArray arrayWithCapacity:0]; [KXMainPageViewModel requestVerticleViewData:^(NSArray *notice) { for (KXVerticleModel *model in notice) { [self.noticeArray addObject:model]; [contentArray addObject:model.content]; } _verticleView.noticeArray = contentArray; }]; CGFloat weight = (SCREEN_WIDTH-1)/2; CGFloat height = Fitsize(73); NSInteger rank = 2; //每列间距 CGFloat rankMargin = (self.view.frame.size.width - rank * weight) / (rank - 1); //每行间距 CGFloat rowMargin = 1; NSUInteger index = 4; for (int i=0; i 3*24*60*60) { // [self createMarkView]; // } // 3.0的处理逻辑 NSInteger clickCount = [[NSUserDefaults standardUserDefaults] integerForKey:KXMARK_CLICKCOUNT]; BOOL isMark = [[NSUserDefaults standardUserDefaults] boolForKey:KXMARK_TOAPPSTORE]; if (isMark == YES) { if (timeInterval > 10*24*60*60) { if (clickCount >3 && clickCount%4 == 0) { [self createThirdMarkView]; } } } else{ if (!date) { if (clickCount >3 && clickCount%4==0) { [self createThirdMarkView]; } } if (timeInterval > 3*24*60*60) { if (clickCount >3 && clickCount%4==0) { [self createThirdMarkView]; } } } } #pragma mark ===================KXThirdMarkVIewDelegate============== - (void)markViewBtnClick:(UIButton *)button{ switch (button.tag) { case 1000: //跳转按钮 { [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:KXMARK_TODAY]; [[NSUserDefaults standardUserDefaults] setBool:YES forKey:KXMARK_TOAPPSTORE]; [[NSUserDefaults standardUserDefaults] synchronize]; if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:KXAPPSTORE_ADDRESS]]) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:KXAPPSTORE_ADDRESS]]; [_thirdMarkView removeFromSuperview]; } } break; case 2000: //关闭按钮 { [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:KXMARK_TODAY]; [[NSUserDefaults standardUserDefaults] setBool:NO forKey:KXMARK_TOAPPSTORE]; [[NSUserDefaults standardUserDefaults] synchronize]; [_thirdMarkView removeFromSuperview]; } break; default: break; } } #pragma mark =============KXMarkViewDelegate====================== - (void)markViewSelectMarkItem:(NSInteger)index { switch (index) { case 0:{ [[NSUserDefaults standardUserDefaults]setObject:[NSDate distantFuture] forKey:KXMARK_TODAY]; [[NSUserDefaults standardUserDefaults]synchronize]; [[UIApplication sharedApplication]openURL:[NSURL URLWithString:KXAPPSTORE_ADDRESS]]; } break; case 1:{ [[NSUserDefaults standardUserDefaults]setObject:[NSDate date] forKey:KXMARK_TODAY]; [[NSUserDefaults standardUserDefaults]synchronize]; } break; case 2:{ [[NSUserDefaults standardUserDefaults]setObject:[NSDate distantFuture] forKey:KXMARK_TODAY]; [[NSUserDefaults standardUserDefaults]synchronize]; } break; default: break; } _markView = nil; } - (void)loanAction:(KXLoanButton *)btn { // if (btn.tag == 555) { // KXLoanNewViewController *new = [[KXLoanNewViewController alloc]init]; // [self.navigationController pushViewController:new animated:YES]; // }else{ KXLoanAllModel *model = self.allArray[btn.tag - 555]; NSArray *eventArr = @[@"speedKind",@"hotKind",@"largeKind",@"newKind"]; [MobClick event:eventArr[btn.tag-555]]; NSDictionary *dict = self.otherSource[btn.tag - 555]; KXLoanAllDetailViewController *detail = [[KXLoanAllDetailViewController alloc]init]; detail.changeLabel = ^{ btn.redButton.hidden = YES; }; detail.ID = [NSNumber numberWithInteger:model.Id.integerValue]; detail.titleString = dict[@"title"]; [self.navigationController pushViewController:detail animated:YES]; } - (void)setloanAllLabelName { [KXMainPageViewModel loanAllLabelName:^(id json) { for (int i = 0; i < [json count]; i ++) { KXLoanAllModel *model = json[i]; [self.allArray addObject:model]; KXLoanButton *btn = [_headerView viewWithTag:556+i]; if (model.label_name.length != 0) { btn.redButton.hidden = NO; [btn.redButton setTitle:model.label_name forState:UIControlStateNormal]; } } }]; } //请求轮播 -(void)requestCycleViewData { NSString *urlString=[NSString stringWithFormat:@"%@/product/homeact",URL]; [KXHTTP post:urlString params:@{@"version":@"2"} success:^(id json) { NSArray *array=[NSArray yy_modelArrayWithClass:[KXCycleModel class] json:json[@"homeact"]]; _cycleView.scrollArray=array; } failure:^(NSError *error) { }]; } -(void)clickScrollViewItemWithIndex:(NSInteger)index { [MobClick event:@"bannerClick"]; KXCycleModel *model=_cycleView.scrollArray[index]; if (model.link.length == 0) { KXMainDetailViewController *detail=[[KXMainDetailViewController alloc]init]; detail.detailID=model.product_id.stringValue; detail.enterSource = @"banner"; [self.navigationController pushViewController:detail animated:YES]; }else{ KXBaseLinkViewController *baseLink = [[KXBaseLinkViewController alloc]init]; baseLink.linkString = model.link; [self.navigationController pushViewController:baseLink animated:YES]; } } -(void)createTabletableView { self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-TabbarHeight) style:UITableViewStylePlain]; if (@available(iOS 11.0, *)) { self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } else { } self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone; [self.tableView registerClass:[KXMainListTableViewCell class] forCellReuseIdentifier:@"main"]; self.tableView.tableHeaderView = _headerView; //下拉刷新 MJRefreshNormalHeader *header = [[MJRefreshNormalHeader alloc]init]; header.arrowView.image = nil; [header setRefreshingTarget:self refreshingAction:@selector(refreshHeader:)]; self.tableView.mj_header = header; [header beginRefreshing]; //上拉加载 MJRefreshAutoNormalFooter *footer=[[MJRefreshAutoNormalFooter alloc]init]; [footer setRefreshingTarget:self refreshingAction:@selector(refreshFooter:)]; self.tableView.mj_footer=footer; self.tableView.delegate = self; self.tableView.dataSource = self; [self.view addSubview:self.tableView]; header.stateLabel.font = FONT_SYS(14); header.stateLabel.textColor=[UIColor detailTitleColor]; header.lastUpdatedTimeLabel.font=FONT_SYS(14); header.lastUpdatedTimeLabel.textColor=[UIColor detailTitleColor]; footer.stateLabel.textColor=[UIColor detailTitleColor]; } #pragma mark 下拉刷新和上拉加载 -(void)refreshHeader:(MJRefreshHeader *)header { self.page=1; [self requestSourceWithPage:self.page]; } -(void)refreshFooter:(MJRefreshAutoFooter *)footer { self.page++; [self requestSourceWithPage:self.page]; } -(void)requestSourceWithPage:(NSInteger)page { NSString *urlString=[NSString stringWithFormat:@"%@/product/searchSecond",URL]; [KXHTTP post:urlString params:@{@"is_hot":@(YES),@"page":@(page)} success:^(id json) { NSArray *array=[NSArray yy_modelArrayWithClass:[KXHotList class] json:json[@"data"]]; if (self.page==1) { [self.dataSource removeAllObjects]; } for (KXHotList *model in array) { [self.dataSource addObject:model]; } if (array.count < 10) { [_tableView.mj_header endRefreshing]; [_tableView.mj_footer endRefreshing]; [_tableView.mj_footer endRefreshingWithNoMoreData]; }else{ [_tableView.mj_header endRefreshing]; [_tableView.mj_footer endRefreshing]; } [_tableView reloadData]; } failure:^(NSError *error) { }]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { KXHotList *model=self.dataSource[indexPath.row]; KXMainListTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"main"]; if (!cell) { cell=[[KXMainListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"main"]; } cell.selectionStyle=UITableViewCellSelectionStyleNone; // cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; cell.hotmodel=model; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 87*SCREEN_MUTI; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [MobClick event:@"homeListClick"]; KXHotList *model=self.dataSource[indexPath.row]; //0跳详情,1跳链接 if ([model.skip_type isEqualToString:@"1"]) { KXWebDetailViewController *web=[[KXWebDetailViewController alloc]init]; web.webStr=model.link; web.titleStr = model.name; web.product_id = model.Id; web.product_apply_id = @"0"; [self.navigationController pushViewController:web animated:YES]; }else{ KXMainDetailViewController *detail=[[KXMainDetailViewController alloc]init]; detail.detailID=model.Id; detail.titleString=model.name; detail.enterSource = @"hotList"; [self.navigationController pushViewController:detail animated:YES]; } } #pragma mark ------------------KXMainVerticleCycleViewDelegate------------------------ - (void)clickmiddleViewWithIndex:(NSInteger)index { [MobClick event:@"noticeClick"]; KXVerticleModel *model = self.noticeArray[index]; if (!model.link) { }else{ if ([model.skip_type isEqualToString:@"0"]) { if ([model.product_id isEqualToString:@"0"]) { KXBaseLinkViewController *baselink = [[KXBaseLinkViewController alloc]init]; baselink.linkString = model.link; [self.navigationController pushViewController:baselink animated:YES]; }else{ KXMainDetailViewController *detail=[[KXMainDetailViewController alloc]init]; detail.detailID=model.product_id; detail.enterSource = @"notice"; [self.navigationController pushViewController:detail animated:YES]; } }else{ KXWebDetailViewController *web=[[KXWebDetailViewController alloc]init]; web.product_apply_id = @"0"; web.product_id = model.product_id; web.webStr=model.link; [self.navigationController pushViewController:web animated:YES]; } } } #pragma mark - NAV的代理方法 - (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{ BOOL isShow = [viewController isKindOfClass:[self class]]; [self.navigationController setNavigationBarHidden:isShow animated:YES]; } -(NSMutableArray *)dataSource { if (!_dataSource) { _dataSource=[[NSMutableArray alloc]init]; } return _dataSource; } -(NSArray *)otherSource { if (!_otherSource) { _otherSource=@[@{@"image":@"main_all_quick",@"title":@"极速贷",@"desc":@"最快2小时到账"},@{@"image":@"main_all_hot",@"title":@"热门贷",@"desc":@"大热贷款平台推荐"},@{@"image":@"main_all_stu",@"title":@"大额贷",@"desc":@"高额度 低门槛"},@{@"image":@"main_all_house",@"title":@"新品区",@"desc":@"最新上架平台"}]; } return _otherSource; } - (NSMutableArray *)noticeArray { if (!_noticeArray) { _noticeArray = [NSMutableArray arrayWithCapacity:0]; } return _noticeArray; } - (NSMutableArray *)allArray { if (!_allArray) { _allArray = [[NSMutableArray alloc]init]; } return _allArray; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end