Files
qhmes/yy-admin-master/YY.Admin/Helper/ServerSettingsStore.cs

118 lines
4.5 KiB
C#

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
namespace YY.Admin.Helper
{
/// <summary>
/// 服务器连接配置读写工具。
/// </summary>
public static class ServerSettingsStore
{
private const string DefaultWebSocketPath = "/websocket/scada-sync";
public class ServerSettingsModel
{
public string Ip { get; set; } = "127.0.0.1";
public int Port { get; set; } = 8080;
public string BaseScheme { get; set; } = "http";
public string BasePath { get; set; } = "/jeecg-boot";
public string WebSocketUrl { get; set; } = string.Empty;
public string WebSocketPath { get; set; } = DefaultWebSocketPath;
}
public static string GetConfigPath()
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configuration", "appsettings.json");
}
public static ServerSettingsModel Load()
{
var model = new ServerSettingsModel();
var path = GetConfigPath();
if (!File.Exists(path))
{
return model;
}
var content = File.ReadAllText(path);
var root = JObject.Parse(content);
var jeecg = root["JeecgIntegration"] as JObject;
if (jeecg == null)
{
return model;
}
var baseUrl = jeecg.Value<string>("BaseUrl") ?? string.Empty;
if (Uri.TryCreate(baseUrl, UriKind.Absolute, out var uri))
{
model.BaseScheme = uri.Scheme;
model.Ip = uri.Host;
model.Port = uri.Port;
model.BasePath = string.IsNullOrWhiteSpace(uri.AbsolutePath) ? string.Empty : uri.AbsolutePath.TrimEnd('/');
}
model.WebSocketUrl = jeecg.Value<string>("WebSocketUrl") ?? string.Empty;
model.WebSocketPath = NormalizeWebSocketPath(jeecg.Value<string>("WebSocketPath"));
return model;
}
public static void Save(ServerSettingsModel model)
{
var path = GetConfigPath();
if (!File.Exists(path))
{
throw new FileNotFoundException("未找到配置文件 appsettings.json", path);
}
var content = File.ReadAllText(path);
var root = JObject.Parse(content);
var jeecg = root["JeecgIntegration"] as JObject;
if (jeecg == null)
{
jeecg = new JObject();
root["JeecgIntegration"] = jeecg;
}
var basePath = string.IsNullOrWhiteSpace(model.BasePath) ? string.Empty : model.BasePath.TrimEnd('/');
if (!string.IsNullOrWhiteSpace(basePath) && !basePath.StartsWith('/'))
{
basePath = "/" + basePath;
}
var baseUrl = $"{model.BaseScheme}://{model.Ip}:{model.Port}{basePath}";
var webSocketPath = NormalizeWebSocketPath(model.WebSocketPath);
var webSocketUrl = string.IsNullOrWhiteSpace(model.WebSocketUrl)
? BuildDefaultWebSocketUrl(model.BaseScheme, model.Ip, model.Port, basePath, webSocketPath)
: model.WebSocketUrl.Trim();
jeecg["BaseUrl"] = baseUrl;
jeecg["WebSocketUrl"] = webSocketUrl;
jeecg["WebSocketPath"] = webSocketPath;
File.WriteAllText(path, root.ToString(Formatting.Indented));
}
public static string BuildDefaultWebSocketUrl(string baseScheme, string ip, int port, string basePath, string webSocketPath = DefaultWebSocketPath)
{
var safeScheme = string.Equals(baseScheme, "https", StringComparison.OrdinalIgnoreCase) ? "wss" : "ws";
var safeBasePath = string.IsNullOrWhiteSpace(basePath) ? string.Empty : basePath.TrimEnd('/');
if (!string.IsNullOrWhiteSpace(safeBasePath) && !safeBasePath.StartsWith('/'))
{
safeBasePath = "/" + safeBasePath;
}
var safeWsPath = NormalizeWebSocketPath(webSocketPath);
return $"{safeScheme}://{ip}:{port}{safeBasePath}{safeWsPath}";
}
private static string NormalizeWebSocketPath(string? webSocketPath)
{
var value = string.IsNullOrWhiteSpace(webSocketPath) ? DefaultWebSocketPath : webSocketPath.Trim();
if (!value.StartsWith('/'))
{
value = "/" + value;
}
return value;
}
}
}