// // RQPayViewController.m // jiaPei // // Created by 张嵘 on 2022/6/7. // Copyright © 2022 JCZ. All rights reserved. // #import "RQPayViewController.h" @interface RQPayViewController () /// viewModel @property (nonatomic, readonly, strong) RQPayViewModel *viewModel; @property (nonatomic, readwrite, strong) RQPayAlertHeaderView *payAlertHeaderView; @property (nonatomic, readwrite, strong) RQPayAlertFooterView *payAlertFooterView; @property (nonatomic, readwrite, assign) NSInteger selectedItemIndex; @end @implementation RQPayViewController @dynamic viewModel; #pragma mark - SystemMethod - (void)viewDidLoad { [super viewDidLoad]; /// 初始化 [self rq_setup]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; /// 当前的分组不在可视区域内,则滚动到可视区域(只对单选有效) if (![self.tableView qmui_cellVisibleAtIndexPath:[NSIndexPath indexPathForRow:self.selectedItemIndex inSection:0]]) { [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedItemIndex inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:animated]; } } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; self.tableView.frame = CGRectMake(0, 57, RQ_SCREEN_WIDTH, [self customHeight] - 57 - RQ_FIT_HORIZONTAL(76.f)); self.payAlertHeaderView.frame = CGRectMake(0, 0, RQ_SCREEN_WIDTH, 57); self.payAlertFooterView.frame = CGRectMake(0, [self customHeight] - RQ_FIT_HORIZONTAL(76.f), RQ_SCREEN_WIDTH, RQ_FIT_HORIZONTAL(76.f)); } #pragma mark - PrivateMethods /// 初始化 - (void)rq_setup { /// set up ... // self.tableView.tableHeaderView = self.payAlertHeaderView; // self.tableView.tableFooterView = self.payAlertFooterView; self.selectedItemIndex = 0; [self.view addSubview:self.payAlertHeaderView]; [self.view addSubview:self.payAlertFooterView]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.selectedItemIndex inSection:0]; [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone]; if ([self.tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:indexPath];} [self initPayNoti]; } - (void)initPayNoti { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ailyPayOrWechatSuccessAction) name:RQUserNotificationAlipayOrWechatSuccess object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ailyPayOrWechatFailAction) name:RQUserNotificationAlipayOrWechatFail object:nil]; } // 支付成功 - (void)ailyPayOrWechatSuccessAction { [self.navigationController dismissViewControllerAnimated:YES completion:NULL]; } // 支付失败 - (void)ailyPayOrWechatFailAction { NSLog(@"处理支付失败结果"); } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } #pragma mark - OverrideMethods - (UIEdgeInsets)contentInset { return UIEdgeInsetsMake(0, 0, 0, 0); } - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath { RQPayTypeCell *cell = [RQPayTypeCell cellWithTableView:tableView]; cell.chooseBtn.selected = (self.selectedItemIndex == indexPath.row); return cell; } - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object { RQPayTypeCell *payTypeCell = (RQPayTypeCell *)cell; RQPayTypeItemViewModel *itemViewModel = object; [payTypeCell bindViewModel:itemViewModel]; } - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { RQPayPriceHeaderView *payPriceHeaderView = [RQPayPriceHeaderView headerViewWithTableView:tableView]; RQPayPriceHeaderGroupViewModel *groupViewModel = self.viewModel.dataSource[section]; [payPriceHeaderView bindViewModel:groupViewModel]; return payPriceHeaderView; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { /// 单选情况下如果重复选中已被选中的cell,则什么都不做 if (self.selectedItemIndex == indexPath.row) { return; } /// 选中新的cell时,先反选之前被选中的那个cell NSIndexPath *selectedIndexPathBefore = [NSIndexPath indexPathForRow:self.selectedItemIndex inSection:0]; // [self.tableView deselectRowAtIndexPath:selectedIndexPathBefore animated:YES]; self.selectedItemIndex = indexPath.row; BOOL isSelectedIndexPathBeforeVisible = [tableView qmui_cellVisibleAtIndexPath:selectedIndexPathBefore]; /// 如果之前被选中的那个cell也在可视区域里,则也要用动画去刷新它,否则只需要用动画刷新当前已选中的cell即可,之前被选中的那个交给cellForRow去刷新 if (isSelectedIndexPathBeforeVisible) { [tableView reloadRowsAtIndexPaths:@[selectedIndexPathBefore, indexPath] withRowAnimation:UITableViewRowAnimationFade]; } else { [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; } } #pragma mark - HWPanModalPresentable - (UIScrollView *)panScrollable { return self.tableView; } - (PanModalHeight)longFormHeight { return PanModalHeightMake(PanModalHeightTypeContent, [self customHeight]); } - (CGFloat)cornerRadius { return 0.0; } - (BOOL)allowsTapBackgroundToDismiss { return NO; } - (BOOL)showDragIndicator { return NO; } #pragma mark - LazyLoad - (RQPayAlertHeaderView *)payAlertHeaderView { @weakify(self) if (!_payAlertHeaderView) { _payAlertHeaderView = [RQPayAlertHeaderView payAlertHeaderView]; [_payAlertHeaderView.closeBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { @strongify(self) [self dismissViewControllerAnimated:YES completion:nil]; }]; } return _payAlertHeaderView; } - (RQPayAlertFooterView *)payAlertFooterView { @weakify(self) if (!_payAlertFooterView) { _payAlertFooterView = [RQPayAlertFooterView payAlertFooterView]; [_payAlertFooterView.payBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { @strongify(self) switch (self.selectedItemIndex) { /// 微信支付 case 0: [MBProgressHUD rq_showProgressHUD:@"创建订单..."]; [[RQ_HTTP_Service postPrepareOrderWithDictCode:self.viewModel.typeModel.dictCode userId:RQ_USER_MANAGER.currentUser._id] subscribeNext:^(RQPrepareOrderModel *prepareOrderModel) { [MBProgressHUD rq_hideHUD]; // 发起微信支付,设置参数 //需要创建这个支付对象 PayReq *req = [[PayReq alloc] init]; //由用户微信号和AppID组成的唯一标识,用于校验微信用户 req.openID = RQ_WECHAT_MANAGER.appID; // 商家id,在注册的时候给的 req.partnerId = prepareOrderModel.partnerId; // 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你 req.prepayId = prepareOrderModel.prepayId; // 根据财付通文档填写的数据和签名 req.package = prepareOrderModel.packageValue; // 随机编码,为了防止重复的,在后台生成 req.nonceStr = prepareOrderModel.nonceStr; // 这个是时间戳,也是在后台生成的,为了验证支付的 NSString * stamp = prepareOrderModel.timeStamp; req.timeStamp = (UInt32)stamp.intValue; // 这个签名也是后台做的 req.sign = prepareOrderModel.sign; [RQWechatManager hangleWechatPayWith:req]; } error:^(NSError * _Nullable error) { [MBProgressHUD rq_hideHUD]; [MBProgressHUD rq_showErrorTips:error]; }]; break; default: break; } }]; } return _payAlertFooterView; } - (CGFloat)customHeight { RQPayPriceHeaderGroupViewModel *groupViewModel = self.viewModel.dataSource.firstObject; CGFloat height = (57 + RQ_FIT_HORIZONTAL(76.f) + RQ_FIT_HORIZONTAL(86.f) + (RQ_FIT_HORIZONTAL(60.0f) * [groupViewModel.itemViewModels count])); CGFloat limitHeight = (RQ_SCREEN_HEIGHT * (2.f / 3.f)); return (height > limitHeight)? limitHeight : height; } @end