From 72ccd32e36fbc848740165b01b9a75ba014adf2c Mon Sep 17 00:00:00 2001
From: chenhaozhe <cgz@hz-kingdee.com>
Date: 星期二, 28 四月 2026 17:05:39 +0800
Subject: [PATCH] 添加 蓝牙打印机获取 打印模板 API 新增 锦隆 物料条码 80×60 100×60 ZPL指令 打印模板
---
WebAPI/Controllers/WebAPIController.cs | 136 +++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 136 insertions(+), 0 deletions(-)
diff --git a/WebAPI/Controllers/WebAPIController.cs b/WebAPI/Controllers/WebAPIController.cs
index 9e1452f..5251999 100644
--- a/WebAPI/Controllers/WebAPIController.cs
+++ b/WebAPI/Controllers/WebAPIController.cs
@@ -20,6 +20,7 @@
using WebAPI.Models;
using WebAPI.Service;
using SyntacticSugar.constant;
+using System.Text.RegularExpressions;
namespace WebAPI.Controllers
{
@@ -24005,6 +24006,7 @@
}
#endregion
+ #region APP鐧诲綍鏉冮檺楠岃瘉鎺ュ彛
/// <summary>
/// APP鐧诲綍鏉冮檺楠岃瘉鎺ュ彛
/// </summary>
@@ -24080,5 +24082,139 @@
return objJsonResult;
}
}
+ #endregion
+
+ #region 钃濈墮鎵撳嵃鏈鸿幏鍙栨墦鍗版ā鏉�
+ public class PrintRequestDto
+ {
+ public string ptMode { get; set; }
+ public string templateName { get; set; }
+ public JArray msg { get; set; } // 浣犵殑鎵归噺鏁版嵁
+ }
+ /// <summary>
+ /// ptMode: 鎵撳嵃妯″紡锛屽嵆闇�瑕侀偅绉嶆墦鍗版寚浠わ紝CPCL,TSPL,ESC,ZPL 涓�夋嫨
+ /// templateName: 妯℃澘鍚嶏紝鏍规嵁妯℃澘鍘绘寚瀹氱洰褰曚笅鏍规嵁鍚庣紑鎵炬枃浠�
+ /// msg: 鎵撳嵃鍐呭
+ /// </summary>
+ /// <param name="ptMode"></param>
+ /// <param name="templateName"></param>
+ /// <param name="msg"></param>
+ /// <returns></returns>
+ [Route("WebAPI/getBLEPrintTemplate")]
+ [HttpPost]
+ public object getBLEPrintTemplate([FromBody] PrintRequestDto dto)
+ {
+ var ptMode = dto.ptMode;
+ var templateName = dto.templateName;
+ var msg = dto.msg;
+ if (string.IsNullOrWhiteSpace(ptMode))
+ {
+ objJsonResult.code = "0";
+ objJsonResult.count = 0;
+ objJsonResult.Message = "鏈�夋嫨鎵撳嵃妯″紡锛屾棤娉曠敓鎴愭墦鍗版ā鏉�!";
+ objJsonResult.data = null;
+ return objJsonResult;
+ }
+ if (string.IsNullOrWhiteSpace(templateName))
+ {
+ objJsonResult.code = "0";
+ objJsonResult.count = 0;
+ objJsonResult.Message = "鏈�夋嫨鎵撳嵃妯℃澘锛屾棤娉曠敓鎴愭墦鍗版ā鏉�!";
+ objJsonResult.data = null;
+ return objJsonResult;
+ }
+
+ string suffix = ".txt";
+ string folder = "ptTemplate";
+ // 鍖归厤 {{ 瀛楁鍚� }} 鎵�闇�姝e垯琛ㄨ揪寮� 鏀寔涓枃
+ var regex = new Regex(@"{{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*}}", RegexOptions.Compiled);
+
+ if (ptMode == "ZPL")
+ {
+ suffix = ".zpl";
+ folder = "ZPLTemplate";
+ }
+
+ try
+ {
+ // 璇诲彇妯℃澘鏂囦欢
+ string rootPath = System.Web.HttpContext.Current.Server.MapPath("~/");
+ string templateDir = Path.Combine(rootPath, folder);
+ string templateFile = Path.Combine(templateDir, templateName+suffix);
+
+ // 濡傛灉鐩綍涓嶅瓨鍦�
+ if (!Directory.Exists(templateDir))
+ {
+ Directory.CreateDirectory(templateDir);
+ }
+
+ // 濡傛灉鏂囦欢涓嶅瓨鍦�
+ if (!File.Exists(templateFile))
+ {
+ objJsonResult.code = "0";
+ objJsonResult.Message = $"妯℃澘鏂囦欢涓嶅瓨鍦細{templateFile}";
+ objJsonResult.data = null;
+ return objJsonResult;
+ }
+
+ // 璇诲彇妯℃澘锛圲TF8 鏀寔涓枃锛�
+ string templateContent = File.ReadAllText(templateFile, Encoding.UTF8);
+
+ // 閫愯璇诲彇妯℃澘鏂囦欢
+ var lines = templateContent.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None);
+ var sb = new StringBuilder();
+
+ foreach(JToken JOneKVP in msg)
+ {
+ if(JOneKVP is JObject item)
+ {
+ foreach (var line in lines)
+ {
+ string currentLine = line;
+
+ // 璺宠繃娉ㄩ噴琛� //
+ if (currentLine.TrimStart().StartsWith("//"))
+ {
+ continue;
+ }
+
+ // 姝e垯琛ㄨ揪寮忓尮閰� {{ 瀛楁鍚� }} 鎵�鍦ㄨ鐨勫瓧娈碉紝濡傛灉鏈夛紝鍒欒繘琛屾浛鎹紝娌℃湁锛屽垯榛樿涓虹┖瀛楃涓�
+ currentLine = regex.Replace(currentLine, match =>
+ {
+ string fieldName = match.Groups[1].Value.Trim();
+ if (msg != null && item.ContainsKey(fieldName))
+ {
+ return item[fieldName]?.ToString() ?? "";
+ }
+ return "";
+ });
+ // 灏嗘墍鏈� \n 鐨勬崲琛岀 鏇挎崲涓� \r\n
+
+ sb.Append(currentLine);
+ sb.Append("\r\n"); // 鏍囧噯鎹㈣
+ }
+ }
+ }
+
+
+ string result = sb.ToString().TrimEnd();
+ // 杩斿洖娓叉煋鍚庣殑鎵撳嵃鎸囦护
+ objJsonResult.code = "1";
+ objJsonResult.count = 1;
+ objJsonResult.Message = $"妯℃澘娓叉煋鎴愬姛";
+ objJsonResult.data = result;
+ return objJsonResult;
+ }
+ catch (Exception ex)
+ {
+ objJsonResult.code = "0";
+ objJsonResult.count = 0;
+ objJsonResult.Message = $"妯℃澘娓叉煋澶辫触锛歿ex.Message}";
+ objJsonResult.data = null;
+ return objJsonResult;
+ }
+
+ }
+ #endregion
}
}
--
Gitblit v1.9.1