This commit is contained in:
2025-12-11 16:39:22 +08:00
parent e4442afe72
commit 7f90240731
6 changed files with 207 additions and 32 deletions

View File

@@ -23,6 +23,10 @@ typedef void(^KBShopBalanceCompletion)(NSNumber *_Nullable balance,
NSError *_Nullable error);
typedef void(^KBShopDetailCompletion)(KBShopThemeDetailModel *_Nullable detail,
NSError *_Nullable error);
typedef void(^KBShopPurchaseCompletion)(BOOL success,
NSError *_Nullable error);
typedef void(^KBShopDownloadInfoCompletion)(NSDictionary *_Nullable info,
NSError *_Nullable error);
@interface KBShopVM : NSObject
@property (nonatomic, copy, readonly, nullable) NSArray<KBShopStyleModel *> *styles;
@@ -41,6 +45,14 @@ typedef void(^KBShopDetailCompletion)(KBShopThemeDetailModel *_Nullable detail,
- (void)fetchThemeDetailWithId:(nullable NSString *)themeId
completion:(KBShopDetailCompletion)completion;
/// 购买主题
- (void)purchaseThemeWithId:(nullable NSString *)themeId
completion:(KBShopPurchaseCompletion)completion;
/// 获取主题下载信息
- (void)fetchThemeDownloadInfoWithId:(nullable NSString *)themeId
completion:(KBShopDownloadInfoCompletion)completion;
@end
NS_ASSUME_NONNULL_END

View File

@@ -13,6 +13,7 @@
@interface KBShopVM ()
@property (nonatomic, copy, readwrite, nullable) NSArray<KBShopStyleModel *> *styles;
- (id)kb_themeIdParamFromString:(NSString *)themeId;
@end
@@ -99,17 +100,17 @@
if (completion) completion(nil, [self kb_invalidResponseError]);
return;
}
NSString *balanceValue = dataObj[@"balanceDisplay"];
// 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(balanceValue, nil);
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);
}];
}
@@ -119,7 +120,7 @@
if (completion) completion(nil, [self kb_invalidParameterError]);
return;
}
NSDictionary *params = @{@"themeId": themeId};
NSDictionary *params = @{@"themeId": [self kb_themeIdParamFromString:themeId]};
[[KBNetworkManager shared] GET:API_THEME_DETAIL
parameters:params
headers:nil
@@ -141,4 +142,56 @@
}];
}
- (void)purchaseThemeWithId:(nullable NSString *)themeId
completion:(KBShopPurchaseCompletion)completion {
if (themeId.length == 0) {
if (completion) completion(NO, [self kb_invalidParameterError]);
return;
}
NSDictionary *body = @{@"themeId": [self kb_themeIdParamFromString:themeId]};
[[KBNetworkManager shared] POST:API_THEME_PURCHASE
jsonBody:body
headers:nil
autoShowBusinessError:NO
completion:^(NSDictionary * _Nullable json,
NSURLResponse * _Nullable response,
NSError * _Nullable error) {
if (completion) completion(error == nil, error);
}];
}
- (void)fetchThemeDownloadInfoWithId:(nullable NSString *)themeId
completion:(KBShopDownloadInfoCompletion)completion {
if (themeId.length == 0) {
if (completion) completion(nil, [self kb_invalidParameterError]);
return;
}
NSDictionary *params = @{@"themeId": [self kb_themeIdParamFromString:themeId]};
[[KBNetworkManager shared] GET:API_THEME_DOWNLOAD
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:[NSDictionary class]]) {
if (completion) completion(nil, [self kb_invalidResponseError]);
return;
}
if (completion) completion((NSDictionary *)dataObj, nil);
}];
}
- (id)kb_themeIdParamFromString:(NSString *)themeId {
if (themeId.length == 0) { return @""; }
NSNumberFormatter *formatter = [NSNumberFormatter new];
NSNumber *number = [formatter numberFromString:themeId];
return number ?: themeId;
}
@end