更新项目配置,新增设备同步模块,优化WebSocket和Swagger配置,增强SCADA系统的免登录接口,支持数据字典项和登录日志的免登录查询与记录。调整Java编译设置,确保更好的开发体验。
This commit is contained in:
117
yy-admin-master/YY.Admin/Helper/ServerSettingsStore.cs
Normal file
117
yy-admin-master/YY.Admin/Helper/ServerSettingsStore.cs
Normal file
@@ -0,0 +1,117 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user