123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437 |
- //
- // RQVIPModule.m
- // jiaPei
- //
- // Created by 张嵘 on 2022/2/28.
- // Copyright © 2022 JCZ. All rights reserved.
- //
- #import "RQVIPModule.h"
- #include <CommonCrypto/CommonCrypto.h>
- @interface RQVIPModule ()
- @end
- @implementation RQVIPModule
- static id rq_VIPModule = nil;
- #pragma mark - init
- + (instancetype)sharedInstance {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- rq_VIPModule = [[self alloc] init];
- });
- return rq_VIPModule;
- }
- - (instancetype)init {
- if (self = [super init]) {
-
- }
- return self;
- }
- #pragma mark - PublicMethods
- - (void)gotoBuyVipWithVipPageType:(RQVIPPageType)vipPageType {
- [self gotoBuyVipWithVipPageType:vipPageType subject:RQ_YDTQuestion_Module.subject];
- }
- - (void)gotoBuyVipWithVipPageType:(RQVIPPageType)vipPageType subject:(RQHomePageSubjectType)subject {
- @weakify(self)
- [RQ_VIP_Module isActiveWithCcomplete:^(RQActivationModel * _Nullable activationModel, BOOL isSuccess) {
- @strongify(self)
- if (isSuccess) {
- if (activationModel.isShare) {
- if (vipPageType == RQVIPPageType_Alert) {
- [self showVipAlertWithDictType:RQ_USER_MANAGER.currentUser.city subject:subject];
- } else {
- [self gotoVipViewWithDictType:RQ_USER_MANAGER.currentUser.city subject:subject];
- }
- } else {
- if (vipPageType == RQVIPPageType_Alert) {
- [self showVipAlertWithDictType:@"vip_package" subject:subject];
- } else {
- [self gotoVipViewWithDictType:@"vip_package" subject:subject];
- }
- }
- }
- }];
- }
- - (void)showVipAlertWithDictType:(NSString *)dictType subject:(RQHomePageSubjectType)subject {
- if (RQ_USER_MANAGER.isShouldLogin) {
- [MBProgressHUD rq_showProgressHUD:@"获取VIP套餐..."];
- [[RQ_HTTP_Service getTypeWithDictType:dictType] subscribeNext:^(NSArray *typeModels) {
- [MBProgressHUD rq_hideHUD];
- RQVipViewModel *vipViewModel = [[RQVipViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{
- RQViewModelIDKey : @(RQVIPPageType_Alert),
- RQViewModelUtilKey : typeModels,
- RQViewCommonValueKey : @(subject),
- }];
- RQVipViewController *vipViewController = [[RQVipViewController alloc] initWithViewModel:vipViewModel];
- [RQControllerHelper.topViewController presentPanModal:vipViewController];
- } error:^(NSError * _Nullable error) {
- [MBProgressHUD rq_hideHUD];
- [MBProgressHUD rq_showErrorTips:error];
- }];
-
-
- }
- }
- - (void)gotoVipViewWithDictType:(NSString *)dictType {
- [self gotoVipViewWithDictType:dictType subject:RQ_YDTQuestion_Module.subject];
- }
- - (void)gotoVipViewWithDictType:(NSString *)dictType subject:(RQHomePageSubjectType)subject {
- if (RQ_USER_MANAGER.isShouldLogin) {
- [MBProgressHUD rq_showProgressHUD:@"获取VIP套餐..."];
- [[RQ_HTTP_Service getTypeWithDictType:dictType] subscribeNext:^(NSArray *typeModels) {
- [MBProgressHUD rq_hideHUD];
- [RQ_APPDELEGATE.services pushViewModel:[[RQVipViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{
- RQViewModelIDKey : @(RQVIPPageType_Full),
- RQViewModelUtilKey : typeModels,
- RQViewCommonValueKey : @(subject),
- }] animated:YES];
- } error:^(NSError * _Nullable error) {
- [MBProgressHUD rq_hideHUD];
- [MBProgressHUD rq_showErrorTips:error];
- }];
- }
- }
- - (void)gotoVipCenter {
- [self gotoVipCenterWithSubject:RQ_YDTQuestion_Module.subject];
- }
- - (void)gotoVipCenterWithSubject:(RQHomePageSubjectType)subject {
- @weakify(self)
- [RQ_VIP_Module isActiveWithCcomplete:^(RQActivationModel * _Nullable activationModel, BOOL isSuccess) {
- @strongify(self)
- if (isSuccess) {
- if (activationModel.isShare) {
- [self gotoVipCenterWithDictType:RQ_USER_MANAGER.currentUser.city subject:subject];
- } else {
- [self gotoVipCenterWithDictType:@"vip_package" subject:subject];
- }
- }
- }];
- }
- - (void)gotoVipCenterWithDictType:(NSString *)dictType subject:(RQHomePageSubjectType)subject {
- if (RQ_USER_MANAGER.isShouldLogin) {
- [MBProgressHUD rq_showProgressHUD:@"获取VIP套餐..."];
- [[RQ_HTTP_Service getTypeWithDictType:dictType] subscribeNext:^(NSArray *typeModels) {
- [MBProgressHUD rq_hideHUD];
- RQVipCenterViewModel *vipCenterViewModel = [[RQVipCenterViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{
- RQViewModelUtilKey : [typeModels isKindOfClass:[NSArray class]]? typeModels : @[],
- RQViewCommonValueKey : @(subject),
- }];
- [RQ_APPDELEGATE.services pushViewModel:vipCenterViewModel animated:YES];
- } error:^(NSError * _Nullable error) {
- [MBProgressHUD rq_hideHUD];
- [MBProgressHUD rq_showErrorTips:error];
- }];
- }
- }
- /// 是否Vip
- - (void)isVipWithSubject:(NSInteger)subject complete:(VoidBlock_Bool)complete {
- if (RQ_USER_MANAGER.isShouldLogin) {
- __block BOOL isVerify;
- [[RQ_HTTP_Service getVideoVipWithUserId:RQ_USER_MANAGER.currentUser._id] subscribeNext:^(RQVideoVipModel *videoVipModel) {
- //rsa 公钥
- NSString *publicKey = [NSString stringWithFormat:@"%@%@%@",@"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCzR04LMmDlwyVRfK+Kgsm/vFMOFyqvGk4QIVC",@"Gj2CKKfhycRExm15N3tKOOouIjl/EU79It45oS+ltJkscvYrJn1zz+1bSoBLelZzJrn7K0NpHF8k", @"bCprsFHd60x+YJ4l8C3SDxfFebo4kqveJzx6JNZKwzFm7LPEO5XB9pEBRwIDAQAB"];
- // 构造被签名串
- NSMutableString *mutableVerifyString = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"%ld",videoVipModel.userId]];
- if (RQStringIsNotEmpty(videoVipModel.subject1)) {
- [mutableVerifyString appendFormat:@"%ld",[RQ_SHARE_FUNCTION getTimeStampWithTimeStr:videoVipModel.subject1 formatter:@"yyyy-MM-dd HH:mm:ss"]];
- }
- if (RQStringIsNotEmpty(videoVipModel.subject2)) {
- [mutableVerifyString appendFormat:@"%ld",[RQ_SHARE_FUNCTION getTimeStampWithTimeStr:videoVipModel.subject2 formatter:@"yyyy-MM-dd HH:mm:ss"]];
- }
- if (RQStringIsNotEmpty(videoVipModel.subject3)) {
- [mutableVerifyString appendFormat:@"%ld",[RQ_SHARE_FUNCTION getTimeStampWithTimeStr:videoVipModel.subject3 formatter:@"yyyy-MM-dd HH:mm:ss"]];
- }
- if (RQStringIsNotEmpty(videoVipModel.subject4)) {
- [mutableVerifyString appendFormat:@"%ld",[RQ_SHARE_FUNCTION getTimeStampWithTimeStr:videoVipModel.subject4 formatter:@"yyyy-MM-dd HH:mm:ss"]];
- }
-
- [mutableVerifyString appendFormat:@"%ld",videoVipModel.subject1Int];
- [mutableVerifyString appendFormat:@"%ld",videoVipModel.subject2Int];
- [mutableVerifyString appendFormat:@"%ld",videoVipModel.subject3Int];
- [mutableVerifyString appendFormat:@"%ld",videoVipModel.subject4Int];
-
-
- //公钥验签
- isVerify = [self verify:mutableVerifyString.copy signature:RQStringIsNotEmpty(videoVipModel.sign)? videoVipModel.sign : @"" withPublivKey:publicKey];
- NSLog(@"验签通过了吧------%@", isVerify ? @"YES":@"NO");
-
- if (complete) {
- BOOL result;
- RQ_VIP_Module.isSubject1Vip = videoVipModel.subject1Int;
- RQ_VIP_Module.isSubject2Vip = videoVipModel.subject2Int;
- RQ_VIP_Module.isSubject3Vip = videoVipModel.subject3Int;
- RQ_VIP_Module.isSubject4Vip = videoVipModel.subject4Int;
- RQ_VIP_Module.isSubjectAllVip = RQ_VIP_Module.isSubject1Vip && RQ_VIP_Module.isSubject2Vip && RQ_VIP_Module.isSubject3Vip && RQ_VIP_Module.isSubject4Vip;
- self.videoVipModel = videoVipModel;
- NSInteger isAds = 0;
- if (subject == 1) {
- isAds ++;
- result = isVerify? RQ_VIP_Module.isSubject1Vip : NO;
- } else if (subject == 2) {
- isAds ++;
- result = isVerify? RQ_VIP_Module.isSubject2Vip : NO;
- } else if (subject == 3) {
- isAds ++;
- result = isVerify? RQ_VIP_Module.isSubject3Vip : NO;
- } else if (subject == 4) {
- isAds ++;
- result = isVerify? RQ_VIP_Module.isSubject4Vip : NO;
- } else {
- result = isVerify? (RQ_VIP_Module.isSubject1Vip || RQ_VIP_Module.isSubject2Vip || RQ_VIP_Module.isSubject3Vip || RQ_VIP_Module.isSubject4Vip) : NO;
- }
- RQ_VIP_Module.isADVip = (isAds >=2);
- RQ_VIP_Module.isVip = result;
- complete(result? result : (RQ_COMMON_MANAGER.APP_SWITCH? YES : NO));
- }
- } error:^(NSError * _Nullable error) {
- [MBProgressHUD rq_showErrorTips:error];
- if (complete) {
- RQ_VIP_Module.isSubject1Vip = NO;
- RQ_VIP_Module.isSubject2Vip = NO;
- RQ_VIP_Module.isSubject3Vip = NO;
- RQ_VIP_Module.isSubject4Vip = NO;
- RQ_VIP_Module.isSubjectAllVip = NO;
- RQ_VIP_Module.isVip = NO;
- RQ_VIP_Module.isADVip = NO;
- self.videoVipModel = nil;
- complete(NO);
- }
- }];
- } else {
- if (complete) {
- RQ_VIP_Module.isSubject1Vip = NO;
- RQ_VIP_Module.isSubject2Vip = NO;
- RQ_VIP_Module.isSubject3Vip = NO;
- RQ_VIP_Module.isSubject4Vip = NO;
- RQ_VIP_Module.isSubjectAllVip = NO;
- RQ_VIP_Module.isVip = NO;
- RQ_VIP_Module.isADVip = NO;
- self.videoVipModel = nil;
- complete(NO);
- }
- }
- }
- - (void)isActiveWithCcomplete:(RQGetActivationModelBlock)complete {
- if (RQ_USER_MANAGER.isShouldLogin) {
- [[RQ_HTTP_Service getActivationWithUserId:RQ_USER_MANAGER.currentUser._id] subscribeNext:^(RQActivationModel *activationModel) {
- if (complete) {
- self.activationModel = activationModel;
- if (!RQObjectIsNil(self.activationModel)) {
- if ([self.activationModel isKindOfClass:[RQActivationModel class]]) {
- complete(self.activationModel, YES);
- } else {
- [MBProgressHUD rq_showTips:@"获取激活信息失败"];
- complete(self.activationModel, NO);
- }
- } else {
- [MBProgressHUD rq_showTips:@"获取激活信息失败"];
- complete(self.activationModel, NO);
- }
- }
- } error:^(NSError * _Nullable error) {
- [MBProgressHUD rq_showErrorTips:error];
- if (complete) {
- self.activationModel = nil;
- complete(self.activationModel, NO);
- }
- }];
- } else {
- if (complete) {
- self.activationModel = nil;
- complete(self.activationModel, NO);
- }
- }
- }
- #pragma mark - PrivateMethods
- // verify Signature
- - (BOOL)verify:(NSString *)content signature:(NSString *)signature withPublivKey:(NSString *)publicKey {
- SecKeyRef publicKeyRef = [self addPublicKey:publicKey];
- if (!publicKeyRef) { NSLog(@"添加公钥失败"); return NO; }
- NSData *originData = [self sha256:content];
- NSData *signatureData = [[NSData alloc] initWithBase64EncodedString:signature options:NSDataBase64DecodingIgnoreUnknownCharacters];
- if (!originData || !signatureData) { return NO; }
- OSStatus status = SecKeyRawVerify(publicKeyRef, kSecPaddingPKCS1SHA256, [originData bytes], originData.length, [signatureData bytes], signatureData.length);
-
- if (status ==noErr) { return YES; }
- else{ NSLog(@"验签失败:%d",status); return NO; }
- }
- // digest message with sha1
- - (NSData *)sha256:(NSString *)str {
- const void *data = [str cStringUsingEncoding:NSUTF8StringEncoding];
- CC_LONG len = (CC_LONG)strlen(data);
- uint8_t * md = malloc( CC_SHA256_DIGEST_LENGTH * sizeof(uint8_t) );;
- CC_SHA256(data, len, md);
- return [NSData dataWithBytes:md length:CC_SHA256_DIGEST_LENGTH];
- }
- - (SecKeyRef)addPublicKey:(NSString *)pubKey {
- NSData *data = [[NSData alloc] initWithBase64EncodedString:pubKey options:NSDataBase64DecodingIgnoreUnknownCharacters];
-
- //a tag to read/write keychain storage
- NSString *tag = @"RSA_PUBLIC_KEY";
- NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];
-
- // Delete any old lingering key with the same tag
- NSMutableDictionary *publicKey = [[NSMutableDictionary alloc] init];
- [publicKey setObject:(__bridge id) kSecClassKey forKey:(__bridge id)kSecClass];
- [publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
- [publicKey setObject:d_tag forKey:(__bridge id)kSecAttrApplicationTag];
- SecItemDelete((__bridge CFDictionaryRef)publicKey);
-
- // Add persistent version of the key to system keychain
- [publicKey setObject:data forKey:(__bridge id)kSecValueData];
- [publicKey setObject:(__bridge id) kSecAttrKeyClassPublic forKey:(__bridge id)kSecAttrKeyClass];
- [publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnPersistentRef];
-
- CFTypeRef persistKey = nil;
- OSStatus status = SecItemAdd((__bridge CFDictionaryRef)publicKey, &persistKey);
- if (persistKey != nil){
- CFRelease(persistKey);
- }
-
- if ((status != noErr) && (status != errSecDuplicateItem)) { return nil; }
-
- [publicKey removeObjectForKey:(__bridge id)kSecValueData];
- [publicKey removeObjectForKey:(__bridge id)kSecReturnPersistentRef];
- [publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];
- [publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
-
- // Now fetch the SecKeyRef version of the key
- SecKeyRef keyRef = nil;
- status = SecItemCopyMatching((__bridge CFDictionaryRef)publicKey, (CFTypeRef *)&keyRef);
- if(status != noErr){
- return nil;
- }
- return keyRef;
- }
- - (BOOL)isVip {
- id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isVip"];
- if (RQObjectIsNil(object)) {
- return NO;
- } else {
- BOOL isVip = [object boolValue];
- return isVip;
- }
- }
- - (void)setIsVip:(BOOL)isVip {
- [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isVip] ForKey:@"isVip"];
- }
- - (BOOL)isADVip {
- id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isADVip"];
- if (RQObjectIsNil(object)) {
- return NO;
- } else {
- BOOL isADVip = [object boolValue];
- return isADVip;
- }
- }
- - (void)setIsADVip:(BOOL)isADVip {
- [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isADVip] ForKey:@"isADVip"];
- }
- - (BOOL)isSubject1Vip {
- id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isSubject1Vip"];
- if (RQObjectIsNil(object)) {
- return NO;
- } else {
- BOOL isSubject1Vip = [object boolValue];
- return isSubject1Vip;
- }
- }
- - (void)setIsSubject1Vip:(BOOL)isSubject1Vip {
- [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isSubject1Vip] ForKey:@"isSubject1Vip"];
- }
- - (BOOL)isSubject2Vip {
- id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isSubject2Vip"];
- if (RQObjectIsNil(object)) {
- return NO;
- } else {
- BOOL isSubject2Vip = [object boolValue];
- return isSubject2Vip;
- }
- }
- - (void)setIsSubject2Vip:(BOOL)isSubject2Vip {
- [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isSubject2Vip] ForKey:@"isSubject2Vip"];
- }
- - (BOOL)isSubject3Vip {
- id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isSubject3Vip"];
- if (RQObjectIsNil(object)) {
- return NO;
- } else {
- BOOL isSubject3Vip = [object boolValue];
- return isSubject3Vip;
- }
- }
- - (void)setIsSubject3Vip:(BOOL)isSubject3Vip {
- [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isSubject3Vip] ForKey:@"isSubject3Vip"];
- }
- - (BOOL)isSubject4Vip {
- id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isSubject4Vip"];
- if (RQObjectIsNil(object)) {
- return NO;
- } else {
- BOOL isSubject4Vip = [object boolValue];
- return isSubject4Vip;
- }
- }
- - (void)setIsSubject4Vip:(BOOL)isSubject4Vip {
- [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isSubject4Vip] ForKey:@"isSubject4Vip"];
- }
- - (BOOL)isSubjectAllVip {
- id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isSubjectAllVip"];
- if (RQObjectIsNil(object)) {
- return NO;
- } else {
- BOOL isSubjectAllVip = [object boolValue];
- return isSubjectAllVip;
- }
- }
- - (void)setIsSubjectAllVip:(BOOL)isSubjectAllVip {
- [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isSubjectAllVip] ForKey:@"isSubjectAllVip"];
- }
- - (NSUInteger)freeLookSimExamNum {
- id object = [RQ_COMMON_MANAGER getObjectWithKey:@"freeLookSimExamNum"];
- if (RQObjectIsNil(object)) {
- return NO;
- } else {
- NSInteger freeLookSimExamNum = [object integerValue];
- return freeLookSimExamNum;
- }
- }
- - (void)setFreeLookSimExamNum:(NSUInteger)freeLookSimExamNum {
- [RQ_COMMON_MANAGER saveObjectWithObject:[NSNumber numberWithInteger:freeLookSimExamNum] ForKey:@"freeLookSimExamNum"];
- }
- @end
|