新增MES模块,包含供应商、客户、车辆和地磅数据记录管理功能,支持免密接口和数据同步。更新相关控制器、实体、服务和数据库配置,优化权限管理和数据字典支持,确保系统的灵活性和可扩展性。
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using YY.Admin.Helper;
|
||||
|
||||
namespace YY.Admin.Infrastructure.Network;
|
||||
|
||||
/// <summary>
|
||||
/// 全局断开保护:用户勾选"断开连接"时,直接短路所有后端 HTTP 请求,
|
||||
/// 返回 499 而不发起真实网络调用,各服务的 catch/IsSuccessStatusCode 分支自行降级。
|
||||
/// </summary>
|
||||
internal sealed class DisconnectGuardHandler : DelegatingHandler
|
||||
{
|
||||
protected override Task<HttpResponseMessage> SendAsync(
|
||||
HttpRequestMessage request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
if (ServerSettingsStore.Load().DisconnectConnection)
|
||||
{
|
||||
return Task.FromResult(new HttpResponseMessage((HttpStatusCode)499)
|
||||
{
|
||||
ReasonPhrase = "User Disconnected"
|
||||
});
|
||||
}
|
||||
return base.SendAsync(request, cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ using Prism.Events;
|
||||
using System.Net.Http;
|
||||
using YY.Admin.Core.Events;
|
||||
using YY.Admin.Core.Services;
|
||||
using YY.Admin.Helper;
|
||||
|
||||
namespace YY.Admin.Infrastructure.Network;
|
||||
|
||||
@@ -12,8 +13,11 @@ public class NetworkMonitor : INetworkMonitor
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private readonly SemaphoreSlim _startLock = new(1, 1);
|
||||
private readonly object _aggregateLock = new();
|
||||
private Task? _loopTask;
|
||||
private CancellationTokenSource? _cts;
|
||||
private volatile bool _httpProbeOnline;
|
||||
private volatile bool _stompTransportOnline;
|
||||
private volatile bool _isOnline;
|
||||
|
||||
public NetworkMonitor(
|
||||
@@ -30,6 +34,13 @@ public class NetworkMonitor : INetworkMonitor
|
||||
|
||||
public event Action<bool>? StatusChanged;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void SetStompTransportOnline(bool online)
|
||||
{
|
||||
_stompTransportOnline = online;
|
||||
RecomputeAggregatedOnline();
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _startLock.WaitAsync(cancellationToken).ConfigureAwait(false);
|
||||
@@ -51,27 +62,63 @@ public class NetworkMonitor : INetworkMonitor
|
||||
|
||||
private async Task MonitorLoopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
// 启动后立即探活一次,避免首屏 10 秒内 IsOnline 恒为 false
|
||||
await RunHttpProbeAndRecomputeAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(10));
|
||||
while (await timer.WaitForNextTickAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
var online = await ProbeAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (online == _isOnline)
|
||||
await RunHttpProbeAndRecomputeAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task RunHttpProbeAndRecomputeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var httpOk = await ProbeAsync(cancellationToken).ConfigureAwait(false);
|
||||
_httpProbeOnline = httpOk;
|
||||
RecomputeAggregatedOnline();
|
||||
}
|
||||
|
||||
private void RecomputeAggregatedOnline()
|
||||
{
|
||||
var combined = ComputeCombinedOnline();
|
||||
bool newValue;
|
||||
lock (_aggregateLock)
|
||||
{
|
||||
if (combined == _isOnline)
|
||||
{
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
_isOnline = online;
|
||||
StatusChanged?.Invoke(online);
|
||||
_eventAggregator.GetEvent<NetworkStatusChangedEvent>().Publish(new NetworkStatusChangedPayload
|
||||
{
|
||||
IsOnline = online,
|
||||
ChangedAt = DateTime.UtcNow
|
||||
});
|
||||
_isOnline = combined;
|
||||
newValue = combined;
|
||||
}
|
||||
|
||||
StatusChanged?.Invoke(newValue);
|
||||
_eventAggregator.GetEvent<NetworkStatusChangedEvent>().Publish(new NetworkStatusChangedPayload
|
||||
{
|
||||
IsOnline = newValue,
|
||||
ChangedAt = DateTime.UtcNow
|
||||
});
|
||||
}
|
||||
|
||||
private bool ComputeCombinedOnline()
|
||||
{
|
||||
if (IsDisconnectedByUser())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return _httpProbeOnline || _stompTransportOnline;
|
||||
}
|
||||
|
||||
private async Task<bool> ProbeAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (IsDisconnectedByUser())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var baseUrl = _configuration.GetValue<string>("JeecgIntegration:BaseUrl")?.TrimEnd('/');
|
||||
if (string.IsNullOrWhiteSpace(baseUrl))
|
||||
{
|
||||
@@ -108,4 +155,16 @@ public class NetworkMonitor : INetworkMonitor
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static bool IsDisconnectedByUser()
|
||||
{
|
||||
try
|
||||
{
|
||||
return ServerSettingsStore.Load().DisconnectConnection;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user