ios之Blots中OC刷新数据请求中Token过期的方法

pod 'Bolts' 

1.数据请求类的封装

#import

#define kRefreshTokenRetryCount 3

typedef NS_ENUM(NSUInteger,ReqestType)

{

    TypeGET =0,

    TypePOST,

    TypeDELETE,

    TypePUT,

};

@interface SqNetworkingTool ()

@property (nonatomic, strong) BFTaskCompletionSource *refreshTokenCS;

@property (nonatomic) NSInteger refreshTokenRetryCount;

@end

@implementation SqNetworkingTool

+ (instancetype)shareManager

{

    static YmdNetworkingLogic * manager = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        manager = [[self alloc] init];

    });

    return manager;

}

+ (void)SqrequestDataParam:(NSDictionay *)params callback:(void (^)(BOOL, SqReturnState, SQAssistanceDetailModel *))callback {

    [SQNetworkingTool accessWithType:TypeGET subUrl:”requestsubURL” params:params callback:^(SqResponseReturnState state, id  _Nonnull data) {

        if (state == SqResponseReturnStateOK) {

            SQModel *model = [SQModel yy_modelWithDictionary:data];

            callback(true, state, model);

        }else {

            callback(false, state, nil);

        }

    }];

}

+ (void)accessWithType: (RequestType)type

                subUrl: (NSString *)subUrl

                params: (NSDictionary *)params

              callback: (void(^)(ReseponseState state, id data))callback {

       [SqNetworkingBase baseAccessWithType: type url: fullUrl params: params callback: ^(ResponseState state, id  _Nonnull responseObj, NSURLSessionDataTask * _Nonnull task) {

        NSLog(@"wrongurl:%@",fullUrl);

        if (state == ResponseStateReturnStateOK) {

            ResponseStateReturnState stateBack = [responseObj[@"code"] integerValue];

            id data = responseObj[@"data"];

            callback(stateBack,data);

        }else if (state == ResponseStateReturnStateEXPIRED_TOKEN) {

//如果token过期,使用blots刷新token

            BFTaskCompletionSource *refreshTokenCS = [BFTaskCompletionSource taskCompletionSource];

            [[SQNetworkingTool shareManager] refreshToken:refreshTokenCS];

            [[refreshTokenCS.task continueWithSuccessBlock:^id(BFTask *task) {

//递归调用

                [self accessWithType:type subUrl:subUrl params:params callback:^(SqResposeReturnState state, id  _Nonnull data) {

                    if (state == SqResposeReturnStateOK) {

//保存token

                        [SQUserManager deleteCurrentUserPermission];

                        NSDictionary *dic = (NSDictionary *)responseObj;

                        UserPermissionModel *permission = [UserPermissionModel yy_modelWithDictionary:dic];

                        [SQUserManager saveCurrentUserPermission:permission];

                    }

                }];

                return nil;

            }]continueWithBlock:^id (BFTask *task) {

                if (task.error) {


                }

                return nil;

            }];

        }else if (state == SQNetworkingReturnStateUNAUTHORIZED_TOKEN || state == SQNetworkingReturnStateNOSEND_TOKEN) {

            [SQUserManager deleteCurrentUserPermission];

            [SQUserManager gotoLoginPage];

        }else if (state == SQNetworkingReturnStateORDER_IS_EXISTS) {

            callback(state,nil);

        }else if (state == SQNetworkingReturnStateUSER_IMAGE_CANNOT_UPDATE){

            callback(state,nil);


        }else{

            callback(state,nil);

            if (responseObj[@"message"]) {

                [YmdTool showToastWithText:nilChange(responseObj[@"message"])];

            }

        }

    }];

}

/*********判断对象是否为空, 解决 nil 插入数组时报的异常************/

#define nilChange(objc) (((objc) == nil) ? @"":(objc))

- (void)refreshToken:(BFTaskCompletionSource *)cs {

    if(self.refreshTokenCS == nil || self.refreshTokenCS.task.completed || self.refreshTokenCS.task.cancelled) {

        self.refreshTokenCS = [BFTaskCompletionSource taskCompletionSource];


        UserPermissionModel *premissin = [SQUserManager fetchUserPermission];

        NSString *token = [NSString stringWithFormat:@"%@%@",premissin.tokenHead,premissin.token];

        NSDictionary *dic = @{

                              @"token" : nilChange(token)

                              };

        [SqNetworkingTool baseAccessRefreshTokenWithType:TypePOST url:fullUrl params:dic callback:^(ResponseReturnState state, id  _Nonnull responseObj, NSURLSessionDataTask * _Nonnull task) {

            if (state == ReturnStateOK) {

                self.refreshTokenRetryCount = kRefreshTokenRetryCount;

                [self.refreshTokenCS setResult:nil];

            }else {

                self.refreshTokenRetryCount --; //retry count --

                if(self.refreshTokenRetryCount < 0) {

                    [self.refreshTokenCS setError:task.error];  //超过尝试次数直接error返回

                }

            }

        }];

    }

    [self.refreshTokenCS.task continueWithBlock:^id(BFTask *task) {

        if(task.error) {

            [cssetError:task.error];

        }else {

            [cssetResult:nil];

        }

        return nil;

    }];

}

+ (void)baseAccessRefreshTokenWithType: (ReqestType)type url: (NSString *)url params: (NSDictionary *)params callback: (void(^)(YmdNetworkingReturnState state, id responseObj, NSURLSessionDataTask *task))callback {


    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager.requestSerializer setValue: @"iOS" forHTTPHeaderField: @"PLATFORM"];


    if (type == TypeGet) {

        [managerGET: url parameters:params progress: nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            callback([responseObject[@"code"] integerValue], responseObject, task);

        }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {


            callback(error, nil, task);

        }];


    }else if (type == TypePOST){

        [managerPOST: url parameters: params progress: nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

            callback([responseObject[@"code"] integerValue], responseObject, task);

        }failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {


            callback(error, nil, task);

        }];

    }

}

#define kUserInfoMainPath          [NSString stringWithFormat:@"%@/Library/",NSHomeDirectory()]

#import "ZWUserManager.h"

@implementation ZWUserManager

+ (void)saveCurrentUserInfo:(UserModel *)user{

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject: user];

    if (![data writeToFile: [NSString stringWithFormat:@"%@currentUser.data",kUserInfoMainPath] atomically: false]) {

        NSLog(@"用户数据写入失败");

    }

}

+ (UserModel *)fetchCurrentUserInfo {

    NSData *data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@currentUser.data",kUserInfoMainPath]];

    return [NSKeyedUnarchiver unarchiveObjectWithData: data];

}

+ (void)deleteCurrentUserInfo {

    [self removeDocumentWithFilePath:[NSString stringWithFormat:@"%@currentUser.data",kUserInfoMainPath]];

}

+ (BOOL)removeDocumentWithFilePath:(NSString*)filePath{


    BOOL isRemove = NO;


    NSFileManager* fileManager=[NSFileManager defaultManager];


    if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {

        isRemove = [fileManagerremoveItemAtPath:filePath error:nil];

    }

    return isRemove;


}

+ (void)saveCurrentUserPermission:(UserPermissionModel *)user {

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject: user];

    if (![data writeToFile: [NSString stringWithFormat:@"%@currentUserPermission.data",kUserInfoMainPath] atomically: false]) {

        NSLog(@"用户数据写入失败");

    }

}

+ (UserPermissionModel *)fetchUserPermission {

    NSData *data = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@currentUserPermission.data",kUserInfoMainPath]];

    return [NSKeyedUnarchiver unarchiveObjectWithData: data];

}

+ (void)deleteCurrentUserPermission {

    [self removeDocumentWithFilePath:[NSString stringWithFormat:@"%@currentUserPermission.data",kUserInfoMainPath]];

}

+ (void)checkLoginStatusWithCallback:(void (^)(BOOL))callback {

    [SqNetWorkToo requestLoginStatusWithCallback:^(BOOL success, SqResponseReturnState state) {

        if (success) {

            callback(true);

        }else {

            [[NSNotificationCenter defaultCenter] postNotificationName:SQUserLogIninvalidNotification object:nil];

            [SQUserManager deleteCurrentUserInfo];

            [SQUserManager deleteCurrentUserPermission];

            [SQCacheManager removeCache];


            callback(false);

        }

    }];

}

@end

@interface UserPermissionModel : NSObject

@property (nonatomic, copy) NSString *tokenHead;

@property (nonatomic, copy) NSString *token;

@property (nonatomic, assign) BOOL credentialsNonExpired;

@property (nonatomic, assign) BOOL accountNonLocked;

@property (nonatomic, assign) BOOL accountNonExpired;

@property (nonatomic, assign) BOOL enabled;

@end

#import "UserPermissionModel.h"

@implementation UserPermissionModel

- (void)encodeWithCoder:(NSCoder *)coder

{

    [coderencodeObject:self.tokenHead                      forKey:@"tokenHead"];

    [coderencodeObject:self.token                          forKey: @"token"];

    [coderencodeBool:self.credentialsNonExpired            forKey: @"credentialsNonExpired"];

    [coderencodeBool:self.accountNonLocked                forKey: @"accountNonLocked"];

    [coderencodeBool:self.accountNonExpired                forKey: @"accountNonExpired"];

    [coderencodeBool:self.enabled                          forKey:@"enabled"];

}

- (instancetype)initWithCoder:(NSCoder *)coder

{

    self = [super init];

    if (self) {

        self.tokenHead                  = [coderdecodeObjectForKey:@"tokenHead"];

        self.token                      = [coderdecodeObjectForKey:@"token"];

        self.credentialsNonExpired      = [coderdecodeBoolForKey:@"nickname"];

        self.accountNonLocked          = [coderdecodeBoolForKey:@"accountNonLocked"];

        self.accountNonExpired          = [coderdecodeBoolForKey:@"accountNonExpired"];

        self.enabled                    = [coderdecodeBoolForKey:@"enabled"];

    }

    return self;

}

@end

你可能感兴趣的:(ios之Blots中OC刷新数据请求中Token过期的方法)