Tz2/Assets/Zion/Scripts/ERP/Interfaces/ApiConfigData.cs

311 lines
8.2 KiB
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.

using System;
using System.Collections.Generic;
using UnityEngine;
namespace Zion.ERP.Inventory
{
/// <summary>
/// API环境枚举
/// </summary>
public enum ApiEnvironment
{
,
,
}
/// <summary>
/// 单个API接口配置
/// </summary>
[Serializable]
public class ApiEndpoint
{
/// <summary>
/// 接口名称
/// </summary>
public string Name;
/// <summary>
/// 接口描述
/// </summary>
public string Description;
/// <summary>
/// 接口URL模板
/// </summary>
public string UrlTemplate;
/// <summary>
/// HTTP方法
/// </summary>
public string Method = "";
/// <summary>
/// 是否需要认证
/// </summary>
public bool RequireAuth = false;
/// <summary>
/// 超时时间(秒)
/// </summary>
public int Timeout = 0;
/// <summary>
/// 重试次数
/// </summary>
public int RetryCount = 0;
/// <summary>
/// 是否启用
/// </summary>
public bool IsEnabled = false;
/// <summary>
/// 自定义参数(键值对)
/// </summary>
public List<ApiParameter> Parameters = new List<ApiParameter>();
/// <summary>
/// 获取完整的URL替换模板变量
/// </summary>
/// <param name="baseUrl">基础URL</param>
/// <param name="dynamicParams">动态参数</param>
/// <returns>完整的URL</returns>
public string GetFullUrl(string baseUrl, Dictionary<string, string> 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;
}
/// <summary>
/// 获取试卷ID
/// </summary>
private string GetPaperId()
{
try
{
return MotionFramework.MotionEngine.GetModule<DefaultNamespace.GlobalDataStorage>()?.ExamInfo?.PaperId ?? "";
}
catch
{
return "";
}
}
/// <summary>
/// 获取考场ID
/// </summary>
private string GetExamRoomId()
{
try
{
return MotionFramework.MotionEngine.GetModule<DefaultNamespace.GlobalDataStorage>()?.ExamInfo?.ExamRoomId ?? "";
}
catch
{
return "";
}
}
/// <summary>
/// 获取IP地址
/// </summary>
private string GetIpAddress()
{
try
{
string ipAddress = MotionFramework.MotionEngine.GetModule<DefaultNamespace.GlobalDataStorage>()?.ExamInfo?.IpAddress ?? "";
Debug.Log($"从GlobalDataStorage获取IP地址{ipAddress}");
return ipAddress;
}
catch (System.Exception ex)
{
Debug.LogError($"获取IP地址失败{ex.Message}");
return "";
}
}
}
/// <summary>
/// API参数配置
/// </summary>
[Serializable]
public class ApiParameter
{
/// <summary>
/// 参数名称
/// </summary>
public string Name;
/// <summary>
/// 参数值
/// </summary>
public string Value;
/// <summary>
/// 参数描述
/// </summary>
public string Description;
/// <summary>
/// 是否必需
/// </summary>
public bool IsRequired = false;
}
/// <summary>
/// API环境配置
/// </summary>
[Serializable]
public class ApiEnvironmentConfig
{
/// <summary>
/// 环境名称
/// </summary>
public ApiEnvironment Environment;
/// <summary>
/// 基础URL
/// </summary>
public string BaseUrl;
/// <summary>
/// 超时时间(秒)
/// </summary>
public int DefaultTimeout = 0;
/// <summary>
/// 默认重试次数
/// </summary>
public int DefaultRetryCount = 0;
/// <summary>
/// 是否启用
/// </summary>
public bool IsEnabled = false;
/// <summary>
/// 环境描述
/// </summary>
public string Description;
}
/// <summary>
/// API配置数据根类
/// </summary>
[Serializable]
public class ApiConfigData
{
/// <summary>
/// 配置版本
/// </summary>
public string Version = "";
/// <summary>
/// 配置描述
/// </summary>
public string Description = "";
/// <summary>
/// 当前环境
/// </summary>
public ApiEnvironment CurrentEnvironment = ApiEnvironment.;
/// <summary>
/// 环境配置列表
/// </summary>
public List<ApiEnvironmentConfig> Environments = new List<ApiEnvironmentConfig>();
/// <summary>
/// API接口配置列表
/// </summary>
public List<ApiEndpoint> Endpoints = new List<ApiEndpoint>();
/// <summary>
/// 全局参数
/// </summary>
public List<ApiParameter> GlobalParameters = new List<ApiParameter>();
/// <summary>
/// 获取当前环境配置
/// </summary>
/// <returns>当前环境配置</returns>
public ApiEnvironmentConfig GetCurrentEnvironmentConfig()
{
return Environments.Find(e => e.Environment == CurrentEnvironment);
}
/// <summary>
/// 根据名称获取API接口配置
/// </summary>
/// <param name="name">接口名称</param>
/// <returns>API接口配置</returns>
public ApiEndpoint GetEndpoint(string name)
{
return Endpoints.Find(e => e.Name == name && e.IsEnabled);
}
/// <summary>
/// 获取所有启用的API接口
/// </summary>
/// <returns>启用的API接口列表</returns>
public List<ApiEndpoint> GetEnabledEndpoints()
{
return Endpoints.FindAll(e => e.IsEnabled);
}
/// <summary>
/// 验证配置完整性
/// </summary>
/// <returns>验证结果</returns>
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;
}
}
}