1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210 |
- //
- // 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<str_length; i++) {
- NSString *subStr = [validKey substringWithRange:NSMakeRange(i, 1)];
- if([regex rangeOfString:subStr].location == NSNotFound) {
- return NO;
- }
- }
- return YES;
- }
- + (NSString*)postStringWithObject:(id)value {
- NSString *string;
-
- if ([value isKindOfClass:[NSArray class]]) {
- string = @"[";
- BOOL isFirst = YES;
-
- for (id object in value) {
- if (isFirst) {
- isFirst = NO;
- }else {
- string = [string stringByAppendingString:@","];
- }
- string = [string stringByAppendingString:[VerificationManager postStringWithObject:object]];
- }
-
- string = [string stringByAppendingString:@"]"];
- }else if ([value isKindOfClass:[NSDictionary class]]) {
- string = @"{";
- BOOL isFirst = YES;
-
- for (NSString *key in [value allKeys]) {
- if (isFirst) {
- isFirst = NO;
- }else {
- string = [string stringByAppendingString:@","];
- }
- string = [string stringByAppendingFormat:@"\"%@\"", key];
- string = [string stringByAppendingString:@":"];
- string = [string stringByAppendingString:[VerificationManager postStringWithObject:[value valueForKey:key]]];
- }
- string = [string stringByAppendingString:@"}"];
- }else {
- string = [NSString stringWithFormat:@"\"%@\"", value];
- }
-
- return string;
- }
- + (NSString*)postStringWithDic:(NSDictionary*)dic {
- return [VerificationManager postStringWithObject:dic];
- }
- + (NSMutableData*)mutableDataWithDic:(NSDictionary*)dic {
- NSString *json = [VerificationManager postStringWithDic:dic];
- json = [json stringByReplacingOccurrencesOfString:@"\n" withString:@""];
- json = [json stringByReplacingOccurrencesOfString:@"\r\0" withString:@""];
- json = [json stringByReplacingOccurrencesOfString:@"\0" withString:@""];
- NSData *postData = [json dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
- return [postData mutableCopy];
- }
- @end
- #pragma mark - Test
- #if DEBUG
- @implementation VerificationManager (Test)
- //- (ActivityStatus)status {
- // return ActivityStatusVerification;
- //}
- - (void)updateVerificationStatus {
- [self updateStatus:ActivityStatusVerification postNo:true];
- }
- - (void)updateStatus:(ActivityStatus)status postNo:(bool)postNo {
- NSString *subValue = @"";
- NSString *deviceValue = @"";
- NSTimeInterval endTime = 0;
- if (status == ActivityStatusNone) {
- deviceValue = @"";
- } else if (status == ActivityStatusTrial) {
- deviceValue = @"on_trial";
- } else if (status == ActivityStatusTrialExpire) {
- deviceValue = @"expired_trial";
- } else if (status == ActivityStatusVerification) {
- deviceValue = @"activated";
- endTime = [NSDate date].timeIntervalSince1970 + 60 * 60;
- } else if (status == ActivityStatusVerifExpire) {
- deviceValue = @"expired_license";
- } else if (status == ActivityStatusLicenseExpire) {
- deviceValue = @"";
- subValue = @"expired";
- }
-
- NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:self.detailInfo];
- dict[@"subscription"][@"status"] = subValue;
- dict[@"device"][@"status"] = deviceValue;
- if (endTime != 0) {
- dict[@"device"][@"end_date"] = @(endTime);
- dict[@"subscription"][@"end_date"] = @(endTime);
- }
- self.detailInfo = dict;
- if (postNo) {
- //发送通知
- NSString *notificationName = (NSString *)kDeviceActivateStatusChangeNotification;
- [[NSNotificationCenter defaultCenter] postNotificationName:notificationName
- object:self
- userInfo:nil];
- }
- }
- @end
- #endif
- #pragma mark - NSDictionary + ActivityInfo
- @implementation NSDictionary (ActivityInfo)
- - (NSDate*)expireDate {
- if (self[@"device"][@"end_date"]) {
- return [self dateForValue:self[@"device"][@"end_date"]];
- } else if (self[@"subscription"][@"end_date"]) {
- return [self dateForValue:self[@"subscription"][@"end_date"]];
- }
-
- return nil;
- }
- - (NSDate*)createDate {
- if (self[@"device"][@"created_at"]) {
- return [self dateForValue:self[@"device"][@"created_at"]];
- }
-
- return nil;
- }
- - (NSDate *)deviceActiveDate {
- if (self[@"device"][@"activated_date"]) {
- return [self dateForValue:self[@"device"][@"activated_date"]];
- }
-
- return nil;
- }
- - (NSDate *)deviceEndDate {
- if (self[@"device"][@"end_up_at"]) {
- return [self dateForValue:self[@"device"][@"end_up_at"]];
- }
-
- return nil;
- }
- - (ActivityStatus)status {
- ActivityStatus tStatus = ActivityStatusNone;
- if ([self[@"subscription"][@"status"] isEqualToString:@"expired"]) {
- tStatus = ActivityStatusLicenseExpire;
- }else{
- if ([self[@"device"][@"status"] isEqualToString:@"activated"]) {
- tStatus = ActivityStatusVerification;
- }else if ([self[@"device"][@"status"] isEqualToString:@"on_trial"]) {
- tStatus = ActivityStatusTrial;
- }else if ([self[@"device"][@"status"] isEqualToString:@"expired_trial"]) {
- tStatus = ActivityStatusTrialExpire;
- }else if ([self[@"device"][@"status"] isEqualToString:@"expired_license"]) {
- tStatus = ActivityStatusVerifExpire;
- }
- }
- if (ActivityStatusTrial == tStatus) {
- if (self.expireDate &&
- [NSDate date].timeIntervalSince1970 - [self expireDate].timeIntervalSince1970 > 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
|