// // TFRechargeViewController.m // TFReader // // Created by 谢腾飞 on 2020/12/14. // Copyright © 2020 xtfei_2011@126.com. All rights reserved. // #import "TFRechargeViewController.h" #import "TFRecordsViewController.h" #import "TFRechargeViewCell.h" #import "TFMemberInstructionsViewCell.h" #import "TFRechargeHeaderView.h" #import "TFRechargeModel.h" #import "TFIAPManager.h" @interface TFRechargeViewController () { NSInteger goodsSelectIndex; // 商品选择位置 UILabel *priceLabel; UIButton *confirmButton; } @property (nonatomic ,strong) TFRechargeModel *rechargeModel; @property (nonatomic ,strong) TFRechargeHeaderView *headerView; @end @implementation TFRechargeViewController - (void)viewDidLoad { [super viewDidLoad]; [self initialize]; [self createSubviews]; [self netRequest]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self setStatusBarLightContentStyle]; [kNotification postNotificationName:Notification_Hidden_Tabbar object:@"1"]; } - (void)initialize { if (self.pushFromReader) { id target = self.navigationController.interactivePopGestureRecognizer.delegate; UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:target action:NSSelectorFromString(@"handleNavigationTransition:")]; panGesture.delegate = self; // 设置手势代理,拦截手势触发 [self.view addGestureRecognizer:panGesture]; // 一定要禁止系统自带的滑动手势 self.navigationController.interactivePopGestureRecognizer.enabled = NO; } [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequest) name:Notification_Recharge_Success object:nil]; [self setNavigationBarTitle:[NSString stringWithFormat:TFLocalizedString(@"%@充值"), TFSystemInfoManager.masterUnit]]; self.navigationBar.backgroundColor = kColorRGB(46, 46, 48); self.navigationBar.navTitleLabel.textColor = kWhiteColor; [self.navigationBar setLightLeftButton]; UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom]; rightButton.frame = CGRectMake(SCREEN_WIDTH - 64.0f - kHalfMargin, PUB_NAVBAR_HEIGHT - 2 - 40.0f, 64.0f, 40.0f); [rightButton setTitle:TFLocalizedString(@"充值记录") forState:UIControlStateNormal]; [rightButton setTitleColor:kWhiteColor forState:UIControlStateNormal]; [rightButton.titleLabel setFont:kFont12]; [rightButton addTarget:self action:@selector(rechargeRecordClick) forControlEvents:UIControlEventTouchUpInside]; [self.navigationBar addSubview:rightButton]; goodsSelectIndex = 0; [TFIAPManager sharedManager].delegate = self; self.view.backgroundColor = kGrayViewColor; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netRequest) name:Notification_Login_Success object:nil]; } - (void)createSubviews { self.mainTableViewGroup.delegate = self; self.mainTableViewGroup.dataSource = self; [self.view addSubview:self.mainTableViewGroup]; [self.mainTableViewGroup mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(0); make.top.mas_equalTo(0); make.width.mas_equalTo(SCREEN_WIDTH); make.height.mas_equalTo(SCREEN_HEIGHT - PUB_TABBAR_HEIGHT); }]; self.headerView = [[TFRechargeHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, PUB_NAVBAR_HEIGHT + 60 + 2 * kMargin)]; [self.mainTableViewGroup setTableHeaderView:self.headerView]; UIView *toolBar = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - PUB_TABBAR_HEIGHT, SCREEN_WIDTH, PUB_TABBAR_HEIGHT)]; toolBar.backgroundColor = kWhiteColor; toolBar.layer.shadowColor = [UIColor blackColor].CGColor; toolBar.layer.shadowOffset = CGSizeMake(0, 0); toolBar.layer.shadowOpacity = 0.1f; toolBar.layer.shadowRadius = 1.0f; [self.view addSubview:toolBar]; confirmButton = [UIButton buttonWithType:UIButtonTypeCustom]; confirmButton.adjustsImageWhenHighlighted = NO; [confirmButton setBackgroundImage:[UIImage imageNamed:@"member_pay_btn"] forState:UIControlStateNormal]; [confirmButton setTitle:TFLocalizedString(@"立即充值") forState:UIControlStateNormal]; [confirmButton setTitleColor:kColorRGBA(122, 70, 0, 1) forState:UIControlStateNormal]; [confirmButton.titleLabel setFont:[UIFont boldSystemFontOfSize:kFontSize14]]; [confirmButton addTarget:self action:@selector(confirmButtonClick) forControlEvents:UIControlEventTouchUpInside]; [toolBar addSubview:confirmButton]; [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) { make.right.mas_equalTo(toolBar.mas_right); make.top.mas_equalTo(0); make.width.mas_equalTo(150); make.height.mas_equalTo(PUB_TABBAR_HEIGHT - PUB_TABBAR_OFFSET); }]; priceLabel = [[UILabel alloc] init]; priceLabel.font = kMainFont; priceLabel.textColor = kBlackColor; [toolBar addSubview:priceLabel]; [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_equalTo(kHalfMargin); make.top.mas_equalTo(0); make.right.mas_equalTo(confirmButton.mas_left); make.height.mas_equalTo(confirmButton.mas_height); }]; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { return self.rechargeModel.list.count; } if (section == 1 && (self.rechargeModel.about.count <= 0 || !self.rechargeModel.about)) { return 0; } return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case 0: { TFRechargeViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TFRechargeViewCell"]; if (!cell) { cell = [[TFRechargeViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFRechargeViewCell"]; } cell.goodsModel = [self.rechargeModel.list objectAtIndex:indexPath.row]; cell.cellSelected = (goodsSelectIndex == indexPath.row); cell.selectionStyle = UITableViewCellAccessoryNone; return cell; } break; case 1: { TFMemberInstructionsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TFMemberInstructionsViewCell"]; if (!cell) { cell = [[TFMemberInstructionsViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"TFMemberInstructionsViewCell"]; } cell.about = self.rechargeModel.about; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } break; default: break; } return [[UITableViewCell alloc] init]; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { goodsSelectIndex = indexPath.row; [self.mainTableViewGroup reloadData]; } } //section头间距 - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 1 && (self.rechargeModel.about.count <= 0 || !self.rechargeModel.about)) { return CGFLOAT_MIN; } return 50; } //section头视图 - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (!self.rechargeModel) { return [[UIView alloc] init]; } UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)]; view.backgroundColor = [UIColor whiteColor]; UILabel *headerTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kHalfMargin, 0, SCREEN_WIDTH - kHalfMargin, 50)]; if (section == 0) { headerTitleLabel.text = TFLocalizedString(@"充值套餐"); } else if (section == 1) { headerTitleLabel.text = TFLocalizedString(@"充值说明"); } headerTitleLabel.font = kBoldFont15; headerTitleLabel.textColor = kBlackColor; [view addSubview:headerTitleLabel]; NSMutableAttributedString *priceString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@", TFLocalizedString(@"总计:"), [self.rechargeModel.list objectAtIndex:goodsSelectIndex].fat_price?:@""]]; NSString *str = [self.rechargeModel.list objectAtIndex:goodsSelectIndex].fat_price?:@""; [priceString addAttribute:NSFontAttributeName value:kBoldFont22 range:NSMakeRange(priceString.length - str.length, str.length)]; [priceString addAttribute:NSForegroundColorAttributeName value:kColorRGB(228, 185, 122) range:NSMakeRange(priceString.length - str.length, str.length)]; priceLabel.attributedText = priceString; return view; } //section底部间距 - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { if (section == 1 && (self.rechargeModel.about.count <= 0 || !self.rechargeModel.about)) { return CGFLOAT_MIN; } return kHalfMargin; } //section底部视图 - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { if (!self.rechargeModel) { return [[UIView alloc] init]; } UIView *view = [[UIView alloc] init]; view.backgroundColor = kGrayViewColor; return view; } #pragma mark IApRequestResultsDelegate - (void)requestSuccess { dispatch_async(dispatch_get_main_queue(), ^{ self.mainTableViewGroup.userInteractionEnabled = YES; }); } - (void)filedWithErrorCode:(NSInteger)errorCode andError:(NSString *)error { dispatch_async(dispatch_get_main_queue(), ^{ self.mainTableViewGroup.userInteractionEnabled = YES; }); } - (void)rechargeRecordClick { TFRecordsViewController *vc = [[TFRecordsViewController alloc] init]; [self.navigationController pushViewController:vc animated:YES]; } - (void)confirmButtonClick { if (!TFUserInfoManager.isLogin) { [TFLoginOptionsViewController presentLoginView:nil]; return; } if (self.rechargeModel.list.count > 0) { TFGoodsModel *goodsModel = [self.rechargeModel.list objectOrNilAtIndex:goodsSelectIndex]; self.mainTableViewGroup.userInteractionEnabled = NO; [TFIAPManager sharedManager].production_id = self.production_id; [TFIAPManager sharedManager].productionType = self.productionType; [[TFIAPManager sharedManager] requestProductWithId:[NSString stringWithFormat:@"%@", goodsModel.apple_id]]; } } - (void)netRequest { WS(weakSelf) [TFNetworkTools POST:Pay_Center parameters:nil model:TFRechargeModel.class success:^(BOOL isSuccess, TFRechargeModel *_Nullable t_model, TFNetworkRequestModel *requestModel) { if (isSuccess) { weakSelf.rechargeModel = t_model; weakSelf.headerView.rechargeModel = t_model; } [weakSelf.mainTableViewGroup reloadData]; } failure:nil]; } @end