From b87998549c40102c8ee85430abd43d30f2e3d6e5 Mon Sep 17 00:00:00 2001 From: CodeST <694468528@qq.com> Date: Wed, 3 Dec 2025 13:31:02 +0800 Subject: [PATCH] 3 --- Shared/KBAPI.h | 2 ++ Shared/KBBizCode.h | 6 ++++- keyBoard.xcodeproj/project.pbxproj | 14 ++++++++++++ keyBoard/AppDelegate.h | 2 +- keyBoard/AppDelegate.m | 17 +++++++++----- keyBoard/Class/Guard/VC/KBSexSelVC.m | 2 +- keyBoard/Class/Login/VC/KBLoginVC.m | 10 +++++++- keyBoard/Class/Me/VC/KBPersonInfoVC.m | 13 +++++++++-- keyBoard/Class/Me/VM/KBMyVM.h | 16 +++++++++++++ keyBoard/Class/Me/VM/KBMyVM.m | 33 +++++++++++++++++++++++++++ keyBoard/KeyBoardPrefixHeader.pch | 1 + 11 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 keyBoard/Class/Me/VM/KBMyVM.h create mode 100644 keyBoard/Class/Me/VM/KBMyVM.m diff --git a/Shared/KBAPI.h b/Shared/KBAPI.h index 3a8ed84..ccf91e7 100644 --- a/Shared/KBAPI.h +++ b/Shared/KBAPI.h @@ -18,6 +18,8 @@ // 兼容旧命名(如有使用 API_APPLE_LOGIN 的位置,映射到统一命名) #define API_APPLE_LOGIN @"/user/appleLogin" // Apple 登录 +#define API_LOGOUT @"/user/logout" // 退出登录 + // 应用配置 #ifndef KB_API_APP_CONFIG diff --git a/Shared/KBBizCode.h b/Shared/KBBizCode.h index 7caef9d..988544d 100644 --- a/Shared/KBBizCode.h +++ b/Shared/KBBizCode.h @@ -14,6 +14,10 @@ #import #import "KBAPI.h" // 引入 SUCCESS_CODE/ERROR_CODE 等基础定义 +#define KBMessage @"message" +#define KBCode @"code" +#define KBData @"data" + /// 服务端业务状态码(按项目实际调整) typedef NS_ENUM(NSInteger, KBBizCode) { /// 业务成功:SUCCESS(0, "ok") @@ -93,7 +97,7 @@ static inline NSInteger KBBizCodeFromJSONObject(id obj) { static inline NSString *KBBizMessageFromJSONObject(id obj) { if (![obj isKindOfClass:NSDictionary.class]) return nil; NSDictionary *d = (NSDictionary *)obj; - NSString *msg = d[@"message"] ?: d[@"msg"] ?: d[@"error"]; + NSString *msg = d[KBMessage] ?: d[@"msg"] ?: d[@"error"]; if (![msg isKindOfClass:NSString.class]) return nil; return msg; } diff --git a/keyBoard.xcodeproj/project.pbxproj b/keyBoard.xcodeproj/project.pbxproj index d6c1788..6fe27a1 100644 --- a/keyBoard.xcodeproj/project.pbxproj +++ b/keyBoard.xcodeproj/project.pbxproj @@ -98,6 +98,7 @@ 04890A042EC0BBBB00FABA60 /* KBCategoryTitleImageCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 04890A012EC0BBBB00FABA60 /* KBCategoryTitleImageCell.m */; }; 04890A052EC0BBBB00FABA60 /* KBCategoryTitleImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = 04890A032EC0BBBB00FABA60 /* KBCategoryTitleImageView.m */; }; 04890B122EC2F00000FABA60 /* KBMyHeaderView.m in Sources */ = {isa = PBXBuildFile; fileRef = 04890B112EC2F00000FABA60 /* KBMyHeaderView.m */; }; + 0498BD622EDFFC12006CC1D5 /* KBMyVM.m in Sources */ = {isa = PBXBuildFile; fileRef = 0498BD612EDFFC12006CC1D5 /* KBMyVM.m */; }; 049FB20B2EC1C13800FAB05D /* KBSkinBottomActionView.m in Sources */ = {isa = PBXBuildFile; fileRef = 049FB20A2EC1C13800FAB05D /* KBSkinBottomActionView.m */; }; 049FB20E2EC1CD2800FAB05D /* KBAlert.m in Sources */ = {isa = PBXBuildFile; fileRef = 049FB20D2EC1CD2800FAB05D /* KBAlert.m */; }; 049FB2112EC1F72F00FAB05D /* KBMyListCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 049FB2102EC1F72F00FAB05D /* KBMyListCell.m */; }; @@ -364,6 +365,8 @@ 04890B102EC2F00000FABA60 /* KBMyHeaderView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBMyHeaderView.h; sourceTree = ""; }; 04890B112EC2F00000FABA60 /* KBMyHeaderView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBMyHeaderView.m; sourceTree = ""; }; 0498BD5E2EDF2157006CC1D5 /* KBBizCode.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBBizCode.h; sourceTree = ""; }; + 0498BD602EDFFC12006CC1D5 /* KBMyVM.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBMyVM.h; sourceTree = ""; }; + 0498BD612EDFFC12006CC1D5 /* KBMyVM.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBMyVM.m; sourceTree = ""; }; 049FB2092EC1C13800FAB05D /* KBSkinBottomActionView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBSkinBottomActionView.h; sourceTree = ""; }; 049FB20A2EC1C13800FAB05D /* KBSkinBottomActionView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KBSkinBottomActionView.m; sourceTree = ""; }; 049FB20C2EC1CD2800FAB05D /* KBAlert.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KBAlert.h; sourceTree = ""; }; @@ -854,6 +857,15 @@ path = UICollectionViewLeftAlignedLayout; sourceTree = ""; }; + 0498BD5F2EDFFBFC006CC1D5 /* VM */ = { + isa = PBXGroup; + children = ( + 0498BD602EDFFC12006CC1D5 /* KBMyVM.h */, + 0498BD612EDFFC12006CC1D5 /* KBMyVM.m */, + ); + path = VM; + sourceTree = ""; + }; 049FB2162EC20A6600FAB05D /* BMLongPressDragCellCollectionView */ = { isa = PBXGroup; children = ( @@ -1135,6 +1147,7 @@ 04FC95BE2EB1E3B1007BD342 /* Me */ = { isa = PBXGroup; children = ( + 0498BD5F2EDFFBFC006CC1D5 /* VM */, 04FC95BB2EB1E3B1007BD342 /* M */, 04FC95BC2EB1E3B1007BD342 /* V */, 04FC95BD2EB1E3B1007BD342 /* VC */, @@ -1742,6 +1755,7 @@ 0477BE042EBC83130055D639 /* HomeMainVC.m in Sources */, 0477BDFD2EBC6A170055D639 /* HomeHotVC.m in Sources */, 04122FAA2EC73C0100EF7AB3 /* KBVipPayHeaderView.m in Sources */, + 0498BD622EDFFC12006CC1D5 /* KBMyVM.m in Sources */, 049FB2432EC4BBB700FAB05D /* KBLoginPopView.m in Sources */, 048908CC2EBE373500FABA60 /* KBSearchBarView.m in Sources */, 04122F872EC6198C00EF7AB3 /* WMDragView.m in Sources */, diff --git a/keyBoard/AppDelegate.h b/keyBoard/AppDelegate.h index 88aaeef..6b6f447 100644 --- a/keyBoard/AppDelegate.h +++ b/keyBoard/AppDelegate.h @@ -10,6 +10,6 @@ @interface AppDelegate : UIResponder @property (nonatomic, strong) UIWindow *window; - +- (void)setupRootVC; @end diff --git a/keyBoard/AppDelegate.m b/keyBoard/AppDelegate.m index ce35538..7b1b73c 100644 --- a/keyBoard/AppDelegate.m +++ b/keyBoard/AppDelegate.m @@ -92,20 +92,25 @@ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; - BaseTabBarController *vc = [[BaseTabBarController alloc] init]; - self.window.rootViewController = vc; + + // 根据当前是否已登录决定入口:有 token 进主 TabBar,否则先进入登录页 + BOOL loggedIn = [[KBUserSessionManager shared] isLoggedIn]; + UIViewController *rootVC = nil; + if (loggedIn) { + rootVC = [[BaseTabBarController alloc] init]; + } else { + rootVC = [[KBLoginVC alloc] init]; + } + self.window.rootViewController = rootVC; } /// 首次安装时,展示性别选择页作为根控制器 - (void)setupSexSelectRootVC { - KBLoginVC *vc = [[KBLoginVC alloc] init]; + KBSexSelVC *vc = [[KBSexSelVC alloc] init]; self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; self.window.rootViewController = vc; - - return; - KBSexSelVC *sexVC = [KBSexSelVC new]; __weak typeof(self) weakSelf = self; sexVC.didFinishSelectBlock = ^{ diff --git a/keyBoard/Class/Guard/VC/KBSexSelVC.m b/keyBoard/Class/Guard/VC/KBSexSelVC.m index f44ebb9..c9edbd4 100644 --- a/keyBoard/Class/Guard/VC/KBSexSelVC.m +++ b/keyBoard/Class/Guard/VC/KBSexSelVC.m @@ -70,7 +70,7 @@ typedef NS_ENUM(NSInteger, KBSexOption) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - [[KBNetworkManager shared] GET:KB_API_APP_CONFIG parameters:nil headers:nil completion:^(id _Nullable jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) { + [[KBNetworkManager shared] GET:@"https://www.apple.com.cn/" parameters:nil headers:nil completion:^(id _Nullable jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"===="); }]; }); diff --git a/keyBoard/Class/Login/VC/KBLoginVC.m b/keyBoard/Class/Login/VC/KBLoginVC.m index 1119bb9..5c5d3eb 100644 --- a/keyBoard/Class/Login/VC/KBLoginVC.m +++ b/keyBoard/Class/Login/VC/KBLoginVC.m @@ -8,6 +8,7 @@ #import "KBLoginVC.h" #import #import "KBLoginVM.h" +#import "AppDelegate.h" @interface KBLoginVC () @@ -155,11 +156,18 @@ #pragma mark - Actions - (void)onTapAppleLogin { - // 后续可接入 Apple Sign-In 逻辑,这里先占位 KBLOG(@"onTapAppleLogin"); [[KBLoginVM shared] signInWithAppleFromViewController:KB_CURRENT_NAV completion:^(BOOL success, NSError * _Nullable error) { if (success) { [KBHUD showInfo:KBLocalized(@"Signed in successfully")]; + // 登录成功后切换到主 TabBar + dispatch_async(dispatch_get_main_queue(), ^{ + id appDelegate = UIApplication.sharedApplication.delegate; + if ([appDelegate respondsToSelector:@selector(setupRootVC)]) { + AppDelegate *delegate = (AppDelegate *)appDelegate; + [delegate setupRootVC]; + } + }); } else { NSString *msg = error.localizedDescription ?: KBLocalized(@"Sign-in failed"); [KBHUD showInfo:msg]; diff --git a/keyBoard/Class/Me/VC/KBPersonInfoVC.m b/keyBoard/Class/Me/VC/KBPersonInfoVC.m index b913c70..42d593b 100644 --- a/keyBoard/Class/Me/VC/KBPersonInfoVC.m +++ b/keyBoard/Class/Me/VC/KBPersonInfoVC.m @@ -17,7 +17,7 @@ #import "LSTPopView.h" #import "KBChangeNicknamePopView.h" #import "KBGenderPickerPopView.h" - +#import "KBMyVM.h" @interface KBPersonInfoVC () // 列表 @@ -37,6 +37,8 @@ // 压缩后的头像 JPEG 数据(可用于上传) @property (nonatomic, strong) NSData *avatarJPEGData; +@property (nonatomic, strong) KBMyVM *myVM; + @end @implementation KBPersonInfoVC @@ -185,7 +187,7 @@ - (void)onTapAvatarEdit { [self presentImagePicker]; } - (void)onTapLogout { - [[KBUserSessionManager shared] logout]; + [self.myVM logout]; } #pragma mark - Lazy UI(懒加载) @@ -366,4 +368,11 @@ return result; } +- (KBMyVM *)myVM{ + if (!_myVM) { + _myVM = [[KBMyVM alloc] init]; + } + return _myVM; +} + @end diff --git a/keyBoard/Class/Me/VM/KBMyVM.h b/keyBoard/Class/Me/VM/KBMyVM.h new file mode 100644 index 0000000..761ced9 --- /dev/null +++ b/keyBoard/Class/Me/VM/KBMyVM.h @@ -0,0 +1,16 @@ +// +// KBMyVM.h +// keyBoard +// +// Created by Mac on 2025/12/3. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface KBMyVM : NSObject +- (void)logout; +@end + +NS_ASSUME_NONNULL_END diff --git a/keyBoard/Class/Me/VM/KBMyVM.m b/keyBoard/Class/Me/VM/KBMyVM.m new file mode 100644 index 0000000..02ffc96 --- /dev/null +++ b/keyBoard/Class/Me/VM/KBMyVM.m @@ -0,0 +1,33 @@ +// +// KBMyVM.m +// keyBoard +// +// Created by Mac on 2025/12/3. +// + +#import "KBMyVM.h" +#import "AppDelegate.h" + +@implementation KBMyVM +- (void)logout{ + [KBHUD show]; + [[KBNetworkManager shared] GET:API_LOGOUT parameters:nil headers:nil completion:^(NSDictionary * jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) { + if (error != nil) { + return; + } + NSString *message = jsonOrData[KBMessage]; +// if (code == KBBizCodeSuccess) { + [KBHUD showSuccess:message]; + [[KBUserSessionManager shared] logout]; + // 登录成功后切换到主 TabBar + dispatch_async(dispatch_get_main_queue(), ^{ + id appDelegate = UIApplication.sharedApplication.delegate; + if ([appDelegate respondsToSelector:@selector(setupRootVC)]) { + AppDelegate *delegate = (AppDelegate *)appDelegate; + [delegate setupRootVC]; + } + }); +// } + }]; +} +@end diff --git a/keyBoard/KeyBoardPrefixHeader.pch b/keyBoard/KeyBoardPrefixHeader.pch index 0c0c0eb..d5ce4da 100644 --- a/keyBoard/KeyBoardPrefixHeader.pch +++ b/keyBoard/KeyBoardPrefixHeader.pch @@ -32,6 +32,7 @@ /// 项目 +#import "KBBizCode.h" #import "KBNetworkManager.h" #import "UIView+KBShadow.h" #import "UIImage+KBColor.h"