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

156 lines
6.2 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-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);
}];
}
///
- (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