RQPayViewController.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // RQPayViewController.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2022/6/7.
  6. // Copyright © 2022 JCZ. All rights reserved.
  7. //
  8. #import "RQPayViewController.h"
  9. @interface RQPayViewController ()
  10. /// viewModel
  11. @property (nonatomic, readonly, strong) RQPayViewModel *viewModel;
  12. @property (nonatomic, readwrite, strong) RQPayAlertHeaderView *payAlertHeaderView;
  13. @property (nonatomic, readwrite, strong) RQPayAlertFooterView *payAlertFooterView;
  14. @property (nonatomic, readwrite, assign) NSInteger selectedItemIndex;
  15. @end
  16. @implementation RQPayViewController
  17. @dynamic viewModel;
  18. #pragma mark - SystemMethod
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. /// 初始化
  22. [self rq_setup];
  23. }
  24. - (void)viewDidAppear:(BOOL)animated {
  25. [super viewDidAppear:animated];
  26. /// 当前的分组不在可视区域内,则滚动到可视区域(只对单选有效)
  27. if (![self.tableView qmui_cellVisibleAtIndexPath:[NSIndexPath indexPathForRow:self.selectedItemIndex inSection:0]]) {
  28. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedItemIndex inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:animated];
  29. }
  30. }
  31. - (void)viewDidLayoutSubviews {
  32. [super viewDidLayoutSubviews];
  33. self.tableView.frame = CGRectMake(0, 57, RQ_SCREEN_WIDTH, [self customHeight] - 57 - RQ_FIT_HORIZONTAL(76.f));
  34. self.payAlertHeaderView.frame = CGRectMake(0, 0, RQ_SCREEN_WIDTH, 57);
  35. self.payAlertFooterView.frame = CGRectMake(0, [self customHeight] - RQ_FIT_HORIZONTAL(76.f), RQ_SCREEN_WIDTH, RQ_FIT_HORIZONTAL(76.f));
  36. }
  37. #pragma mark - PrivateMethods
  38. /// 初始化
  39. - (void)rq_setup {
  40. /// set up ...
  41. // self.tableView.tableHeaderView = self.payAlertHeaderView;
  42. // self.tableView.tableFooterView = self.payAlertFooterView;
  43. self.selectedItemIndex = 0;
  44. [self.view addSubview:self.payAlertHeaderView];
  45. [self.view addSubview:self.payAlertFooterView];
  46. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.selectedItemIndex inSection:0];
  47. [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
  48. if ([self.tableView.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)]) {[self.tableView.delegate tableView:self.tableView didSelectRowAtIndexPath:indexPath];}
  49. [self initPayNoti];
  50. }
  51. - (void)initPayNoti {
  52. [[NSNotificationCenter defaultCenter] addObserver:self
  53. selector:@selector(ailyPayOrWechatSuccessAction)
  54. name:RQUserNotificationAlipayOrWechatSuccess
  55. object:nil];
  56. [[NSNotificationCenter defaultCenter] addObserver:self
  57. selector:@selector(ailyPayOrWechatFailAction)
  58. name:RQUserNotificationAlipayOrWechatFail
  59. object:nil];
  60. }
  61. // 支付成功
  62. - (void)ailyPayOrWechatSuccessAction {
  63. [self.navigationController dismissViewControllerAnimated:YES completion:NULL];
  64. }
  65. // 支付失败
  66. - (void)ailyPayOrWechatFailAction {
  67. NSLog(@"处理支付失败结果");
  68. }
  69. - (void)dealloc {
  70. [[NSNotificationCenter defaultCenter] removeObserver:self];
  71. }
  72. #pragma mark - OverrideMethods
  73. - (UIEdgeInsets)contentInset {
  74. return UIEdgeInsetsMake(0, 0, 0, 0);
  75. }
  76. - (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
  77. RQPayTypeCell *cell = [RQPayTypeCell cellWithTableView:tableView];
  78. cell.chooseBtn.selected = (self.selectedItemIndex == indexPath.row);
  79. return cell;
  80. }
  81. - (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object {
  82. RQPayTypeCell *payTypeCell = (RQPayTypeCell *)cell;
  83. RQPayTypeItemViewModel *itemViewModel = object;
  84. [payTypeCell bindViewModel:itemViewModel];
  85. }
  86. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  87. RQPayPriceHeaderView *payPriceHeaderView = [RQPayPriceHeaderView headerViewWithTableView:tableView];
  88. RQPayPriceHeaderGroupViewModel *groupViewModel = self.viewModel.dataSource[section];
  89. [payPriceHeaderView bindViewModel:groupViewModel];
  90. return payPriceHeaderView;
  91. }
  92. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  93. /// 单选情况下如果重复选中已被选中的cell,则什么都不做
  94. if (self.selectedItemIndex == indexPath.row) {
  95. return;
  96. }
  97. /// 选中新的cell时,先反选之前被选中的那个cell
  98. NSIndexPath *selectedIndexPathBefore = [NSIndexPath indexPathForRow:self.selectedItemIndex inSection:0];
  99. // [self.tableView deselectRowAtIndexPath:selectedIndexPathBefore animated:YES];
  100. self.selectedItemIndex = indexPath.row;
  101. BOOL isSelectedIndexPathBeforeVisible = [tableView qmui_cellVisibleAtIndexPath:selectedIndexPathBefore];
  102. /// 如果之前被选中的那个cell也在可视区域里,则也要用动画去刷新它,否则只需要用动画刷新当前已选中的cell即可,之前被选中的那个交给cellForRow去刷新
  103. if (isSelectedIndexPathBeforeVisible) {
  104. [tableView reloadRowsAtIndexPaths:@[selectedIndexPathBefore, indexPath] withRowAnimation:UITableViewRowAnimationFade];
  105. } else {
  106. [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
  107. }
  108. }
  109. #pragma mark - HWPanModalPresentable
  110. - (UIScrollView *)panScrollable {
  111. return self.tableView;
  112. }
  113. - (PanModalHeight)longFormHeight {
  114. return PanModalHeightMake(PanModalHeightTypeContent, [self customHeight]);
  115. }
  116. - (CGFloat)cornerRadius {
  117. return 0.0;
  118. }
  119. - (BOOL)allowsTapBackgroundToDismiss {
  120. return NO;
  121. }
  122. - (BOOL)showDragIndicator {
  123. return NO;
  124. }
  125. #pragma mark - LazyLoad
  126. - (RQPayAlertHeaderView *)payAlertHeaderView {
  127. @weakify(self)
  128. if (!_payAlertHeaderView) {
  129. _payAlertHeaderView = [RQPayAlertHeaderView payAlertHeaderView];
  130. [_payAlertHeaderView.closeBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  131. @strongify(self)
  132. [self dismissViewControllerAnimated:YES completion:nil];
  133. }];
  134. }
  135. return _payAlertHeaderView;
  136. }
  137. - (RQPayAlertFooterView *)payAlertFooterView {
  138. @weakify(self)
  139. if (!_payAlertFooterView) {
  140. _payAlertFooterView = [RQPayAlertFooterView payAlertFooterView];
  141. [_payAlertFooterView.payBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) {
  142. @strongify(self)
  143. switch (self.selectedItemIndex) {
  144. /// 微信支付
  145. case 0:
  146. [MBProgressHUD rq_showProgressHUD:@"创建订单..."];
  147. [[RQ_HTTP_Service postPrepareOrderWithDictCode:self.viewModel.typeModel.dictCode userId:RQ_USER_MANAGER.currentUser._id] subscribeNext:^(RQPrepareOrderModel *prepareOrderModel) {
  148. [MBProgressHUD rq_hideHUD];
  149. // 发起微信支付,设置参数
  150. //需要创建这个支付对象
  151. PayReq *req = [[PayReq alloc] init];
  152. //由用户微信号和AppID组成的唯一标识,用于校验微信用户
  153. req.openID = RQ_WECHAT_MANAGER.appID;
  154. // 商家id,在注册的时候给的
  155. req.partnerId = prepareOrderModel.partnerId;
  156. // 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你
  157. req.prepayId = prepareOrderModel.prepayId;
  158. // 根据财付通文档填写的数据和签名
  159. req.package = prepareOrderModel.packageValue;
  160. // 随机编码,为了防止重复的,在后台生成
  161. req.nonceStr = prepareOrderModel.nonceStr;
  162. // 这个是时间戳,也是在后台生成的,为了验证支付的
  163. NSString * stamp = prepareOrderModel.timeStamp;
  164. req.timeStamp = (UInt32)stamp.intValue;
  165. // 这个签名也是后台做的
  166. req.sign = prepareOrderModel.sign;
  167. [RQWechatManager hangleWechatPayWith:req];
  168. } error:^(NSError * _Nullable error) {
  169. [MBProgressHUD rq_hideHUD];
  170. [MBProgressHUD rq_showErrorTips:error];
  171. }];
  172. break;
  173. default:
  174. break;
  175. }
  176. }];
  177. }
  178. return _payAlertFooterView;
  179. }
  180. - (CGFloat)customHeight {
  181. RQPayPriceHeaderGroupViewModel *groupViewModel = self.viewModel.dataSource.firstObject;
  182. CGFloat height = (57 + RQ_FIT_HORIZONTAL(76.f) + RQ_FIT_HORIZONTAL(86.f) + (RQ_FIT_HORIZONTAL(60.0f) * [groupViewModel.itemViewModels count]));
  183. CGFloat limitHeight = (RQ_SCREEN_HEIGHT * (2.f / 3.f));
  184. return (height > limitHeight)? limitHeight : height;
  185. }
  186. @end