// // FKVipBuyController.m // FirstLink // // Created by ascii on 2017/5/17. // Copyright © 2017年 FirstLink. All rights reserved. // #import "FKVipBuyController.h" #import "FKVipBuyRequest.h" #import "FKVipBuyReform.h" #import "FKVipBuyViewModel.h" #import "FKVipBuyPriceCell.h" #import "FKVipPrivilegeDetailCell.h" #import "FKVipPayController.h" @interface FKVipBuyController () @property (nonatomic, strong) FKCardSegmentView *cardSegmentView; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) FKVipBuyViewModel *viewModel; @end @implementation FKVipBuyController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = UIColorFromRGB(0xf4f4f4); self.navigationItem.title = @"我的会员"; [self setupViews]; [self requestBuyRule]; [self.cardSegmentView setCardSegmentIndex:self.segmentType]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.navigationController setNavigationBarHidden:NO animated:YES]; } /* #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. } */ #pragma mark - Request - (void)requestBuyRule { [FKVipBuyRequest requestVipBuyRulesWithDelegate:self]; } #pragma mark - Response - (void)networkDidReceiveError:(NSError*)error identify:(int)identify header:(MSGHeader*)header{ [self.hudView hide:NO]; [FLProgressHUDHelper showText:header.msg inView:self.view]; } - (void)networkDidSuccessResponse:(NSDictionary*)response identify:(int)identify header:(MSGHeader*)header{ [self.hudView hide:NO]; if (header.code.intValue == RESPONSE_MSG_NORMAL) { if (identify == VIP_BUY_RULES_REQ) { self.viewModel.ruleItemArray = [FKVipBuyReform parserVIPPrice:response]; [self.viewModel groupedRuleItem]; [self.tableView reloadData]; } else if (identify == VIP_BUY_ORDER) { FKVipPayController *controller = [[FKVipPayController alloc] init]; controller.payItem = [FKVipBuyReform parserForPaymentItem:response]; controller.delegate = self; [self.navigationController pushViewController:controller animated:YES]; } } else { [FLProgressHUDHelper showText:header.msg inView:self.view]; } } #pragma mark - UITableViewDelegate, UITableViewDataSource - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 2; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (section == 0) { if (self.segmentType == kCardSegmentIndexLeft) { return self.viewModel.vipRuleItemArray.count; } else { return self.viewModel.superVipRuleItemArray.count; } } return 1; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { if (section == 0) { return 48; } return 10; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { if (section == 0) { return self.cardSegmentView; } return nil; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { return 75; } return 350; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 0) { FKVipBuyPriceCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKVipBuyPriceCell class])]; [cell.actionBtn addTarget:self action:@selector(clickVipBuyActionBtn:) forControlEvents:UIControlEventTouchUpInside]; cell.actionBtn.tag = indexPath.row; cell.horizonLine.hidden = (indexPath.row == 0); FKVipBuyRuleItem *item = nil; if (self.segmentType == kCardSegmentIndexLeft) { item = self.viewModel.vipRuleItemArray[indexPath.row]; } else { item = self.viewModel.superVipRuleItemArray[indexPath.row]; } if (item) { NSString *priceTip = item.title; NSString *price = item.price; if (priceTip.length > 0 && price.floatValue > 0) { NSString *string = [NSString stringWithFormat:@"%@: ¥%.2f", priceTip, price.floatValue/100.0]; cell.priceLabel.text = string; } } return cell; } else { FKVipPrivilegeDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([FKVipPrivilegeDetailCell class])]; if (self.segmentType == kCardSegmentIndexLeft) { [cell reloadData:self.viewModel.vipRuleItemArray]; } else { [cell reloadData:self.viewModel.superVipRuleItemArray]; } return cell; } } #pragma mark - FKCardSegmentViewDelegate - (void)cardSegmentView:(FKCardSegmentView *)view didSelectWithVipIndex:(kCardSegmentIndex)vip { self.segmentType = vip; [self.tableView reloadData]; } #pragma mark - vipPayDelegate - (void)vipPaySuccess { [self.navigationController popToViewController:self animated:YES]; } - (void)vipPayFail { [self.navigationController popToViewController:self animated:YES]; [FLProgressHUDHelper showTipAlert:@"VIP购买失败"]; } #pragma mark - Action - (IBAction)clickVipBuyActionBtn:(UIButton *)sender { FKVipBuyRuleItem *item = nil; if (self.segmentType == kCardSegmentIndexLeft) { item = self.viewModel.vipRuleItemArray[sender.tag]; } else { item = self.viewModel.superVipRuleItemArray[sender.tag]; } if (item) { [FKVipBuyRequest requestOrderWithBuyId:item.itemID.integerValue delegate:self]; } } #pragma mark - Method - (void)setupViews { [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.top.right.bottom.equalTo(self.view); }]; } #pragma mark - Property - (FKCardSegmentView *)cardSegmentView { if (!_cardSegmentView) { _cardSegmentView = [[FKCardSegmentView alloc] initWithFrame:CGRectMake(0, 0, UISCREENWIDTH, 48)]; _cardSegmentView.delegate = self; } return _cardSegmentView; } - (UITableView *)tableView{ if (_tableView == nil) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.sectionFooterHeight = 0; _tableView.contentInset = UIEdgeInsetsMake(6, 0, 0, 0); [_tableView registerClass:[FKVipBuyPriceCell class] forCellReuseIdentifier:NSStringFromClass([FKVipBuyPriceCell class])]; [_tableView registerClass:[FKVipPrivilegeDetailCell class] forCellReuseIdentifier:NSStringFromClass([FKVipPrivilegeDetailCell class])]; if (@available(iOS 11.0, *)) { _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } } return _tableView; } - (FKVipBuyViewModel *)viewModel { if (!_viewModel) { _viewModel = [FKVipBuyViewModel new]; } return _viewModel; } @end