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-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"];
|
|
|
|
|
|
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-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
|