Files
keyboard/Shared/KBBizCode.h
2025-12-02 21:32:49 +08:00

63 lines
2.0 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// KBBizCode.h
// 服务端业务状态码与通用解析工具
//
// 说明:
// - 将所有常用的业务 code 集中到这里,避免在项目里到处写裸数字;
// - 实际数值请按照你们后端的约定修改;
// - 若后端新增/调整 code只需要维护这一处即可。
//
#ifndef KBBizCode_h
#define KBBizCode_h
#import <Foundation/Foundation.h>
#import "KBAPI.h" // 引入 SUCCESS_CODE/ERROR_CODE 等基础定义
/// 服务端业务状态码(按项目实际调整)
typedef NS_ENUM(NSInteger, KBBizCode) {
/// 通用成功(通常为 200对应 SUCCESS_CODE
KBBizCodeSuccess = SUCCESS_CODE,
/// token 失效/未登录(示例值;请按后端实际 code 修改)
KBBizCodeTokenInvalid = 40101,
/// token 过期(可选;如无区分可与 KBBizCodeTokenInvalid 复用)
KBBizCodeTokenExpired = 40102,
/// 账号在其他设备登录,被服务端强制下线
KBBizCodeAccountKicked = 40103,
};
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;
NSString *msg = d[@"message"] ?: d[@"msg"] ?: d[@"error"];
if (![msg isKindOfClass:NSString.class]) return nil;
return msg;
}
NS_ASSUME_NONNULL_END
#endif /* KBBizCode_h */