222 lines
8.8 KiB
Objective-C
222 lines
8.8 KiB
Objective-C
//
|
||
// KBMyVM.m
|
||
// keyBoard
|
||
//
|
||
// Created by Mac on 2025/12/3.
|
||
//
|
||
|
||
#import "KBMyVM.h"
|
||
#import "AppDelegate.h"
|
||
#import "KBNetworkManager.h"
|
||
#import "KBUser.h"
|
||
#import "KBAPI.h"
|
||
|
||
NSString * const KBUserCharacterDeletedNotification = @"KBUserCharacterDeletedNotification";
|
||
|
||
@implementation KBMyVM
|
||
|
||
- (void)fetchUserDetailWithCompletion:(KBMyUserDetailCompletion)completion {
|
||
// [KBHUD show];
|
||
[[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);
|
||
}];
|
||
}
|
||
|
||
- (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);
|
||
}];
|
||
}
|
||
|
||
/// 更新用户人设排序
|
||
- (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);
|
||
}
|
||
}];
|
||
}
|
||
|
||
/// 删除用户人设
|
||
- (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) {
|
||
BOOL success = (error == nil);
|
||
|
||
if (success) {
|
||
// 通知 App 内其他页面(如 HomeRankContentVC / HomeHotVC)该人设已被删除
|
||
NSDictionary *info = @{@"characterId": characterId ?: @0};
|
||
dispatch_async(dispatch_get_main_queue(), ^{
|
||
[[NSNotificationCenter defaultCenter] postNotificationName:KBUserCharacterDeletedNotification
|
||
object:nil
|
||
userInfo:info];
|
||
});
|
||
}
|
||
|
||
if (completion) {
|
||
completion(success, error);
|
||
}
|
||
}];
|
||
}
|
||
|
||
/// 上传头像
|
||
- (void)upLoadAvatarWithData:(NSData *)avatarData completion:(KBUpLoadAvatarCompletion)completion{
|
||
KBWeakSelf;
|
||
[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];
|
||
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);
|
||
}];
|
||
}];
|
||
}
|
||
|
||
|
||
/// 更新用户信息
|
||
- (void)updateUserInfo:(KBUser *)user completion:(KBUpdateUserInfoCompletion)completion{
|
||
/// 获取用户信息
|
||
|
||
NSMutableDictionary *params = [NSMutableDictionary dictionary];
|
||
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);
|
||
}];
|
||
|
||
}
|
||
|
||
- (void)logout{
|
||
[KBHUD show];
|
||
[[KBNetworkManager shared] GET:API_LOGOUT
|
||
parameters:nil
|
||
headers:nil
|
||
autoShowBusinessError:NO
|
||
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];
|
||
return;
|
||
}
|
||
|
||
NSString *message = jsonOrData[KBMessage] ?: KBLocalized(@"Success");
|
||
[KBHUD showSuccess:message];
|
||
|
||
// 本地会话退出
|
||
[[KBUserSessionManager shared] logout];
|
||
|
||
// 回到登录 / 主界面
|
||
dispatch_async(dispatch_get_main_queue(), ^{
|
||
id<UIApplicationDelegate> appDelegate = UIApplication.sharedApplication.delegate;
|
||
if ([appDelegate respondsToSelector:@selector(toMainTabbarVC)]) {
|
||
AppDelegate *delegate = (AppDelegate *)appDelegate;
|
||
[delegate toMainTabbarVC];
|
||
}
|
||
});
|
||
}];
|
||
}
|
||
@end
|