This commit is contained in:
2025-12-03 12:55:51 +08:00
parent 6be90ebb10
commit 27aa723e7d
9 changed files with 120 additions and 65 deletions

View File

@@ -6,7 +6,7 @@
#import "KBNetworkManager.h"
#import "KBAPI.h"
#import "KBHUD.h"
#import "KBBizCode.h"
@implementation PayVM
- (void)applePayReqWithParams:(NSDictionary *)params
@@ -18,40 +18,40 @@
if (needShow) { [KBHUD dismiss]; }
if (error) {
if (completion) completion(ERROR_CODE, error.localizedDescription ?: KBLocalized(@"Network error"));
// if (completion) completion(ERROR_CODE, error.localizedDescription ?: KBLocalized(@"Network error"));
return;
}
NSInteger sta = [self.class extractStatusFromResponseObject:jsonOrData response:response];
NSString *msg = [self.class extractMessageFromResponseObject:jsonOrData] ?: (sta == SUCCESS_CODE ? @"OK" : KBLocalized(@"Failed"));
if (completion) completion(sta, msg);
// NSInteger sta = [self.class extractStatusFromResponseObject:jsonOrData response:response];
// NSString *msg = [self.class extractMessageFromResponseObject:jsonOrData] ?: (sta == KBBizCodeSuccess ? @"OK" : KBLocalized(@"Failed"));
if (completion) completion(KBBizCodeSuccess, @"ok");
}];
}
#pragma mark - Helpers
+ (NSInteger)extractStatusFromResponseObject:(id)obj response:(NSURLResponse *)resp {
// JSON code/status/success
if ([obj isKindOfClass:NSDictionary.class]) {
NSDictionary *d = (NSDictionary *)obj;
id code = d[@"code"] ?: d[@"status"] ?: d[@"retcode"];
if ([code isKindOfClass:NSNumber.class]) {
return [((NSNumber *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE;
}
if ([code isKindOfClass:NSString.class]) {
// "0"
return [((NSString *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE;
}
id success = d[@"success"] ?: d[@"ok"];
if ([success respondsToSelector:@selector(boolValue)]) {
return [success boolValue] ? SUCCESS_CODE : ERROR_CODE;
}
}
// HTTP 2xx
NSInteger http = 0;
if ([resp isKindOfClass:NSHTTPURLResponse.class]) { http = ((NSHTTPURLResponse *)resp).statusCode; }
return (http >= 200 && http < 300) ? SUCCESS_CODE : ERROR_CODE;
}
//+ (NSInteger)extractStatusFromResponseObject:(id)obj response:(NSURLResponse *)resp {
// // JSON code/status/success
// if ([obj isKindOfClass:NSDictionary.class]) {
// NSDictionary *d = (NSDictionary *)obj;
// id code = d[@"code"] ?: d[@"status"] ?: d[@"retcode"];
// if ([code isKindOfClass:NSNumber.class]) {
// return [((NSNumber *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE;
// }
// if ([code isKindOfClass:NSString.class]) {
// // "0"
// return [((NSString *)code) integerValue] == 0 ? SUCCESS_CODE : ERROR_CODE;
// }
// id success = d[@"success"] ?: d[@"ok"];
// if ([success respondsToSelector:@selector(boolValue)]) {
// return [success boolValue] ? SUCCESS_CODE : ERROR_CODE;
// }
// }
// // HTTP 2xx
// NSInteger http = 0;
// if ([resp isKindOfClass:NSHTTPURLResponse.class]) { http = ((NSHTTPURLResponse *)resp).statusCode; }
// return (http >= 200 && http < 300) ? SUCCESS_CODE : ERROR_CODE;
//}
+ (NSString *)extractMessageFromResponseObject:(id)obj {
if (![obj isKindOfClass:NSDictionary.class]) return nil;