RQVIPModule.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. //
  2. // RQVIPModule.m
  3. // jiaPei
  4. //
  5. // Created by 张嵘 on 2022/2/28.
  6. // Copyright © 2022 JCZ. All rights reserved.
  7. //
  8. #import "RQVIPModule.h"
  9. #include <CommonCrypto/CommonCrypto.h>
  10. @interface RQVIPModule ()
  11. @end
  12. @implementation RQVIPModule
  13. static id rq_VIPModule = nil;
  14. #pragma mark - init
  15. + (instancetype)sharedInstance {
  16. static dispatch_once_t onceToken;
  17. dispatch_once(&onceToken, ^{
  18. rq_VIPModule = [[self alloc] init];
  19. });
  20. return rq_VIPModule;
  21. }
  22. - (instancetype)init {
  23. if (self = [super init]) {
  24. }
  25. return self;
  26. }
  27. #pragma mark - PublicMethods
  28. - (void)gotoBuyVipWithVipPageType:(RQVIPPageType)vipPageType {
  29. [self gotoBuyVipWithVipPageType:vipPageType subject:RQ_YDTQuestion_Module.subject];
  30. }
  31. - (void)gotoBuyVipWithVipPageType:(RQVIPPageType)vipPageType subject:(RQHomePageSubjectType)subject {
  32. WeakSelf(weakSelf)
  33. [RQ_VIP_Module isActiveWithCcomplete:^(RQActivationModel * _Nullable activationModel, BOOL isSuccess) {
  34. if (isSuccess) {
  35. if (activationModel.isShare) {
  36. if (vipPageType == RQVIPPageType_Alert) {
  37. [weakSelf showVipAlertWithDictType:RQ_USER_MANAGER.currentUser.city subject:subject];
  38. } else {
  39. [weakSelf gotoVipViewWithDictType:RQ_USER_MANAGER.currentUser.city subject:subject];
  40. }
  41. } else {
  42. if (vipPageType == RQVIPPageType_Alert) {
  43. [weakSelf showVipAlertWithDictType:@"vip_package" subject:subject];
  44. } else {
  45. [weakSelf gotoVipViewWithDictType:@"vip_package" subject:subject];
  46. }
  47. }
  48. }
  49. }];
  50. }
  51. - (void)showVipAlertWithDictType:(NSString *)dictType subject:(RQHomePageSubjectType)subject {
  52. if (RQ_USER_MANAGER.isShouldLogin) {
  53. [MBProgressHUD rq_showProgressHUD:@"获取VIP套餐..."];
  54. [[RQ_HTTP_Service getTypeWithDictType:dictType] subscribeNext:^(NSArray *typeModels) {
  55. [MBProgressHUD rq_hideHUD];
  56. RQVipViewModel *vipViewModel = [[RQVipViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{
  57. RQViewModelIDKey : @(RQVIPPageType_Alert),
  58. RQViewModelUtilKey : typeModels,
  59. RQViewCommonValueKey : @(subject),
  60. }];
  61. RQVipViewController *vipViewController = [[RQVipViewController alloc] initWithViewModel:vipViewModel];
  62. [RQControllerHelper.topViewController presentPanModal:vipViewController];
  63. } error:^(NSError * _Nullable error) {
  64. [MBProgressHUD rq_hideHUD];
  65. [MBProgressHUD rq_showErrorTips:error];
  66. }];
  67. }
  68. }
  69. - (void)gotoVipViewWithDictType:(NSString *)dictType {
  70. [self gotoVipViewWithDictType:dictType subject:RQ_YDTQuestion_Module.subject];
  71. }
  72. - (void)gotoVipViewWithDictType:(NSString *)dictType subject:(RQHomePageSubjectType)subject {
  73. if (RQ_USER_MANAGER.isShouldLogin) {
  74. [MBProgressHUD rq_showProgressHUD:@"获取VIP套餐..."];
  75. [[RQ_HTTP_Service getTypeWithDictType:dictType] subscribeNext:^(NSArray *typeModels) {
  76. [MBProgressHUD rq_hideHUD];
  77. [RQ_APPDELEGATE.services pushViewModel:[[RQVipViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{
  78. RQViewModelIDKey : @(RQVIPPageType_Full),
  79. RQViewModelUtilKey : typeModels,
  80. RQViewCommonValueKey : @(subject),
  81. }] animated:YES];
  82. } error:^(NSError * _Nullable error) {
  83. [MBProgressHUD rq_hideHUD];
  84. [MBProgressHUD rq_showErrorTips:error];
  85. }];
  86. }
  87. }
  88. - (void)gotoVipCenter {
  89. [self gotoVipCenterWithSubject:RQ_YDTQuestion_Module.subject];
  90. }
  91. - (void)gotoVipCenterWithSubject:(RQHomePageSubjectType)subject {
  92. @weakify(self)
  93. [RQ_VIP_Module isActiveWithCcomplete:^(RQActivationModel * _Nullable activationModel, BOOL isSuccess) {
  94. @strongify(self)
  95. if (isSuccess) {
  96. if (activationModel.isShare) {
  97. [self gotoVipCenterWithDictType:RQ_USER_MANAGER.currentUser.city subject:subject];
  98. } else {
  99. [self gotoVipCenterWithDictType:@"vip_package" subject:subject];
  100. }
  101. }
  102. }];
  103. }
  104. - (void)gotoVipCenterWithDictType:(NSString *)dictType subject:(RQHomePageSubjectType)subject {
  105. if (RQ_USER_MANAGER.isShouldLogin) {
  106. [MBProgressHUD rq_showProgressHUD:@"获取VIP套餐..."];
  107. [[RQ_HTTP_Service getTypeWithDictType:dictType] subscribeNext:^(NSArray *typeModels) {
  108. [MBProgressHUD rq_hideHUD];
  109. RQVipCenterViewModel *vipCenterViewModel = [[RQVipCenterViewModel alloc] initWithServices:RQ_APPDELEGATE.services params:@{
  110. RQViewModelUtilKey : [typeModels isKindOfClass:[NSArray class]]? typeModels : @[],
  111. RQViewCommonValueKey : @(subject),
  112. }];
  113. [RQ_APPDELEGATE.services pushViewModel:vipCenterViewModel animated:YES];
  114. } error:^(NSError * _Nullable error) {
  115. [MBProgressHUD rq_hideHUD];
  116. [MBProgressHUD rq_showErrorTips:error];
  117. }];
  118. }
  119. }
  120. /// 是否Vip
  121. - (void)isVipWithSubject:(NSInteger)subject complete:(VoidBlock_Bool)complete {
  122. if (RQ_USER_MANAGER.isShouldLogin) {
  123. __block BOOL isVerify;
  124. [[RQ_HTTP_Service getVideoVipWithUserId:RQ_USER_MANAGER.currentUser._id] subscribeNext:^(RQVideoVipModel *videoVipModel) {
  125. //rsa 公钥
  126. NSString *publicKey = [NSString stringWithFormat:@"%@%@%@",@"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCzR04LMmDlwyVRfK+Kgsm/vFMOFyqvGk4QIVC",@"Gj2CKKfhycRExm15N3tKOOouIjl/EU79It45oS+ltJkscvYrJn1zz+1bSoBLelZzJrn7K0NpHF8k", @"bCprsFHd60x+YJ4l8C3SDxfFebo4kqveJzx6JNZKwzFm7LPEO5XB9pEBRwIDAQAB"];
  127. // 构造被签名串
  128. NSMutableString *mutableVerifyString = [[NSMutableString alloc] initWithString:[NSString stringWithFormat:@"%ld",videoVipModel.userId]];
  129. if (RQStringIsNotEmpty(videoVipModel.subject1)) {
  130. [mutableVerifyString appendFormat:@"%ld",[RQ_SHARE_FUNCTION getTimeStampWithTimeStr:videoVipModel.subject1 formatter:@"yyyy-MM-dd HH:mm:ss"]];
  131. }
  132. if (RQStringIsNotEmpty(videoVipModel.subject2)) {
  133. [mutableVerifyString appendFormat:@"%ld",[RQ_SHARE_FUNCTION getTimeStampWithTimeStr:videoVipModel.subject2 formatter:@"yyyy-MM-dd HH:mm:ss"]];
  134. }
  135. if (RQStringIsNotEmpty(videoVipModel.subject3)) {
  136. [mutableVerifyString appendFormat:@"%ld",[RQ_SHARE_FUNCTION getTimeStampWithTimeStr:videoVipModel.subject3 formatter:@"yyyy-MM-dd HH:mm:ss"]];
  137. }
  138. if (RQStringIsNotEmpty(videoVipModel.subject4)) {
  139. [mutableVerifyString appendFormat:@"%ld",[RQ_SHARE_FUNCTION getTimeStampWithTimeStr:videoVipModel.subject4 formatter:@"yyyy-MM-dd HH:mm:ss"]];
  140. }
  141. [mutableVerifyString appendFormat:@"%ld",videoVipModel.subject1Int];
  142. [mutableVerifyString appendFormat:@"%ld",videoVipModel.subject2Int];
  143. [mutableVerifyString appendFormat:@"%ld",videoVipModel.subject3Int];
  144. [mutableVerifyString appendFormat:@"%ld",videoVipModel.subject4Int];
  145. //公钥验签
  146. isVerify = [self verify:mutableVerifyString.copy signature:RQStringIsNotEmpty(videoVipModel.sign)? videoVipModel.sign : @"" withPublivKey:publicKey];
  147. NSLog(@"验签通过了吧------%@", isVerify ? @"YES":@"NO");
  148. if (complete) {
  149. BOOL result;
  150. RQ_VIP_Module.isSubject1Vip = videoVipModel.subject1Int;
  151. RQ_VIP_Module.isSubject2Vip = videoVipModel.subject2Int;
  152. RQ_VIP_Module.isSubject3Vip = videoVipModel.subject3Int;
  153. RQ_VIP_Module.isSubject4Vip = videoVipModel.subject4Int;
  154. RQ_VIP_Module.isSubjectAllVip = RQ_VIP_Module.isSubject1Vip && RQ_VIP_Module.isSubject2Vip && RQ_VIP_Module.isSubject3Vip && RQ_VIP_Module.isSubject4Vip;
  155. self.videoVipModel = videoVipModel;
  156. NSInteger isAds = 0;
  157. if (subject == 1) {
  158. isAds ++;
  159. result = isVerify? RQ_VIP_Module.isSubject1Vip : NO;
  160. } else if (subject == 2) {
  161. isAds ++;
  162. result = isVerify? RQ_VIP_Module.isSubject2Vip : NO;
  163. } else if (subject == 3) {
  164. isAds ++;
  165. result = isVerify? RQ_VIP_Module.isSubject3Vip : NO;
  166. } else if (subject == 4) {
  167. isAds ++;
  168. result = isVerify? RQ_VIP_Module.isSubject4Vip : NO;
  169. } else {
  170. result = isVerify? (RQ_VIP_Module.isSubject1Vip || RQ_VIP_Module.isSubject2Vip || RQ_VIP_Module.isSubject3Vip || RQ_VIP_Module.isSubject4Vip) : NO;
  171. }
  172. RQ_VIP_Module.isADVip = (isAds >=2);
  173. RQ_VIP_Module.isVip = result;
  174. complete(result? result : (RQ_COMMON_MANAGER.APP_SWITCH? YES : NO));
  175. }
  176. } error:^(NSError * _Nullable error) {
  177. [MBProgressHUD rq_showErrorTips:error];
  178. if (complete) {
  179. RQ_VIP_Module.isSubject1Vip = NO;
  180. RQ_VIP_Module.isSubject2Vip = NO;
  181. RQ_VIP_Module.isSubject3Vip = NO;
  182. RQ_VIP_Module.isSubject4Vip = NO;
  183. RQ_VIP_Module.isSubjectAllVip = NO;
  184. RQ_VIP_Module.isVip = NO;
  185. RQ_VIP_Module.isADVip = NO;
  186. self.videoVipModel = nil;
  187. complete(NO);
  188. }
  189. }];
  190. } else {
  191. if (complete) {
  192. RQ_VIP_Module.isSubject1Vip = NO;
  193. RQ_VIP_Module.isSubject2Vip = NO;
  194. RQ_VIP_Module.isSubject3Vip = NO;
  195. RQ_VIP_Module.isSubject4Vip = NO;
  196. RQ_VIP_Module.isSubjectAllVip = NO;
  197. RQ_VIP_Module.isVip = NO;
  198. RQ_VIP_Module.isADVip = NO;
  199. self.videoVipModel = nil;
  200. complete(NO);
  201. }
  202. }
  203. }
  204. - (void)isActiveWithCcomplete:(RQGetActivationModelBlock)complete {
  205. if (RQ_USER_MANAGER.isShouldLogin) {
  206. [[RQ_HTTP_Service getActivationWithUserId:RQ_USER_MANAGER.currentUser._id] subscribeNext:^(RQActivationModel *activationModel) {
  207. if (complete) {
  208. self.activationModel = activationModel;
  209. if (!RQObjectIsNil(self.activationModel)) {
  210. if ([self.activationModel isKindOfClass:[RQActivationModel class]]) {
  211. complete(self.activationModel, YES);
  212. } else {
  213. [MBProgressHUD rq_showTips:@"获取激活信息失败"];
  214. complete(self.activationModel, NO);
  215. }
  216. } else {
  217. [MBProgressHUD rq_showTips:@"获取激活信息失败"];
  218. complete(self.activationModel, NO);
  219. }
  220. }
  221. } error:^(NSError * _Nullable error) {
  222. [MBProgressHUD rq_showErrorTips:error];
  223. if (complete) {
  224. self.activationModel = nil;
  225. complete(self.activationModel, NO);
  226. }
  227. }];
  228. } else {
  229. if (complete) {
  230. self.activationModel = nil;
  231. complete(self.activationModel, NO);
  232. }
  233. }
  234. }
  235. #pragma mark - PrivateMethods
  236. // verify Signature
  237. - (BOOL)verify:(NSString *)content signature:(NSString *)signature withPublivKey:(NSString *)publicKey {
  238. SecKeyRef publicKeyRef = [self addPublicKey:publicKey];
  239. if (!publicKeyRef) { NSLog(@"添加公钥失败"); return NO; }
  240. NSData *originData = [self sha256:content];
  241. NSData *signatureData = [[NSData alloc] initWithBase64EncodedString:signature options:NSDataBase64DecodingIgnoreUnknownCharacters];
  242. if (!originData || !signatureData) { return NO; }
  243. OSStatus status = SecKeyRawVerify(publicKeyRef, kSecPaddingPKCS1SHA256, [originData bytes], originData.length, [signatureData bytes], signatureData.length);
  244. if (status ==noErr) { return YES; }
  245. else{ NSLog(@"验签失败:%d",status); return NO; }
  246. }
  247. // digest message with sha1
  248. - (NSData *)sha256:(NSString *)str {
  249. const void *data = [str cStringUsingEncoding:NSUTF8StringEncoding];
  250. CC_LONG len = (CC_LONG)strlen(data);
  251. uint8_t * md = malloc( CC_SHA256_DIGEST_LENGTH * sizeof(uint8_t) );;
  252. CC_SHA256(data, len, md);
  253. return [NSData dataWithBytes:md length:CC_SHA256_DIGEST_LENGTH];
  254. }
  255. - (SecKeyRef)addPublicKey:(NSString *)pubKey {
  256. NSData *data = [[NSData alloc] initWithBase64EncodedString:pubKey options:NSDataBase64DecodingIgnoreUnknownCharacters];
  257. //a tag to read/write keychain storage
  258. NSString *tag = @"RSA_PUBLIC_KEY";
  259. NSData *d_tag = [NSData dataWithBytes:[tag UTF8String] length:[tag length]];
  260. // Delete any old lingering key with the same tag
  261. NSMutableDictionary *publicKey = [[NSMutableDictionary alloc] init];
  262. [publicKey setObject:(__bridge id) kSecClassKey forKey:(__bridge id)kSecClass];
  263. [publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
  264. [publicKey setObject:d_tag forKey:(__bridge id)kSecAttrApplicationTag];
  265. SecItemDelete((__bridge CFDictionaryRef)publicKey);
  266. // Add persistent version of the key to system keychain
  267. [publicKey setObject:data forKey:(__bridge id)kSecValueData];
  268. [publicKey setObject:(__bridge id) kSecAttrKeyClassPublic forKey:(__bridge id)kSecAttrKeyClass];
  269. [publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnPersistentRef];
  270. CFTypeRef persistKey = nil;
  271. OSStatus status = SecItemAdd((__bridge CFDictionaryRef)publicKey, &persistKey);
  272. if (persistKey != nil){
  273. CFRelease(persistKey);
  274. }
  275. if ((status != noErr) && (status != errSecDuplicateItem)) { return nil; }
  276. [publicKey removeObjectForKey:(__bridge id)kSecValueData];
  277. [publicKey removeObjectForKey:(__bridge id)kSecReturnPersistentRef];
  278. [publicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef];
  279. [publicKey setObject:(__bridge id) kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType];
  280. // Now fetch the SecKeyRef version of the key
  281. SecKeyRef keyRef = nil;
  282. status = SecItemCopyMatching((__bridge CFDictionaryRef)publicKey, (CFTypeRef *)&keyRef);
  283. if(status != noErr){
  284. return nil;
  285. }
  286. return keyRef;
  287. }
  288. - (BOOL)isVip {
  289. id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isVip"];
  290. if (RQObjectIsNil(object)) {
  291. return NO;
  292. } else {
  293. BOOL isVip = [object boolValue];
  294. return isVip;
  295. }
  296. }
  297. - (void)setIsVip:(BOOL)isVip {
  298. [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isVip] ForKey:@"isVip"];
  299. }
  300. - (BOOL)isADVip {
  301. id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isADVip"];
  302. if (RQObjectIsNil(object)) {
  303. return NO;
  304. } else {
  305. BOOL isADVip = [object boolValue];
  306. return isADVip;
  307. }
  308. }
  309. - (void)setIsADVip:(BOOL)isADVip {
  310. [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isADVip] ForKey:@"isADVip"];
  311. }
  312. - (BOOL)isSubject1Vip {
  313. id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isSubject1Vip"];
  314. if (RQObjectIsNil(object)) {
  315. return NO;
  316. } else {
  317. BOOL isSubject1Vip = [object boolValue];
  318. return isSubject1Vip;
  319. }
  320. }
  321. - (void)setIsSubject1Vip:(BOOL)isSubject1Vip {
  322. [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isSubject1Vip] ForKey:@"isSubject1Vip"];
  323. }
  324. - (BOOL)isSubject2Vip {
  325. id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isSubject2Vip"];
  326. if (RQObjectIsNil(object)) {
  327. return NO;
  328. } else {
  329. BOOL isSubject2Vip = [object boolValue];
  330. return isSubject2Vip;
  331. }
  332. }
  333. - (void)setIsSubject2Vip:(BOOL)isSubject2Vip {
  334. [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isSubject2Vip] ForKey:@"isSubject2Vip"];
  335. }
  336. - (BOOL)isSubject3Vip {
  337. id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isSubject3Vip"];
  338. if (RQObjectIsNil(object)) {
  339. return NO;
  340. } else {
  341. BOOL isSubject3Vip = [object boolValue];
  342. return isSubject3Vip;
  343. }
  344. }
  345. - (void)setIsSubject3Vip:(BOOL)isSubject3Vip {
  346. [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isSubject3Vip] ForKey:@"isSubject3Vip"];
  347. }
  348. - (BOOL)isSubject4Vip {
  349. id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isSubject4Vip"];
  350. if (RQObjectIsNil(object)) {
  351. return NO;
  352. } else {
  353. BOOL isSubject4Vip = [object boolValue];
  354. return isSubject4Vip;
  355. }
  356. }
  357. - (void)setIsSubject4Vip:(BOOL)isSubject4Vip {
  358. [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isSubject4Vip] ForKey:@"isSubject4Vip"];
  359. }
  360. - (BOOL)isSubjectAllVip {
  361. id object = [RQ_SHARE_FUNCTION getObjectWithKey:@"isSubjectAllVip"];
  362. if (RQObjectIsNil(object)) {
  363. return NO;
  364. } else {
  365. BOOL isSubjectAllVip = [object boolValue];
  366. return isSubjectAllVip;
  367. }
  368. }
  369. - (void)setIsSubjectAllVip:(BOOL)isSubjectAllVip {
  370. [RQ_SHARE_FUNCTION saveObjectWithObject:[NSNumber numberWithBool:isSubjectAllVip] ForKey:@"isSubjectAllVip"];
  371. }
  372. - (NSUInteger)freeLookSimExamNum {
  373. id object = [RQ_COMMON_MANAGER getObjectWithKey:@"freeLookSimExamNum"];
  374. if (RQObjectIsNil(object)) {
  375. return NO;
  376. } else {
  377. NSInteger freeLookSimExamNum = [object integerValue];
  378. return freeLookSimExamNum;
  379. }
  380. }
  381. - (void)setFreeLookSimExamNum:(NSUInteger)freeLookSimExamNum {
  382. [RQ_COMMON_MANAGER saveObjectWithObject:[NSNumber numberWithInteger:freeLookSimExamNum] ForKey:@"freeLookSimExamNum"];
  383. }
  384. @end