// // RQVipViewController.m // jiaPei // // Created by 张嵘 on 2022/3/1. // Copyright © 2022 JCZ. All rights reserved. // #import "RQVipViewController.h" #import "RQWebViewModel.h" #import "NYWPViewModel.h" @interface RQVipViewController () /// viewModel @property (nonatomic, readonly, strong) RQVipViewModel *viewModel; @property (nonatomic, readwrite, strong) RQVipBuyView *vipBuyView; @property (nonatomic, readwrite, strong) RQVipBuyNewView *vipBuyNewView; @property (nonatomic, readwrite, strong) RQTypeModel *chooseTypeModel; @property (nonatomic, readwrite, strong) IAPManager *iapManager; @property (nonatomic, strong) NYWPViewModel *wpviewModel; @end @implementation RQVipViewController @dynamic viewModel; #pragma mark - SystemMethod - (void)viewDidLoad { [super viewDidLoad]; /// 初始化 [self rq_setup]; [self.wpviewModel.wView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) { NSLog(@"navigator.userAgent.result is ++++ %@", result); }]; [self.wpviewModel wkvPL:NYPiapph]; } - (void)dealloc { self.wpviewModel = nil; RQ_VIP_Module.chooseTypeModel = nil; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)viewDidLayoutSubviews { [super viewDidLayoutSubviews]; CGFloat customHeight = (self.viewModel.vipPageType == RQVIPPageType_Alert)? self.view.rq_height : RQ_SCREEN_HEIGHT; self.collectionView.size = CGSizeMake(RQ_SCREEN_WIDTH, self.view.rq_height - RQ_FIT_HORIZONTAL(110.f) - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT); // self.vipBuyView.frame = CGRectMake(0, customHeight - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT - RQ_FIT_HORIZONTAL(93.f), RQ_SCREEN_WIDTH, RQ_FIT_HORIZONTAL(93.f) + RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT); self.vipBuyNewView.frame = CGRectMake(0, customHeight - RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT - RQ_FIT_HORIZONTAL(110.f), RQ_SCREEN_WIDTH, RQ_FIT_HORIZONTAL(110.f) + RQ_APPLICATION_SAFEAREA_BOTTOM_HEIGHT); } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; /// 清除缓存 NSSet *websiteDataTypes = [WKWebsiteDataStore allWebsiteDataTypes]; NSDate *dateFrom = [NSDate dateWithTimeIntervalSince1970:0]; [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:websiteDataTypes modifiedSince:dateFrom completionHandler:^{ }]; } #pragma mark - PrivateMethods /// 初始化 - (void)rq_setup { /// set up ... @weakify(self) self.collectionView.bounces = NO; self.flowLayout.sectionHeadersPinToVisibleBounds = (self.viewModel.vipPageType == RQVIPPageType_Alert); // [self.collectionView setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageWithGradualChangingColor:^(QQGradualChangingColor *graColor) { // @strongify(self) // graColor.fromColor = (self.viewModel.vipPageType == RQVIPPageType_Alert)? [UIColor qmui_colorWithHexString:@"#FFFFFF"] : [UIColor qmui_colorWithHexString:@"#2C2D3C"]; // graColor.toColor = (self.viewModel.vipPageType == RQVIPPageType_Alert)? [UIColor qmui_colorWithHexString:@"#FEEEDF"] : [UIColor qmui_colorWithHexString:@"#2C2D3C"]; // graColor.type = QQGradualChangeTypeLeftToRight; // } size:CGSizeMake(RQ_SCREEN_WIDTH, self.view.rq_height) cornerRadius:QQRadiusZero]]]; self.collectionView.backgroundColor = RQ_MAIN_TEXT_COLOR_WHITE; // [self.view addSubview:self.vipBuyView]; [self.view addSubview:self.vipBuyNewView]; [[[RACObserve(self.viewModel, dataSource) distinctUntilChanged] deliverOnMainThread] subscribeNext:^(id _Nullable x) { @strongify(self) if (self.viewModel.dataSource.count > 0) { RQVipHeaderImageViewNewGroupViewModel *vipHeaderImageViewGroupViewModel = self.viewModel.dataSource.firstObject; if (vipHeaderImageViewGroupViewModel.itemViewModels.count > 0) { NSArray *currentArr = [vipHeaderImageViewGroupViewModel.itemViewModels.rac_sequence filter:^BOOL(RQVipSinglePriceItemViewModel *vipSinglePriceItemViewModel) { return ![vipSinglePriceItemViewModel.typeModel.dictLabel containsString:@"理论实操计时"]; }].array; RQVipSinglePriceItemViewModel *vipSinglePriceItemViewModel; if (currentArr.count > 0) { vipSinglePriceItemViewModel = currentArr.firstObject; } else { vipSinglePriceItemViewModel = vipHeaderImageViewGroupViewModel.itemViewModels.firstObject; } if (RQObjectIsNil(RQ_VIP_Module.chooseTypeModel)) { RQ_VIP_Module.chooseTypeModel = vipSinglePriceItemViewModel.typeModel; } else { } } } }]; [self initPayNoti]; [self.vipBuyView.buyBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { @strongify(self) if (!self.vipBuyView.userAgreeBtn.selected) { [[RACScheduler mainThreadScheduler] schedule:^{ [NSObject rq_showAlertViewWithTitle:@"温馨提示" message:@"请阅读用户协议!才能开通会员哦!" confirmTitle:@"立即阅读" cancelTitle:@"取消" confirmAction:^{ @strongify(self) self.vipBuyView.userAgreeBtn.selected = YES; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://ys.zzxcx.net/jsjp-sf.html"]]; RQWebViewModel *viewModel = [[RQWebViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{RQViewModelRequestKey : request,RQViewModelWebViewTypeKey:@(RQWebViewType_VIP)}]; RQWebViewViewController *viewController = [[RQWebViewViewController alloc] initWithViewModel:viewModel]; RQBaseNavigationController *nav = [[RQBaseNavigationController alloc] initWithRootViewController:viewController]; [RQControllerHelper.currentViewController presentViewController:nav animated:YES completion:nil]; } cancelAction:nil]; }]; return; } if (RQObjectIsNil(RQ_VIP_Module.chooseTypeModel)) { [MBProgressHUD rq_showTips:@"暂无可用VIP套餐"]; } else { [self.wpviewModel wwvPLJS:RQ_VIP_Module.chooseTypeModel.dictCode]; } }]; [self.vipBuyNewView.buyBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { @strongify(self) if (!self.vipBuyNewView.userAgreeBtn.selected) { [[RACScheduler mainThreadScheduler] schedule:^{ [NSObject rq_showAlertViewWithTitle:@"温馨提示" message:@"请阅读用户协议!才能开通会员哦!" confirmTitle:@"立即阅读" cancelTitle:@"取消" confirmAction:^{ @strongify(self) self.vipBuyNewView.userAgreeBtn.selected = YES; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://ys.zzxcx.net/jsjp-sf.html"]]; RQWebViewModel *viewModel = [[RQWebViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{RQViewModelRequestKey : request,RQViewModelWebViewTypeKey:@(RQWebViewType_VIP)}]; RQWebViewViewController *viewController = [[RQWebViewViewController alloc] initWithViewModel:viewModel]; RQBaseNavigationController *nav = [[RQBaseNavigationController alloc] initWithRootViewController:viewController]; [RQControllerHelper.currentViewController presentViewController:nav animated:YES completion:nil]; } cancelAction:nil]; }]; return; } if (RQObjectIsNil(RQ_VIP_Module.chooseTypeModel)) { [MBProgressHUD rq_showTips:@"暂无可用VIP套餐"]; } else { [self.wpviewModel wwvPLJS:RQ_VIP_Module.chooseTypeModel.dictCode]; } }]; } - (void)initPayNoti { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ailyPayOrWechatSuccessAction) name:RQUserNotificationOrWechatSuccess object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ailyPayOrWechatFailAction) name:RQUserNotificationOrWechatFail object:nil]; } - (void)ailyPayOrWechatSuccessAction { @weakify(self) RQVideoVipModel *historyVideoVipModel = RQ_VIP_Module.videoVipModel; [MBProgressHUD rq_showProgressHUD:@"更新VIP信息..."]; NSLog(@"处理支付成功结果"); [[[RACSignal createSignal:^RACDisposable *(id subscriber) { [subscriber sendNext:@"延时3.f秒"]; return nil; }] delay:3.f] subscribeNext:^(id x) { [RQ_VIP_Module isVipWithSubject:0 complete:^(BOOL isVip) { @strongify(self) [MBProgressHUD rq_hideHUD]; if ([RQ_VIP_Module.chooseTypeModel.dictLabel containsString:@"科一"]) { if (RQStringIsNotEmpty(historyVideoVipModel.subject1)) { /// 续费 if ([historyVideoVipModel.subject1 isEqualToString:RQ_VIP_Module.videoVipModel.subject1]) { /// 续费未查询到结果 [self checkVipFail]; } else { [RQ_ALERTVIEW_MANAGER showAlertWithTitle:@"温馨提示" message:@"您已续费成功!" confirmTitle:@"确定" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) { @strongify(self) // if (self.payViewController) { // [self.payViewController dismissViewControllerAnimated:YES completion:nil]; // } }]; } } else { /// 购买 if (RQStringIsEmpty(RQ_VIP_Module.videoVipModel.subject1)) { /// 购买未查询到结果 [self checkVipFail]; } else { [RQ_ALERTVIEW_MANAGER showAlertWithTitle:@"温馨提示" message:@"您已购买成功!" confirmTitle:@"确定" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) { @strongify(self) // if (self.payViewController) { // [self.payViewController dismissViewControllerAnimated:YES completion:nil]; // } }]; } } } else if ([RQ_VIP_Module.chooseTypeModel.dictLabel containsString:@"科二"]) { if (RQStringIsNotEmpty(historyVideoVipModel.subject2)) { /// 续费 if ([historyVideoVipModel.subject2 isEqualToString:RQ_VIP_Module.videoVipModel.subject2]) { /// 续费未查询到结果 [self checkVipFail]; } else { [RQ_ALERTVIEW_MANAGER showAlertWithTitle:@"温馨提示" message:@"您已续费成功!" confirmTitle:@"确定" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) { @strongify(self) // if (self.payViewController) { // [self.payViewController dismissViewControllerAnimated:YES completion:nil]; // } }]; } } else { /// 购买 if (RQStringIsEmpty(RQ_VIP_Module.videoVipModel.subject2)) { /// 购买未查询到结果 [self checkVipFail]; } else { [RQ_ALERTVIEW_MANAGER showAlertWithTitle:@"温馨提示" message:@"您已购买成功!" confirmTitle:@"确定" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) { @strongify(self) // if (self.payViewController) { // [self.payViewController dismissViewControllerAnimated:YES completion:nil]; // } }]; } } } else if ([RQ_VIP_Module.chooseTypeModel.dictLabel containsString:@"科三"]) { if (RQStringIsNotEmpty(historyVideoVipModel.subject3)) { /// 续费 if ([historyVideoVipModel.subject3 isEqualToString:RQ_VIP_Module.videoVipModel.subject3]) { /// 续费未查询到结果 [self checkVipFail]; } else { [RQ_ALERTVIEW_MANAGER showAlertWithTitle:@"温馨提示" message:@"您已续费成功!" confirmTitle:@"确定" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) { @strongify(self) // if (self.payViewController) { // [self.payViewController dismissViewControllerAnimated:YES completion:nil]; // } }]; } } else { /// 购买 if (RQStringIsEmpty(RQ_VIP_Module.videoVipModel.subject3)) { /// 购买未查询到结果 [self checkVipFail]; } else { [RQ_ALERTVIEW_MANAGER showAlertWithTitle:@"温馨提示" message:@"您已购买成功!" confirmTitle:@"确定" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) { @strongify(self) // if (self.payViewController) { // [self.payViewController dismissViewControllerAnimated:YES completion:nil]; // } }]; } } } else if ([RQ_VIP_Module.chooseTypeModel.dictLabel containsString:@"科四"]) { if (RQStringIsNotEmpty(historyVideoVipModel.subject4)) { /// 续费 if ([historyVideoVipModel.subject4 isEqualToString:RQ_VIP_Module.videoVipModel.subject4]) { /// 续费未查询到结果 [self checkVipFail]; } else { [RQ_ALERTVIEW_MANAGER showAlertWithTitle:@"温馨提示" message:@"您已续费成功!" confirmTitle:@"确定" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) { @strongify(self) // if (self.payViewController) { // [self.payViewController dismissViewControllerAnimated:YES completion:nil]; // } }]; } } else { /// 购买 if (RQStringIsEmpty(RQ_VIP_Module.videoVipModel.subject4)) { /// 购买未查询到结果 [self checkVipFail]; } else { [RQ_ALERTVIEW_MANAGER showAlertWithTitle:@"温馨提示" message:@"您已购买成功!" confirmTitle:@"确定" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) { @strongify(self) // if (self.payViewController) { // [self.payViewController dismissViewControllerAnimated:YES completion:nil]; // } }]; } } } else if ([RQ_VIP_Module.chooseTypeModel.dictLabel containsString:@"理论实操计时"]) { [RQ_VIP_Module isActiveWithCcomplete:^(RQActivationModel * _Nullable activationModel, BOOL isSuccess) { @strongify(self) if (isSuccess) { if (activationModel.isShare && activationModel.isActive == 1) { [RQ_ALERTVIEW_MANAGER showAlertWithTitle:@"温馨提示" message:@"您已购买成功!" confirmTitle:@"确定" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) { @strongify(self) // if (self.payViewController) { // [self.payViewController dismissViewControllerAnimated:YES completion:nil]; // } }]; } else { [self checkVipFail]; } } else { [self checkVipFail]; } }]; } else { if ([historyVideoVipModel.subject1 isEqualToString:RQ_VIP_Module.videoVipModel.subject1] && [historyVideoVipModel.subject2 isEqualToString:RQ_VIP_Module.videoVipModel.subject2] && [historyVideoVipModel.subject3 isEqualToString:RQ_VIP_Module.videoVipModel.subject3] && [historyVideoVipModel.subject4 isEqualToString:RQ_VIP_Module.videoVipModel.subject4]) { /// 续费未查询到结果 [self checkVipFail]; } else { [RQ_ALERTVIEW_MANAGER showAlertWithTitle:@"温馨提示" message:@"您已购买成功!" confirmTitle:@"确定" confirmAction:^(__kindof QMUIDialogViewController * _Nonnull dialogViewController) { @strongify(self) // if (self.payViewController) { // [self.payViewController dismissViewControllerAnimated:YES completion:nil]; // } }]; } } }]; }]; } - (void)checkVipFail { @weakify(self) [NSObject rq_showAlertViewWithTitle:@"温馨提示" message:@"更新VIP信息失败" confirmTitle:@"继续更新" cancelTitle:@"取消" confirmAction:^{ @strongify(self) [self ailyPayOrWechatSuccessAction]; } cancelAction:nil]; } // 支付失败 - (void)ailyPayOrWechatFailAction { NSLog(@"处理支付失败结果"); } #pragma mark - OverrideMethods /// 配置collectionView的区域 - (UIEdgeInsets)contentInset { return UIEdgeInsetsMake((self.viewModel.vipPageType == RQVIPPageType_Alert)? 0 : (RQ_APPLICATION_NAV_BAR_HEIGHT + RQ_APPLICATION_STATUS_BAR_HEIGHT), 0, 0, 0); } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath { RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section]; RQCommonCollectionItemViewModel *itemViewModel = groupViewModel.itemViewModels[indexPath.row]; return [NSClassFromString(itemViewModel.itemClassName) cellWithCollectionView:collectionView forIndexPath:indexPath]; } - (void)configureCell:(RQCommonCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object { [cell bindViewModel:object]; } - (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section { switch (section) { case 0: { return UIEdgeInsetsMake(0, 16, 0, 16); } case 1: { return UIEdgeInsetsMake(0, 16, 0, 16); } case 2: { return UIEdgeInsetsMake(0, 16, 0, 16); } case 3: { return UIEdgeInsetsMake(0, 16, 0, 16); } case 4: { return UIEdgeInsetsMake(0, 16, 0, 16); } case 5: { return UIEdgeInsetsMake(0, 16, 0, 16); } case 6: { return UIEdgeInsetsMake(0, 16, 0, 16); } default: { return UIEdgeInsetsZero; } } } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section { switch (section) { case 0: case 1: return 16; default: return 8; } } - (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section { switch (section) { case 1: return 0; default: return 8; } } - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { @weakify(self) // 如果是头视图 if (kind == UICollectionElementKindSectionHeader) { if (indexPath.section == 0) { if (self.viewModel.vipPageType == RQVIPPageType_Alert) { RQVipAlertHeaderCollectionReusableView *headerView = [RQVipAlertHeaderCollectionReusableView reusableViewWithCollectionView:collectionView OfKind:kind forIndexPath:indexPath]; [headerView.closeBtn setTapActionWithBlock:^(UITapGestureRecognizer *tap) { @strongify(self) [self dismissViewControllerAnimated:YES completion:nil]; }]; return headerView; } else { RQVipHeaderImageViewNewReusableView *vipHeaderImageViewReusableView = [RQVipHeaderImageViewNewReusableView reusableViewWithCollectionView:collectionView OfKind:kind forIndexPath:indexPath]; RQVipHeaderImageViewNewGroupViewModel *vipHeaderImageViewGroupViewModel = self.viewModel.dataSource[indexPath.section]; [vipHeaderImageViewReusableView bindViewModel:vipHeaderImageViewGroupViewModel]; return vipHeaderImageViewReusableView; } } else { RQCommonReusableView *headerView = [RQCommonReusableView reusableViewWithCollectionView:collectionView OfKind:kind forIndexPath:indexPath]; RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section]; [headerView bindViewModel:groupViewModel]; headerView.headerContentLabel.hidden = YES; headerView.footerContentLabel.hidden = YES; if (indexPath.section == 1 && self.viewModel.vipPageType == RQVIPPageType_Alert) { headerView.backgroundColor = UIColor.clearColor; } headerView.backgroundColor = UIColor.clearColor; return headerView; } } else { RQCommonReusableView *footerView = [RQCommonReusableView reusableViewWithCollectionView:collectionView OfKind:kind forIndexPath:indexPath]; RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section]; [footerView bindViewModel:groupViewModel]; footerView.headerContentLabel.hidden = YES; footerView.footerContentLabel.hidden = YES; footerView.backgroundColor = UIColor.clearColor; return footerView; } } - (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath { switch (indexPath.section) { case 0: case 1:{ RQCommonGroupViewModel *groupViewModel = self.viewModel.dataSource[indexPath.section]; if ([groupViewModel.itemViewModels[indexPath.row] isKindOfClass:[RQVipTrainPriceItemViewModel class]]) { return (!RQObjectIsNil(RQ_VIP_Module.activationModel) && RQ_VIP_Module.activationModel.isShare && !(RQ_VIP_Module.activationModel.isActive == 1)); } else { return YES; } } default: return NO; } } #pragma mark - HWPanModalPresentable3 - (NYWPViewModel *)wpviewModel{ if(!_wpviewModel){ _wpviewModel = [[NYWPViewModel alloc] init]; } return _wpviewModel; } - (UIScrollView *)panScrollable { return self.collectionView; } - (PanModalHeight)longFormHeight { return PanModalHeightMake(PanModalHeightTypeContent, RQ_SCREEN_HEIGHT * (459.f / 812.f)); } - (CGFloat)cornerRadius { return 0.0; } - (BOOL)allowsTapBackgroundToDismiss { return NO; } - (BOOL)showDragIndicator { return NO; } #pragma mark - LazyLoad - (RQVipBuyView *)vipBuyView { if (!_vipBuyView) { _vipBuyView = [RQVipBuyView vipBuyView]; RAC(_vipBuyView, typeModel) = [[RACObserve(RQ_VIP_Module, chooseTypeModel) distinctUntilChanged] takeUntil:self.rac_willDeallocSignal]; } return _vipBuyView; } - (RQVipBuyNewView *)vipBuyNewView { if (!_vipBuyNewView) { _vipBuyNewView = [RQVipBuyNewView vipBuyNewView]; RAC(_vipBuyNewView, typeModel) = [[RACObserve(RQ_VIP_Module, chooseTypeModel) distinctUntilChanged] takeUntil:self.rac_willDeallocSignal]; } return _vipBuyNewView; } @end