Closed JanXu-Dev closed 6 years ago
(void)failedOnCallingAPI:(CTURLResponse *)response withErrorType:(CTAPIManagerErrorType)errorType { self.isLoading = NO; if (response) { self.response = response; } self.errorType = errorType; [self removeRequestIdWithRequestID:response.requestId];
// user token 无效,重新登录 if (errorType == CTAPIManagerErrorTypeNeedLogin) { [[NSNotificationCenter defaultCenter] postNotificationName:kCTUserTokenIllegalNotification object:nil userInfo:@{ kCTUserTokenNotificationUserInfoKeyManagerToContinue:self }]; return; } NSString *resCode = [NSString stringWithFormat:@"%@", response.content[@"resCode"]]; if ([resCode isEqualToString:@"00100009"] || [resCode isEqualToString:@"05111001"] || [resCode isEqualToString:@"05111002"] || [resCode isEqualToString:@"1080002"] ) { [[NSNotificationCenter defaultCenter] postNotificationName:kCTUserTokenIllegalNotification object:nil userInfo:@{ kCTUserTokenNotificationUserInfoKeyManagerToContinue:self }]; return; }
// 可以自动处理的错误 if (errorType == CTAPIManagerErrorTypeNeedAccessToken) { [[NSNotificationCenter defaultCenter] postNotificationName:kCTUserTokenInvalidNotification object:nil userInfo:@{ kCTUserTokenNotificationUserInfoKeyManagerToContinue:self }]; return; }
NSString *errorCode = [NSString stringWithFormat:@"%@", response.content[@"errorCode"]]; if ([response.content[@"errorMsg"] isEqualToString:@"invalid token"] || [response.content[@"errorMsg"] isEqualToString:@"access_token is required"] || [errorCode isEqualToString:@"BL10015"] ) { // token 失效 [[NSNotificationCenter defaultCenter] postNotificationName:kCTUserTokenInvalidNotification object:nil userInfo:@{ kCTUserTokenNotificationUserInfoKeyManagerToContinue:self }]; return; }
// 常规错误 if (errorType == CTAPIManagerErrorTypeNoNetWork) { self.errorMessage = @"无网络连接,请检查网络"; } if (errorType == CTAPIManagerErrorTypeTimeout) { self.errorMessage = @"请求超时"; } if (errorType == CTAPIManagerErrorTypeCanceled) { self.errorMessage = @"您已取消"; } if (errorType == CTAPIManagerErrorTypeDownGrade) { self.errorMessage = @"网络拥塞"; }
// 其他错误 dispatch_async(dispatch_get_main_queue(), ^{ if ([self.interceptor respondsToSelector:@selector(manager:didReceiveResponse:)]) { [self.interceptor manager:self didReceiveResponse:response]; } if ([self beforePerformFailWithResponse:response]) { [self.delegate managerCallAPIDidFailed:self]; } if (self.failBlock) { self.failBlock(self); } [self afterPerformFailWithResponse:response]; }); }
这段代码的编排在 issues 中显示有点奇怪, 望体谅;
Casatwy 大大, 我的疑问是 : 1.该方法中涉及到的 resCode, errorMsg 等 key 的判断是否是业务相关的数据? 2.如果是业务相关数据, 是否可以想 Target_CTAppContext 类似的方式来引入, 实现业务数据的分离?
这确实是业务相关的数据,应该按照你说的方式来修改~
我发了新版,我把这部分内容挪到service中去了。
(void)failedOnCallingAPI:(CTURLResponse *)response withErrorType:(CTAPIManagerErrorType)errorType { self.isLoading = NO; if (response) { self.response = response; } self.errorType = errorType; [self removeRequestIdWithRequestID:response.requestId];
// 可以自动处理的错误 if (errorType == CTAPIManagerErrorTypeNeedAccessToken) { [[NSNotificationCenter defaultCenter] postNotificationName:kCTUserTokenInvalidNotification object:nil userInfo:@{ kCTUserTokenNotificationUserInfoKeyManagerToContinue:self }]; return; }
NSString *errorCode = [NSString stringWithFormat:@"%@", response.content[@"errorCode"]]; if ([response.content[@"errorMsg"] isEqualToString:@"invalid token"] || [response.content[@"errorMsg"] isEqualToString:@"access_token is required"] || [errorCode isEqualToString:@"BL10015"] ) { // token 失效 [[NSNotificationCenter defaultCenter] postNotificationName:kCTUserTokenInvalidNotification object:nil userInfo:@{ kCTUserTokenNotificationUserInfoKeyManagerToContinue:self }]; return; }
// 常规错误 if (errorType == CTAPIManagerErrorTypeNoNetWork) { self.errorMessage = @"无网络连接,请检查网络"; } if (errorType == CTAPIManagerErrorTypeTimeout) { self.errorMessage = @"请求超时"; } if (errorType == CTAPIManagerErrorTypeCanceled) { self.errorMessage = @"您已取消"; } if (errorType == CTAPIManagerErrorTypeDownGrade) { self.errorMessage = @"网络拥塞"; }
// 其他错误 dispatch_async(dispatch_get_main_queue(), ^{ if ([self.interceptor respondsToSelector:@selector(manager:didReceiveResponse:)]) { [self.interceptor manager:self didReceiveResponse:response]; } if ([self beforePerformFailWithResponse:response]) { [self.delegate managerCallAPIDidFailed:self]; } if (self.failBlock) { self.failBlock(self); } [self afterPerformFailWithResponse:response]; }); }
这段代码的编排在 issues 中显示有点奇怪, 望体谅;
Casatwy 大大, 我的疑问是 : 1.该方法中涉及到的 resCode, errorMsg 等 key 的判断是否是业务相关的数据? 2.如果是业务相关数据, 是否可以想 Target_CTAppContext 类似的方式来引入, 实现业务数据的分离?