处理网络请求token为空的问题

This commit is contained in:
2025-12-17 15:39:45 +08:00
parent 5bd20a911f
commit c6b4444589
4 changed files with 18 additions and 8 deletions

View File

@@ -30,10 +30,8 @@ NSErrorDomain const KBNetworkErrorDomain = @"com.company.keyboard.network";
// Accept + 使 Accept-Language // Accept + 使 Accept-Language
NSString *lang = [KBLocalizationManager shared].currentLanguageCode ?: KBLanguageCodeEnglish; NSString *lang = [KBLocalizationManager shared].currentLanguageCode ?: KBLanguageCodeEnglish;
// NSString *token = [KBUserSessionManager shared].accessToken ? [KBUserSessionManager shared].accessToken : @""; // NSString *token = [KBUserSessionManager shared].accessToken ? [KBUserSessionManager shared].accessToken : @"";
NSString *token = KBAuthManager.shared.current.accessToken ? KBAuthManager.shared.current.accessToken : @"";
_defaultHeaders = @{ _defaultHeaders = @{
@"Accept": @"*/*", @"Accept": @"*/*",
@"auth-token" : token,
@"Accept-Language": lang @"Accept-Language": lang
}; };
// //
@@ -191,6 +189,12 @@ NSErrorDomain const KBNetworkErrorDomain = @"com.company.keyboard.network";
- (void)applyHeaders:(NSDictionary<NSString *,NSString *> *)headers toMutableRequest:(NSMutableURLRequest *)req contentType:(NSString *)contentType { - (void)applyHeaders:(NSDictionary<NSString *,NSString *> *)headers toMutableRequest:(NSMutableURLRequest *)req contentType:(NSString *)contentType {
// //
NSMutableDictionary *all = [self.defaultHeaders mutableCopy] ?: [NSMutableDictionary new]; NSMutableDictionary *all = [self.defaultHeaders mutableCopy] ?: [NSMutableDictionary new];
NSString *token = [KBAuthManager shared].current.accessToken;
if (token.length > 0) {
all[@"auth-token"] = token;
} else {
[all removeObjectForKey:@"auth-token"];
}
NSDictionary *auth = [[KBAuthManager shared] authorizationHeader]; NSDictionary *auth = [[KBAuthManager shared] authorizationHeader];
[auth enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) { all[key] = obj; }]; [auth enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) { all[key] = obj; }];
if (contentType) all[@"Content-Type"] = contentType; if (contentType) all[@"Content-Type"] = contentType;

View File

@@ -171,7 +171,7 @@
self.myVM = [[KBMyVM alloc] init]; self.myVM = [[KBMyVM alloc] init];
} }
/// tokenheadtoken /// tokenheadtoken
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.myVM fetchCharacterListByUserWithCompletion:^(NSArray<KBCharacter *> * _Nonnull characterArray, NSError * _Nullable error) { [self.myVM fetchCharacterListByUserWithCompletion:^(NSArray<KBCharacter *> * _Nonnull characterArray, NSError * _Nullable error) {
if (error) { if (error) {
KBLOG(@"[Login] Failed to sync keyboard characters after login: %@", error); KBLOG(@"[Login] Failed to sync keyboard characters after login: %@", error);

View File

@@ -44,7 +44,6 @@ NSErrorDomain const KBNetworkErrorDomain = @"com.company.keyboard.network";
_timeout = 10.0; _timeout = 10.0;
NSString *lang = [KBLocalizationManager shared].currentLanguageCode ?: KBLanguageCodeEnglish; NSString *lang = [KBLocalizationManager shared].currentLanguageCode ?: KBLanguageCodeEnglish;
NSString *token = [KBUserSessionManager shared].accessToken ? [KBUserSessionManager shared].accessToken : @"";
// query // query
// signParams[@"lang"] = @"zh"; // signParams[@"lang"] = @"zh";
@@ -54,7 +53,6 @@ NSErrorDomain const KBNetworkErrorDomain = @"com.company.keyboard.network";
_defaultHeaders = @{ _defaultHeaders = @{
@"Accept": @"*/*", @"Accept": @"*/*",
@"Accept-Language": lang, @"Accept-Language": lang,
@"auth-token" : token,
}; };
// //
_baseURL = [NSURL URLWithString:KB_BASE_URL]; _baseURL = [NSURL URLWithString:KB_BASE_URL];
@@ -357,6 +355,12 @@ autoShowBusinessError:YES
- (void)applyHeaders:(NSDictionary<NSString *,NSString *> *)headers toMutableRequest:(NSMutableURLRequest *)req contentType:(NSString *)contentType { - (void)applyHeaders:(NSDictionary<NSString *,NSString *> *)headers toMutableRequest:(NSMutableURLRequest *)req contentType:(NSString *)contentType {
// //
NSMutableDictionary *all = [self.defaultHeaders mutableCopy] ?: [NSMutableDictionary new]; NSMutableDictionary *all = [self.defaultHeaders mutableCopy] ?: [NSMutableDictionary new];
NSString *token = [KBAuthManager shared].current.accessToken;
if (token.length > 0) {
all[@"auth-token"] = token;
} else {
[all removeObjectForKey:@"auth-token"];
}
NSDictionary *auth = [[KBAuthManager shared] authorizationHeader]; NSDictionary *auth = [[KBAuthManager shared] authorizationHeader];
[auth enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) { all[key] = obj; }]; [auth enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) { all[key] = obj; }];
if (contentType) all[@"Content-Type"] = contentType; if (contentType) all[@"Content-Type"] = contentType;

View File

@@ -14,8 +14,8 @@
- (void)applePayReqWithParams:(NSDictionary *)params - (void)applePayReqWithParams:(NSDictionary *)params
needShow:(BOOL)needShow needShow:(BOOL)needShow
completion:(KBPayCompletion)completion { completion:(KBPayCompletion)completion {
if (needShow) { [KBHUD show]; } // if (needShow) { [KBHUD show]; }
[KBHUD showWithStatus:@"Please wait"];
[[KBNetworkManager shared] POST:API_VALIDATE_RECEIPT [[KBNetworkManager shared] POST:API_VALIDATE_RECEIPT
jsonBody:params jsonBody:params
headers:nil headers:nil
@@ -39,7 +39,9 @@
return; return;
} }
if (completion) completion(KBBizCodeSuccess, @"JWS-ok"); // if (completion) completion(KBBizCodeSuccess, @"JWS-ok");
if (completion) completion(KBBizCodeSuccess, nil);
}]; }];
} }