2025-12-02 21:32:49 +08:00
|
|
|
|
//
|
|
|
|
|
|
// KBBizCode.h
|
|
|
|
|
|
// 服务端业务状态码与通用解析工具
|
|
|
|
|
|
//
|
|
|
|
|
|
// 说明:
|
|
|
|
|
|
// - 将所有常用的业务 code 集中到这里,避免在项目里到处写裸数字;
|
|
|
|
|
|
// - 实际数值请按照你们后端的约定修改;
|
|
|
|
|
|
// - 若后端新增/调整 code,只需要维护这一处即可。
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef KBBizCode_h
|
|
|
|
|
|
#define KBBizCode_h
|
|
|
|
|
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
|
#import "KBAPI.h" // 引入 SUCCESS_CODE/ERROR_CODE 等基础定义
|
|
|
|
|
|
|
2025-12-03 13:31:02 +08:00
|
|
|
|
#define KBMessage @"message"
|
|
|
|
|
|
#define KBCode @"code"
|
|
|
|
|
|
#define KBData @"data"
|
|
|
|
|
|
|
2025-12-02 21:32:49 +08:00
|
|
|
|
/// 服务端业务状态码(按项目实际调整)
|
|
|
|
|
|
typedef NS_ENUM(NSInteger, KBBizCode) {
|
2025-12-03 12:55:51 +08:00
|
|
|
|
/// 业务成功:SUCCESS(0, "ok")
|
|
|
|
|
|
KBBizCodeSuccess = 0,
|
2025-12-02 21:32:49 +08:00
|
|
|
|
|
2025-12-03 12:55:51 +08:00
|
|
|
|
/// 参数错误:PARAMS_ERROR(40000, "请求参数错误")
|
|
|
|
|
|
KBBizCodeParamsError = 40000,
|
2025-12-02 21:32:49 +08:00
|
|
|
|
|
2025-12-03 12:55:51 +08:00
|
|
|
|
/// 上传文件为空:FILE_IS_EMPTY(40001, "上传文件为空")
|
|
|
|
|
|
KBBizCodeFileIsEmpty = 40001,
|
2025-12-02 21:32:49 +08:00
|
|
|
|
|
2025-12-03 12:55:51 +08:00
|
|
|
|
/// 文件名错误:FILE_NAME_ERROR(40002, "文件名错误")
|
|
|
|
|
|
KBBizCodeFileNameError = 40002,
|
|
|
|
|
|
|
|
|
|
|
|
/// Apple 登录失败:APPLE_LOGIN_ERROR(40003, "Apple登录失败")
|
|
|
|
|
|
KBBizCodeAppleLoginError = 40003,
|
|
|
|
|
|
|
|
|
|
|
|
/// 未登录:NOT_LOGIN_ERROR(40100, "未登录")
|
|
|
|
|
|
KBBizCodeNotLogin = 40100,
|
|
|
|
|
|
|
|
|
|
|
|
/// 无权限:NO_AUTH_ERROR(40101, "无权限")
|
|
|
|
|
|
KBBizCodeNoAuth = 40101,
|
|
|
|
|
|
|
|
|
|
|
|
/// 未能读取到有效用户令牌:TOKEN_NOT_FOUND(40102)
|
|
|
|
|
|
KBBizCodeTokenNotFound = 40102,
|
|
|
|
|
|
|
|
|
|
|
|
/// 令牌无效:TOKEN_INVALID(40103)
|
|
|
|
|
|
KBBizCodeTokenInvalid = 40103,
|
|
|
|
|
|
|
|
|
|
|
|
/// 令牌已过期:TOKEN_TIMEOUT(40104)
|
|
|
|
|
|
KBBizCodeTokenTimeout = 40104,
|
|
|
|
|
|
|
|
|
|
|
|
/// 令牌已被顶下线:TOKEN_BE_REPLACED(40105)
|
|
|
|
|
|
KBBizCodeTokenBeReplaced = 40105,
|
|
|
|
|
|
|
|
|
|
|
|
/// 令牌已被踢下线:TOKEN_KICK_OUT(40107)
|
|
|
|
|
|
KBBizCodeTokenKickOut = 40107,
|
|
|
|
|
|
|
|
|
|
|
|
/// 令牌已被冻结:TOKEN_FREEZE(40108)
|
|
|
|
|
|
KBBizCodeTokenFreeze = 40108,
|
|
|
|
|
|
|
|
|
|
|
|
/// 未按照指定前缀提交令牌:TOKEN_NO_PREFIX(40109)
|
|
|
|
|
|
KBBizCodeTokenNoPrefix = 40109,
|
|
|
|
|
|
|
|
|
|
|
|
/// 禁止访问:FORBIDDEN_ERROR(40300, "禁止访问")
|
|
|
|
|
|
KBBizCodeForbidden = 40300,
|
|
|
|
|
|
|
|
|
|
|
|
/// 请求数据不存在:NOT_FOUND_ERROR(40400, "请求数据不存在")
|
|
|
|
|
|
KBBizCodeNotFound = 40400,
|
|
|
|
|
|
|
|
|
|
|
|
/// 系统内部异常:SYSTEM_ERROR(50000, "系统内部异常")
|
|
|
|
|
|
KBBizCodeSystemError = 50000,
|
|
|
|
|
|
|
|
|
|
|
|
/// 操作失败:OPERATION_ERROR(50001, "操作失败")
|
|
|
|
|
|
KBBizCodeOperationError = 50001,
|
2025-12-02 21:32:49 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
/// 从 JSON 响应中提取业务 code。
|
|
|
|
|
|
/// 支持常见字段:code / status / retcode(数字或字符串)。
|
|
|
|
|
|
/// 若未找到返回 NSNotFound。
|
|
|
|
|
|
static inline NSInteger KBBizCodeFromJSONObject(id obj) {
|
|
|
|
|
|
if (![obj isKindOfClass:NSDictionary.class]) return NSNotFound;
|
|
|
|
|
|
NSDictionary *d = (NSDictionary *)obj;
|
|
|
|
|
|
id code = d[@"code"] ?: d[@"status"] ?: d[@"retcode"];
|
|
|
|
|
|
if ([code isKindOfClass:NSNumber.class]) {
|
|
|
|
|
|
return ((NSNumber *)code).integerValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ([code isKindOfClass:NSString.class]) {
|
|
|
|
|
|
return ((NSString *)code).integerValue;
|
|
|
|
|
|
}
|
|
|
|
|
|
return NSNotFound;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// 从 JSON 响应中提取错误文案,支持 message/msg/error。
|
|
|
|
|
|
static inline NSString *KBBizMessageFromJSONObject(id obj) {
|
|
|
|
|
|
if (![obj isKindOfClass:NSDictionary.class]) return nil;
|
|
|
|
|
|
NSDictionary *d = (NSDictionary *)obj;
|
2025-12-03 13:31:02 +08:00
|
|
|
|
NSString *msg = d[KBMessage] ?: d[@"msg"] ?: d[@"error"];
|
2025-12-02 21:32:49 +08:00
|
|
|
|
if (![msg isKindOfClass:NSString.class]) return nil;
|
|
|
|
|
|
return msg;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* KBBizCode_h */
|