This commit is contained in:
2025-12-02 20:33:17 +08:00
parent c56655c728
commit 2f55e7bfa1
12 changed files with 83 additions and 133 deletions

View File

@@ -38,8 +38,8 @@
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 1.
[[FGIAPManager shared] setConfigureWith:[IAPVerifyTransactionObj new]];
/// 2
[KBLocalizationManager shared].supportedLanguageCodes = @[ @"en", @"zh-Hans"];
/// 2使
[KBLocalizationManager shared].supportedLanguageCodes = KBDefaultSupportedLanguageCodes();
/// 3 : token
// [[KBUserSessionManager shared] bootstrapIfNeeded];

View File

@@ -71,7 +71,10 @@
if (!self) return;
KBLocalizationManager *mgr = [KBLocalizationManager shared];
NSString *next = [mgr.currentLanguageCode.lowercaseString hasPrefix:@"zh"] ? @"en" : @"zh-Hans";
//
NSString *next = [mgr.currentLanguageCode.lowercaseString hasPrefix:@"zh"]
? KBLanguageCodeEnglish
: KBLanguageCodeSimplifiedChinese;
[mgr setCurrentLanguageCode:next persist:YES];
//

View File

@@ -11,22 +11,16 @@ NS_ASSUME_NONNULL_BEGIN
// 标识
@property (nonatomic, copy, nullable) NSString *userId; // id/user_id/uid
@property (nonatomic, copy, nullable) NSString *appleUserId; // 用 Apple 登录返回的 userID可选
// 基本信息
@property (nonatomic, copy, nullable) NSString *nickname;
@property (nonatomic, copy, nullable) NSString *avatar; // 头像 URL
@property (nonatomic, copy, nullable) NSString *nickName;
@property (nonatomic, copy, nullable) NSString *avatarUrl; // 头像 URL
@property (nonatomic, copy, nullable) NSString *gender; // 性别(后端可能返回 string/int统一转字符串存
@property (nonatomic, copy, nullable) NSString *mobile;
@property (nonatomic, copy, nullable) NSString *email;
/// 邮箱是否验证
@property (nonatomic, assign) BOOL emailVerified;
// 会话信息
@property (nonatomic, copy, nullable) NSString *token; // token/access_token/accessToken
@property (nonatomic, copy, nullable) NSString *refreshToken; // refresh_token/refreshToken
@property (nonatomic, strong, nullable) NSDate *expiryDate; // 若后端返回过期时间,转为日期
/// 从后端返回(可能顶层或 data/user 嵌套)中解析用户模型。内部使用 MJExtension。
+ (instancetype)userFromResponseObject:(id)jsonObject;
@end

View File

@@ -9,89 +9,10 @@
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{
@"userId": @[@"id", @"user_id", @"uid"],
@"appleUserId": @[@"appleUserId", @"apple_user_id", @"apple_userid", @"appleUserID"],
@"nickname": @[@"nickname", @"nick", @"name"],
@"avatar": @[@"avatar", @"avatar_url", @"head", @"headimg"],
@"userId": @[@"uid"],
@"gender": @[@"gender", @"sex"],
@"mobile": @[@"mobile", @"phone"],
@"email": @[@"email"],
@"token": @[@"token", @"access_token", @"accessToken"],
@"refreshToken": @[@"refresh_token", @"refreshToken"],
@"expiryDate": @[@"expire_at", @"expireAt", @"expires_at", @"expiresAt", @"expired_at"],
};
}
// // NSDate
- (void)setExpiryDate:(NSDate *)expiryDate { _expiryDate = expiryDate; }
+ (instancetype)userFromResponseObject:(id)jsonObject {
if (!jsonObject) return nil;
NSDictionary *dict = nil;
if ([jsonObject isKindOfClass:NSDictionary.class]) {
dict = (NSDictionary *)jsonObject;
} else if ([jsonObject isKindOfClass:NSData.class]) {
// JSON data -> dict
id obj = [NSJSONSerialization JSONObjectWithData:(NSData *)jsonObject options:0 error:NULL];
if ([obj isKindOfClass:NSDictionary.class]) dict = obj;
}
if (!dict) return nil;
// data.user data
NSDictionary *candidate = nil;
id data = dict[@"data"]; if ([data isKindOfClass:NSDictionary.class]) { candidate = data; }
id user = [candidate objectForKey:@"user"]; if (![user isKindOfClass:NSDictionary.class]) { user = dict[@"user"]; }
NSDictionary *userDict = ([user isKindOfClass:NSDictionary.class]) ? (NSDictionary *)user : (candidate ?: dict);
KBUser *u = [KBUser mj_objectWithKeyValues:userDict];
// token
if (u.token.length == 0) {
NSString *t = [self pickTokenFromDictionary:dict];
if (t.length) u.token = t;
}
//
id exp = userDict[@"expire_at"] ?: userDict[@"expireAt"] ?: userDict[@"expires_at"] ?: userDict[@"expiresAt"] ?: userDict[@"expired_at"];
if ([exp isKindOfClass:NSNumber.class]) {
// /> 10^11
NSTimeInterval ts = [(NSNumber *)exp doubleValue];
if (ts > 1e11) ts = ts / 1000.0;
u.expiryDate = [NSDate dateWithTimeIntervalSince1970:ts];
} else if ([exp isKindOfClass:NSString.class]) {
// ISO8601
NSDateFormatter *fmt = [NSDateFormatter new];
fmt.locale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
fmt.dateFormat = @"yyyy-MM-dd'T'HH:mm:ssZ";
NSDate *d = [fmt dateFromString:(NSString *)exp];
if (!d) {
//
NSTimeInterval ts = [(NSString *)exp doubleValue];
if (ts > 0) d = [NSDate dateWithTimeIntervalSince1970:ts];
}
if (d) u.expiryDate = d;
}
return u;
}
+ (NSString *)pickTokenFromDictionary:(NSDictionary *)dict {
if (![dict isKindOfClass:NSDictionary.class]) return nil;
NSString *(^pick)(NSDictionary *) = ^NSString *(NSDictionary *d) {
NSArray *keys = @[ @"token", @"access_token", @"accessToken" ];
for (NSString *k in keys) {
id v = d[k]; if ([v isKindOfClass:NSString.class] && ((NSString *)v).length > 0) return v;
}
return nil;
};
NSString *t = pick(dict); if (t.length) return t;
id data = dict[@"data"]; if ([data isKindOfClass:NSDictionary.class]) { t = pick(data); if (t.length) return t; }
id user = dict[@"user"]; if ([user isKindOfClass:NSDictionary.class]) { t = pick(user); if (t.length) return t; }
NSDictionary *d2 = dict[@"data"]; if ([d2 isKindOfClass:NSDictionary.class]) {
NSDictionary *session = d2[@"session"]; if ([session isKindOfClass:NSDictionary.class]) { t = pick(session); if (t.length) return t; }
}
return nil;
}
@end

View File

@@ -117,9 +117,9 @@
// terms/privacy
[self.contentContainerView addSubview:self.agreementTextView];
[self.agreementTextView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.contentContainerView);
make.left.greaterThanOrEqualTo(self.contentContainerView).offset(30);
make.right.lessThanOrEqualTo(self.contentContainerView).offset(-30);
// 30 textAlignmentCenter
make.left.equalTo(self.contentContainerView).offset(30);
make.right.equalTo(self.contentContainerView).offset(-30);
make.bottom.equalTo(self.contentContainerView).offset(-KB_SAFE_BOTTOM - 84);
}];
@@ -128,14 +128,14 @@
[self.contentContainerView addSubview:forgot];
[forgot mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.contentContainerView);
make.bottom.equalTo(self.contentContainerView).offset(-KB_SAFE_BOTTOM - 24);
make.bottom.equalTo(self.contentContainerView).offset(-KB_SAFE_BOTTOM - 10);
}];
UIView *accountLine = [UIView new];
[self.contentContainerView addSubview:accountLine];
[accountLine mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.contentContainerView);
make.bottom.equalTo(forgot.mas_top).offset(-8);
make.bottom.equalTo(forgot.mas_top).offset(-2);
}];
[accountLine addSubview:self.noAccountLabel];
@@ -246,7 +246,7 @@
_emailLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_emailLoginButton setTitle:KBLocalized(@"Continue Via Email") forState:UIControlStateNormal];
[_emailLoginButton setTitleColor:[UIColor colorWithHex:KBBlackValue] forState:UIControlStateNormal];
_emailLoginButton.titleLabel.font = [KBFont medium:16];
_emailLoginButton.titleLabel.font = [KBFont medium:19];
// _emailLoginButton.backgroundColor = [UIColor colorWithHex:0xF7F7F7];
_emailLoginButton.layer.cornerRadius = 10.0;
_emailLoginButton.layer.masksToBounds = YES;
@@ -291,10 +291,14 @@
NSString *termsText = @"terms of service";
NSString *privacyText = @"privacy policy";
NSMutableParagraphStyle *paragraph = [[NSMutableParagraphStyle alloc] init];
paragraph.alignment = NSTextAlignmentCenter; //
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:fullText
attributes:@{
NSFontAttributeName : [KBFont regular:12],
NSForegroundColorAttributeName : [UIColor colorWithHex:0x717171]
NSFontAttributeName : [KBFont regular:10],
NSForegroundColorAttributeName : [UIColor colorWithHex:0x717171],
NSParagraphStyleAttributeName : paragraph
}];
NSString *lowerFull = fullText.lowercaseString;
@@ -332,8 +336,8 @@
if (!_noAccountLabel) {
_noAccountLabel = [UILabel new];
_noAccountLabel.text = KBLocalized(@"Don't Have An Account?");
_noAccountLabel.font = [KBFont regular:13];
_noAccountLabel.textColor = [UIColor colorWithWhite:0.45 alpha:1.0];
_noAccountLabel.font = [KBFont regular:10];
_noAccountLabel.textColor = [UIColor colorWithHex:KBBlackValue];
}
return _noAccountLabel;
}
@@ -343,7 +347,7 @@
_signUpButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_signUpButton setTitle:KBLocalized(@"Sign Up") forState:UIControlStateNormal];
[_signUpButton setTitleColor:[UIColor colorWithHex:KBColorValue] forState:UIControlStateNormal];
_signUpButton.titleLabel.font = [KBFont medium:13];
_signUpButton.titleLabel.font = [KBFont medium:10];
[_signUpButton addTarget:self action:@selector(onTapSignUp) forControlEvents:UIControlEventTouchUpInside];
}
return _signUpButton;
@@ -354,7 +358,7 @@
_forgotPasswordButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_forgotPasswordButton setTitle:KBLocalized(@"Forgot Password?") forState:UIControlStateNormal];
[_forgotPasswordButton setTitleColor:[UIColor colorWithHex:KBColorValue] forState:UIControlStateNormal];
_forgotPasswordButton.titleLabel.font = [KBFont regular:13];
_forgotPasswordButton.titleLabel.font = [KBFont regular:10];
[_forgotPasswordButton addTarget:self action:@selector(onTapForgotPassword) forControlEvents:UIControlEventTouchUpInside];
}
return _forgotPasswordButton;

View File

@@ -43,27 +43,29 @@
if (identityToken.length) params[@"identityToken"] = identityToken;
if (authorizationCode.length) params[@"accessCode"] = authorizationCode; // 使
if (cred.user.length) params[@"userID"] = cred.user; //
[KBHUD show];
//
[[KBNetworkManager shared] POST:API_APPLE_LOGIN jsonBody:params headers:nil completion:^(id _Nullable jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[KBHUD dismiss];
if (error) { if (completion) completion(NO, error); return; }
// JSON token data/user
// 使 MJExtension
KBUser *user = [KBUser userFromResponseObject:jsonOrData];
NSDictionary *dict = jsonOrData[@"data"];
KBUser *user = [KBUser mj_objectWithKeyValues:dict];
self.currentUser = user;
NSString *token = user.token ?: [self.class tokenFromResponseObject:jsonOrData];
if (token.length == 0) {
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: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")}]);
// 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")}]);
}];
}];
}

View File

@@ -97,9 +97,9 @@ static NSString * const kKBSessionInstallFlagKey = @"KBSession.installInitialize
NSString *token = user.token;
if (token.length > 0) {
[[KBAuthManager shared] saveAccessToken:token
refreshToken:user.refreshToken
expiryDate:user.expiryDate
userIdentifier:user.appleUserId ?: user.userId];
refreshToken:nil
expiryDate:nil
userIdentifier:nil];
}
// App/使

View File

@@ -37,7 +37,7 @@ NSErrorDomain const KBNetworkErrorDomain = @"com.company.keyboard.network";
_enabled = NO; //
_timeout = 10.0;
// Accept + 使 Accept-Language
NSString *lang = [KBLocalizationManager shared].currentLanguageCode ?: @"en";
NSString *lang = [KBLocalizationManager shared].currentLanguageCode ?: KBLanguageCodeEnglish;
_defaultHeaders = @{
@"Accept": @"*/*",
@"Accept-Language": lang

View File

@@ -15,6 +15,8 @@
/// 三方
#import <Masonry/Masonry.h>
#import <MJExtension/MJExtension.h>
// 公共配置
#import "KBConfig.h"
#import "KBAPI.h" // 接口路径宏(统一管理)