Files
keyboard/keyBoard/Class/Me/VM/KBMyVM.m

267 lines
11 KiB
Mathematica
Raw Normal View History

2025-12-03 13:31:02 +08:00
//
// KBMyVM.m
// keyBoard
//
// Created by Mac on 2025/12/3.
//
#import "KBMyVM.h"
#import "AppDelegate.h"
2025-12-03 20:14:14 +08:00
#import "KBNetworkManager.h"
#import "KBUser.h"
2025-12-04 16:18:43 +08:00
#import "KBAPI.h"
2025-12-08 16:39:47 +08:00
//#import <MJExtension/MJExtension.h>
#import "KBMyMainModel.h"
2025-12-11 20:40:49 +08:00
#import "KBSkinService.h"
2025-12-11 19:43:55 +08:00
#import "KBSkinInstallBridge.h"
2025-12-03 13:31:02 +08:00
2025-12-04 16:59:59 +08:00
NSString * const KBUserCharacterDeletedNotification = @"KBUserCharacterDeletedNotification";
2025-12-03 13:31:02 +08:00
@implementation KBMyVM
2025-12-03 20:14:14 +08:00
- (void)fetchUserDetailWithCompletion:(KBMyUserDetailCompletion)completion {
2025-12-03 20:31:33 +08:00
// [KBHUD show];
2025-12-03 20:14:14 +08:00
[[KBNetworkManager shared] GET:KB_API_USER_DETAIL
parameters:nil
headers:nil
autoShowBusinessError:NO
completion:^(NSDictionary *jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[KBHUD dismiss];
if (error) {
NSString *msg = KBBizMessageFromJSONObject(jsonOrData) ?: error.localizedDescription ?: KBLocalized(@"Network error");
[KBHUD showInfo:msg];
if (completion) completion(nil, error);
return;
}
id dataObj = jsonOrData[KBData] ?: jsonOrData[@"data"];
if (![dataObj isKindOfClass:[NSDictionary class]]) {
NSError *e = [NSError errorWithDomain:KBNetworkErrorDomain
code:KBNetworkErrorInvalidResponse
userInfo:@{NSLocalizedDescriptionKey: KBLocalized(@"Invalid response")}];
[KBHUD showInfo:e.localizedDescription];
if (completion) completion(nil, e);
return;
}
KBUser *user = [KBUser mj_objectWithKeyValues:(NSDictionary *)dataObj];
if (completion) completion(user, nil);
}];
}
2025-12-04 13:37:11 +08:00
- (void)fetchCharacterListByUserWithCompletion:(KBCharacterListCompletion)completion{
[[KBNetworkManager shared] GET:KB_API_CHARACTER_LISTBYUSER
parameters:nil
headers:nil
autoShowBusinessError:NO
completion:^(NSDictionary *jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[KBHUD dismiss];
if (error) {
NSString *msg = KBBizMessageFromJSONObject(jsonOrData) ?: error.localizedDescription ?: KBLocalized(@"Network error");
[KBHUD showInfo:msg];
if (completion) completion([NSArray new], error);
return;
}
id dataObj = jsonOrData[KBData] ?: jsonOrData[@"data"];
2025-12-08 16:39:47 +08:00
if ([jsonOrData isKindOfClass:[NSDictionary class]]) {
/// MJNull 💥
KBMyMainModel *mainModel = [KBMyMainModel mj_objectWithKeyValues:jsonOrData];
NSDictionary *dict = [mainModel mj_keyValues];
NSUserDefaults *sharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:AppGroup];
[sharedDefaults setObject:dict forKey:AppGroup_MyKbJson];
[sharedDefaults synchronize];
KBLOG(@"[MainApp] 写入完成");
}
2025-12-04 13:37:11 +08:00
if (![dataObj isKindOfClass:[NSArray class]]) {
NSError *e = [NSError errorWithDomain:KBNetworkErrorDomain
code:KBNetworkErrorInvalidResponse
userInfo:@{NSLocalizedDescriptionKey: KBLocalized(@"Invalid response")}];
[KBHUD showInfo:e.localizedDescription];
if (completion) completion([NSArray new], e);
return;
}
NSArray<KBCharacter *> *list = [KBCharacter mj_objectArrayWithKeyValuesArray:(NSArray *)dataObj];
if (completion) completion(list, nil);
}];
}
2025-12-11 19:43:55 +08:00
- (void)fetchDownloadedThemesWithCompletion:(KBMyPurchasedThemesCompletion)completion {
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
NSArray<KBSkinDownloadRecord *> *records = [KBSkinInstallBridge installedSkinRecords];
NSMutableArray<KBMyTheme *> *themes = [NSMutableArray arrayWithCapacity:records.count];
for (KBSkinDownloadRecord *record in records) {
KBMyTheme *theme = [KBMyTheme new];
theme.themeId = record.skinId;
theme.themeName = record.name;
theme.themeDownload = record.zipURL;
theme.themePreviewImageUrl = record.previewImage;
[themes addObject:theme];
2025-12-11 13:40:32 +08:00
}
2025-12-11 19:43:55 +08:00
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) completion(themes.copy, nil);
});
});
2025-12-11 13:40:32 +08:00
}
2025-12-11 19:43:55 +08:00
- (void)deleteDownloadedThemesWithIds:(NSArray<NSString *> *)themeIds
completion:(KBDeleteThemesCompletion)completion {
2025-12-11 18:36:14 +08:00
if (themeIds.count == 0) {
if (completion) {
NSError *e = [NSError errorWithDomain:KBNetworkErrorDomain
code:KBNetworkErrorInvalidResponse
userInfo:@{NSLocalizedDescriptionKey: KBLocalized(@"Invalid parameter")}];
completion(NO, e);
}
return;
}
2025-12-11 20:40:49 +08:00
[[KBSkinService shared] deleteSkinsWithIds:themeIds
completion:^(BOOL success, NSError * _Nullable error) {
if (completion) {
completion(success, error);
2025-12-11 18:36:14 +08:00
}
2025-12-11 20:40:49 +08:00
}];
2025-12-11 18:36:14 +08:00
}
2025-12-04 16:18:43 +08:00
///
- (void)updateUserCharacterSortWithSortArray:(NSArray<NSNumber *> *)sortArray
completion:(KBUpdateCharacterSortCompletion)completion {
// {"sort": [id1, id2, ...]}
NSMutableDictionary *params = [NSMutableDictionary dictionary];
if (sortArray.count > 0) {
params[@"sort"] = sortArray;
} else {
params[@"sort"] = @[];
}
[[KBNetworkManager shared] POST:API_CHARACTER_UPDATE_USER_CHARTSORT
jsonBody:params
headers:nil
autoShowBusinessError:true
completion:^(NSDictionary * _Nullable json,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
if (completion) {
completion(error == nil, error);
}
}];
}
2025-12-04 16:41:33 +08:00
///
- (void)deleteUserCharacterWithId:(NSNumber *)characterId
completion:(KBDeleteUserCharacterCompletion)completion {
if (!characterId) {
if (completion) {
NSError *e = [NSError errorWithDomain:KBNetworkErrorDomain
code:KBNetworkErrorInvalidResponse
userInfo:@{NSLocalizedDescriptionKey: KBLocalized(@"Invalid parameter")}];
completion(NO, e);
}
return;
}
NSDictionary *params = @{@"id": characterId};
[[KBNetworkManager shared] GET:API_CHARACTER_DEL_USER_CHARACTER
parameters:params
headers:nil
autoShowBusinessError:YES
completion:^(NSDictionary *jsonOrData,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
2025-12-04 16:59:59 +08:00
BOOL success = (error == nil);
if (success) {
2025-12-04 19:12:34 +08:00
2025-12-04 16:59:59 +08:00
}
2025-12-04 16:41:33 +08:00
if (completion) {
2025-12-04 16:59:59 +08:00
completion(success, error);
2025-12-04 16:41:33 +08:00
}
}];
}
2025-12-04 13:37:11 +08:00
///
- (void)upLoadAvatarWithData:(NSData *)avatarData completion:(KBUpLoadAvatarCompletion)completion{
2025-12-04 14:07:12 +08:00
KBWeakSelf;
2025-12-04 13:37:11 +08:00
[KBHUD show];
[[KBNetworkManager shared] uploadFile:KB_API_FILE_UPLOAD
fileData:avatarData
fileName:@"avatar.jpg"
mimeType:@"image/jpeg"
headers:nil
completion:^(NSDictionary * _Nullable json,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
[KBHUD dismiss];
if (error) {
NSLog(@"上传失败: %@", error);
return;
}
NSString *avImageString = json[@"data"];
// [weakSelf.avatarView kb_setImageURL:[NSURL URLWithString:avImageString] placeholder:KBPlaceholderImage];
2025-12-04 14:07:12 +08:00
KBUser *localUser = [KBUserSessionManager shared].currentUser;
localUser.avatarUrl = avImageString;
[weakSelf updateUserInfo:localUser completion:^(BOOL success, NSError * _Nullable error) {
if (error) { if (completion) completion(NO, error); return; }
completion(true,nil);
}];
2025-12-04 13:37:11 +08:00
}];
}
///
- (void)updateUserInfo:(KBUser *)user completion:(KBUpdateUserInfoCompletion)completion{
///
2025-12-04 14:07:12 +08:00
2025-12-04 13:37:11 +08:00
NSMutableDictionary *params = [NSMutableDictionary dictionary];
2025-12-04 14:07:12 +08:00
if (user.userId.length) params[@"uid"] = user.userId;
if (user.nickName.length) params[@"nickName"] = user.nickName;
params[@"gender"] = @(user.gender);
if (user.avatarUrl.length) params[@"avatarUrl"] = user.avatarUrl;
[KBHUD show];
[[KBNetworkManager shared] POST:API_UPDATA_INFO jsonBody:params headers:nil autoShowBusinessError:true completion:^(NSDictionary * _Nullable json, NSURLResponse * _Nullable response, NSError * _Nullable error) {
[KBHUD dismiss];
if (error) { if (completion) completion(NO, error); return; }
completion(true,nil);
}];
2025-12-04 13:37:11 +08:00
}
2025-12-03 13:31:02 +08:00
- (void)logout{
[KBHUD show];
2025-12-03 13:54:57 +08:00
[[KBNetworkManager shared] GET:API_LOGOUT
parameters:nil
headers:nil
2025-12-03 14:30:02 +08:00
autoShowBusinessError:NO
2025-12-03 13:54:57 +08:00
completion:^(NSDictionary *jsonOrData, NSURLResponse * _Nullable response, NSError * _Nullable error) {
// HUD
[KBHUD dismiss];
if (error) {
// message
NSString *msg = KBBizMessageFromJSONObject(jsonOrData) ?: error.localizedDescription ?: KBLocalized(@"Network error");
[KBHUD showInfo:msg];
2025-12-03 13:31:02 +08:00
return;
}
2025-12-03 13:54:57 +08:00
NSString *message = jsonOrData[KBMessage] ?: KBLocalized(@"Success");
2025-12-03 13:31:02 +08:00
[KBHUD showSuccess:message];
2025-12-03 13:54:57 +08:00
// 退
[[KBUserSessionManager shared] logout];
// /
dispatch_async(dispatch_get_main_queue(), ^{
id<UIApplicationDelegate> appDelegate = UIApplication.sharedApplication.delegate;
2025-12-03 14:30:02 +08:00
if ([appDelegate respondsToSelector:@selector(toMainTabbarVC)]) {
2025-12-03 13:54:57 +08:00
AppDelegate *delegate = (AppDelegate *)appDelegate;
2025-12-03 14:30:02 +08:00
[delegate toMainTabbarVC];
2025-12-03 13:54:57 +08:00
}
});
2025-12-03 13:31:02 +08:00
}];
}
@end