增强打印预览功能,新增离线打印功能,新增缩放控制按钮以提升用户体验。优化打印数据准备逻辑,支持实时预览缩放,确保打印效果的一致性。同时,重构相关视图和服务以增强系统的可维护性和扩展性。
This commit is contained in:
@@ -23,7 +23,7 @@ public static class NativePrintRenderService
|
||||
/// 将模板 JSON + 数据对象渲染为完整的可打印 HTML 页面(自包含,无外部依赖)。
|
||||
/// 屏幕样式与后端前端预览保持一致:深灰底(#525659)+ 白纸居中 + 页间分割线。
|
||||
/// </summary>
|
||||
public static string RenderToHtml(string templateJson, JsonObject data)
|
||||
public static string RenderToHtml(string templateJson, JsonObject data, bool enableScreenAutoFit = true)
|
||||
{
|
||||
var schema = JsonNode.Parse(templateJson) ?? throw new ArgumentException("无效模板 JSON");
|
||||
var page = schema["page"] ?? throw new ArgumentException("模板缺少 page 配置");
|
||||
@@ -86,6 +86,8 @@ public static class NativePrintRenderService
|
||||
sb.Append(" .qhmes-native-screen-page-sep { display: none !important; }\n");
|
||||
sb.Append(" }\n");
|
||||
sb.Append("</style>\n");
|
||||
if (enableScreenAutoFit)
|
||||
{
|
||||
sb.Append("<script>\n");
|
||||
sb.Append("(function(){\n");
|
||||
sb.Append(" function fitPage(){\n");
|
||||
@@ -109,6 +111,7 @@ public static class NativePrintRenderService
|
||||
sb.Append(" setTimeout(fitPage,400);\n");
|
||||
sb.Append("})();\n");
|
||||
sb.Append("</script>\n");
|
||||
}
|
||||
sb.Append("</head>\n<body>\n");
|
||||
sb.Append($"<div class=\"qhmes-native-print-root\" style=\"width:{wStr}mm;min-height:{thStr}mm;height:auto;overflow:visible;box-sizing:border-box;\">\n");
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Net.Http;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Web;
|
||||
using Prism.Events;
|
||||
@@ -20,6 +21,8 @@ public class RawMaterialCardService : IRawMaterialCardService, ISingletonDepende
|
||||
private readonly INetworkMonitor _networkMonitor;
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private readonly ILoggerService _logger;
|
||||
private readonly IPrintBizTemplateBindService _printBizTemplateBindService;
|
||||
private readonly IPrintTemplateService _printTemplateService;
|
||||
private readonly SemaphoreSlim _syncLock = new(1, 1);
|
||||
private readonly object _cacheLock = new();
|
||||
private readonly string _pendingOpsFilePath;
|
||||
@@ -39,13 +42,17 @@ public class RawMaterialCardService : IRawMaterialCardService, ISingletonDepende
|
||||
IConfiguration configuration,
|
||||
INetworkMonitor networkMonitor,
|
||||
IEventAggregator eventAggregator,
|
||||
ILoggerService logger)
|
||||
ILoggerService logger,
|
||||
IPrintBizTemplateBindService printBizTemplateBindService,
|
||||
IPrintTemplateService printTemplateService)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_configuration = configuration;
|
||||
_networkMonitor = networkMonitor;
|
||||
_eventAggregator = eventAggregator;
|
||||
_logger = logger;
|
||||
_printBizTemplateBindService = printBizTemplateBindService;
|
||||
_printTemplateService = printTemplateService;
|
||||
|
||||
var appDataDir = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||||
@@ -66,6 +73,7 @@ public class RawMaterialCardService : IRawMaterialCardService, ISingletonDepende
|
||||
}
|
||||
|
||||
private const int MaxPendingRetries = 5;
|
||||
private const string RawMaterialCardTemplateCode = "MES_RAW_MATERIAL_CARD";
|
||||
private string BaseUrl => (_configuration.GetValue<string>("JeecgIntegration:BaseUrl") ?? "http://localhost:8080/jeecg-boot").TrimEnd('/');
|
||||
private int DefaultTenantId => (int?)_configuration.GetValue<long?>("JeecgIntegration:DefaultTenantId") ?? 1002;
|
||||
|
||||
@@ -319,8 +327,8 @@ public class RawMaterialCardService : IRawMaterialCardService, ISingletonDepende
|
||||
|
||||
public async Task<(string templateJson, string printDataJson, string? errorMessage)> PrepareNativePrintAsync(string id, CancellationToken ct = default)
|
||||
{
|
||||
if (!_networkMonitor.IsOnline)
|
||||
return (string.Empty, "{}", "当前离线,无法获取打印数据");
|
||||
if (_networkMonitor.IsOnline)
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = $"{BaseUrl}/xslmes/mesXslRawMaterialCard/anon/prepareNativePrint?id={Uri.EscapeDataString(id)}&tenantId={DefaultTenantId}";
|
||||
@@ -345,11 +353,239 @@ public class RawMaterialCardService : IRawMaterialCardService, ISingletonDepende
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warning($"[原材料卡片] 准备打印数据失败 id={id}: {ex.Message}");
|
||||
return (string.Empty, "{}", $"获取打印数据失败:{ex.Message}");
|
||||
_logger.Warning($"[原材料卡片] 远端准备打印数据失败,回退本地模板渲染 id={id}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return await PrepareLocalNativePrintAsync(id, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task<(string templateJson, string printDataJson, string? errorMessage)> PrepareLocalNativePrintAsync(string id, CancellationToken ct)
|
||||
{
|
||||
var card = GetCardSnapshotById(id);
|
||||
if (card == null)
|
||||
{
|
||||
return (string.Empty, "{}", "本地未找到原材料卡片,无法离线打印");
|
||||
}
|
||||
|
||||
var bindList = _printBizTemplateBindService.GetCached();
|
||||
if (bindList.Count == 0)
|
||||
{
|
||||
try { bindList = await _printBizTemplateBindService.ListAsync(ct).ConfigureAwait(false); } catch { }
|
||||
}
|
||||
var bind = bindList.FirstOrDefault(x => string.Equals(x.TemplateCode, RawMaterialCardTemplateCode, StringComparison.OrdinalIgnoreCase))
|
||||
?? bindList.FirstOrDefault(x => (x.BizName ?? string.Empty).Contains("原材料卡片", StringComparison.OrdinalIgnoreCase));
|
||||
if (bind == null)
|
||||
{
|
||||
return (string.Empty, "{}", "未找到本地业务打印绑定,请先在线同步「原材料卡片」模板配置");
|
||||
}
|
||||
|
||||
var templates = _printTemplateService.GetCached();
|
||||
if (templates.Count == 0)
|
||||
{
|
||||
try { templates = await _printTemplateService.ListAsync(ct).ConfigureAwait(false); } catch { }
|
||||
}
|
||||
var templateCode = string.IsNullOrWhiteSpace(bind.TemplateCode) ? RawMaterialCardTemplateCode : bind.TemplateCode!;
|
||||
var tpl = templates.FirstOrDefault(t => !string.IsNullOrWhiteSpace(bind.TemplateId) && string.Equals(t.Id, bind.TemplateId, StringComparison.OrdinalIgnoreCase))
|
||||
?? templates.FirstOrDefault(t => string.Equals(t.TemplateCode, templateCode, StringComparison.OrdinalIgnoreCase));
|
||||
if (tpl == null || string.IsNullOrWhiteSpace(tpl.TemplateJson))
|
||||
{
|
||||
return (string.Empty, "{}", "本地未找到打印模板,请先在线同步模板后再离线打印");
|
||||
}
|
||||
|
||||
var mappingJson = string.IsNullOrWhiteSpace(bind.FieldMappingJson) ? "[]" : bind.FieldMappingJson!;
|
||||
var printData = BuildPrintDataFromMapping(card, mappingJson, tpl.TemplateJson!);
|
||||
return (tpl.TemplateJson!, printData.ToJsonString(), null);
|
||||
}
|
||||
|
||||
private MesXslRawMaterialCard? GetCardSnapshotById(string id)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id)) return null;
|
||||
lock (_cacheLock)
|
||||
{
|
||||
var snapshot = ApplyPendingOpsSnapshotUnsafe(_localCache.Select(Clone).ToList());
|
||||
return snapshot.FirstOrDefault(c => string.Equals(c.Id, id, StringComparison.OrdinalIgnoreCase)) is { } found
|
||||
? Clone(found)
|
||||
: null;
|
||||
}
|
||||
}
|
||||
|
||||
private JsonObject BuildPrintDataFromMapping<T>(T source, string mappingJson, string templateJson)
|
||||
{
|
||||
JsonObject printData = new();
|
||||
JsonNode? bizRoot = JsonSerializer.SerializeToNode(source, _jsonOpts);
|
||||
|
||||
try
|
||||
{
|
||||
var mappingNode = JsonNode.Parse(mappingJson) as JsonArray;
|
||||
if (mappingNode != null)
|
||||
{
|
||||
foreach (var rule in mappingNode)
|
||||
{
|
||||
if (rule is not JsonObject obj) continue;
|
||||
var templateField = obj["templateField"]?.GetValue<string>()?.Trim();
|
||||
if (string.IsNullOrWhiteSpace(templateField)) continue;
|
||||
var bizField = obj["bizField"]?.GetValue<string>()?.Trim();
|
||||
JsonNode? value = string.IsNullOrWhiteSpace(bizField)
|
||||
? JsonValue.Create(string.Empty)
|
||||
: ResolvePath(bizRoot, bizField!);
|
||||
SetPath(printData, templateField!, NormalizePrintNodeValue(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 映射异常时继续按模板字段补空,避免影响整体打印
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var templateNode = JsonNode.Parse(templateJson);
|
||||
var bindFields = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
CollectTemplateBindFields(templateNode, bindFields);
|
||||
foreach (var key in bindFields)
|
||||
{
|
||||
if (!HasPath(printData, key))
|
||||
{
|
||||
SetPath(printData, key, JsonValue.Create(string.Empty)!);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 模板结构异常时忽略补空逻辑
|
||||
}
|
||||
|
||||
return printData;
|
||||
}
|
||||
|
||||
private static void CollectTemplateBindFields(JsonNode? node, HashSet<string> fields)
|
||||
{
|
||||
if (node == null) return;
|
||||
if (node is JsonObject obj)
|
||||
{
|
||||
if (obj["dataBinding"] is JsonObject db && db["params"] is JsonArray paramArr)
|
||||
{
|
||||
foreach (var p in paramArr)
|
||||
{
|
||||
var key = p?["key"]?.GetValue<string>()?.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(key)) fields.Add(key!);
|
||||
}
|
||||
}
|
||||
|
||||
var bindField = obj["bindField"]?.GetValue<string>()?.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(bindField)) fields.Add(bindField!);
|
||||
|
||||
if (obj["columns"] is JsonArray cols)
|
||||
{
|
||||
foreach (var c in cols)
|
||||
{
|
||||
var cBind = c?["bindField"]?.GetValue<string>()?.Trim();
|
||||
var cField = c?["field"]?.GetValue<string>()?.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(cBind)) fields.Add(cBind!);
|
||||
else if (!string.IsNullOrWhiteSpace(cField)) fields.Add(cField!);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var kv in obj)
|
||||
{
|
||||
CollectTemplateBindFields(kv.Value, fields);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (node is JsonArray arr)
|
||||
{
|
||||
foreach (var item in arr)
|
||||
{
|
||||
CollectTemplateBindFields(item, fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonNode? ResolvePath(JsonNode? root, string path)
|
||||
{
|
||||
if (root == null || string.IsNullOrWhiteSpace(path)) return null;
|
||||
var parts = path.Split('.', StringSplitOptions.RemoveEmptyEntries);
|
||||
JsonNode? current = root;
|
||||
foreach (var part in parts)
|
||||
{
|
||||
if (current == null) return null;
|
||||
if (current is JsonArray arr)
|
||||
{
|
||||
if (int.TryParse(part, out var index))
|
||||
{
|
||||
current = index >= 0 && index < arr.Count ? arr[index] : null;
|
||||
}
|
||||
else
|
||||
{
|
||||
current = arr.Count > 0 ? arr[0]?[part] : null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
current = current[part];
|
||||
}
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
private static bool HasPath(JsonObject root, string path)
|
||||
{
|
||||
var parts = path.Split('.', StringSplitOptions.RemoveEmptyEntries);
|
||||
JsonNode? current = root;
|
||||
for (int i = 0; i < parts.Length; i++)
|
||||
{
|
||||
if (current is not JsonObject obj) return false;
|
||||
if (!obj.TryGetPropertyValue(parts[i], out current)) return false;
|
||||
if (i == parts.Length - 1) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static JsonNode NormalizePrintNodeValue(JsonNode? node)
|
||||
{
|
||||
if (node == null) return JsonValue.Create(string.Empty)!;
|
||||
if (node is JsonValue value)
|
||||
{
|
||||
if (value.TryGetValue<DateTime>(out var dt))
|
||||
{
|
||||
return JsonValue.Create(dt.ToString("yyyy-MM-dd HH:mm:ss"))!;
|
||||
}
|
||||
if (value.TryGetValue<DateTimeOffset>(out var dto))
|
||||
{
|
||||
return JsonValue.Create(dto.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"))!;
|
||||
}
|
||||
if (value.TryGetValue<string>(out var text) && !string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
if ((text.Contains('T') || text.EndsWith("Z", StringComparison.OrdinalIgnoreCase) || text.Contains('+'))
|
||||
&& DateTimeOffset.TryParse(text, out var parsed))
|
||||
{
|
||||
return JsonValue.Create(parsed.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"))!;
|
||||
}
|
||||
}
|
||||
}
|
||||
return node.DeepClone();
|
||||
}
|
||||
|
||||
private static void SetPath(JsonObject target, string path, JsonNode value)
|
||||
{
|
||||
var parts = path.Split('.', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length == 0) return;
|
||||
|
||||
JsonObject current = target;
|
||||
for (int i = 0; i < parts.Length - 1; i++)
|
||||
{
|
||||
if (current[parts[i]] is not JsonObject child)
|
||||
{
|
||||
child = new JsonObject();
|
||||
current[parts[i]] = child;
|
||||
}
|
||||
current = child;
|
||||
}
|
||||
current[parts[^1]] = value;
|
||||
}
|
||||
|
||||
public async Task<bool> UpdatePriorityAsync(string id, string priorityPickup, CancellationToken ct = default)
|
||||
{
|
||||
if (_networkMonitor.IsOnline)
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Net.Http;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Nodes;
|
||||
using System.Text.Json.Serialization;
|
||||
using System.Web;
|
||||
using Prism.Events;
|
||||
@@ -20,6 +21,8 @@ public class RawMaterialEntryService : IRawMaterialEntryService, ISingletonDepen
|
||||
private readonly INetworkMonitor _networkMonitor;
|
||||
private readonly IEventAggregator _eventAggregator;
|
||||
private readonly ILoggerService _logger;
|
||||
private readonly IPrintBizTemplateBindService _printBizTemplateBindService;
|
||||
private readonly IPrintTemplateService _printTemplateService;
|
||||
private readonly SemaphoreSlim _syncLock = new(1, 1);
|
||||
private readonly object _cacheLock = new();
|
||||
private readonly string _pendingOpsFilePath;
|
||||
@@ -39,13 +42,17 @@ public class RawMaterialEntryService : IRawMaterialEntryService, ISingletonDepen
|
||||
IConfiguration configuration,
|
||||
INetworkMonitor networkMonitor,
|
||||
IEventAggregator eventAggregator,
|
||||
ILoggerService logger)
|
||||
ILoggerService logger,
|
||||
IPrintBizTemplateBindService printBizTemplateBindService,
|
||||
IPrintTemplateService printTemplateService)
|
||||
{
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_configuration = configuration;
|
||||
_networkMonitor = networkMonitor;
|
||||
_eventAggregator = eventAggregator;
|
||||
_logger = logger;
|
||||
_printBizTemplateBindService = printBizTemplateBindService;
|
||||
_printTemplateService = printTemplateService;
|
||||
|
||||
var appDataDir = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||||
@@ -66,6 +73,8 @@ public class RawMaterialEntryService : IRawMaterialEntryService, ISingletonDepen
|
||||
}
|
||||
|
||||
private const int MaxPendingRetries = 5;
|
||||
private const string RawMaterialEntryBizCode = "1900000000000000530";
|
||||
private const string RawMaterialEntryTemplateCode = "MES_RAW_MATERIAL_ENTRY";
|
||||
private string BaseUrl => (_configuration.GetValue<string>("JeecgIntegration:BaseUrl") ?? "http://localhost:8080/jeecg-boot").TrimEnd('/');
|
||||
private int DefaultTenantId => (int?)_configuration.GetValue<long?>("JeecgIntegration:DefaultTenantId") ?? 1002;
|
||||
|
||||
@@ -239,8 +248,8 @@ public class RawMaterialEntryService : IRawMaterialEntryService, ISingletonDepen
|
||||
|
||||
public async Task<(string templateJson, string printDataJson, string? errorMessage)> PrepareNativePrintAsync(string id, CancellationToken ct = default)
|
||||
{
|
||||
if (!_networkMonitor.IsOnline)
|
||||
return (string.Empty, "{}", "当前离线,无法获取打印数据");
|
||||
if (_networkMonitor.IsOnline)
|
||||
{
|
||||
try
|
||||
{
|
||||
var url = $"{BaseUrl}/xslmes/mesXslRawMaterialEntry/anon/prepareNativePrint?id={Uri.EscapeDataString(id)}&tenantId={DefaultTenantId}";
|
||||
@@ -265,11 +274,240 @@ public class RawMaterialEntryService : IRawMaterialEntryService, ISingletonDepen
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Warning($"[原料入场] 准备打印数据失败 id={id}: {ex.Message}");
|
||||
return (string.Empty, "{}", $"获取打印数据失败:{ex.Message}");
|
||||
_logger.Warning($"[原料入场] 远端准备打印数据失败,回退本地模板渲染 id={id}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return await PrepareLocalNativePrintAsync(id, ct).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
private async Task<(string templateJson, string printDataJson, string? errorMessage)> PrepareLocalNativePrintAsync(string id, CancellationToken ct)
|
||||
{
|
||||
var entry = GetEntrySnapshotById(id);
|
||||
if (entry == null)
|
||||
{
|
||||
return (string.Empty, "{}", "本地未找到入场记录,无法离线打印");
|
||||
}
|
||||
|
||||
var bindList = _printBizTemplateBindService.GetCached();
|
||||
if (bindList.Count == 0)
|
||||
{
|
||||
try { bindList = await _printBizTemplateBindService.ListAsync(ct).ConfigureAwait(false); } catch { }
|
||||
}
|
||||
var bind = bindList.FirstOrDefault(x => string.Equals(x.BizCode, RawMaterialEntryBizCode, StringComparison.OrdinalIgnoreCase))
|
||||
?? bindList.FirstOrDefault(x => string.Equals(x.TemplateCode, RawMaterialEntryTemplateCode, StringComparison.OrdinalIgnoreCase))
|
||||
?? bindList.FirstOrDefault(x => (x.BizName ?? string.Empty).Contains("原料入场记录", StringComparison.OrdinalIgnoreCase));
|
||||
if (bind == null)
|
||||
{
|
||||
return (string.Empty, "{}", "未找到本地业务打印绑定,请先在线同步「原料入场记录」模板配置");
|
||||
}
|
||||
|
||||
var templates = _printTemplateService.GetCached();
|
||||
if (templates.Count == 0)
|
||||
{
|
||||
try { templates = await _printTemplateService.ListAsync(ct).ConfigureAwait(false); } catch { }
|
||||
}
|
||||
var templateCode = string.IsNullOrWhiteSpace(bind.TemplateCode) ? RawMaterialEntryTemplateCode : bind.TemplateCode!;
|
||||
var tpl = templates.FirstOrDefault(t => !string.IsNullOrWhiteSpace(bind.TemplateId) && string.Equals(t.Id, bind.TemplateId, StringComparison.OrdinalIgnoreCase))
|
||||
?? templates.FirstOrDefault(t => string.Equals(t.TemplateCode, templateCode, StringComparison.OrdinalIgnoreCase));
|
||||
if (tpl == null || string.IsNullOrWhiteSpace(tpl.TemplateJson))
|
||||
{
|
||||
return (string.Empty, "{}", "本地未找到打印模板,请先在线同步模板后再离线打印");
|
||||
}
|
||||
|
||||
var mappingJson = string.IsNullOrWhiteSpace(bind.FieldMappingJson) ? "[]" : bind.FieldMappingJson!;
|
||||
var printData = BuildPrintDataFromMapping(entry, mappingJson, tpl.TemplateJson!);
|
||||
return (tpl.TemplateJson!, printData.ToJsonString(), null);
|
||||
}
|
||||
|
||||
private MesXslRawMaterialEntry? GetEntrySnapshotById(string id)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id)) return null;
|
||||
lock (_cacheLock)
|
||||
{
|
||||
var snapshot = ApplyPendingOpsSnapshotUnsafe(_localCache.Select(Clone).ToList());
|
||||
return snapshot.FirstOrDefault(e => string.Equals(e.Id, id, StringComparison.OrdinalIgnoreCase)) is { } found
|
||||
? Clone(found)
|
||||
: null;
|
||||
}
|
||||
}
|
||||
|
||||
private JsonObject BuildPrintDataFromMapping<T>(T source, string mappingJson, string templateJson)
|
||||
{
|
||||
JsonObject printData = new();
|
||||
JsonNode? bizRoot = JsonSerializer.SerializeToNode(source, _jsonOpts);
|
||||
|
||||
try
|
||||
{
|
||||
var mappingNode = JsonNode.Parse(mappingJson) as JsonArray;
|
||||
if (mappingNode != null)
|
||||
{
|
||||
foreach (var rule in mappingNode)
|
||||
{
|
||||
if (rule is not JsonObject obj) continue;
|
||||
var templateField = obj["templateField"]?.GetValue<string>()?.Trim();
|
||||
if (string.IsNullOrWhiteSpace(templateField)) continue;
|
||||
var bizField = obj["bizField"]?.GetValue<string>()?.Trim();
|
||||
JsonNode? value = string.IsNullOrWhiteSpace(bizField)
|
||||
? JsonValue.Create(string.Empty)
|
||||
: ResolvePath(bizRoot, bizField!);
|
||||
SetPath(printData, templateField!, NormalizePrintNodeValue(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 映射异常时继续按模板字段补空,避免影响整体打印
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var templateNode = JsonNode.Parse(templateJson);
|
||||
var bindFields = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
|
||||
CollectTemplateBindFields(templateNode, bindFields);
|
||||
foreach (var key in bindFields)
|
||||
{
|
||||
if (!HasPath(printData, key))
|
||||
{
|
||||
SetPath(printData, key, JsonValue.Create(string.Empty)!);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 模板结构异常时忽略补空逻辑
|
||||
}
|
||||
|
||||
return printData;
|
||||
}
|
||||
|
||||
private static void CollectTemplateBindFields(JsonNode? node, HashSet<string> fields)
|
||||
{
|
||||
if (node == null) return;
|
||||
if (node is JsonObject obj)
|
||||
{
|
||||
if (obj["dataBinding"] is JsonObject db && db["params"] is JsonArray paramArr)
|
||||
{
|
||||
foreach (var p in paramArr)
|
||||
{
|
||||
var key = p?["key"]?.GetValue<string>()?.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(key)) fields.Add(key!);
|
||||
}
|
||||
}
|
||||
|
||||
var bindField = obj["bindField"]?.GetValue<string>()?.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(bindField)) fields.Add(bindField!);
|
||||
|
||||
if (obj["columns"] is JsonArray cols)
|
||||
{
|
||||
foreach (var c in cols)
|
||||
{
|
||||
var cBind = c?["bindField"]?.GetValue<string>()?.Trim();
|
||||
var cField = c?["field"]?.GetValue<string>()?.Trim();
|
||||
if (!string.IsNullOrWhiteSpace(cBind)) fields.Add(cBind!);
|
||||
else if (!string.IsNullOrWhiteSpace(cField)) fields.Add(cField!);
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var kv in obj)
|
||||
{
|
||||
CollectTemplateBindFields(kv.Value, fields);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (node is JsonArray arr)
|
||||
{
|
||||
foreach (var item in arr)
|
||||
{
|
||||
CollectTemplateBindFields(item, fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonNode? ResolvePath(JsonNode? root, string path)
|
||||
{
|
||||
if (root == null || string.IsNullOrWhiteSpace(path)) return null;
|
||||
var parts = path.Split('.', StringSplitOptions.RemoveEmptyEntries);
|
||||
JsonNode? current = root;
|
||||
foreach (var part in parts)
|
||||
{
|
||||
if (current == null) return null;
|
||||
if (current is JsonArray arr)
|
||||
{
|
||||
if (int.TryParse(part, out var index))
|
||||
{
|
||||
current = index >= 0 && index < arr.Count ? arr[index] : null;
|
||||
}
|
||||
else
|
||||
{
|
||||
current = arr.Count > 0 ? arr[0]?[part] : null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
current = current[part];
|
||||
}
|
||||
}
|
||||
return current;
|
||||
}
|
||||
|
||||
private static bool HasPath(JsonObject root, string path)
|
||||
{
|
||||
var parts = path.Split('.', StringSplitOptions.RemoveEmptyEntries);
|
||||
JsonNode? current = root;
|
||||
for (int i = 0; i < parts.Length; i++)
|
||||
{
|
||||
if (current is not JsonObject obj) return false;
|
||||
if (!obj.TryGetPropertyValue(parts[i], out current)) return false;
|
||||
if (i == parts.Length - 1) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static JsonNode NormalizePrintNodeValue(JsonNode? node)
|
||||
{
|
||||
if (node == null) return JsonValue.Create(string.Empty)!;
|
||||
if (node is JsonValue value)
|
||||
{
|
||||
if (value.TryGetValue<DateTime>(out var dt))
|
||||
{
|
||||
return JsonValue.Create(dt.ToString("yyyy-MM-dd HH:mm:ss"))!;
|
||||
}
|
||||
if (value.TryGetValue<DateTimeOffset>(out var dto))
|
||||
{
|
||||
return JsonValue.Create(dto.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"))!;
|
||||
}
|
||||
if (value.TryGetValue<string>(out var text) && !string.IsNullOrWhiteSpace(text))
|
||||
{
|
||||
if ((text.Contains('T') || text.EndsWith("Z", StringComparison.OrdinalIgnoreCase) || text.Contains('+'))
|
||||
&& DateTimeOffset.TryParse(text, out var parsed))
|
||||
{
|
||||
return JsonValue.Create(parsed.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss"))!;
|
||||
}
|
||||
}
|
||||
}
|
||||
return node.DeepClone();
|
||||
}
|
||||
|
||||
private static void SetPath(JsonObject target, string path, JsonNode value)
|
||||
{
|
||||
var parts = path.Split('.', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length == 0) return;
|
||||
|
||||
JsonObject current = target;
|
||||
for (int i = 0; i < parts.Length - 1; i++)
|
||||
{
|
||||
if (current[parts[i]] is not JsonObject child)
|
||||
{
|
||||
child = new JsonObject();
|
||||
current[parts[i]] = child;
|
||||
}
|
||||
current = child;
|
||||
}
|
||||
current[parts[^1]] = value;
|
||||
}
|
||||
|
||||
public IReadOnlyList<MesXslRawMaterialEntry> GetCachedSnapshot()
|
||||
{
|
||||
// 注意:不允许直接返回 _localCache 引用,避免外部修改污染缓存;用 Clone 做深拷贝。
|
||||
|
||||
@@ -5,6 +5,7 @@ using Microsoft.Extensions.Configuration;
|
||||
using YY.Admin.Core.Helper;
|
||||
using YY.Admin.Core.Session;
|
||||
using YY.Admin.FluentValidation;
|
||||
using YY.Admin.Helper;
|
||||
using YY.Admin.Services;
|
||||
using YY.Admin.Services.Service.Auth;
|
||||
using YY.Admin.Services.Service.Jeecg;
|
||||
@@ -220,6 +221,31 @@ namespace YY.Admin.ViewModels
|
||||
while (!cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
bool connected = false;
|
||||
var settings = ServerSettingsStore.Load();
|
||||
if (settings.DisconnectConnection)
|
||||
{
|
||||
try
|
||||
{
|
||||
await Application.Current.Dispatcher.InvokeAsync(() =>
|
||||
{
|
||||
IsBackendConnected = false;
|
||||
});
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 忽略窗口关闭后的调度异常
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(ConnectivityCheckIntervalSeconds), cancellationToken);
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
try
|
||||
{
|
||||
// 每轮都重新读取配置,保存服务器设置后可即时生效
|
||||
@@ -269,6 +295,11 @@ namespace YY.Admin.ViewModels
|
||||
{
|
||||
if (r.Result == ButtonResult.OK)
|
||||
{
|
||||
var settings = ServerSettingsStore.Load();
|
||||
if (settings.DisconnectConnection)
|
||||
{
|
||||
IsBackendConnected = false;
|
||||
}
|
||||
LoginMessage = "服务器配置已保存";
|
||||
}
|
||||
});
|
||||
|
||||
@@ -108,6 +108,9 @@ public class RawMaterialEntryOperationViewModel : RawMaterialEntryEditDialogView
|
||||
public DelegateCommand ResplitCommand { get; }
|
||||
public DelegateCommand SaveAndPrintCommand { get; }
|
||||
public DelegateCommand RefreshPrintersCommand { get; }
|
||||
public DelegateCommand ZoomOutPrintPreviewCommand { get; }
|
||||
public DelegateCommand ZoomInPrintPreviewCommand { get; }
|
||||
public DelegateCommand ResetPrintPreviewZoomCommand { get; }
|
||||
|
||||
/// <summary>PrintDot 桥接器返回的打印机列表(与打印模板页一致)。</summary>
|
||||
public ObservableCollection<PrintDotPrinter> Printers { get; } = new();
|
||||
@@ -169,6 +172,27 @@ public class RawMaterialEntryOperationViewModel : RawMaterialEntryEditDialogView
|
||||
private set => SetProperty(ref _printPreviewStatus, value);
|
||||
}
|
||||
|
||||
private const double PrintPreviewMinZoom = 0.5d;
|
||||
private const double PrintPreviewMaxZoom = 2.0d;
|
||||
private const double PrintPreviewDefaultZoom = 0.7d;
|
||||
private const double PrintPreviewZoomStep = 0.1d;
|
||||
|
||||
private double _printPreviewZoomFactor = PrintPreviewDefaultZoom;
|
||||
/// <summary>打印预览缩放倍率(WebView2 ZoomFactor)。</summary>
|
||||
public double PrintPreviewZoomFactor
|
||||
{
|
||||
get => _printPreviewZoomFactor;
|
||||
set
|
||||
{
|
||||
var clamped = Math.Round(Math.Clamp(value, PrintPreviewMinZoom, PrintPreviewMaxZoom), 2);
|
||||
if (!SetProperty(ref _printPreviewZoomFactor, clamped)) return;
|
||||
RaisePropertyChanged(nameof(PrintPreviewZoomText));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>打印预览缩放显示文本(百分比)。</summary>
|
||||
public string PrintPreviewZoomText => $"{Math.Round(PrintPreviewZoomFactor * 100):0}%";
|
||||
|
||||
/// <summary>由 View 订阅,在 UI 线程将 HTML 交给 WebView2。</summary>
|
||||
public event EventHandler<string>? PrintPreviewHtmlReady;
|
||||
|
||||
@@ -220,6 +244,9 @@ public class RawMaterialEntryOperationViewModel : RawMaterialEntryEditDialogView
|
||||
.ObservesProperty(() => Entry);
|
||||
SaveAndPrintCommand = new DelegateCommand(async () => await SaveAndPrintAsync());
|
||||
RefreshPrintersCommand = new DelegateCommand(async () => await RefreshPrintersAsync(verbose: true));
|
||||
ZoomOutPrintPreviewCommand = new DelegateCommand(() => PrintPreviewZoomFactor -= PrintPreviewZoomStep);
|
||||
ZoomInPrintPreviewCommand = new DelegateCommand(() => PrintPreviewZoomFactor += PrintPreviewZoomStep);
|
||||
ResetPrintPreviewZoomCommand = new DelegateCommand(() => PrintPreviewZoomFactor = PrintPreviewDefaultZoom);
|
||||
// 集合变化:批量重订阅 item.PropertyChanged 监听 HasCard/Portions,并同步刷新两个 Can*。
|
||||
SplitCodeDetails.CollectionChanged += OnSplitCodeDetailsCollectionChangedForCanFlags;
|
||||
_ = RefreshPrintersAsync(verbose: false);
|
||||
@@ -808,7 +835,8 @@ public class RawMaterialEntryOperationViewModel : RawMaterialEntryEditDialogView
|
||||
string html;
|
||||
try
|
||||
{
|
||||
html = NativePrintRenderService.RenderToHtml(_previewTemplateJson, dataObj);
|
||||
// 实时预览关闭模板内部 fitPage 自适应,避免与 WebView2 外层缩放叠加后出现“越放大越小”。
|
||||
html = NativePrintRenderService.RenderToHtml(_previewTemplateJson, dataObj, enableScreenAutoFit: false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
|
||||
@@ -1057,8 +1057,41 @@
|
||||
Margin="8,0,10,0"
|
||||
VerticalAlignment="Center"
|
||||
Foreground="{DynamicResource SecondaryTextBrush}"/>
|
||||
<ToggleButton Grid.Column="2"
|
||||
IsChecked="{Binding IsPrintPreviewExpanded, Mode=TwoWay}"
|
||||
<StackPanel Grid.Column="2"
|
||||
Orientation="Horizontal"
|
||||
VerticalAlignment="Center"
|
||||
HorizontalAlignment="Right">
|
||||
<StackPanel Orientation="Horizontal"
|
||||
Margin="0,0,8,0"
|
||||
VerticalAlignment="Center"
|
||||
Visibility="{Binding IsPrintPreviewExpanded, Converter={StaticResource Boolean2VisibilityConverter}}">
|
||||
<Button Content="-"
|
||||
Width="24"
|
||||
Height="24"
|
||||
Padding="0"
|
||||
FontSize="12"
|
||||
Command="{Binding ZoomOutPrintPreviewCommand}"
|
||||
Style="{StaticResource ButtonDefault}"
|
||||
ToolTip="缩小预览"/>
|
||||
<Button Content="{Binding PrintPreviewZoomText}"
|
||||
MinWidth="56"
|
||||
Height="24"
|
||||
Margin="4,0,4,0"
|
||||
Padding="10,0"
|
||||
FontSize="11"
|
||||
Command="{Binding ResetPrintPreviewZoomCommand}"
|
||||
Style="{StaticResource ButtonDefault}"
|
||||
ToolTip="重置为 70%"/>
|
||||
<Button Content="+"
|
||||
Width="24"
|
||||
Height="24"
|
||||
Padding="0"
|
||||
FontSize="12"
|
||||
Command="{Binding ZoomInPrintPreviewCommand}"
|
||||
Style="{StaticResource ButtonDefault}"
|
||||
ToolTip="放大预览"/>
|
||||
</StackPanel>
|
||||
<ToggleButton IsChecked="{Binding IsPrintPreviewExpanded, Mode=TwoWay}"
|
||||
MinWidth="56"
|
||||
Height="24"
|
||||
Background="Transparent"
|
||||
@@ -1093,6 +1126,7 @@
|
||||
</TextBlock.Style>
|
||||
</TextBlock>
|
||||
</ToggleButton>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Border>
|
||||
<Border BorderBrush="{DynamicResource BorderBrush}"
|
||||
|
||||
@@ -49,12 +49,24 @@ public partial class RawMaterialEntryOperationView : UserControl
|
||||
// null/空 表示“所有属性”通知(Prism/BindableBase 批量刷新时),需同步分割布局
|
||||
if (!string.IsNullOrEmpty(e.PropertyName)
|
||||
&& e.PropertyName is not nameof(RawMaterialEntryOperationViewModel.IsRightPanelExpanded)
|
||||
&& e.PropertyName is not nameof(RawMaterialEntryOperationViewModel.ExpandedRightPanelWidth))
|
||||
&& e.PropertyName is not nameof(RawMaterialEntryOperationViewModel.ExpandedRightPanelWidth)
|
||||
&& e.PropertyName is not nameof(RawMaterialEntryOperationViewModel.PrintPreviewZoomFactor))
|
||||
return;
|
||||
|
||||
if (string.IsNullOrEmpty(e.PropertyName)
|
||||
|| e.PropertyName is nameof(RawMaterialEntryOperationViewModel.IsRightPanelExpanded)
|
||||
|| e.PropertyName is nameof(RawMaterialEntryOperationViewModel.ExpandedRightPanelWidth))
|
||||
{
|
||||
_ = Dispatcher.InvokeAsync(ApplySplitLayout);
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(e.PropertyName)
|
||||
|| e.PropertyName is nameof(RawMaterialEntryOperationViewModel.PrintPreviewZoomFactor))
|
||||
{
|
||||
_ = Dispatcher.InvokeAsync(ApplyPrintPreviewZoom);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>按钮 Click 在 Command 之后执行,用于兜底刷新列宽(不重复切换状态)。</summary>
|
||||
private void ToggleTodayPanelButton_OnClick(object sender, RoutedEventArgs e) => ApplySplitLayout();
|
||||
|
||||
@@ -66,6 +78,7 @@ public partial class RawMaterialEntryOperationView : UserControl
|
||||
EnsureVmAttached();
|
||||
|
||||
ApplySplitLayout();
|
||||
ApplyPrintPreviewZoom();
|
||||
|
||||
if (DataContext is RawMaterialEntryOperationViewModel vm && !_initialized)
|
||||
{
|
||||
@@ -92,6 +105,7 @@ public partial class RawMaterialEntryOperationView : UserControl
|
||||
try
|
||||
{
|
||||
await PrintPreviewWebView.EnsureCoreWebView2Async();
|
||||
ApplyPrintPreviewZoom();
|
||||
PrintPreviewWebView.NavigateToString(html ?? string.Empty);
|
||||
}
|
||||
catch
|
||||
@@ -100,6 +114,15 @@ public partial class RawMaterialEntryOperationView : UserControl
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyPrintPreviewZoom()
|
||||
{
|
||||
var vm = _vm ?? DataContext as RawMaterialEntryOperationViewModel;
|
||||
if (vm == null) return;
|
||||
if (PrintPreviewWebView?.CoreWebView2 == null) return;
|
||||
// 实时预览已关闭 HTML 内部 fitPage,自定义缩放直接映射到 WebView2 即可(+ 放大,- 缩小)。
|
||||
PrintPreviewWebView.ZoomFactor = vm.PrintPreviewZoomFactor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据 ViewModel 同步右侧栏与分割条:展开时使用持久化宽度;折叠时右栏与分割条占宽均为 0(完全隐藏)。
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user