using System; using System.Collections.Generic; using UnityEngine; using Kill.Managers; using Kill.UI.Components; using Kill.UI; namespace Kill.Network { /// /// 服务器响应状态码 /// public enum ResponseCode { SUCCESS = 200, // 正常 NOT_FOUND = 404, // 访问对象不存在 FORBIDDEN = 403, // 禁止访问 INTERNAL_ERROR = 502, // 内部处理错误 RECORD_EXISTS = 610, // 访问对象已存在 UNAUTHORIZED = 601, // 访问未授权(Token失效) ERROR_PASSWORD = 602, // 密码错误 INVALID_PARAM = 603, // 参数错误 INVALID_STATUS = 608, // 数据状态致操作无效 INVALID_FORMAT = 607, // 格式错误 } /// /// 响应码处理工具 /// public static class ResponseCodeHandler { /// /// 状态码与提示信息的映射 /// private static readonly Dictionary CodeMessages = new Dictionary { { 200, "操作成功" }, { 404, "访问对象不存在" }, { 403, "禁止访问" }, { 502, "服务器内部错误" }, { 610, "访问对象已存在" }, { 601, "登录已过期,请重新登录" }, { 602, "密码错误" }, { 603, "参数错误" }, { 608, "数据状态导致操作无效" }, { 607, "格式错误" }, }; /// /// 检查是否需要全局处理(只处理601 Token失效) /// /// 状态码 /// 是否已全局处理 public static bool CheckGlobalHandle(int code) { // 只处理 601 Token失效 if (code == 601) { HandleUnauthorized(); return true; } return false; } /// /// 处理响应码(只全局处理601,其他返回给调用方) /// /// 状态码 /// 服务器返回的消息(可选) /// 是否成功(code == 200) public static bool Handle(int code, string message = null) { // 成功 if (code == 200) { return true; } // 全局处理 601 if (CheckGlobalHandle(code)) { return false; } // 其他错误码由调用方处理,这里只返回失败状态 return false; } /// /// 检查是否需要全局处理(601 Token失效、网络错误等) /// /// HTTP响应 /// 输出状态码 /// 输出消息 /// 是否已全局处理 public static bool CheckGlobalHandle(HttpResponse response, out int code, out string message) { code = -1; message = null; // 空响应(网络异常) if (response == null) { ShowErrorTip("100036"); return true; } // HTTP错误(网络层错误) if (!response.IsSuccess) { ShowErrorTip("100036"); return true; } // 获取业务状态码 code = GetCodeFromResponse(response.Data); message = GetMessageFromResponse(response.Data); // 处理 601 Token失效 if (code == 601) { HandleUnauthorized(); return true; } return false; } /// /// 处理响应结果(全局处理网络错误和601,其他返回给调用方) /// /// 响应数据类型 /// HTTP响应 /// 成功回调(code=200) /// 其他错误回调(可选,不传则自动提示) public static void HandleResponse(HttpResponse response, Action onSuccess, Action onError = null) { // 全局处理(网络错误、601等) int code; string message; if (CheckGlobalHandle(response, out code, out message)) { return; // 已被全局处理 } // 成功 if (code == 200) { onSuccess?.Invoke(response.Data); return; } // 有自定义错误处理 if (onError != null) { onError.Invoke(code, message); } else { // 自动显示错误提示 ShowErrorTip(code.ToString()); } } /// /// 处理响应结果(简化版,只返回是否成功,错误自动提示) /// public static bool HandleSimple(HttpResponse response, Action onSuccess) where T : class { HandleResponse(response, onSuccess, null); // 返回是否成功(用于链式判断) if (response?.Data == null) return false; var code = GetCodeFromResponse(response.Data); return code == 200; } /// /// 获取状态码对应的消息 /// public static string GetMessage(int code, string serverMessage = null) { // 优先使用服务器返回的消息 if (!string.IsNullOrEmpty(serverMessage)) { return serverMessage; } // 使用本地映射 if (CodeMessages.TryGetValue(code, out string message)) { return message; } // 未知错误码 return $"未知错误({code})"; } /// /// 处理未授权(Token失效) /// private static void HandleUnauthorized() { Debug.LogWarning("Token失效,需要重新登录"); // 清除本地登录状态 ClearAuthData(); // 显示提示 ShowErrorTip("100038"); JumpToLoginPage(); } /// /// 清除登录数据 /// private static void ClearAuthData() { // 使用 LocalDataManager 清除数据 // LocalDataManager.ClearLoginData(); // 清除全局请求头 if (NetworkCtrl.Instance != null) { NetworkCtrl.Instance.RemoveGlobalHeader("token"); } } /// /// 跳转到登录页 /// private static void JumpToLoginPage() { UIManager.Instance.OpenPage(UIManager.PageName.loginPage,null,false); Debug.Log("跳转到登录页"); } /// /// 显示错误提示(使用ToastUI) /// private static void ShowErrorTip(string code) { // 使用 ToastUI 显示横幅提示 ToastUI.Show(code); } /// /// 从响应数据中获取code字段 /// private static int GetCodeFromResponse(T data) { if (data == null) return -1; // 使用反射获取code字段 var type = data.GetType(); var codeField = type.GetField("code"); if (codeField != null) { var value = codeField.GetValue(data); if (value is int code) { return code; } } // 尝试获取属性 var codeProperty = type.GetProperty("code"); if (codeProperty != null) { var value = codeProperty.GetValue(data); if (value is int code) { return code; } } return 200; // 默认成功 } /// /// 从响应数据中获取message字段 /// private static string GetMessageFromResponse(T data) { if (data == null) return null; var type = data.GetType(); // 尝试获取message字段 var messageField = type.GetField("message"); if (messageField != null) { return messageField.GetValue(data)?.ToString(); } // 尝试获取属性 var messageProperty = type.GetProperty("message"); if (messageProperty != null) { return messageProperty.GetValue(data)?.ToString(); } return null; } } }