using System; using System.Collections.Generic; using UnityEngine; namespace Zion.ERP.Inventory { /// /// API环境枚举 /// public enum ApiEnvironment { 开发环境, 测试环境, 生产环境 } /// /// 单个API接口配置 /// [Serializable] public class ApiEndpoint { /// /// 接口名称 /// public string Name; /// /// 接口描述 /// public string Description; /// /// 接口URL模板 /// public string UrlTemplate; /// /// HTTP方法 /// public string Method = ""; /// /// 是否需要认证 /// public bool RequireAuth = false; /// /// 超时时间(秒) /// public int Timeout = 0; /// /// 重试次数 /// public int RetryCount = 0; /// /// 是否启用 /// public bool IsEnabled = false; /// /// 自定义参数(键值对) /// public List Parameters = new List(); /// /// 获取完整的URL(替换模板变量) /// /// 基础URL /// 动态参数 /// 完整的URL public string GetFullUrl(string baseUrl, Dictionary dynamicParams = null) { string fullUrl = UrlTemplate; Debug.Log($"开始构建API URL,原始模板:{fullUrl}"); // 替换基础URL fullUrl = fullUrl.Replace("{BaseUrl}", baseUrl); Debug.Log($"替换基础URL后:{fullUrl}"); // 替换动态参数 if (dynamicParams != null) { foreach (var param in dynamicParams) { fullUrl = fullUrl.Replace($"{{{param.Key}}}", param.Value); Debug.Log($"替换动态参数 {{{param.Key}}} = {param.Value}"); } } // 替换内置参数 string paperId = GetPaperId(); string examRoomId = GetExamRoomId(); string ipAddress = GetIpAddress(); fullUrl = fullUrl.Replace("{PaperId}", paperId); fullUrl = fullUrl.Replace("{ExamRoomId}", examRoomId); fullUrl = fullUrl.Replace("{IpAddress}", ipAddress); Debug.Log($"替换内置参数完成 - PaperId: {paperId}, ExamRoomId: {examRoomId}, IpAddress: {ipAddress}"); Debug.Log($"最终生成的URL:{fullUrl}"); return fullUrl; } /// /// 获取试卷ID /// private string GetPaperId() { try { return MotionFramework.MotionEngine.GetModule()?.ExamInfo?.PaperId ?? ""; } catch { return ""; } } /// /// 获取考场ID /// private string GetExamRoomId() { try { return MotionFramework.MotionEngine.GetModule()?.ExamInfo?.ExamRoomId ?? ""; } catch { return ""; } } /// /// 获取IP地址 /// private string GetIpAddress() { try { string ipAddress = MotionFramework.MotionEngine.GetModule()?.ExamInfo?.IpAddress ?? ""; Debug.Log($"从GlobalDataStorage获取IP地址:{ipAddress}"); return ipAddress; } catch (System.Exception ex) { Debug.LogError($"获取IP地址失败:{ex.Message}"); return ""; } } } /// /// API参数配置 /// [Serializable] public class ApiParameter { /// /// 参数名称 /// public string Name; /// /// 参数值 /// public string Value; /// /// 参数描述 /// public string Description; /// /// 是否必需 /// public bool IsRequired = false; } /// /// API环境配置 /// [Serializable] public class ApiEnvironmentConfig { /// /// 环境名称 /// public ApiEnvironment Environment; /// /// 基础URL /// public string BaseUrl; /// /// 超时时间(秒) /// public int DefaultTimeout = 0; /// /// 默认重试次数 /// public int DefaultRetryCount = 0; /// /// 是否启用 /// public bool IsEnabled = false; /// /// 环境描述 /// public string Description; } /// /// API配置数据根类 /// [Serializable] public class ApiConfigData { /// /// 配置版本 /// public string Version = ""; /// /// 配置描述 /// public string Description = ""; /// /// 当前环境 /// public ApiEnvironment CurrentEnvironment = ApiEnvironment.开发环境; /// /// 环境配置列表 /// public List Environments = new List(); /// /// API接口配置列表 /// public List Endpoints = new List(); /// /// 全局参数 /// public List GlobalParameters = new List(); /// /// 获取当前环境配置 /// /// 当前环境配置 public ApiEnvironmentConfig GetCurrentEnvironmentConfig() { return Environments.Find(e => e.Environment == CurrentEnvironment); } /// /// 根据名称获取API接口配置 /// /// 接口名称 /// API接口配置 public ApiEndpoint GetEndpoint(string name) { return Endpoints.Find(e => e.Name == name && e.IsEnabled); } /// /// 获取所有启用的API接口 /// /// 启用的API接口列表 public List GetEnabledEndpoints() { return Endpoints.FindAll(e => e.IsEnabled); } /// /// 验证配置完整性 /// /// 验证结果 public bool Validate() { if (Environments == null || Environments.Count == 0) { Debug.LogError("API配置错误:未配置任何环境"); return false; } if (GetCurrentEnvironmentConfig() == null) { Debug.LogError($"API配置错误:未找到当前环境配置 {CurrentEnvironment}"); return false; } if (Endpoints == null || Endpoints.Count == 0) { Debug.LogWarning("API配置警告:未配置任何API接口"); } return true; } } }