340 lines
10 KiB
C#
340 lines
10 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using UnityEngine;
|
||
using Newtonsoft.Json;
|
||
using MotionFramework;
|
||
|
||
namespace Zion.ERP.Inventory
|
||
{
|
||
/// <summary>
|
||
/// API配置管理器,负责统一管理API接口配置
|
||
/// </summary>
|
||
public class ApiConfigManager : ModuleSingleton<ApiConfigManager>, IModule
|
||
{
|
||
/// <summary>
|
||
/// 当前配置数据
|
||
/// </summary>
|
||
private ApiConfigData _currentConfig;
|
||
|
||
/// <summary>
|
||
/// ScriptableObject配置(已废弃)
|
||
/// </summary>
|
||
private ApiConfig _scriptableConfig = null;
|
||
|
||
/// <summary>
|
||
/// JSON配置文件路径
|
||
/// </summary>
|
||
private string _jsonConfigPath;
|
||
|
||
/// <summary>
|
||
/// 配置文件最后修改时间
|
||
/// </summary>
|
||
private DateTime _lastConfigModifyTime;
|
||
|
||
/// <summary>
|
||
/// 是否启用热重载
|
||
/// </summary>
|
||
private bool _enableHotReload = false;
|
||
|
||
/// <summary>
|
||
/// 热重载检查间隔
|
||
/// </summary>
|
||
private float _hotReloadInterval = 30f;
|
||
|
||
/// <summary>
|
||
/// 上次热重载检查时间
|
||
/// </summary>
|
||
private float _lastHotReloadCheck = 0f;
|
||
|
||
/// <summary>
|
||
/// 配置加载完成事件
|
||
/// </summary>
|
||
public event Action<ApiConfigData> OnConfigLoaded;
|
||
|
||
/// <summary>
|
||
/// 配置重载事件
|
||
/// </summary>
|
||
public event Action<ApiConfigData> OnConfigReloaded;
|
||
|
||
/// <summary>
|
||
/// 配置错误事件
|
||
/// </summary>
|
||
public event Action<string> OnConfigError;
|
||
|
||
/// <summary>
|
||
/// 获取当前配置
|
||
/// </summary>
|
||
public ApiConfigData CurrentConfig => _currentConfig;
|
||
|
||
/// <summary>
|
||
/// 获取ScriptableObject配置(已废弃)
|
||
/// </summary>
|
||
public ApiConfig ScriptableConfig => null;
|
||
|
||
/// <summary>
|
||
/// 是否已初始化
|
||
/// </summary>
|
||
public bool IsInitialized { get; private set; } = false;
|
||
|
||
/// <summary>
|
||
/// 模块创建时调用
|
||
/// </summary>
|
||
public void OnCreate(object createParam)
|
||
{
|
||
Debug.Log("API配置管理器创建成功");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 初始化配置管理器
|
||
/// </summary>
|
||
/// <param name="enableHotReload">是否启用热重载</param>
|
||
/// <param name="hotReloadInterval">热重载检查间隔</param>
|
||
public void Initialize(bool enableHotReload = false, float hotReloadInterval = 30f)
|
||
{
|
||
Debug.Log("开始初始化API配置管理器");
|
||
|
||
_enableHotReload = enableHotReload;
|
||
_hotReloadInterval = hotReloadInterval;
|
||
|
||
// 设置JSON配置文件路径
|
||
_jsonConfigPath = Path.Combine(Application.streamingAssetsPath, "DataConfig", "api_config.json");
|
||
|
||
// 加载配置
|
||
LoadConfig();
|
||
|
||
IsInitialized = true;
|
||
Debug.Log("API配置管理器初始化完成");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加载配置
|
||
/// </summary>
|
||
public void LoadConfig()
|
||
{
|
||
try
|
||
{
|
||
// 只从JSON文件加载配置
|
||
if (File.Exists(_jsonConfigPath))
|
||
{
|
||
LoadFromJson();
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"API配置文件不存在:{_jsonConfigPath}");
|
||
OnConfigError?.Invoke($"API配置文件不存在:{_jsonConfigPath}");
|
||
return;
|
||
}
|
||
|
||
// 验证配置
|
||
if (_currentConfig == null || !_currentConfig.Validate())
|
||
{
|
||
Debug.LogError("API配置验证失败");
|
||
OnConfigError?.Invoke("API配置验证失败");
|
||
return;
|
||
}
|
||
|
||
// 记录配置文件修改时间
|
||
_lastConfigModifyTime = File.GetLastWriteTime(_jsonConfigPath);
|
||
|
||
Debug.Log($"API配置加载成功,当前环境:{_currentConfig.CurrentEnvironment}");
|
||
OnConfigLoaded?.Invoke(_currentConfig);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"加载API配置失败:{ex.Message}");
|
||
OnConfigError?.Invoke($"加载API配置失败:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从JSON文件加载配置
|
||
/// </summary>
|
||
private void LoadFromJson()
|
||
{
|
||
Debug.Log($"从JSON文件加载配置:{_jsonConfigPath}");
|
||
string jsonContent = File.ReadAllText(_jsonConfigPath);
|
||
_currentConfig = JsonConvert.DeserializeObject<ApiConfigData>(jsonContent);
|
||
Debug.Log("JSON配置文件加载成功");
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 创建默认配置
|
||
/// </summary>
|
||
private void CreateDefaultConfig()
|
||
{
|
||
Debug.LogError("未找到API配置文件,请确保配置文件存在:api_config.json");
|
||
_currentConfig = null;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 保存配置到JSON文件
|
||
/// </summary>
|
||
public void SaveConfigToJson()
|
||
{
|
||
try
|
||
{
|
||
if (_currentConfig == null)
|
||
{
|
||
Debug.LogError("没有可保存的配置数据");
|
||
return;
|
||
}
|
||
|
||
// 确保目录存在
|
||
string directory = Path.GetDirectoryName(_jsonConfigPath);
|
||
if (!Directory.Exists(directory))
|
||
{
|
||
Directory.CreateDirectory(directory);
|
||
}
|
||
|
||
// 序列化并保存
|
||
string jsonContent = JsonConvert.SerializeObject(_currentConfig, Formatting.Indented);
|
||
File.WriteAllText(_jsonConfigPath, jsonContent);
|
||
|
||
Debug.Log($"API配置已保存到:{_jsonConfigPath}");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"保存API配置失败:{ex.Message}");
|
||
OnConfigError?.Invoke($"保存API配置失败:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取API接口URL
|
||
/// </summary>
|
||
/// <param name="endpointName">接口名称</param>
|
||
/// <param name="dynamicParams">动态参数</param>
|
||
/// <returns>完整的URL</returns>
|
||
public string GetApiUrl(string endpointName, Dictionary<string, string> dynamicParams = null)
|
||
{
|
||
if (_currentConfig == null)
|
||
{
|
||
Debug.LogError("API配置未初始化");
|
||
return "";
|
||
}
|
||
|
||
var endpoint = _currentConfig.GetEndpoint(endpointName);
|
||
if (endpoint == null)
|
||
{
|
||
Debug.LogError($"未找到API接口配置:{endpointName}");
|
||
return "";
|
||
}
|
||
|
||
var envConfig = _currentConfig.GetCurrentEnvironmentConfig();
|
||
if (envConfig == null)
|
||
{
|
||
Debug.LogError("未找到当前环境配置");
|
||
return "";
|
||
}
|
||
|
||
return endpoint.GetFullUrl(envConfig.BaseUrl, dynamicParams);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取API接口配置
|
||
/// </summary>
|
||
/// <param name="endpointName">接口名称</param>
|
||
/// <returns>API接口配置</returns>
|
||
public ApiEndpoint GetEndpoint(string endpointName)
|
||
{
|
||
return _currentConfig?.GetEndpoint(endpointName);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取当前环境配置
|
||
/// </summary>
|
||
/// <returns>当前环境配置</returns>
|
||
public ApiEnvironmentConfig GetCurrentEnvironmentConfig()
|
||
{
|
||
return _currentConfig?.GetCurrentEnvironmentConfig();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 切换环境
|
||
/// </summary>
|
||
/// <param name="environment">目标环境</param>
|
||
public void SwitchEnvironment(ApiEnvironment environment)
|
||
{
|
||
if (_currentConfig == null)
|
||
{
|
||
Debug.LogError("API配置未初始化");
|
||
return;
|
||
}
|
||
|
||
var envConfig = _currentConfig.Environments.Find(e => e.Environment == environment);
|
||
if (envConfig == null)
|
||
{
|
||
Debug.LogError($"未找到环境配置:{environment}");
|
||
return;
|
||
}
|
||
|
||
if (!envConfig.IsEnabled)
|
||
{
|
||
Debug.LogWarning($"环境 {environment} 未启用");
|
||
return;
|
||
}
|
||
|
||
_currentConfig.CurrentEnvironment = environment;
|
||
Debug.Log($"已切换到环境:{environment}");
|
||
|
||
// 保存配置
|
||
SaveConfigToJson();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 检查配置是否需要重载
|
||
/// </summary>
|
||
private void CheckConfigReload()
|
||
{
|
||
if (!_enableHotReload || !File.Exists(_jsonConfigPath))
|
||
return;
|
||
|
||
try
|
||
{
|
||
DateTime currentModifyTime = File.GetLastWriteTime(_jsonConfigPath);
|
||
if (currentModifyTime > _lastConfigModifyTime)
|
||
{
|
||
Debug.Log("检测到配置文件变更,开始重载配置");
|
||
LoadConfig();
|
||
OnConfigReloaded?.Invoke(_currentConfig);
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.LogError($"检查配置重载时发生错误:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 模块更新时调用
|
||
/// </summary>
|
||
public void OnUpdate()
|
||
{
|
||
// 检查热重载
|
||
if (_enableHotReload && Time.time - _lastHotReloadCheck > _hotReloadInterval)
|
||
{
|
||
_lastHotReloadCheck = Time.time;
|
||
CheckConfigReload();
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 模块销毁时调用
|
||
/// </summary>
|
||
public void OnDestroy()
|
||
{
|
||
Debug.Log("API配置管理器销毁");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 模块GUI绘制时调用
|
||
/// </summary>
|
||
public void OnGUI()
|
||
{
|
||
// 可以在这里添加调试信息显示
|
||
}
|
||
}
|
||
} |