VerificationManager.m 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352
  1. //
  2. // VerificationManager.m
  3. // Filmage Screen
  4. //
  5. // Created by 朱东勇 on 2018/2/8.
  6. // Copyright © 2018年 kdan mobile. All rights reserved.
  7. //
  8. #import "VerificationManager.h"
  9. #import "NSNULL+Filtration.h"
  10. #import "NSObject+DeviceInfo.h"
  11. #import "JSONKit.h"
  12. #import "ASIFormDataRequest.h"
  13. #import <PDF_Reader_Pro-Swift.h>
  14. const NSString *kDeviceVerifyFinishNotification = @"kDeviceVerifyFinishNotification";
  15. const NSString *kDeviceActivateStatusChangeNotification = @"kDeviceActivateNotification";
  16. static VerificationManager *__verificationManager = nil;
  17. @interface VerificationManager ()
  18. @property (nonatomic, strong) NSTimer *timer;
  19. @property (nonatomic) BOOL isVerifing;
  20. @property (nonatomic, strong) KMVerificationRSA *rsa;
  21. @end
  22. @implementation VerificationManager
  23. + (VerificationManager*)manager {
  24. #if VERSION_DMG
  25. if (!__verificationManager)
  26. __verificationManager = [[VerificationManager alloc] init];
  27. #else
  28. return nil;
  29. #endif
  30. return __verificationManager;
  31. }
  32. + (VerificationManager*)defaultManager {
  33. #if VERSION_DMG
  34. if (!__verificationManager)
  35. __verificationManager = [[VerificationManager alloc] init];
  36. #else
  37. return nil;
  38. #endif
  39. return __verificationManager;
  40. }
  41. - (instancetype)init {
  42. self = [super init];
  43. self.autoVerification = YES;
  44. #if !DEBUG // 非测试环境时
  45. #if kTestMode // 测试模式时 报错
  46. //#error 服务器未切换到正式服务器!
  47. #endif
  48. #endif
  49. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  50. [self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) {
  51. }];
  52. });
  53. return self;
  54. }
  55. #pragma mark - Setter & Getter
  56. - (ActivityStatus)status {
  57. if (self.detailInfo == nil) {
  58. return ActivityStatusTrialExpire;
  59. }
  60. return self.detailInfo.status;
  61. }
  62. - (BOOL)needUpgradeLicense {
  63. if ([self.detailInfo[@"device"][@"product_code"] isEqualToString:@"com.brother.pdfreaderpro.mac.product_1"] ||
  64. [self.detailInfo[@"device"][@"product_code"] isEqualToString:@"com.brother.pdfreaderpro.cross.platform.product_1"]) {
  65. return YES;
  66. }
  67. // if ([self.detailInfo[@"subscription"][@"product"][@"code"] isEqualToString:@"com.brother.pdfreaderpro.mac.product_1"]) {
  68. // return YES;
  69. // }
  70. return NO;
  71. }
  72. - (BOOL)secondTrialEnabled {
  73. return self.detailInfo.repeatTrial;
  74. }
  75. - (NSInteger)trialTimes {
  76. return self.detailInfo.trialTimes;
  77. }
  78. - (NSString *)licenseCode {
  79. return self.detailInfo.licenseCode;
  80. }
  81. - (NSString *)originLicenseCode {
  82. return self.detailInfo.originLicenseCode;
  83. }
  84. - (NSString *)email {
  85. return self.detailInfo.email?:@"";
  86. }
  87. - (NSString *)subscriptionEmail {
  88. return self.detailInfo.subscriptionEmail?:@"";
  89. }
  90. - (NSString *)accountName {
  91. return self.detailInfo.accountName;
  92. }
  93. - (BOOL)allowUnbind {
  94. return self.detailInfo.allowUnbind;
  95. }
  96. - (NSInteger)unbindTimes {
  97. return self.detailInfo.unbindTimes;
  98. }
  99. - (NSString*)udid {
  100. //#if DEBUG
  101. // NSString *cachePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"uuid.plist"];
  102. // NSMutableDictionary *info = [[NSDictionary dictionaryWithContentsOfFile:cachePath] mutableCopy];
  103. //
  104. // if (!info) {
  105. // info = [NSMutableDictionary dictionary];
  106. //
  107. // [info setValue:[NSString stringWithFormat:@"TestInvate%.0f", [NSDate date].timeIntervalSince1970]
  108. // forKey:@"uuid"];
  109. //
  110. //
  111. // [[NSFileManager defaultManager] createDirectoryAtPath:NSTemporaryDirectory()
  112. // withIntermediateDirectories:YES
  113. // attributes:nil
  114. // error:nil];
  115. // [info writeToFile:cachePath atomically:YES];
  116. // }
  117. //
  118. // return info[@"uuid"]?:(GetHardwareUUID()?:@"");
  119. //#endif
  120. return GetHardwareUUID()?:@"";
  121. }
  122. - (NSString*)bundleIdentify {
  123. return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]?:@"";
  124. }
  125. - (void)setDetailInfo:(NSDictionary *)detailInfo {
  126. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  127. [defaults setValue:[detailInfo JSONData]
  128. forKey:@"detailInfo"];
  129. [defaults synchronize];
  130. }
  131. - (NSDictionary*)detailInfo {
  132. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  133. return [[[defaults valueForKey:@"detailInfo"] objectFromJSONData] filterNullObject];
  134. }
  135. //
  136. //- (void)setIsTest:(BOOL)isTest {
  137. // _isTest = isTest;
  138. //
  139. // [self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) {
  140. //
  141. // }];
  142. //}
  143. - (void)setAutoVerification:(BOOL)autoVerification {
  144. _autoVerification = autoVerification;
  145. [self restartTimer];
  146. }
  147. #pragma mark - Timer
  148. - (void)restartTimer {
  149. if (self.timer)
  150. [self.timer invalidate];
  151. self.timer = nil;
  152. if (self.autoVerification &&
  153. self.status != ActivityStatusNone) {
  154. self.timer = [NSTimer scheduledTimerWithTimeInterval:60 * 60//一个小时更新一次
  155. target:self
  156. selector:@selector(verificationFire)
  157. userInfo:nil
  158. repeats:NO];
  159. }
  160. }
  161. - (void)verificationFire {
  162. [self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) {
  163. }];
  164. }
  165. #pragma mark - Interface
  166. - (void)activateDeviceWithInfo:(NSDictionary*)info
  167. complention:(ActivityComplention)complention {
  168. // if (![info[@"cdkey"] length]) {
  169. // //激活码为空
  170. // NSError *error = [NSError errorWithDomain:@""
  171. // code:ActivityErrorTypeCDKeyEmpty
  172. // userInfo:nil];
  173. // if (complention)
  174. // complention(NO, nil, error);
  175. //
  176. // return;
  177. // }
  178. // if (![self isValidLicenseCode:info[@"cdkey"]]) {
  179. // //激活码格式不正确
  180. // NSError *error = [NSError errorWithDomain:@"激活失败"
  181. // code:ActivityErrorTypeFormatError
  182. // userInfo:nil];
  183. // if (complention)
  184. // complention(NO, @{@"errors":@"FormatError"}, error);
  185. //
  186. // return;
  187. // }
  188. __block ActivityComplention tComplention = complention;
  189. __block VerificationManager *weak_self = self;
  190. //Process Params
  191. NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description];
  192. if (!app_version.length)
  193. app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description];
  194. NSDictionary *params =
  195. @{
  196. @"data":@{
  197. //激活信息
  198. @"subscription":@{
  199. @"app_code":[self bundleIdentify]?:@"",
  200. @"email":info[@"email"]?:@"",
  201. // @"cdkey":info[@"cdkey"]?:@"",
  202. },
  203. //设备信息
  204. @"device":@{
  205. @"unique_sn":[self udid]?:@"",
  206. @"model":GetProductName()?:@"",
  207. @"os":GetSystemVersion()?:@"",
  208. @"time_zone":[[NSTimeZone localTimeZone] localizedName:NSTimeZoneNameStyleShortDaylightSaving locale:[NSLocale currentLocale]]?:@"",
  209. @"language":[[NSLocale currentLocale] localeIdentifier]?:@"",
  210. @"app_version":app_version?:@"",
  211. @"platform":@"DMG"
  212. }
  213. }
  214. };
  215. if (!self.rsa) {
  216. self.rsa = [[KMVerificationRSA alloc] init];
  217. }
  218. NSString *uuid = [self udid]?:@"";
  219. // if (uuid.length > @"8D397BE0-F478-5269-A8F1".length) {
  220. // uuid = [uuid substringToIndex:@"8D397BE0-F478-5269-A8F1".length];
  221. // }
  222. NSString *verifyString = [self.rsa activeWithUniquesn:uuid
  223. cdkey:info[@"email"]?:@""
  224. model:GetProductName()?:@""
  225. os:GetSystemVersion()?:@""
  226. language:[[NSLocale currentLocale] localeIdentifier]?:@""
  227. appversion:app_version?:@""];
  228. if (verifyString.length == 0) {
  229. return;
  230. }
  231. params = @{@"verifyParam":verifyString};
  232. NSMutableData *postData = [VerificationManager mutableDataWithDic:params];
  233. //Send Request
  234. NSString *urlString = [kVerificationPDFullServer stringByAppendingString:@"oem/activate"];
  235. NSURL *url = [NSURL URLWithString:urlString];
  236. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  237. request.requestMethod = @"POST";
  238. [request setPostBody:postData];
  239. NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];
  240. NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  241. postLength, @"Content-Length", nil];
  242. request.defaultResponseEncoding = NSUTF8StringEncoding;
  243. // [request setAuthenticationScheme:@"https"];//设置验证方式
  244. // [request setValidatesSecureCertificate:NO];//设置验证证书
  245. [request setRequestHeaders:requestHeaders];
  246. [request addRequestHeader:@"Content-Type" value:@"application/json"];
  247. [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"];
  248. __block void(^processRequest)(void) = ^{
  249. dispatch_async(dispatch_get_main_queue(), ^{
  250. NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject];
  251. NSError *error = request.error;
  252. if ([info valueForKey:@"msg"]) {
  253. NSString *detailKey = [info valueForKey:@"msg"];
  254. if ([detailKey isEqualToString:@"exceed_max_device_num"]) {
  255. error = [NSError errorWithDomain:@"激活失败"
  256. code:ActivityErrorTypeOutNumber
  257. userInfo:info];
  258. }else if ([detailKey isEqualToString:@"invalid_activate_info"]) {
  259. error = [NSError errorWithDomain:@"激活失败"
  260. code:ActivityErrorTypeInvalidInfo
  261. userInfo:info];
  262. }else if ([detailKey isEqualToString:@"invalid_license_key"]) {
  263. error = [NSError errorWithDomain:@"激活失败"
  264. code:ActivityErrorTypeCDKeyNotExist
  265. userInfo:info];
  266. }else if ([detailKey isEqualToString:@"license_not_match_product"]) {
  267. error = [NSError errorWithDomain:@"激活失败"
  268. code:ActivityErrorTypeNotMatchProduct
  269. userInfo:info];
  270. }else if ([detailKey isEqualToString:@"license_expired"]) {
  271. error = [NSError errorWithDomain:@"激活失败"
  272. code:ActivityErrorTypeCDKeyExpire
  273. userInfo:info];
  274. }else if ([detailKey isEqualToString:@" Activated success!"]) {
  275. error = nil;
  276. }else {
  277. error = [NSError errorWithDomain:@"激活失败"
  278. code:ActivityErrorTypeUnknow
  279. userInfo:info];
  280. }
  281. } else {
  282. if(error) {
  283. error = [NSError errorWithDomain:error.domain code:ActivityErrorTypeNetworkDisable userInfo:error.userInfo];
  284. } else {
  285. error = [NSError errorWithDomain:@"激活失败"
  286. code:ActivityErrorTypeUnknow
  287. userInfo:@{}];
  288. }
  289. }
  290. if (!error) {
  291. NSMutableDictionary *dic = [weak_self.detailInfo?:@{} mutableCopy];
  292. for (NSString *key in info[@"data"]) {
  293. [dic setValue:info[@"data"][key] forKey:key];
  294. }
  295. weak_self.detailInfo = dic;
  296. NSString *notificationName = (NSString *)kDeviceActivateStatusChangeNotification;
  297. [[NSNotificationCenter defaultCenter] postNotificationName:notificationName
  298. object:self
  299. userInfo:info];
  300. // [self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) {
  301. [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) {
  302. }];
  303. }
  304. [weak_self restartTimer];
  305. if (tComplention)
  306. tComplention(weak_self.status, info, error);
  307. });
  308. };
  309. [request setFailedBlock:^{
  310. processRequest();
  311. }];
  312. [request setCompletionBlock:^{
  313. processRequest();
  314. }];
  315. [request startAsynchronous];
  316. }
  317. - (void)verificationWithComplention:(ActivityComplention)complention {
  318. __block ActivityComplention tComplention = complention;
  319. __block VerificationManager *weak_self = self;
  320. if (!self.isVerifing) {
  321. self.isVerifing = YES;
  322. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  323. //Process Params
  324. NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description];
  325. if (!app_version.length)
  326. app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description];
  327. NSString *platform = @"DMG";
  328. #if VERSION_FREE
  329. #if VERSION_DMG
  330. #else
  331. platform = @"AppleStore";
  332. #endif
  333. #else
  334. platform = @"AppleStorePro";
  335. #endif
  336. NSDictionary *params =
  337. @{
  338. @"data":@{
  339. //验证信息
  340. @"subscription":@{
  341. @"app_code":[self bundleIdentify]?:@"",
  342. // @"email":info[@"email"]?:@"",
  343. // @"cdkey":info[@"cdkey"]?:@"",
  344. },
  345. //设备信息
  346. @"device":@{
  347. @"unique_sn":[self udid]?:@"",
  348. @"model":GetProductName()?:@"",
  349. @"os":GetSystemVersion()?:@"",
  350. @"time_zone":[[NSTimeZone localTimeZone] localizedName:NSTimeZoneNameStyleShortDaylightSaving locale:[NSLocale currentLocale]]?:@"",
  351. @"language":[[NSLocale currentLocale] localeIdentifier]?:@"",
  352. @"app_version":app_version?:@"",
  353. @"platform":platform?:@"AppleStore"
  354. }
  355. }
  356. };
  357. if (!self.rsa) {
  358. self.rsa = [[KMVerificationRSA alloc] init];
  359. }
  360. NSString *uuid = [self udid]?:@"";
  361. // if (uuid.length > @"8D397BE0-F478-5269-A8F1".length) {
  362. // uuid = [uuid substringToIndex:@"8D397BE0-F478-5269-A8F1".length];
  363. // }
  364. NSString *verifyString = [self.rsa verifyWithUniquesn:uuid
  365. model:GetProductName()?:@""
  366. os:GetSystemVersion()?:@""
  367. language:[[NSLocale currentLocale] localeIdentifier]?:@""
  368. appversion:app_version?:@""];
  369. if (verifyString.length == 0) {
  370. return;
  371. }
  372. params = @{@"verifyParam":verifyString};
  373. NSMutableData *postData = [VerificationManager mutableDataWithDic:params];
  374. //Send Request
  375. NSString *urlString = [kVerificationPDFullServer stringByAppendingString:@"oem/verify"];
  376. NSURL *url = [NSURL URLWithString:urlString];
  377. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  378. request.requestMethod = @"POST";
  379. [request setPostBody:postData];
  380. NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];
  381. NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  382. postLength, @"Content-Length", nil];
  383. // [request setAuthenticationScheme:@"https"];//设置验证方式
  384. // [request setValidatesSecureCertificate:NO];//设置验证证书
  385. [request setRequestHeaders:requestHeaders];
  386. [request addRequestHeader:@"Content-Type" value:@"application/json"];
  387. [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"];
  388. __block void(^processRequest)(void) = ^{
  389. dispatch_async(dispatch_get_main_queue(), ^{
  390. NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject];
  391. ActivityStatus status = weak_self.status;
  392. if ([info valueForKey:@"result"] &&
  393. [[info valueForKey:@"result"] valueForKey:@"device"]) {
  394. ActivityStatus tStatus = [(NSDictionary*)info[@"result"] status];
  395. //存储信息,并发送更新通知
  396. weak_self.detailInfo = info[@"result"];
  397. // if (status != tStatus ||
  398. // [[info valueForKey:@"data"] valueForKey:@"invite"]) {
  399. //发送通知
  400. NSString *notificationName = (NSString *)kDeviceActivateStatusChangeNotification;
  401. [[NSNotificationCenter defaultCenter] postNotificationName:notificationName
  402. object:weak_self
  403. userInfo:info];
  404. // }
  405. } else {
  406. weak_self.detailInfo = info;
  407. }
  408. [[NSNotificationCenter defaultCenter] postNotificationName:(NSString *)kDeviceVerifyFinishNotification
  409. object:nil
  410. userInfo:nil];
  411. weak_self.isVerifing = NO;
  412. //重置计时器
  413. [weak_self restartTimer];
  414. if (tComplention)
  415. tComplention(weak_self.status, info, request.error);
  416. });
  417. };
  418. [request setFailedBlock:^{
  419. processRequest();
  420. }];
  421. [request setCompletionBlock:^{
  422. processRequest();
  423. }];
  424. [request startAsynchronous];
  425. });
  426. }else {
  427. __block void(^waitBlock)(void) = ^{
  428. if (weak_self.isVerifing) {
  429. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  430. waitBlock();
  431. });
  432. }else {
  433. if (tComplention)
  434. tComplention(weak_self.status, @{@"data":weak_self.detailInfo?:@{}}, nil);
  435. waitBlock = NULL;
  436. }
  437. };
  438. waitBlock();
  439. }
  440. }
  441. - (void)trialForDays:(float)days
  442. complention:(ActivityComplention)complention {
  443. [self trialForDays:days email:nil name:nil complention:complention];
  444. }
  445. - (void)trialForDays:(float)days
  446. email:(NSString*)email
  447. name:(NSString*)name
  448. complention:(ActivityComplention)complention {
  449. __block ActivityComplention tComplention = complention;
  450. __block VerificationManager *weak_self = self;
  451. //Process Params
  452. NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description];
  453. if (!app_version.length)
  454. app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description];
  455. NSDictionary *params =
  456. @{
  457. @"data":@{
  458. //激活信息
  459. @"subscription":@{
  460. @"app_code":[self bundleIdentify]?:@"",
  461. @"trail_days":@(MAX(days, 1)),
  462. @"email":email?:@"",
  463. @"contact_name":name?:@""
  464. },
  465. //设备信息
  466. @"device":@{
  467. @"unique_sn":[self udid]?:@"",
  468. @"model":GetProductName()?:@"",
  469. @"os":GetSystemVersion()?:@"",
  470. @"time_zone":[[NSTimeZone localTimeZone] localizedName:NSTimeZoneNameStyleShortDaylightSaving locale:[NSLocale currentLocale]]?:@"",
  471. @"language":[[NSLocale currentLocale] localeIdentifier]?:@"",
  472. @"app_version":app_version?:@""
  473. }
  474. }
  475. };
  476. NSMutableData *postData = [VerificationManager mutableDataWithDic:params];
  477. //Send Request
  478. NSString *urlString = [kVerificationServer stringByAppendingString:@"api/auth_devices/trial"];
  479. NSURL *url = [NSURL URLWithString:urlString];
  480. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  481. request.requestMethod = @"POST";
  482. [request setPostBody:postData];
  483. NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];
  484. NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  485. postLength, @"Content-Length", nil];
  486. request.defaultResponseEncoding = NSUTF8StringEncoding;
  487. // [request setAuthenticationScheme:@"https"];//设置验证方式
  488. // [request setValidatesSecureCertificate:NO];//设置验证证书
  489. [request setRequestHeaders:requestHeaders];
  490. [request addRequestHeader:@"Content-Type" value:@"application/json"];
  491. [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"];
  492. __block void(^processRequest)(void) = ^{
  493. dispatch_async(dispatch_get_main_queue(), ^{
  494. NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject];
  495. ActivityStatus status = weak_self.status;
  496. if ([info valueForKey:@"data"] &&
  497. [[info valueForKey:@"data"] valueForKey:@"device"]) {
  498. ActivityStatus tStatus = [(NSDictionary*)info[@"data"] status];
  499. //存储信息,并发送更新通知
  500. NSMutableDictionary *dic = [weak_self.detailInfo?:@{} mutableCopy];
  501. for (NSString *key in info[@"data"]) {
  502. [dic setValue:info[@"data"][key] forKey:key];
  503. }
  504. weak_self.detailInfo = dic;
  505. if (status != tStatus) {
  506. //发送通知
  507. NSString *notificationName = (NSString *)kDeviceActivateStatusChangeNotification;
  508. [[NSNotificationCenter defaultCenter] postNotificationName:notificationName
  509. object:self
  510. userInfo:info];
  511. }
  512. }
  513. //重置计时器
  514. [weak_self restartTimer];
  515. if (tComplention)
  516. tComplention(weak_self.status, info, request.error);
  517. });
  518. };
  519. [request setFailedBlock:^{
  520. processRequest();
  521. }];
  522. [request setCompletionBlock:^{
  523. processRequest();
  524. }];
  525. [request startAsynchronous];
  526. }
  527. - (void)unactivateDeviceWithcomplention:(ActivityComplention)complention {
  528. __block ActivityComplention tComplention = complention;
  529. __block VerificationManager *weak_self = self;
  530. //Process Params
  531. NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description];
  532. if (!app_version.length)
  533. app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description];
  534. NSDictionary *params =
  535. @{
  536. @"data":@{
  537. //激活信息
  538. @"subscription":@{
  539. @"app_code":[self bundleIdentify]?:@"",
  540. },
  541. //设备信息
  542. @"device":@{
  543. @"unique_sn":[self udid]?:@"",
  544. @"model":GetProductName()?:@"",
  545. @"os":GetSystemVersion()?:@"",
  546. @"time_zone":[[NSTimeZone localTimeZone] localizedName:NSTimeZoneNameStyleShortDaylightSaving locale:[NSLocale currentLocale]]?:@"",
  547. @"language":[[NSLocale currentLocale] localeIdentifier]?:@"",
  548. @"app_version":app_version?:@"",
  549. @"platform":@"DMG"
  550. }
  551. }
  552. };
  553. if (!self.rsa) {
  554. self.rsa = [[KMVerificationRSA alloc] init];
  555. }
  556. NSString *uuid = [self udid]?:@"";
  557. // if (uuid.length > @"8D397BE0-F478-5269-A8F1".length) {
  558. // uuid = [uuid substringToIndex:@"8D397BE0-F478-5269-A8F1".length];
  559. // }
  560. NSString *verifyString = [self.rsa activeWithUniquesn:uuid
  561. cdkey:@""
  562. model:GetProductName()?:@""
  563. os:GetSystemVersion()?:@""
  564. language:[[NSLocale currentLocale] localeIdentifier]?:@""
  565. appversion:app_version?:@""];
  566. if (verifyString.length == 0) {
  567. return;
  568. }
  569. params = @{@"verifyParam":verifyString};
  570. NSMutableData *postData = [VerificationManager mutableDataWithDic:params];
  571. // Send Request
  572. NSString *urlString = [kVerificationPDFullServer stringByAppendingString:@"oem/cancel"];
  573. NSURL *url = [NSURL URLWithString:urlString];
  574. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  575. request.requestMethod = @"POST";
  576. [request setPostBody:postData];
  577. NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];
  578. NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  579. postLength, @"Content-Length", nil];
  580. request.defaultResponseEncoding = NSUTF8StringEncoding;
  581. // [request setAuthenticationScheme:@"https"];//设置验证方式
  582. // [request setValidatesSecureCertificate:NO];//设置验证证书
  583. [request setRequestHeaders:requestHeaders];
  584. [request addRequestHeader:@"Content-Type" value:@"application/json"];
  585. [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"];
  586. __block void(^processRequest)(void) = ^{
  587. dispatch_async(dispatch_get_main_queue(), ^{
  588. NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject];
  589. NSError *error = request.error;
  590. if ([info valueForKey:@"msg"]) {
  591. NSString *detailKey = [info valueForKey:@"msg"];
  592. if ([detailKey isEqualToString:@"exceed_max_device_num"]) {
  593. error = [NSError errorWithDomain:@"激活失败"
  594. code:ActivityErrorTypeOutNumber
  595. userInfo:info];
  596. }else if ([detailKey isEqualToString:@"invalid_activate_info"]) {
  597. error = [NSError errorWithDomain:@"激活失败"
  598. code:ActivityErrorTypeInvalidInfo
  599. userInfo:info];
  600. }else if ([detailKey isEqualToString:@"invalid_license_key"]) {
  601. error = [NSError errorWithDomain:@"激活失败"
  602. code:ActivityErrorTypeCDKeyNotExist
  603. userInfo:info];
  604. }else if ([detailKey isEqualToString:@"license_not_match_product"]) {
  605. error = [NSError errorWithDomain:@"激活失败"
  606. code:ActivityErrorTypeNotMatchProduct
  607. userInfo:info];
  608. }else if ([detailKey isEqualToString:@"license_expired"]) {
  609. error = [NSError errorWithDomain:@"激活失败"
  610. code:ActivityErrorTypeCDKeyExpire
  611. userInfo:info];
  612. }else if ([detailKey isEqualToString:@" Activated success!"]) {
  613. error = nil;
  614. }else {
  615. error = [NSError errorWithDomain:@"激活失败"
  616. code:ActivityErrorTypeUnknow
  617. userInfo:info];
  618. }
  619. } else {
  620. if(error) {
  621. error = [NSError errorWithDomain:error.domain code:ActivityErrorTypeNetworkDisable userInfo:error.userInfo];
  622. } else {
  623. error = [NSError errorWithDomain:@"激活失败"
  624. code:ActivityErrorTypeUnknow
  625. userInfo:@{}];
  626. }
  627. }
  628. if (!error) {
  629. NSMutableDictionary *dic = [weak_self.detailInfo?:@{} mutableCopy];
  630. for (NSString *key in info[@"data"]) {
  631. [dic setValue:info[@"data"][key] forKey:key];
  632. }
  633. weak_self.detailInfo = dic;
  634. NSString *notificationName = (NSString *)kDeviceActivateStatusChangeNotification;
  635. [[NSNotificationCenter defaultCenter] postNotificationName:notificationName
  636. object:self
  637. userInfo:info];
  638. // [self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) {
  639. [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) {
  640. }];
  641. }
  642. [weak_self restartTimer];
  643. if (tComplention)
  644. tComplention(weak_self.status, info, error);
  645. });
  646. };
  647. [request setFailedBlock:^{
  648. processRequest();
  649. }];
  650. [request setCompletionBlock:^{
  651. processRequest();
  652. }];
  653. [request startAsynchronous];
  654. }
  655. - (void)getCodeWithEmail:(NSString*)email
  656. complention:(ActivityComplention)complention {
  657. __block ActivityComplention tComplention = complention;
  658. __block VerificationManager *weak_self = self;
  659. //Process Params
  660. NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description];
  661. if (!app_version.length)
  662. app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description];
  663. NSString *platform = @"DMG";
  664. #ifdef AppStore_Version
  665. #if Lite_Version
  666. platform = @"AppleStore";
  667. #else
  668. platform = @"AppleStorePro";
  669. #endif
  670. #endif
  671. NSDictionary *params =
  672. @{
  673. @"data":@{
  674. @"app_code":[self bundleIdentify]?:@"",
  675. @"email":email?:@"",
  676. @"unique_sn":[self udid]?:@"",
  677. @"platform":platform?:@"AppleStore"
  678. }
  679. };
  680. NSMutableData *postData = [VerificationManager mutableDataWithDic:params];
  681. //Send Request
  682. NSString *urlString = [kVerificationServer stringByAppendingString:@"api/invites/getCode"];
  683. NSURL *url = [NSURL URLWithString:urlString];
  684. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  685. request.requestMethod = @"POST";
  686. [request setPostBody:postData];
  687. NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];
  688. NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  689. postLength, @"Content-Length", nil];
  690. request.defaultResponseEncoding = NSUTF8StringEncoding;
  691. // [request setAuthenticationScheme:@"https"];//设置验证方式
  692. // [request setValidatesSecureCertificate:NO];//设置验证证书
  693. [request setRequestHeaders:requestHeaders];
  694. [request addRequestHeader:@"Content-Type" value:@"application/json"];
  695. [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"];
  696. __block void(^processRequest)(void) = ^{
  697. dispatch_async(dispatch_get_main_queue(), ^{
  698. NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject];
  699. if ([info valueForKey:@"data"] &&
  700. [[info valueForKey:@"data"] valueForKey:@"email"] &&
  701. [[info valueForKey:@"data"] valueForKey:@"share_code"]) {
  702. //存储信息,并发送更新通知
  703. NSMutableDictionary *dic = [weak_self.detailInfo?:@{} mutableCopy];
  704. [dic setValue:info[@"data"] forKey:@"invite"];
  705. weak_self.detailInfo = dic;
  706. [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) {
  707. }];
  708. }
  709. //重置计时器
  710. [weak_self restartTimer];
  711. if (tComplention)
  712. tComplention(weak_self.status, info, request.error);
  713. });
  714. };
  715. [request setFailedBlock:^{
  716. processRequest();
  717. }];
  718. [request setCompletionBlock:^{
  719. processRequest();
  720. }];
  721. [request startAsynchronous];
  722. }
  723. //使用邀请码
  724. - (void)userCode:(NSString*)code
  725. complention:(ActivityComplention)complention {
  726. __block ActivityComplention tComplention = complention;
  727. __block VerificationManager *weak_self = self;
  728. //Process Params
  729. NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description];
  730. if (!app_version.length)
  731. app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description];
  732. BOOL iszhcn = !([NSLocalizedString(@"Language", nil) isEqualToString:@"English"] &&
  733. [NSLocalizedString(@"Language", nil) isEqualToString:@"Français"]);
  734. NSString *platform = @"DMG";
  735. #ifdef AppStore_Version
  736. #if Lite_Version
  737. platform = @"AppleStore";
  738. #else
  739. platform = @"AppleStorePro";
  740. #endif
  741. #endif
  742. NSDictionary *params =
  743. @{
  744. @"data":@{
  745. @"app_code":[self bundleIdentify]?:@"",
  746. @"code":code?:@"",
  747. @"unique_sn":[self udid]?:@"",
  748. @"lang":@(iszhcn),
  749. @"platform":platform?:@"AppleStore"
  750. }
  751. };
  752. NSMutableData *postData = [VerificationManager mutableDataWithDic:params];
  753. //Send Request
  754. NSString *urlString = [kVerificationServer stringByAppendingString:@"api/invites/activeInvite"];
  755. NSURL *url = [NSURL URLWithString:urlString];
  756. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  757. request.requestMethod = @"POST";
  758. [request setPostBody:postData];
  759. NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];
  760. NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  761. postLength, @"Content-Length", nil];
  762. request.defaultResponseEncoding = NSUTF8StringEncoding;
  763. // [request setAuthenticationScheme:@"https"];//设置验证方式
  764. // [request setValidatesSecureCertificate:NO];//设置验证证书
  765. [request setRequestHeaders:requestHeaders];
  766. [request addRequestHeader:@"Content-Type" value:@"application/json"];
  767. [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"];
  768. __block void(^processRequest)(void) = ^{
  769. dispatch_async(dispatch_get_main_queue(), ^{
  770. NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject];
  771. ActivityStatus status = weak_self.status;
  772. if ([info valueForKey:@"data"] &&
  773. [[info valueForKey:@"data"] valueForKey:@"device"]) {
  774. ActivityStatus tStatus = [(NSDictionary*)info[@"data"] status];
  775. weak_self.detailInfo = info[@"data"];
  776. if (status != tStatus) {
  777. //发送通知
  778. NSString *notificationName = (NSString *)kDeviceActivateStatusChangeNotification;
  779. [[NSNotificationCenter defaultCenter] postNotificationName:notificationName
  780. object:self
  781. userInfo:info];
  782. }
  783. [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) {
  784. }];
  785. }
  786. //重置计时器
  787. [weak_self restartTimer];
  788. if (tComplention)
  789. tComplention(weak_self.status, info, request.error);
  790. });
  791. };
  792. [request setFailedBlock:^{
  793. processRequest();
  794. }];
  795. [request setCompletionBlock:^{
  796. processRequest();
  797. }];
  798. [request startAsynchronous];
  799. }
  800. - (void)modifyEmail:(NSString*)email
  801. complention:(ActivityComplention)complention {
  802. __block ActivityComplention tComplention = complention;
  803. __block VerificationManager *weak_self = self;
  804. //Process Params
  805. NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description];
  806. if (!app_version.length)
  807. app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description];
  808. BOOL iszhcn = !([NSLocalizedString(@"Language", nil) isEqualToString:@"English"] &&
  809. [NSLocalizedString(@"Language", nil) isEqualToString:@"Français"]);
  810. NSDictionary *params =
  811. @{
  812. @"data":@{
  813. @"app_code":[self bundleIdentify]?:@"",
  814. @"email":email?:@"",
  815. @"unique_sn":[self udid]?:@"",
  816. @"lang":@(iszhcn),
  817. }
  818. };
  819. NSMutableData *postData = [VerificationManager mutableDataWithDic:params];
  820. //Send Request
  821. NSString *urlString = [kVerificationServer stringByAppendingString:@"api/invites/updateEmail"];
  822. NSURL *url = [NSURL URLWithString:urlString];
  823. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  824. request.requestMethod = @"POST";
  825. [request setPostBody:postData];
  826. NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];
  827. NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  828. postLength, @"Content-Length", nil];
  829. request.defaultResponseEncoding = NSUTF8StringEncoding;
  830. // [request setAuthenticationScheme:@"https"];//设置验证方式
  831. // [request setValidatesSecureCertificate:NO];//设置验证证书
  832. [request setRequestHeaders:requestHeaders];
  833. [request addRequestHeader:@"Content-Type" value:@"application/json"];
  834. [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"];
  835. __block void(^processRequest)(void) = ^{
  836. dispatch_async(dispatch_get_main_queue(), ^{
  837. NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject];
  838. if ([info valueForKey:@"data"] &&
  839. [[info valueForKey:@"data"] valueForKey:@"email"] &&
  840. [[info valueForKey:@"data"] valueForKey:@"share_code"]) {
  841. //存储信息,并发送更新通知
  842. NSMutableDictionary *dic = [weak_self.detailInfo?:@{} mutableCopy];
  843. [dic setValue:info[@"data"] forKey:@"invite"];
  844. weak_self.detailInfo = dic;
  845. [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) {
  846. }];
  847. }
  848. //重置计时器
  849. [weak_self restartTimer];
  850. if (tComplention)
  851. tComplention(weak_self.status, info, request.error);
  852. });
  853. };
  854. [request setFailedBlock:^{
  855. processRequest();
  856. }];
  857. [request setCompletionBlock:^{
  858. processRequest();
  859. }];
  860. [request startAsynchronous];
  861. }
  862. //给好友发送邀请码
  863. - (void)sendInvateEmails:(NSString*)emails
  864. code:(NSString*)code
  865. complention:(ActivityComplention)complention {
  866. __block ActivityComplention tComplention = complention;
  867. __block VerificationManager *weak_self = self;
  868. //Process Params
  869. NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description];
  870. if (!app_version.length)
  871. app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description];
  872. BOOL iszhcn = !([NSLocalizedString(@"Language", nil) isEqualToString:@"English"] &&
  873. [NSLocalizedString(@"Language", nil) isEqualToString:@"Français"]);
  874. NSDictionary *params =
  875. @{
  876. @"data":@{
  877. @"app_code":[self bundleIdentify]?:@"",
  878. @"email":emails?:@"",
  879. @"code":[code stringByReplacingOccurrencesOfString:@" " withString:@""]?:@"",
  880. @"unique_sn":[self udid]?:@"",
  881. @"lang":@(iszhcn),
  882. }
  883. };
  884. NSMutableData *postData = [VerificationManager mutableDataWithDic:params];
  885. //Send Request
  886. NSString *urlString = [kVerificationServer stringByAppendingString:@"api/invites/shareEmail"];
  887. NSURL *url = [NSURL URLWithString:urlString];
  888. ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
  889. request.requestMethod = @"POST";
  890. [request setPostBody:postData];
  891. NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]];
  892. NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys:
  893. postLength, @"Content-Length", nil];
  894. request.defaultResponseEncoding = NSUTF8StringEncoding;
  895. // [request setAuthenticationScheme:@"https"];//设置验证方式
  896. // [request setValidatesSecureCertificate:NO];//设置验证证书
  897. [request setRequestHeaders:requestHeaders];
  898. [request addRequestHeader:@"Content-Type" value:@"application/json"];
  899. [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"];
  900. __block void(^processRequest)(void) = ^{
  901. dispatch_async(dispatch_get_main_queue(), ^{
  902. NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject];
  903. //重置计时器
  904. [weak_self restartTimer];
  905. if (tComplention)
  906. tComplention(weak_self.status, info, request.error);
  907. [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) {
  908. }];
  909. });
  910. };
  911. [request setFailedBlock:^{
  912. processRequest();
  913. }];
  914. [request setCompletionBlock:^{
  915. processRequest();
  916. }];
  917. [request startAsynchronous];
  918. }
  919. #pragma mark - Data Formatter
  920. - (BOOL)isValidLicenseCode:(NSString *)cdKey {
  921. NSString *validKey = [cdKey stringByReplacingOccurrencesOfString:@" " withString:@""];
  922. if (validKey.length != 19) {
  923. return NO;
  924. }
  925. //计算字符串长度
  926. NSInteger str_length = [validKey length];
  927. NSString *regex = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-";
  928. for (int i = 0; i<str_length; i++) {
  929. NSString *subStr = [validKey substringWithRange:NSMakeRange(i, 1)];
  930. if([regex rangeOfString:subStr].location == NSNotFound) {
  931. return NO;
  932. }
  933. }
  934. return YES;
  935. }
  936. + (NSString*)postStringWithObject:(id)value {
  937. NSString *string;
  938. if ([value isKindOfClass:[NSArray class]]) {
  939. string = @"[";
  940. BOOL isFirst = YES;
  941. for (id object in value) {
  942. if (isFirst) {
  943. isFirst = NO;
  944. }else {
  945. string = [string stringByAppendingString:@","];
  946. }
  947. string = [string stringByAppendingString:[VerificationManager postStringWithObject:object]];
  948. }
  949. string = [string stringByAppendingString:@"]"];
  950. }else if ([value isKindOfClass:[NSDictionary class]]) {
  951. string = @"{";
  952. BOOL isFirst = YES;
  953. for (NSString *key in [value allKeys]) {
  954. if (isFirst) {
  955. isFirst = NO;
  956. }else {
  957. string = [string stringByAppendingString:@","];
  958. }
  959. string = [string stringByAppendingFormat:@"\"%@\"", key];
  960. string = [string stringByAppendingString:@":"];
  961. string = [string stringByAppendingString:[VerificationManager postStringWithObject:[value valueForKey:key]]];
  962. }
  963. string = [string stringByAppendingString:@"}"];
  964. }else {
  965. string = [NSString stringWithFormat:@"\"%@\"", value];
  966. }
  967. return string;
  968. }
  969. + (NSString*)postStringWithDic:(NSDictionary*)dic {
  970. return [VerificationManager postStringWithObject:dic];
  971. }
  972. + (NSMutableData*)mutableDataWithDic:(NSDictionary*)dic {
  973. NSString *json = [VerificationManager postStringWithDic:dic];
  974. json = [json stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  975. json = [json stringByReplacingOccurrencesOfString:@"\r\0" withString:@""];
  976. json = [json stringByReplacingOccurrencesOfString:@"\0" withString:@""];
  977. NSData *postData = [json dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
  978. return [postData mutableCopy];
  979. }
  980. @end
  981. #pragma mark - Test
  982. #if DEBUG
  983. @implementation VerificationManager (Test)
  984. //- (ActivityStatus)status {
  985. // return ActivityStatusVerification;
  986. //}
  987. - (void)updateVerificationStatus {
  988. [self updateStatus:ActivityStatusVerification postNo:true];
  989. }
  990. - (void)updateStatus:(ActivityStatus)status postNo:(bool)postNo {
  991. NSString *subValue = @"";
  992. NSString *deviceValue = @"";
  993. NSTimeInterval endTime = 0;
  994. if (status == ActivityStatusNone) {
  995. deviceValue = @"";
  996. } else if (status == ActivityStatusTrial) {
  997. deviceValue = @"on_trial";
  998. } else if (status == ActivityStatusTrialExpire) {
  999. deviceValue = @"expired_trial";
  1000. } else if (status == ActivityStatusVerification) {
  1001. deviceValue = @"activated";
  1002. endTime = [NSDate date].timeIntervalSince1970 + 60 * 60;
  1003. } else if (status == ActivityStatusVerifExpire) {
  1004. deviceValue = @"expired_license";
  1005. } else if (status == ActivityStatusLicenseExpire) {
  1006. deviceValue = @"";
  1007. subValue = @"expired";
  1008. }
  1009. NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:self.detailInfo];
  1010. dict[@"subscription"][@"status"] = subValue;
  1011. dict[@"device"][@"status"] = deviceValue;
  1012. if (endTime != 0) {
  1013. dict[@"device"][@"end_date"] = @(endTime);
  1014. dict[@"subscription"][@"end_date"] = @(endTime);
  1015. }
  1016. self.detailInfo = dict;
  1017. if (postNo) {
  1018. //发送通知
  1019. NSString *notificationName = (NSString *)kDeviceActivateStatusChangeNotification;
  1020. [[NSNotificationCenter defaultCenter] postNotificationName:notificationName
  1021. object:self
  1022. userInfo:nil];
  1023. }
  1024. }
  1025. @end
  1026. #endif
  1027. #pragma mark - NSDictionary + ActivityInfo
  1028. @implementation NSDictionary (ActivityInfo)
  1029. - (NSDate*)expireDate {
  1030. if (self[@"device"][@"end_date"]) {
  1031. return [self dateForValue:self[@"device"][@"end_date"]];
  1032. } else if (self[@"subscription"][@"endDate"]) {
  1033. return [self dateForValue:self[@"subscription"][@"endDate"]];
  1034. }
  1035. return nil;
  1036. }
  1037. - (NSDate*)createDate {
  1038. if (self[@"device"][@"created_at"]) {
  1039. return [self dateForValue:self[@"device"][@"created_at"]];
  1040. }
  1041. return nil;
  1042. }
  1043. - (ActivityStatus)status {
  1044. ActivityStatus tStatus = ActivityStatusTrialExpire;
  1045. if ([[self[@"subscription"][@"status"] lowercaseString] isEqualToString:@"expired"]) {
  1046. tStatus = ActivityStatusLicenseExpire;
  1047. }else{
  1048. if ([[self[@"device"][@"status"] lowercaseString] isEqualToString:@"activated"]) {
  1049. tStatus = ActivityStatusVerification;
  1050. }else if ([[self[@"device"][@"status"] lowercaseString] isEqualToString:@"on_trial"]) {
  1051. tStatus = ActivityStatusTrial;
  1052. }else if ([[self[@"device"][@"status"] lowercaseString] isEqualToString:@"expired_trial"]) {
  1053. tStatus = ActivityStatusTrialExpire;
  1054. }else if ([[self[@"device"][@"status"] lowercaseString] isEqualToString:@"expired_license"]) {
  1055. tStatus = ActivityStatusVerifExpire;
  1056. }
  1057. }
  1058. if (ActivityStatusTrial == tStatus) {
  1059. if (self.expireDate &&
  1060. [NSDate date].timeIntervalSince1970 - [self expireDate].timeIntervalSince1970 > 0) {
  1061. tStatus = ActivityStatusTrialExpire;
  1062. }
  1063. }else if (ActivityStatusVerification == tStatus) {
  1064. if (self.expireDate &&
  1065. [NSDate date].timeIntervalSince1970 - [self expireDate].timeIntervalSince1970 > 0) {
  1066. tStatus = ActivityStatusVerifExpire;
  1067. }
  1068. if (self[@"dejvice"][@"status"] &&
  1069. [self[@"device"][@"status"] isEqualToString:@"expired_license"]) {
  1070. //激活码过期
  1071. tStatus = ActivityStatusVerifExpire;
  1072. }
  1073. }
  1074. return tStatus;
  1075. }
  1076. - (NSString*)licenseCode {
  1077. return self[@"device"][@"cdkey"];
  1078. }
  1079. - (NSString*)originLicenseCode {
  1080. return self[@"device"][@"origin_cdkey"];
  1081. }
  1082. - (NSString*)shareCode {
  1083. return self[@"invite"][@"share_code"]?:@"";
  1084. }
  1085. - (NSUInteger)invateUsersCount {
  1086. return [self[@"invite"][@"new_users_num"] integerValue];
  1087. }
  1088. - (NSString*)fromCode {
  1089. return self[@"invite"][@"from_code"]?:@"";
  1090. }
  1091. - (NSDate*)fetchInvateDate {
  1092. if (self[@"invite"][@"created_at"]) {
  1093. return [self dateForValue:self[@"invite"][@"created_at"]];
  1094. }
  1095. return nil;
  1096. }
  1097. - (NSDate*)dateForValue:(id)value {
  1098. if ([value isKindOfClass:[NSString class]]) {
  1099. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  1100. [formatter setLocale:[NSLocale systemLocale]];
  1101. [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
  1102. return [formatter dateFromString:[value substringToIndex:@"yyyy-MM-dd HH:mm:ss".length]];
  1103. }else if ([value isKindOfClass:[NSNumber class]]) {
  1104. return [NSDate dateWithTimeIntervalSince1970:[value doubleValue]];
  1105. }
  1106. return nil;
  1107. }
  1108. - (BOOL)repeatTrial {
  1109. return [self[@"device"][@"trial_again"] boolValue];
  1110. }
  1111. - (NSUInteger)trialTimes {
  1112. return [self[@"device"][@"trial_times"]?:@"1" intValue];
  1113. }
  1114. - (NSString *)email {
  1115. return self[@"device"][@"email"]?:@"";
  1116. }
  1117. - (NSString *)subscriptionEmail {
  1118. return self[@"subscription"][@"email"]?:@"";
  1119. }
  1120. - (NSString *)accountName {
  1121. return self[@"device"][@"contact_name"]?:@"";
  1122. }
  1123. - (NSUInteger)maxFreeDays {
  1124. return [self[@"invite"][@"max_free_days"]?:@"365" intValue];
  1125. }
  1126. - (BOOL)allowUnbind {
  1127. return [self[@"device"][@"allow_unbind"] boolValue];
  1128. }
  1129. - (NSInteger)unbindTimes {
  1130. return [self[@"device"][@"unbind_time"]?:@"0" integerValue];
  1131. }
  1132. @end