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

@@ -8,12 +8,12 @@
#define KBAPI_h #define KBAPI_h
#ifndef SUCCESS_CODE //#ifndef SUCCESS_CODE
#define SUCCESS_CODE 200 //#define SUCCESS_CODE 200
#endif //#endif
#ifndef ERROR_CODE //#ifndef ERROR_CODE
#define ERROR_CODE 500 //#define ERROR_CODE 500
#endif //#endif
// 兼容旧命名(如有使用 API_APPLE_LOGIN 的位置,映射到统一命名) // 兼容旧命名(如有使用 API_APPLE_LOGIN 的位置,映射到统一命名)

View File

@@ -112,7 +112,9 @@ static void KBAuthDarwinCallback(CFNotificationCenterRef center, void *observer,
} @catch (__unused NSException *e) { session = nil; } } @catch (__unused NSException *e) { session = nil; }
} }
self.current = session; self.current = session;
[[NSNotificationCenter defaultCenter] postNotificationName:KBAuthChangedNotification object:nil]; // // object self +shared
[[NSNotificationCenter defaultCenter] postNotificationName:KBAuthChangedNotification
object:self];
} }
- (BOOL)saveAccessToken:(NSString *)accessToken - (BOOL)saveAccessToken:(NSString *)accessToken
@@ -133,7 +135,8 @@ static void KBAuthDarwinCallback(CFNotificationCenterRef center, void *observer,
if (ok) { if (ok) {
self.current = s; self.current = s;
// //
[[NSNotificationCenter defaultCenter] postNotificationName:KBAuthChangedNotification object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:KBAuthChangedNotification
object:self];
// App <-> // App <->
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge CFStringRef)kKBDarwinAuthChanged, NULL, NULL, true); CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge CFStringRef)kKBDarwinAuthChanged, NULL, NULL, true);
} }
@@ -143,7 +146,8 @@ static void KBAuthDarwinCallback(CFNotificationCenterRef center, void *observer,
- (void)signOut { - (void)signOut {
[self keychainDelete]; [self keychainDelete];
self.current = nil; self.current = nil;
[[NSNotificationCenter defaultCenter] postNotificationName:KBAuthChangedNotification object:nil]; [[NSNotificationCenter defaultCenter] postNotificationName:KBAuthChangedNotification
object:self];
CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge CFStringRef)kKBDarwinAuthChanged, NULL, NULL, true); CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter(), (__bridge CFStringRef)kKBDarwinAuthChanged, NULL, NULL, true);
} }

View File

@@ -16,17 +16,59 @@
/// 服务端业务状态码(按项目实际调整) /// 服务端业务状态码(按项目实际调整)
typedef NS_ENUM(NSInteger, KBBizCode) { typedef NS_ENUM(NSInteger, KBBizCode) {
/// 通用成功(通常为 200对应 SUCCESS_CODE /// 业务成功SUCCESS(0, "ok")
KBBizCodeSuccess = SUCCESS_CODE, KBBizCodeSuccess = 0,
/// token 失效/未登录(示例值;请按后端实际 code 修改) /// 参数错误PARAMS_ERROR(40000, "请求参数错误")
KBBizCodeTokenInvalid = 40101, KBBizCodeParamsError = 40000,
/// token 过期(可选;如无区分可与 KBBizCodeTokenInvalid 复用) /// 上传文件为空FILE_IS_EMPTY(40001, "上传文件为空")
KBBizCodeTokenExpired = 40102, KBBizCodeFileIsEmpty = 40001,
/// 账号在其他设备登录,被服务端强制下线 /// 文件名错误FILE_NAME_ERROR(40002, "文件名错误")
KBBizCodeAccountKicked = 40103, KBBizCodeFileNameError = 40002,
/// Apple 登录失败APPLE_LOGIN_ERROR(40003, "Apple登录失败")
KBBizCodeAppleLoginError = 40003,
/// 未登录NOT_LOGIN_ERROR(40100, "未登录")
KBBizCodeNotLogin = 40100,
/// 无权限NO_AUTH_ERROR(40101, "无权限")
KBBizCodeNoAuth = 40101,
/// 未能读取到有效用户令牌TOKEN_NOT_FOUND(40102)
KBBizCodeTokenNotFound = 40102,
/// 令牌无效TOKEN_INVALID(40103)
KBBizCodeTokenInvalid = 40103,
/// 令牌已过期TOKEN_TIMEOUT(40104)
KBBizCodeTokenTimeout = 40104,
/// 令牌已被顶下线TOKEN_BE_REPLACED(40105)
KBBizCodeTokenBeReplaced = 40105,
/// 令牌已被踢下线TOKEN_KICK_OUT(40107)
KBBizCodeTokenKickOut = 40107,
/// 令牌已被冻结TOKEN_FREEZE(40108)
KBBizCodeTokenFreeze = 40108,
/// 未按照指定前缀提交令牌TOKEN_NO_PREFIX(40109)
KBBizCodeTokenNoPrefix = 40109,
/// 禁止访问FORBIDDEN_ERROR(40300, "禁止访问")
KBBizCodeForbidden = 40300,
/// 请求数据不存在NOT_FOUND_ERROR(40400, "请求数据不存在")
KBBizCodeNotFound = 40400,
/// 系统内部异常SYSTEM_ERROR(50000, "系统内部异常")
KBBizCodeSystemError = 50000,
/// 操作失败OPERATION_ERROR(50001, "操作失败")
KBBizCodeOperationError = 50001,
}; };
NS_ASSUME_NONNULL_BEGIN NS_ASSUME_NONNULL_BEGIN
@@ -59,4 +101,3 @@ static inline NSString *KBBizMessageFromJSONObject(id obj) {
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END
#endif /* KBBizCode_h */ #endif /* KBBizCode_h */

View File

@@ -1341,6 +1341,7 @@
04A9FE1F2EB893F10020DB6D /* Localization */, 04A9FE1F2EB893F10020DB6D /* Localization */,
04FC98012EB36AAB007BD342 /* KBConfig.h */, 04FC98012EB36AAB007BD342 /* KBConfig.h */,
04122F592EC5D40000EF7AB3 /* KBAPI.h */, 04122F592EC5D40000EF7AB3 /* KBAPI.h */,
0498BD5E2EDF2157006CC1D5 /* KBBizCode.h */,
04791F962ED49CE7004E8522 /* KBFont.h */, 04791F962ED49CE7004E8522 /* KBFont.h */,
04791F972ED49CE7004E8522 /* KBFont.m */, 04791F972ED49CE7004E8522 /* KBFont.m */,
A1B2C4002EB4A0A100000001 /* KBAuthManager.h */, A1B2C4002EB4A0A100000001 /* KBAuthManager.h */,
@@ -1357,7 +1358,6 @@
04D1F6B12EDFF10A00B12345 /* KBSkinInstallBridge.m */, 04D1F6B12EDFF10A00B12345 /* KBSkinInstallBridge.m */,
04791F8C2ED469C0004E8522 /* KBHostAppLauncher.h */, 04791F8C2ED469C0004E8522 /* KBHostAppLauncher.h */,
04791F8D2ED469C0004E8522 /* KBHostAppLauncher.m */, 04791F8D2ED469C0004E8522 /* KBHostAppLauncher.m */,
0498BD5E2EDF2157006CC1D5 /* KBBizCode.h */,
); );
path = Shared; path = Shared;
sourceTree = "<group>"; sourceTree = "<group>";

View File

@@ -41,7 +41,7 @@
/// 2使 /// 2使
[KBLocalizationManager shared].supportedLanguageCodes = KBDefaultSupportedLanguageCodes(); [KBLocalizationManager shared].supportedLanguageCodes = KBDefaultSupportedLanguageCodes();
/// 3 : token /// 3 : token
// [[KBUserSessionManager shared] bootstrapIfNeeded]; [[KBUserSessionManager shared] bootstrapIfNeeded];
// /访 Keychain // /访 Keychain
@@ -54,12 +54,12 @@
} }
// Skip TabBar // Skip TabBar
// BOOL hasShownSexVC = [[NSUserDefaults standardUserDefaults] boolForKey:KBSexSelectShownKey]; BOOL hasShownSexVC = [[NSUserDefaults standardUserDefaults] boolForKey:KBSexSelectShownKey];
// if (hasShownSexVC) { if (hasShownSexVC) {
// [self setupRootVC]; [self setupRootVC];
// } else { } else {
[self setupSexSelectRootVC]; [self setupSexSelectRootVC];
// } }
// 访 // 访
[KBNetworkManager shared].enabled = YES; [KBNetworkManager shared].enabled = YES;

View File

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

View File

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

View File

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

View File

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