2025-12-11 13:16:06 +08:00
|
|
|
//
|
|
|
|
|
// KBShopVM.m
|
|
|
|
|
// keyBoard
|
|
|
|
|
//
|
|
|
|
|
// Created by Mac on 2025/12/9.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "KBShopVM.h"
|
|
|
|
|
#import "KBNetworkManager.h"
|
|
|
|
|
#import "KBAPI.h"
|
|
|
|
|
#import "KBBizCode.h"
|
|
|
|
|
#import <MJExtension/MJExtension.h>
|
|
|
|
|
|
|
|
|
|
@interface KBShopVM ()
|
|
|
|
|
@property (nonatomic, copy, readwrite, nullable) NSArray<KBShopStyleModel *> *styles;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation KBShopVM
|
|
|
|
|
|
|
|
|
|
- (NSError *)kb_invalidResponseError {
|
|
|
|
|
return [NSError errorWithDomain:KBNetworkErrorDomain
|
|
|
|
|
code:KBNetworkErrorInvalidResponse
|
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey: KBLocalized(@"Invalid response")}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSError *)kb_invalidParameterError {
|
|
|
|
|
return [NSError errorWithDomain:KBNetworkErrorDomain
|
|
|
|
|
code:KBNetworkErrorInvalidResponse
|
|
|
|
|
userInfo:@{NSLocalizedDescriptionKey: KBLocalized(@"Invalid parameter")}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)fetchAllStylesWithCompletion:(KBShopStylesCompletion)completion {
|
|
|
|
|
[[KBNetworkManager shared] GET:API_THEME_LIST_ALL_STYLES
|
|
|
|
|
parameters:nil
|
|
|
|
|
headers:nil
|
|
|
|
|
autoShowBusinessError:NO
|
|
|
|
|
completion:^(NSDictionary * _Nullable json,
|
|
|
|
|
NSURLResponse * _Nullable response,
|
|
|
|
|
NSError * _Nullable error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
if (completion) completion(nil, error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
id dataObj = json[KBData] ?: json[@"data"];
|
|
|
|
|
if (![dataObj isKindOfClass:[NSArray class]]) {
|
|
|
|
|
if (completion) completion(nil, [self kb_invalidResponseError]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
NSArray<KBShopStyleModel *> *list = [KBShopStyleModel mj_objectArrayWithKeyValuesArray:(NSArray *)dataObj];
|
|
|
|
|
self.styles = list;
|
|
|
|
|
if (completion) completion(list, nil);
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)fetchThemesForStyleId:(nullable NSString *)styleId
|
|
|
|
|
completion:(KBShopThemesCompletion)completion {
|
|
|
|
|
if (styleId.length == 0) {
|
|
|
|
|
if (completion) completion(nil, [self kb_invalidParameterError]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
NSMutableDictionary *params = [NSMutableDictionary dictionary];
|
|
|
|
|
params[@"themeStyle"] = @([styleId integerValue]);
|
|
|
|
|
[[KBNetworkManager shared] GET:API_THEME_LIST_BY_STYLE
|
|
|
|
|
parameters:params
|
|
|
|
|
headers:nil
|
|
|
|
|
autoShowBusinessError:NO
|
|
|
|
|
completion:^(NSDictionary * _Nullable json,
|
|
|
|
|
NSURLResponse * _Nullable response,
|
|
|
|
|
NSError * _Nullable error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
if (completion) completion(nil, error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
id dataObj = json[KBData] ?: json[@"data"];
|
|
|
|
|
if (![dataObj isKindOfClass:[NSArray class]]) {
|
|
|
|
|
if (completion) completion(nil, [self kb_invalidResponseError]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
NSArray<KBShopThemeModel *> *list = [KBShopThemeModel mj_objectArrayWithKeyValuesArray:(NSArray *)dataObj];
|
|
|
|
|
if (completion) completion(list, nil);
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 14:03:13 +08:00
|
|
|
- (void)fetchWalletBalanceWithCompletion:(KBShopBalanceCompletion)completion {
|
|
|
|
|
[[KBNetworkManager shared] GET:API_WALLET_BALANCE
|
|
|
|
|
parameters:nil
|
|
|
|
|
headers:nil
|
|
|
|
|
autoShowBusinessError:NO
|
|
|
|
|
completion:^(NSDictionary * _Nullable json,
|
|
|
|
|
NSURLResponse * _Nullable response,
|
|
|
|
|
NSError * _Nullable error) {
|
|
|
|
|
if (error) {
|
|
|
|
|
if (completion) completion(nil, error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
id dataObj = json[KBData] ?: json[@"data"];
|
|
|
|
|
if (![dataObj isKindOfClass:[NSDictionary class]]) {
|
|
|
|
|
if (completion) completion(nil, [self kb_invalidResponseError]);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
id balanceValue = dataObj[@"balance"];
|
|
|
|
|
NSNumber *balanceNumber = nil;
|
|
|
|
|
if ([balanceValue isKindOfClass:[NSNumber class]]) {
|
|
|
|
|
balanceNumber = balanceValue;
|
|
|
|
|
} else if ([balanceValue isKindOfClass:[NSString class]]) {
|
|
|
|
|
balanceNumber = @([(NSString *)balanceValue doubleValue]);
|
|
|
|
|
}
|
|
|
|
|
if (!balanceNumber) {
|
|
|
|
|
balanceNumber = @(0);
|
|
|
|
|
}
|
|
|
|
|
if (completion) completion(balanceNumber, nil);
|
|
|
|
|
}];
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-11 13:16:06 +08:00
|
|
|
@end
|