RQVIPModule.m 17 KB

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