| | |
| | | using WebAPI.Models; |
| | | using WebAPI.Service; |
| | | using SyntacticSugar.constant; |
| | | using System.Text.RegularExpressions; |
| | | |
| | | namespace WebAPI.Controllers |
| | | { |
| | |
| | | } |
| | | #endregion |
| | | |
| | | #region APP登录权限验证接口 |
| | | /// <summary> |
| | | /// APP登录权限验证接口 |
| | | /// </summary> |
| | |
| | | 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"; |
| | | // 匹配 {{ 字段名 }} 所需正则表达式 支持中文 |
| | | 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; |
| | | } |
| | | |
| | | // 读取模板(UTF8 支持中文) |
| | | 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; |
| | | } |
| | | |
| | | // 正则表达式匹配 {{ 字段名 }} 所在行的字段,如果有,则进行替换,没有,则默认为空字符串 |
| | | 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 |
| | | } |
| | | } |