// // VerificationManager.m // Filmage Screen // // Created by 朱东勇 on 2018/2/8. // Copyright © 2018年 kdan mobile. All rights reserved. // #import "VerificationManager.h" #import "NSNULL+Filtration.h" #import "NSObject+DeviceInfo.h" #import "JSONKit.h" #import "ASIFormDataRequest.h" const NSString *kDeviceVerifyFinishNotification = @"kDeviceVerifyFinishNotification"; const NSString *kDeviceActivateStatusChangeNotification = @"kDeviceActivateNotification"; static VerificationManager *__verificationManager = nil; @interface VerificationManager () @property (nonatomic, strong) NSTimer *timer; @property (nonatomic) BOOL isVerifing; @end @implementation VerificationManager + (VerificationManager*)manager { #if VERSION_DMG if (!__verificationManager) __verificationManager = [[VerificationManager alloc] init]; #else return nil; #endif return __verificationManager; } + (VerificationManager*)defaultManager { #if VERSION_DMG if (!__verificationManager) __verificationManager = [[VerificationManager alloc] init]; #else return nil; #endif return __verificationManager; } - (instancetype)init { self = [super init]; self.autoVerification = YES; #if !DEBUG // 非测试环境时 #if kTestMode // 测试模式时 报错 //#error 服务器未切换到正式服务器! #endif #endif dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) { }]; }); return self; } #pragma mark - Setter & Getter - (ActivityStatus)status { // if (self.detailInfo.status == ActivityStatusNone) { // return ActivityStatusTrialExpire; // } return self.detailInfo.status; } - (BOOL)needUpgradeLicense { if ([self.detailInfo[@"device"][@"product_code"] isEqualToString:@"com.brother.pdfreaderpro.mac.product_1"] || [self.detailInfo[@"device"][@"product_code"] isEqualToString:@"com.brother.pdfreaderpro.cross.platform.product_1"]) { return YES; } // if ([self.detailInfo[@"subscription"][@"product"][@"code"] isEqualToString:@"com.brother.pdfreaderpro.mac.product_1"]) { // return YES; // } return NO; } - (BOOL)secondTrialEnabled { return self.detailInfo.repeatTrial; } - (NSInteger)trialTimes { return self.detailInfo.trialTimes; } - (NSString *)licenseCode { return self.detailInfo.licenseCode; } - (NSString *)originLicenseCode { return self.detailInfo.originLicenseCode; } - (NSString *)email { return self.detailInfo.email?:@""; } - (NSString *)subscriptionEmail { return self.detailInfo.subscriptionEmail?:@""; } - (NSString *)accountName { return self.detailInfo.accountName; } - (BOOL)allowUnbind { return self.detailInfo.allowUnbind; } - (NSInteger)unbindTimes { return self.detailInfo.unbindTimes; } - (NSString*)udid { //#if DEBUG // NSString *cachePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"uuid.plist"]; // NSMutableDictionary *info = [[NSDictionary dictionaryWithContentsOfFile:cachePath] mutableCopy]; // // if (!info) { // info = [NSMutableDictionary dictionary]; // // [info setValue:[NSString stringWithFormat:@"TestInvate%.0f", [NSDate date].timeIntervalSince1970] // forKey:@"uuid"]; // // // [[NSFileManager defaultManager] createDirectoryAtPath:NSTemporaryDirectory() // withIntermediateDirectories:YES // attributes:nil // error:nil]; // [info writeToFile:cachePath atomically:YES]; // } // // return info[@"uuid"]?:(GetHardwareUUID()?:@""); //#endif return GetHardwareUUID()?:@""; } - (NSString *)bundleIdentify { return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleIdentifier"]?:@""; } - (void)setDetailInfo:(NSDictionary *)detailInfo { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setValue:[detailInfo JSONData] forKey:@"detailInfo"]; [defaults synchronize]; } - (NSDictionary*)detailInfo { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; return [[[defaults valueForKey:@"detailInfo"] objectFromJSONData] filterNullObject]; } // //- (void)setIsTest:(BOOL)isTest { // _isTest = isTest; // // [self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) { // // }]; //} - (void)setAutoVerification:(BOOL)autoVerification { _autoVerification = autoVerification; [self restartTimer]; } - (BOOL)isVerificationCode { if (self.detailInfo.status == ActivityStatusVerification) { return YES; } return NO; } - (BOOL)showTrialGuide { if (self.detailInfo.status == ActivityStatusNone || self.detailInfo.status == ActivityStatusTrial) { return YES; } return NO; } #pragma mark - Timer - (void)restartTimer { if (self.timer) [self.timer invalidate]; self.timer = nil; if (self.autoVerification && self.status != ActivityStatusNone) { self.timer = [NSTimer scheduledTimerWithTimeInterval:60 * 60//一个小时更新一次 target:self selector:@selector(verificationFire) userInfo:nil repeats:NO]; } } - (void)verificationFire { [self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) { }]; } #pragma mark - Interface - (void)activateDeviceWithInfo:(NSDictionary*)info complention:(ActivityComplention)complention { if (![info[@"cdkey"] length]) { //激活码为空 NSError *error = [NSError errorWithDomain:@"" code:ActivityErrorTypeCDKeyEmpty userInfo:nil]; if (complention) complention(NO, nil, error); return; } if (![self isValidLicenseCode:info[@"cdkey"]]) { //激活码格式不正确 NSError *error = [NSError errorWithDomain:@"激活失败" code:ActivityErrorTypeFormatError userInfo:nil]; if (complention) complention(NO, @{@"errors":@"FormatError"}, error); return; } __block ActivityComplention tComplention = complention; __block VerificationManager *weak_self = self; //Process Params NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description]; if (!app_version.length) app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description]; NSDictionary *params = @{ @"data":@{ //激活信息 @"subscription":@{ // @"app_code":[self bundleIdentify]?:@"", @"app_code":@"com.brother.pdftecheditor", @"email":info[@"email"]?:@"", @"cdkey":info[@"cdkey"]?:@"", }, //设备信息 @"device":@{ @"unique_sn":[self udid]?:@"", @"model":GetProductName()?:@"", @"os":GetSystemVersion()?:@"", @"time_zone":[[NSTimeZone localTimeZone] localizedName:NSTimeZoneNameStyleShortDaylightSaving locale:[NSLocale currentLocale]]?:@"", @"language":[[NSLocale currentLocale] localeIdentifier]?:@"", @"app_version":app_version?:@"", @"platform":@"DMG" } } }; NSMutableData *postData = [VerificationManager mutableDataWithDic:params]; //Send Request NSString *urlString = [kVerificationServer stringByAppendingString:@"api/auth_devices/activate"]; NSURL *url = [NSURL URLWithString:urlString]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; request.requestMethod = @"POST"; [request setPostBody:postData]; NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]]; NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys: postLength, @"Content-Length", nil]; request.defaultResponseEncoding = NSUTF8StringEncoding; // [request setAuthenticationScheme:@"https"];//设置验证方式 // [request setValidatesSecureCertificate:NO];//设置验证证书 [request setRequestHeaders:requestHeaders]; [request addRequestHeader:@"Content-Type" value:@"application/json"]; [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"]; __block void(^processRequest)(void) = ^{ dispatch_async(dispatch_get_main_queue(), ^{ NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject]; NSError *error = request.error; if (info && [info isKindOfClass:[NSDictionary class]]) { if ([info valueForKey:@"errors"]) { NSString *detailKey = [[info[@"errors"] firstObject] valueForKey:@"detail_key"]; if ([detailKey isEqualToString:@"exceed_max_device_num"]) { error = [NSError errorWithDomain:@"激活失败" code:ActivityErrorTypeOutNumber userInfo:info]; }else if ([detailKey isEqualToString:@"invalid_activate_info"]) { error = [NSError errorWithDomain:@"激活失败" code:ActivityErrorTypeInvalidInfo userInfo:info]; }else if ([detailKey isEqualToString:@"invalid_license_key"]) { error = [NSError errorWithDomain:@"激活失败" code:ActivityErrorTypeCDKeyNotExist userInfo:info]; }else if ([detailKey isEqualToString:@"license_not_match_product"]) { error = [NSError errorWithDomain:@"激活失败" code:ActivityErrorTypeNotMatchProduct userInfo:info]; }else if ([detailKey isEqualToString:@"license_expired"]) { error = [NSError errorWithDomain:@"激活失败" code:ActivityErrorTypeCDKeyExpire userInfo:info]; }else { error = [NSError errorWithDomain:@"激活失败" code:ActivityErrorTypeUnknow userInfo:info]; } }else if ([info valueForKey:@"error"]) { error = [NSError errorWithDomain:@"激活失败" code:ActivityErrorTypeUnknow userInfo:info]; } }else { if(error) { error = [NSError errorWithDomain:error.domain code:ActivityErrorTypeNetworkDisable userInfo:error.userInfo]; } else { error = [NSError errorWithDomain:@"激活失败" code:ActivityErrorTypeUnknow userInfo:@{}]; } } if (!error) { NSMutableDictionary *dic = [weak_self.detailInfo?:@{} mutableCopy]; for (NSString *key in info[@"data"]) { [dic setValue:info[@"data"][key] forKey:key]; } weak_self.detailInfo = dic; NSString *notificationName = (NSString *)kDeviceActivateStatusChangeNotification; [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:self userInfo:info]; [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) { }]; } [weak_self restartTimer]; [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) { }]; if (tComplention) tComplention(weak_self.status, info, error); }); }; [request setFailedBlock:^{ processRequest(); }]; [request setCompletionBlock:^{ processRequest(); }]; [request startAsynchronous]; } - (void)verificationWithComplention:(ActivityComplention)complention { __block ActivityComplention tComplention = complention; __block VerificationManager *weak_self = self; if (!self.isVerifing) { self.isVerifing = YES; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //Process Params NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description]; if (!app_version.length) app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description]; NSString *platform = @"DMG"; #if VERSION_FREE #if VERSION_DMG #else platform = @"AppleStore"; #endif #else platform = @"AppleStorePro"; #endif NSDictionary *params = @{ @"data":@{ //验证信息 @"subscription":@{ // @"app_code":[self bundleIdentify]?:@"", @"app_code":@"com.brother.pdftecheditor", // @"email":info[@"email"]?:@"", // @"cdkey":info[@"cdkey"]?:@"", }, //设备信息 @"device":@{ @"unique_sn":[self udid]?:@"", @"model":GetProductName()?:@"", @"os":GetSystemVersion()?:@"", @"time_zone":[[NSTimeZone localTimeZone] localizedName:NSTimeZoneNameStyleShortDaylightSaving locale:[NSLocale currentLocale]]?:@"", @"language":[[NSLocale currentLocale] localeIdentifier]?:@"", @"app_version":app_version?:@"", @"platform":platform?:@"AppleStore" } } }; NSMutableData *postData = [VerificationManager mutableDataWithDic:params]; //Send Request NSString *urlString = [kVerificationServer stringByAppendingString:@"api/auth_devices/verify"]; NSURL *url = [NSURL URLWithString:urlString]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; request.requestMethod = @"POST"; [request setPostBody:postData]; NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]]; NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys: postLength, @"Content-Length", nil]; // [request setAuthenticationScheme:@"https"];//设置验证方式 // [request setValidatesSecureCertificate:NO];//设置验证证书 [request setRequestHeaders:requestHeaders]; [request addRequestHeader:@"Content-Type" value:@"application/json"]; [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"]; __block void(^processRequest)(void) = ^{ dispatch_async(dispatch_get_main_queue(), ^{ NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject]; ActivityStatus status = weak_self.status; if ([info valueForKey:@"data"] && [[info valueForKey:@"data"] valueForKey:@"device"]) { ActivityStatus tStatus = [(NSDictionary*)info[@"data"] status]; //存储信息,并发送更新通知 weak_self.detailInfo = info[@"data"]; // if (status != tStatus || // [[info valueForKey:@"data"] valueForKey:@"invite"]) { //发送通知 NSString *notificationName = (NSString *)kDeviceActivateStatusChangeNotification; [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:weak_self userInfo:info]; // } } [[NSNotificationCenter defaultCenter] postNotificationName:(NSString *)kDeviceVerifyFinishNotification object:nil userInfo:nil]; weak_self.isVerifing = NO; //重置计时器 [weak_self restartTimer]; if (tComplention) tComplention(weak_self.status, info, request.error); }); }; [request setFailedBlock:^{ processRequest(); }]; [request setCompletionBlock:^{ processRequest(); }]; [request startAsynchronous]; }); }else { __block void(^waitBlock)(void) = ^{ if (weak_self.isVerifing) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ waitBlock(); }); }else { if (tComplention) tComplention(weak_self.status, @{@"data":weak_self.detailInfo?:@{}}, nil); waitBlock = NULL; } }; waitBlock(); } } - (void)trialForDays:(float)days complention:(ActivityComplention)complention { [self trialForDays:days email:nil name:nil complention:complention]; } - (void)trialForDays:(float)days email:(NSString*)email name:(NSString*)name complention:(ActivityComplention)complention { __block ActivityComplention tComplention = complention; __block VerificationManager *weak_self = self; //Process Params NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description]; if (!app_version.length) app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description]; NSDictionary *params = @{ @"data":@{ //激活信息 @"subscription":@{ // @"app_code":[self bundleIdentify]?:@"", @"app_code":@"com.brother.pdftecheditor", @"trail_days":@(MAX(days, 1)), @"email":email?:@"", @"contact_name":name?:@"" }, //设备信息 @"device":@{ @"unique_sn":[self udid]?:@"", @"model":GetProductName()?:@"", @"os":GetSystemVersion()?:@"", @"time_zone":[[NSTimeZone localTimeZone] localizedName:NSTimeZoneNameStyleShortDaylightSaving locale:[NSLocale currentLocale]]?:@"", @"language":[[NSLocale currentLocale] localeIdentifier]?:@"", @"app_version":app_version?:@"" } } }; NSMutableData *postData = [VerificationManager mutableDataWithDic:params]; //Send Request NSString *urlString = [kVerificationServer stringByAppendingString:@"api/auth_devices/trial"]; NSURL *url = [NSURL URLWithString:urlString]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; request.requestMethod = @"POST"; [request setPostBody:postData]; NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]]; NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys: postLength, @"Content-Length", nil]; request.defaultResponseEncoding = NSUTF8StringEncoding; // [request setAuthenticationScheme:@"https"];//设置验证方式 // [request setValidatesSecureCertificate:NO];//设置验证证书 [request setRequestHeaders:requestHeaders]; [request addRequestHeader:@"Content-Type" value:@"application/json"]; [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"]; __block void(^processRequest)(void) = ^{ dispatch_async(dispatch_get_main_queue(), ^{ NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject]; ActivityStatus status = weak_self.status; if ([info valueForKey:@"data"] && [[info valueForKey:@"data"] valueForKey:@"device"]) { ActivityStatus tStatus = [(NSDictionary*)info[@"data"] status]; //存储信息,并发送更新通知 NSMutableDictionary *dic = [weak_self.detailInfo?:@{} mutableCopy]; for (NSString *key in info[@"data"]) { [dic setValue:info[@"data"][key] forKey:key]; } weak_self.detailInfo = dic; if (status != tStatus) { //发送通知 NSString *notificationName = (NSString *)kDeviceActivateStatusChangeNotification; [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:self userInfo:info]; } } //重置计时器 [weak_self restartTimer]; if (tComplention) tComplention(weak_self.status, info, request.error); }); }; [request setFailedBlock:^{ processRequest(); }]; [request setCompletionBlock:^{ processRequest(); }]; [request startAsynchronous]; } - (void)getCodeWithEmail:(NSString*)email complention:(ActivityComplention)complention { __block ActivityComplention tComplention = complention; __block VerificationManager *weak_self = self; //Process Params NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description]; if (!app_version.length) app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description]; NSString *platform = @"DMG"; #ifdef AppStore_Version #if Lite_Version platform = @"AppleStore"; #else platform = @"AppleStorePro"; #endif #endif NSDictionary *params = @{ @"data":@{ // @"app_code":[self bundleIdentify]?:@"", @"app_code":@"com.brother.pdftecheditor", @"email":email?:@"", @"unique_sn":[self udid]?:@"", @"platform":platform?:@"AppleStore" } }; NSMutableData *postData = [VerificationManager mutableDataWithDic:params]; //Send Request NSString *urlString = [kVerificationServer stringByAppendingString:@"api/invites/getCode"]; NSURL *url = [NSURL URLWithString:urlString]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; request.requestMethod = @"POST"; [request setPostBody:postData]; NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]]; NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys: postLength, @"Content-Length", nil]; request.defaultResponseEncoding = NSUTF8StringEncoding; // [request setAuthenticationScheme:@"https"];//设置验证方式 // [request setValidatesSecureCertificate:NO];//设置验证证书 [request setRequestHeaders:requestHeaders]; [request addRequestHeader:@"Content-Type" value:@"application/json"]; [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"]; __block void(^processRequest)(void) = ^{ dispatch_async(dispatch_get_main_queue(), ^{ NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject]; if ([info valueForKey:@"data"] && [[info valueForKey:@"data"] valueForKey:@"email"] && [[info valueForKey:@"data"] valueForKey:@"share_code"]) { //存储信息,并发送更新通知 NSMutableDictionary *dic = [weak_self.detailInfo?:@{} mutableCopy]; [dic setValue:info[@"data"] forKey:@"invite"]; weak_self.detailInfo = dic; [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) { }]; } //重置计时器 [weak_self restartTimer]; if (tComplention) tComplention(weak_self.status, info, request.error); }); }; [request setFailedBlock:^{ processRequest(); }]; [request setCompletionBlock:^{ processRequest(); }]; [request startAsynchronous]; } //使用邀请码 - (void)userCode:(NSString*)code complention:(ActivityComplention)complention { __block ActivityComplention tComplention = complention; __block VerificationManager *weak_self = self; //Process Params NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description]; if (!app_version.length) app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description]; BOOL iszhcn = !([NSLocalizedString(@"Language", nil) isEqualToString:@"English"] && [NSLocalizedString(@"Language", nil) isEqualToString:@"Français"]); NSString *platform = @"DMG"; #ifdef AppStore_Version #if Lite_Version platform = @"AppleStore"; #else platform = @"AppleStorePro"; #endif #endif NSDictionary *params = @{ @"data":@{ // @"app_code":[self bundleIdentify]?:@"", @"app_code":@"com.brother.pdftecheditor", @"code":code?:@"", @"unique_sn":[self udid]?:@"", @"lang":@(iszhcn), @"platform":platform?:@"AppleStore" } }; NSMutableData *postData = [VerificationManager mutableDataWithDic:params]; //Send Request NSString *urlString = [kVerificationServer stringByAppendingString:@"api/invites/activeInvite"]; NSURL *url = [NSURL URLWithString:urlString]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; request.requestMethod = @"POST"; [request setPostBody:postData]; NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]]; NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys: postLength, @"Content-Length", nil]; request.defaultResponseEncoding = NSUTF8StringEncoding; // [request setAuthenticationScheme:@"https"];//设置验证方式 // [request setValidatesSecureCertificate:NO];//设置验证证书 [request setRequestHeaders:requestHeaders]; [request addRequestHeader:@"Content-Type" value:@"application/json"]; [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"]; __block void(^processRequest)(void) = ^{ dispatch_async(dispatch_get_main_queue(), ^{ NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject]; ActivityStatus status = weak_self.status; if ([info valueForKey:@"data"] && [[info valueForKey:@"data"] valueForKey:@"device"]) { ActivityStatus tStatus = [(NSDictionary*)info[@"data"] status]; weak_self.detailInfo = info[@"data"]; if (status != tStatus) { //发送通知 NSString *notificationName = (NSString *)kDeviceActivateStatusChangeNotification; [[NSNotificationCenter defaultCenter] postNotificationName:notificationName object:self userInfo:info]; } [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) { }]; } //重置计时器 [weak_self restartTimer]; if (tComplention) tComplention(weak_self.status, info, request.error); }); }; [request setFailedBlock:^{ processRequest(); }]; [request setCompletionBlock:^{ processRequest(); }]; [request startAsynchronous]; } - (void)modifyEmail:(NSString*)email complention:(ActivityComplention)complention { __block ActivityComplention tComplention = complention; __block VerificationManager *weak_self = self; //Process Params NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description]; if (!app_version.length) app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description]; BOOL iszhcn = !([NSLocalizedString(@"Language", nil) isEqualToString:@"English"] && [NSLocalizedString(@"Language", nil) isEqualToString:@"Français"]); NSDictionary *params = @{ @"data":@{ // @"app_code":[self bundleIdentify]?:@"", @"app_code":@"com.brother.pdftecheditor", @"email":email?:@"", @"unique_sn":[self udid]?:@"", @"lang":@(iszhcn), } }; NSMutableData *postData = [VerificationManager mutableDataWithDic:params]; //Send Request NSString *urlString = [kVerificationServer stringByAppendingString:@"api/invites/updateEmail"]; NSURL *url = [NSURL URLWithString:urlString]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; request.requestMethod = @"POST"; [request setPostBody:postData]; NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]]; NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys: postLength, @"Content-Length", nil]; request.defaultResponseEncoding = NSUTF8StringEncoding; // [request setAuthenticationScheme:@"https"];//设置验证方式 // [request setValidatesSecureCertificate:NO];//设置验证证书 [request setRequestHeaders:requestHeaders]; [request addRequestHeader:@"Content-Type" value:@"application/json"]; [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"]; __block void(^processRequest)(void) = ^{ dispatch_async(dispatch_get_main_queue(), ^{ NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject]; if ([info valueForKey:@"data"] && [[info valueForKey:@"data"] valueForKey:@"email"] && [[info valueForKey:@"data"] valueForKey:@"share_code"]) { //存储信息,并发送更新通知 NSMutableDictionary *dic = [weak_self.detailInfo?:@{} mutableCopy]; [dic setValue:info[@"data"] forKey:@"invite"]; weak_self.detailInfo = dic; [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) { }]; } //重置计时器 [weak_self restartTimer]; if (tComplention) tComplention(weak_self.status, info, request.error); }); }; [request setFailedBlock:^{ processRequest(); }]; [request setCompletionBlock:^{ processRequest(); }]; [request startAsynchronous]; } //给好友发送邀请码 - (void)sendInvateEmails:(NSString*)emails code:(NSString*)code complention:(ActivityComplention)complention { __block ActivityComplention tComplention = complention; __block VerificationManager *weak_self = self; //Process Params NSString *app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"] description]; if (!app_version.length) app_version = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] description]; BOOL iszhcn = !([NSLocalizedString(@"Language", nil) isEqualToString:@"English"] && [NSLocalizedString(@"Language", nil) isEqualToString:@"Français"]); NSDictionary *params = @{ @"data":@{ // @"app_code":[self bundleIdentify]?:@"", @"app_code":@"com.brother.pdftecheditor", @"email":emails?:@"", @"code":[code stringByReplacingOccurrencesOfString:@" " withString:@""]?:@"", @"unique_sn":[self udid]?:@"", @"lang":@(iszhcn), } }; NSMutableData *postData = [VerificationManager mutableDataWithDic:params]; //Send Request NSString *urlString = [kVerificationServer stringByAppendingString:@"api/invites/shareEmail"]; NSURL *url = [NSURL URLWithString:urlString]; ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; request.requestMethod = @"POST"; [request setPostBody:postData]; NSString *postLength = [NSString stringWithFormat:@"%ld", (unsigned long)[postData length]]; NSMutableDictionary *requestHeaders = [NSMutableDictionary dictionaryWithObjectsAndKeys: postLength, @"Content-Length", nil]; request.defaultResponseEncoding = NSUTF8StringEncoding; // [request setAuthenticationScheme:@"https"];//设置验证方式 // [request setValidatesSecureCertificate:NO];//设置验证证书 [request setRequestHeaders:requestHeaders]; [request addRequestHeader:@"Content-Type" value:@"application/json"]; [request addRequestHeader:@"Accept" value:@"application/vnd.api+json;version=1"]; __block void(^processRequest)(void) = ^{ dispatch_async(dispatch_get_main_queue(), ^{ NSDictionary *info = [[request.responseString objectFromJSONString] filterNullObject]; //重置计时器 [weak_self restartTimer]; if (tComplention) tComplention(weak_self.status, info, request.error); [weak_self verificationWithComplention:^(ActivityStatus status, NSDictionary *info, NSError *error) { }]; }); }; [request setFailedBlock:^{ processRequest(); }]; [request setCompletionBlock:^{ processRequest(); }]; [request startAsynchronous]; } #pragma mark - Data Formatter - (BOOL)isValidLicenseCode:(NSString *)cdKey { NSString *validKey = [cdKey stringByReplacingOccurrencesOfString:@" " withString:@""]; if (validKey.length != 19) { return NO; } //计算字符串长度 NSInteger str_length = [validKey length]; NSString *regex = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-"; for (int i = 0; i 0) { tStatus = ActivityStatusTrialExpire; } } else if (ActivityStatusVerification == tStatus) { if (self.expireDate && [NSDate date].timeIntervalSince1970 - [self expireDate].timeIntervalSince1970 > 0) { tStatus = ActivityStatusVerifExpire; } if (self[@"dejvice"][@"status"] && [self[@"device"][@"status"] isEqualToString:@"expired_license"]) { //激活码过期 tStatus = ActivityStatusVerifExpire; } if ([self deviceActiveDate] && [self deviceEndDate]) { NSDate *activeDate = [self deviceActiveDate]; NSDate *deviceEndDate = [self deviceEndDate]; if (deviceEndDate.timeIntervalSince1970 - activeDate.timeIntervalSince1970 < 30*3600*24) { tStatus = ActivityStatusTrial; if (self.expireDate && [NSDate date].timeIntervalSince1970 - [self expireDate].timeIntervalSince1970 > 0) { tStatus = ActivityStatusTrialExpire; } } } } return tStatus; } - (NSString*)licenseCode { return self[@"device"][@"cdkey"]; } - (NSString*)originLicenseCode { return self[@"device"][@"origin_cdkey"]; } - (NSString*)shareCode { return self[@"invite"][@"share_code"]?:@""; } - (NSUInteger)invateUsersCount { return [self[@"invite"][@"new_users_num"] integerValue]; } - (NSString*)fromCode { return self[@"invite"][@"from_code"]?:@""; } - (NSDate*)fetchInvateDate { if (self[@"invite"][@"created_at"]) { return [self dateForValue:self[@"invite"][@"created_at"]]; } return nil; } - (NSDate *)dateForValue:(id)value { if ([value isKindOfClass:[NSString class]]) { NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; formatter.locale = [NSLocale systemLocale]; return [formatter dateFromString:value]; }else if ([value isKindOfClass:[NSNumber class]]) { return [NSDate dateWithTimeIntervalSince1970:[value doubleValue]]; } return nil; } - (BOOL)repeatTrial { return [self[@"device"][@"trial_again"] boolValue]; } - (NSUInteger)trialTimes { return [self[@"device"][@"trial_times"]?:@"1" intValue]; } - (NSString *)email { return self[@"device"][@"email"]?:@""; } - (NSString *)subscriptionEmail { return self[@"subscription"][@"email"]?:@""; } - (NSString *)accountName { return self[@"device"][@"contact_name"]?:@""; } - (NSUInteger)maxFreeDays { return [self[@"invite"][@"max_free_days"]?:@"365" intValue]; } - (BOOL)allowUnbind { return [self[@"device"][@"allow_unbind"] boolValue]; } - (NSInteger)unbindTimes { return [self[@"device"][@"unbind_time"]?:@"0" integerValue]; } @end