Tz2/Assets/Zion/Scripts/ERP/Editor/ApiConfigEditorWindow.cs

282 lines
12 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 UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
namespace Zion.ERP.Inventory
{
/// <summary>
/// API配置编辑器窗口
/// </summary>
public class ApiConfigEditorWindow : EditorWindow
{
private ApiConfigData configData;
private Vector2 scrollPosition;
private const string CONFIG_PATH = "Assets/StreamingAssets/DataConfig/api_config.json";
private List<bool> environmentFoldouts = new List<bool>();
private List<bool> endpointFoldouts = new List<bool>();
private List<bool> parameterFoldouts = new List<bool>();
private bool showEnvironments = true;
private bool showEndpoints = true;
private bool showGlobalParameters = true;
[MenuItem("工具/API配置编辑器")]
public static void ShowWindow()
{
GetWindow<ApiConfigEditorWindow>("API配置编辑器");
}
private void OnEnable()
{
LoadConfigData();
SyncFoldouts();
}
private void LoadConfigData()
{
if (!Directory.Exists("Assets/StreamingAssets/DataConfig"))
{
Directory.CreateDirectory("Assets/StreamingAssets/DataConfig");
}
if (File.Exists(CONFIG_PATH))
{
string jsonContent = File.ReadAllText(CONFIG_PATH);
configData = JsonConvert.DeserializeObject<ApiConfigData>(jsonContent);
}
if (configData == null)
{
Debug.LogError("未找到API配置文件请确保配置文件存在");
configData = new ApiConfigData();
}
}
private void CreateDefaultConfig()
{
Debug.LogWarning("CreateDefaultConfig方法已废弃请使用JSON配置文件进行配置");
configData = new ApiConfigData();
}
private void SaveConfigData()
{
string jsonContent = JsonConvert.SerializeObject(configData, Formatting.Indented);
File.WriteAllText(CONFIG_PATH, jsonContent);
AssetDatabase.Refresh();
}
private void SyncFoldouts()
{
while (environmentFoldouts.Count < configData.Environments.Count)
environmentFoldouts.Add(false);
while (environmentFoldouts.Count > configData.Environments.Count)
environmentFoldouts.RemoveAt(environmentFoldouts.Count - 1);
while (endpointFoldouts.Count < configData.Endpoints.Count)
endpointFoldouts.Add(false);
while (endpointFoldouts.Count > configData.Endpoints.Count)
endpointFoldouts.RemoveAt(endpointFoldouts.Count - 1);
while (parameterFoldouts.Count < configData.GlobalParameters.Count)
parameterFoldouts.Add(false);
while (parameterFoldouts.Count > configData.GlobalParameters.Count)
parameterFoldouts.RemoveAt(parameterFoldouts.Count - 1);
}
private void OnGUI()
{
if (configData == null)
{
LoadConfigData();
return;
}
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
// 标题
EditorGUILayout.LabelField("API接口配置编辑器", EditorStyles.boldLabel);
EditorGUILayout.Space();
// 基本信息
EditorGUILayout.LabelField("基本信息", EditorStyles.boldLabel);
configData.Version = EditorGUILayout.TextField("版本", configData.Version);
configData.Description = EditorGUILayout.TextField("描述", configData.Description);
configData.CurrentEnvironment = (ApiEnvironment)EditorGUILayout.EnumPopup("当前环境", configData.CurrentEnvironment);
EditorGUILayout.Space();
// 环境配置
showEnvironments = EditorGUILayout.Foldout(showEnvironments, "环境配置", true);
if (showEnvironments)
{
EditorGUI.indentLevel++;
for (int i = 0; i < configData.Environments.Count; i++)
{
var env = configData.Environments[i];
environmentFoldouts[i] = EditorGUILayout.Foldout(environmentFoldouts[i], $"{env.Environment} - {env.BaseUrl}", true);
if (environmentFoldouts[i])
{
EditorGUI.indentLevel++;
env.Environment = (ApiEnvironment)EditorGUILayout.EnumPopup("环境", env.Environment);
env.BaseUrl = EditorGUILayout.TextField("基础URL", env.BaseUrl);
env.DefaultTimeout = EditorGUILayout.IntField("默认超时时间", env.DefaultTimeout);
env.DefaultRetryCount = EditorGUILayout.IntField("默认重试次数", env.DefaultRetryCount);
env.IsEnabled = EditorGUILayout.Toggle("启用", env.IsEnabled);
env.Description = EditorGUILayout.TextField("描述", env.Description);
if (GUILayout.Button("删除环境"))
{
configData.Environments.RemoveAt(i);
SyncFoldouts();
break;
}
EditorGUI.indentLevel--;
}
}
if (GUILayout.Button("添加环境"))
{
configData.Environments.Add(new ApiEnvironmentConfig());
SyncFoldouts();
}
EditorGUI.indentLevel--;
}
EditorGUILayout.Space();
// API接口配置
showEndpoints = EditorGUILayout.Foldout(showEndpoints, "API接口配置", true);
if (showEndpoints)
{
EditorGUI.indentLevel++;
for (int i = 0; i < configData.Endpoints.Count; i++)
{
var endpoint = configData.Endpoints[i];
endpointFoldouts[i] = EditorGUILayout.Foldout(endpointFoldouts[i], $"{endpoint.Name} - {endpoint.Description}", true);
if (endpointFoldouts[i])
{
EditorGUI.indentLevel++;
endpoint.Name = EditorGUILayout.TextField("接口名称", endpoint.Name);
endpoint.Description = EditorGUILayout.TextField("描述", endpoint.Description);
endpoint.UrlTemplate = EditorGUILayout.TextField("URL模板", endpoint.UrlTemplate);
endpoint.Method = EditorGUILayout.TextField("HTTP方法", endpoint.Method);
endpoint.RequireAuth = EditorGUILayout.Toggle("需要认证", endpoint.RequireAuth);
endpoint.Timeout = EditorGUILayout.IntField("超时时间", endpoint.Timeout);
endpoint.RetryCount = EditorGUILayout.IntField("重试次数", endpoint.RetryCount);
endpoint.IsEnabled = EditorGUILayout.Toggle("启用", endpoint.IsEnabled);
// 参数配置
EditorGUILayout.LabelField("参数配置", EditorStyles.boldLabel);
for (int j = 0; j < endpoint.Parameters.Count; j++)
{
var param = endpoint.Parameters[j];
parameterFoldouts[j] = EditorGUILayout.Foldout(parameterFoldouts[j], $"参数 {j + 1}", true);
if (parameterFoldouts[j])
{
param.Name = EditorGUILayout.TextField("参数名", param.Name);
param.Value = EditorGUILayout.TextField("参数值", param.Value);
param.Description = EditorGUILayout.TextField("描述", param.Description);
param.IsRequired = EditorGUILayout.Toggle("必需", param.IsRequired);
if (GUILayout.Button("删除参数"))
{
endpoint.Parameters.RemoveAt(j);
break;
}
}
}
if (GUILayout.Button("添加参数"))
{
endpoint.Parameters.Add(new ApiParameter());
}
if (GUILayout.Button("删除接口"))
{
configData.Endpoints.RemoveAt(i);
SyncFoldouts();
break;
}
EditorGUI.indentLevel--;
}
}
if (GUILayout.Button("添加接口"))
{
configData.Endpoints.Add(new ApiEndpoint());
SyncFoldouts();
}
EditorGUI.indentLevel--;
}
EditorGUILayout.Space();
// 全局参数
showGlobalParameters = EditorGUILayout.Foldout(showGlobalParameters, "全局参数", true);
if (showGlobalParameters)
{
EditorGUI.indentLevel++;
for (int i = 0; i < configData.GlobalParameters.Count; i++)
{
var param = configData.GlobalParameters[i];
parameterFoldouts[i] = EditorGUILayout.Foldout(parameterFoldouts[i], $"全局参数 {i + 1}", true);
if (parameterFoldouts[i])
{
param.Name = EditorGUILayout.TextField("参数名", param.Name);
param.Value = EditorGUILayout.TextField("参数值", param.Value);
param.Description = EditorGUILayout.TextField("描述", param.Description);
param.IsRequired = EditorGUILayout.Toggle("必需", param.IsRequired);
if (GUILayout.Button("删除参数"))
{
configData.GlobalParameters.RemoveAt(i);
SyncFoldouts();
break;
}
}
}
if (GUILayout.Button("添加全局参数"))
{
configData.GlobalParameters.Add(new ApiParameter());
SyncFoldouts();
}
EditorGUI.indentLevel--;
}
EditorGUILayout.Space();
// 操作按钮
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("保存配置"))
{
SaveConfigData();
EditorUtility.DisplayDialog("保存成功", "API配置已保存到JSON文件", "确定");
}
if (GUILayout.Button("重新加载"))
{
LoadConfigData();
SyncFoldouts();
}
if (GUILayout.Button("重置为默认"))
{
EditorUtility.DisplayDialog("提示", "重置功能已废弃请直接编辑JSON配置文件", "确定");
}
EditorGUILayout.EndHorizontal();
// 验证配置
if (GUILayout.Button("验证配置"))
{
if (configData.Validate())
{
EditorUtility.DisplayDialog("验证成功", "API配置验证通过", "确定");
}
else
{
EditorUtility.DisplayDialog("验证失败", "API配置验证失败请检查配置", "确定");
}
}
EditorGUILayout.EndScrollView();
}
}
}