This commit is contained in:
2025-12-04 13:37:11 +08:00
parent f770f8055e
commit b216ddaa61
11 changed files with 377 additions and 10 deletions

View File

@@ -6,7 +6,11 @@
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
typedef NS_ENUM(NSInteger, UserSex) {
UserSexMan = 0, // 男
UserSexWeman = 1, // 女
UserSexTwoSex = 2, // 两性
};
@interface KBUser : NSObject
// 标识
@@ -14,12 +18,13 @@ NS_ASSUME_NONNULL_BEGIN
// 基本信息
@property (nonatomic, copy, nullable) NSString *nickName;
@property (nonatomic, copy, nullable) NSString *avatarUrl; // 头像 URL
@property (nonatomic, copy, nullable) NSString *gender; // 性别(后端可能返回 string/int统一转字符串存
/// 0
@property (nonatomic, assign) UserSex gender; // 性别(后端可能返回 string/int统一转字符串存
@property (nonatomic, copy, nullable) NSString *email;
/// 邮箱是否验证
@property (nonatomic, assign) BOOL emailVerified;
// 会话信息
// token
@property (nonatomic, copy, nullable) NSString *token; // token/access_token/accessToken
@end

View File

@@ -10,9 +10,26 @@
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{
@"userId": @[@"uid"],
@"gender": @[@"gender", @"sex"],
};
}
+ (id)mj_newValueFromOldValue:(id)oldValue property:(MJProperty *)property {
if ([property.name isEqualToString:@"gender"]) {
if ([oldValue isKindOfClass:[NSNumber class]]) {
NSInteger intValue = [(NSNumber *)oldValue integerValue];
if (intValue >= UserSexTwoSex && intValue <= UserSexMan) {
return @(intValue);
} else {
//
KBLOG(@"⚠️ 收到非法的userStatus值: %ld", (long)intValue);
return @(UserSexMan);
}
}
return @(UserSexMan);
}
return oldValue;
}
@end

View File

@@ -24,6 +24,14 @@ typedef void(^KBLoginCompletion)(BOOL success, NSError * _Nullable error);
- (void)signInWithAppleFromViewController:(UIViewController *)presenter
completion:(KBLoginCompletion)completion;
/// 邮箱登录
- (void)signInWithAppleFromViewController:(UIViewController *)presenter
completion:(KBLoginCompletion)completion;
/// 邮箱登录
- (void)emailLoginEmail:(NSString *)email password:(NSString *)password WithCompletion:(KBLoginCompletion)completion;
/// 是否已登录:由 KBAuthManager 判断(是否存在有效 token
- (BOOL)isLoggedIn;

View File

@@ -70,6 +70,35 @@
}];
}
///
- (void)emailLoginEmail:(NSString *)email password:(NSString *)password WithCompletion:(KBLoginCompletion)completion;
{
[KBHUD show];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
if (email.length) params[@"email"] = email;
if (password.length) params[@"password"] = password;
//
[[KBNetworkManager shared] POST:API_EMAIL_LOGIN jsonBody:params headers:nil completion:^(NSDictionary * _Nullable jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[KBHUD dismiss];
if (error) { if (completion) completion(NO, error); return; }
NSDictionary *dict = jsonOrData[@"data"];
KBUser *user = [KBUser mj_objectWithKeyValues:dict];
self.currentUser = user;
if (user.token.length == 0) {
if (completion) completion(NO, [NSError errorWithDomain:@"KBLogin" code:-2 userInfo:@{NSLocalizedDescriptionKey: KBLocalized(@"No token returned")}]);
return;
}
[[KBUserSessionManager shared] handleLoginSuccessWithUser:user];
completion(true,nil);
// App
// BOOL ok = [[KBAuthManager shared] saveAccessToken:user.token
// refreshToken:nil
// expiryDate:nil
// userIdentifier:cred.user];
// if (completion) completion(ok, ok ? nil : [NSError errorWithDomain:@"KBLogin" code:-3 userInfo:@{NSLocalizedDescriptionKey: KBLocalized(@"Failed to save login state")}]);
}];
}
#pragma mark - Helpers
// token / access_token / accessToken data/user