chenhaozhe
12 小时以前 62c1c41aab86ad3bf8967149769f9c2a40ac4313
WebAPI/Controllers/WebAPIController.cs
@@ -24127,12 +24127,12 @@
            string suffix = ".txt";
            string folder = "ptTemplate";
            // 匹配 {{ 字段名 }} 所需正则表达式 支持中文
            var regex = new Regex(@"{{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*}}", RegexOptions.Compiled);
            var regex = new Regex(@"{{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*}}", RegexOptions.Singleline);
            // 匹配 矩形框 位置 宽高 所需正则表达式
            var RectRegex = new Regex(@"^\^FO(\d+),(\d+)\^GB(\d+),(\d+)", RegexOptions.Compiled);
            var RectRegex = new Regex(@"^\^FO(\d+),(\d+)\^GB(\d+),(\d+)", RegexOptions.Singleline);
            // 匹配 模板字符串 所在行 位置 宽高 所需正则表达式 支持中文
            var fieldRegex = new Regex(@"\^FO(\d+),(\d+)\^A[A-Z]+,(\d+),(\d+).*\{\{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*}\}", RegexOptions.Compiled);
            var fieldRegex = new Regex(@"\^FO(\d+),(\d+)\^A[A-Z]+,(\d+),(\d+).*?\{\{\s*([\u4e00-\u9fa5a-zA-Z0-9_\s]+?)\s*\}\}", RegexOptions.Singleline);
            if (ptMode == "ZPL")
            {
@@ -24221,7 +24221,9 @@
                            // 判断矩形框,如果前一行是矩形框,则需判断该行字符串长度是否超长
                            if(!getConcatStr(rectRegex: RectRegex, fieldRegex: fieldRegex,
                                PreLine: preLine, CurrLine: currentLine, fieldObject: item, ref concatStr))
                                PreLine: preLine, CurrLine: currentLine, fieldObject: item,
                                templateRegex: regex,
                                ref concatStr))
                            {
                                // 正则表达式匹配 {{ 字段名 }} 所在行的字段,如果有,则进行替换,没有,则默认为空字符串
                                concatStr = regex.Replace(currentLine, match =>
@@ -24235,6 +24237,10 @@
                                });
                            }
                            if(string.IsNullOrWhiteSpace(concatStr))
                            {
                                concatStr = currentLine;
                            }
                            
                            // 将所有 \n 的换行符 替换为 \r\n
@@ -24257,7 +24263,7 @@
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = $"模板渲染失败:{ex.Message}";
                objJsonResult.Message = $"模板渲染失败:{ex}";
                objJsonResult.data = null;
                return objJsonResult;
            }
@@ -24265,10 +24271,11 @@
        }
        private bool getConcatStr(Regex rectRegex, Regex fieldRegex,
            string PreLine, string CurrLine, JObject fieldObject, ref string concatStr)
            string PreLine, string CurrLine, JObject fieldObject, Regex templateRegex, ref string concatStr)
        {
            var rectRegexResult = rectRegex.Match(PreLine);
            var fieldRegexResult = fieldRegex.Match(CurrLine);
            var templateRegexResult = templateRegex.Match(CurrLine);
            if (!rectRegexResult.Success)
            {
                return false;
@@ -24279,26 +24286,37 @@
                return false;
            }
            if(!templateRegexResult.Success)
            {
                return false;
            }
            // 矩形 位置 宽高
            int rectLocationX = int.Parse(rectRegexResult.Groups[1].Value);
            int rectLocationY = int.Parse(rectRegexResult.Groups[2].Value);
            int rectWidth = int.Parse(rectRegexResult.Groups[3].Value);
            int rectHeight = int.Parse(rectRegexResult.Groups[4].Value);
            var fiEnum = fieldRegexResult.Groups;
            // 模板字符串 位置 字符宽高
            int fieldLocationX = int.Parse(fieldRegexResult.Groups[1].Value);
            int fieldLocationY = int.Parse(fieldRegexResult.Groups[2].Value);
            int fieldWidth = int.Parse(fieldRegexResult.Groups[3].Value);
            int fieldHeight = int.Parse(fieldRegexResult.Groups[4].Value);
            string fieldName = fieldRegexResult.Groups[5].Value;
            var fieldName = fieldRegexResult.Groups[5].Value;
            JToken fieldValueToken = "";
            fieldObject.TryGetValue(fieldName, out fieldValueToken);
            string fieldValue = fieldObject[fieldName].ToString() ?? "";
            if(string.IsNullOrWhiteSpace(fieldValue))
            if (string.IsNullOrWhiteSpace(fieldValueToken?.ToString()))
            {
                return false;
            }
            if(fieldValue.Length * fieldWidth > rectWidth)
            string fieldValue = fieldValueToken.ToString();
            if (fieldValue.Length * fieldWidth > rectWidth)
            {
                // 字符串长度大于矩形的最大长度 则文本需要从矩形的Y位置开始渲染
                fieldLocationY = rectLocationY;
@@ -24308,21 +24326,43 @@
                // 计算换行后的最大行数 * 字符高度 是否大于矩形最大高度
                if(lineCount * fieldHeight > rectHeight)
                {
                    fieldWidth = int.Parse(Math.Floor((float)fieldHeight / lineCount).ToString());
                    fieldHeight = int.Parse(Math.Floor((float)fieldHeight / lineCount).ToString());
                    fieldHeight = int.Parse(Math.Floor((float)rectHeight / lineCount).ToString());
                    fieldWidth = fieldHeight;
                }
                for(uint i=0;i<lineCount; i++)
                // 计算每行应该放多少字符(正确算法)
                int maxCharsPerLine = (int)Math.Ceiling((double)fieldValue.Length / lineCount);
                for (int i = 0; i < lineCount; i++)
                {
                    concatStr += fieldRegex.Replace(CurrLine, match =>
                    {
                        return "";
                    });
                }
                    // 起始位置
                    int start = i * maxCharsPerLine;
                    // 剩余多少字符(核心:保证不越界)
                    int remaining = fieldValue.Length - start;
                    if (remaining <= 0)
                        break;
                    // 本次取几个字符(绝对安全)
                    int take = Math.Min(maxCharsPerLine, remaining);
                    // 截取当前行文本
                    string lineText = fieldValue.Substring(start, take);
                    // 计算新行坐标
                    int newY = fieldLocationY + i * fieldHeight;
                    // 替换拼接
                    concatStr += fieldRegex.Replace(CurrLine, m =>
                        $"^FO{fieldLocationX},{newY}^AJN,{fieldWidth},{fieldHeight}^CI28^FD{lineText}^FS"
                    );
                }
                return true;
            }
            return true;
            return false;
        }
        #endregion