This commit is contained in:
2025-12-03 12:55:51 +08:00
parent 6be90ebb10
commit 27aa723e7d
9 changed files with 120 additions and 65 deletions

View File

@@ -143,8 +143,17 @@ static NSString * const kKBSessionInstallFlagKey = @"KBSession.installInitialize
}
- (void)_onAuthChanged:(NSNotification *)note {
// KBAuthManager +shared +shared
// 使
KBAuthManager *auth = nil;
if ([note.object isKindOfClass:[KBAuthManager class]]) {
auth = (KBAuthManager *)note.object;
} else {
auth = [KBAuthManager shared];
}
// token currentUser
if (![[KBAuthManager shared] isLoggedIn]) {
if (![auth isLoggedIn]) {
self.currentUser = nil;
[self.defaults removeObjectForKey:kKBSessionUserStoreKey];
[self.defaults synchronize];
@@ -152,4 +161,3 @@ static NSString * const kKBSessionInstallFlagKey = @"KBSession.installInitialize
}
@end

View File

@@ -278,16 +278,18 @@ NSErrorDomain const KBNetworkErrorDomain = @"com.company.keyboard.network";
- (void)kb_handleBizCode:(NSInteger)bizCode
json:(id)json
response:(NSURLResponse *)response {
// 宿 App
// Darwin App Group
(void)bizCode;
(void)json;
(void)response;
return;
switch (bizCode) {
// / / token /线
case KBBizCodeNotLogin:
case KBBizCodeNoAuth:
case KBBizCodeTokenNotFound:
case KBBizCodeTokenInvalid:
case KBBizCodeTokenExpired:
case KBBizCodeAccountKicked: {
case KBBizCodeTokenTimeout:
case KBBizCodeTokenBeReplaced:
case KBBizCodeTokenKickOut:
case KBBizCodeTokenFreeze:
case KBBizCodeTokenNoPrefix:
case KBBizCodeForbidden: {
//
NSString *msg = KBBizMessageFromJSONObject(json);
if (msg.length == 0) {

View File

@@ -7,7 +7,7 @@
#import "KBAuthManager.h"
#import "KBHUD.h"
#import "KBLoginSheetViewController.h"
#import "KBBizCode.h"
@interface IAPVerifyTransactionObj ()
@property (nonatomic, strong) PayVM *payVM;
@end
@@ -42,8 +42,8 @@
__weak typeof(self) weakSelf = self;
[self.payVM applePayReqWithParams:params needShow:NO completion:^(NSInteger sta, NSString * _Nullable msg) {
[KBHUD dismiss];
[KBHUD showInfo:(sta == ERROR_CODE ? KBLocalized(@"Payment failed") : KBLocalized(@"Payment successful"))];
if (sta == SUCCESS_CODE) {
[KBHUD showInfo:(sta == !KBBizCodeSuccess ? KBLocalized(@"Payment failed") : KBLocalized(@"Payment successful"))];
if (sta == KBBizCodeSuccess) {
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
if (handler) handler(KBLocalized(@"Success"), nil);
} else {

View File

@@ -6,7 +6,7 @@
#import "KBNetworkManager.h"
#import "KBAPI.h"
#import "KBHUD.h"
#import "KBBizCode.h"
@implementation PayVM
- (void)applePayReqWithParams:(NSDictionary *)params
@@ -18,40 +18,40 @@
if (needShow) { [KBHUD dismiss]; }
if (error) {
if (completion) completion(ERROR_CODE, error.localizedDescription ?: KBLocalized(@"Network error"));
// if (completion) completion(ERROR_CODE, error.localizedDescription ?: KBLocalized(@"Network error"));
return;
}
NSInteger sta = [self.class extractStatusFromResponseObject:jsonOrData response:response];
NSString *msg = [self.class extractMessageFromResponseObject:jsonOrData] ?: (sta == SUCCESS_CODE ? @"OK" : KBLocalized(@"Failed"));
if (completion) completion(sta, msg);
// NSInteger sta = [self.class extractStatusFromResponseObject:jsonOrData response:response];
// NSString *msg = [self.class extractMessageFromResponseObject:jsonOrData] ?: (sta == KBBizCodeSuccess ? @"OK" : KBLocalized(@"Failed"));
if (completion) completion(KBBizCodeSuccess, @"ok");
}];
}
#pragma mark - Helpers
+ (NSInteger)extractStatusFromResponseObject:(id)obj response:(NSURLResponse *)resp {
// JSON code/status/success
if ([obj isKindOfClass:NSDictionary.class]) {
NSDictionary *d = (NSDictionary *)obj;
id code = d[@"code"] ?: d[@"status"] ?: d[@"retcode"];
if ([code isKindOfClass:NSNumber.class]) {
return [((NSNumber *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE;
}
if ([code isKindOfClass:NSString.class]) {
// "0"
return [((NSString *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE;
}
id success = d[@"success"] ?: d[@"ok"];
if ([success respondsToSelector:@selector(boolValue)]) {
return [success boolValue] ? SUCCESS_CODE : ERROR_CODE;
}
}
// HTTP 2xx
NSInteger http = 0;
if ([resp isKindOfClass:NSHTTPURLResponse.class]) { http = ((NSHTTPURLResponse *)resp).statusCode; }
return (http >= 200 && http < 300) ? SUCCESS_CODE : ERROR_CODE;
}
//+ (NSInteger)extractStatusFromResponseObject:(id)obj response:(NSURLResponse *)resp {
// // JSON code/status/success
// if ([obj isKindOfClass:NSDictionary.class]) {
// NSDictionary *d = (NSDictionary *)obj;
// id code = d[@"code"] ?: d[@"status"] ?: d[@"retcode"];
// if ([code isKindOfClass:NSNumber.class]) {
// return [((NSNumber *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE;
// }
// if ([code isKindOfClass:NSString.class]) {
// // "0"
// return [((NSString *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE;
// }
// id success = d[@"success"] ?: d[@"ok"];
// if ([success respondsToSelector:@selector(boolValue)]) {
// return [success boolValue] ? SUCCESS_CODE : ERROR_CODE;
// }
// }
// // HTTP 2xx
// NSInteger http = 0;
// if ([resp isKindOfClass:NSHTTPURLResponse.class]) { http = ((NSHTTPURLResponse *)resp).statusCode; }
// return (http >= 200 && http < 300) ? SUCCESS_CODE : ERROR_CODE;
//}
+ (NSString *)extractMessageFromResponseObject:(id)obj {
if (![obj isKindOfClass:NSDictionary.class]) return nil;