chenhaozhe
19 小时以前 62c1c41aab86ad3bf8967149769f9c2a40ac4313
WebAPI/Controllers/WebAPIController.cs
@@ -23569,7 +23569,7 @@
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "获取失败, 单据无返回值!";
                    objjson.Message = "无待上模单据!";
                    objjson.data = null;
                    return objjson;
                }else
@@ -23624,7 +23624,7 @@
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "获取失败, 单据无返回值!";
                    objjson.Message = "无待开工单据!";
                    objjson.data = null;
                    return objjson;
                }
@@ -23681,7 +23681,7 @@
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "获取失败, 单据无返回值!";
                    objjson.Message = "无待下模单据!";
                    objjson.data = null;
                    return objjson;
                }
@@ -24127,7 +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.Singleline);
            // 匹配 模板字符串 所在行 位置 宽高 所需正则表达式 支持中文
            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")
            {
@@ -24168,9 +24173,45 @@
                {
                    if(JOneKVP is JObject item)
                    {
                        foreach (var line in lines)
                        //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"); // 标准换行
                        //}
                        for(int i = 0; i<lines.Length; i++)
                        {
                            string currentLine = line;
                            string currentLine = lines[i];
                            string preLine = "";
                            string concatStr = "";
                            if(i>0)
                            {
                                // 获取当前行的上一行,用于判断是否是矩形框
                                preLine = lines[i - 1];
                            }
                            // 跳过注释行 //
                            if (currentLine.TrimStart().StartsWith("//"))
@@ -24178,19 +24219,32 @@
                                continue;
                            }
                            // 正则表达式匹配 {{ 字段名 }} 所在行的字段,如果有,则进行替换,没有,则默认为空字符串
                            currentLine = regex.Replace(currentLine, match =>
                            // 判断矩形框,如果前一行是矩形框,则需判断该行字符串长度是否超长
                            if(!getConcatStr(rectRegex: RectRegex, fieldRegex: fieldRegex,
                                PreLine: preLine, CurrLine: currentLine, fieldObject: item,
                                templateRegex: regex,
                                ref concatStr))
                            {
                                string fieldName = match.Groups[1].Value.Trim();
                                if (msg != null && item.ContainsKey(fieldName))
                                // 正则表达式匹配 {{ 字段名 }} 所在行的字段,如果有,则进行替换,没有,则默认为空字符串
                                concatStr = regex.Replace(currentLine, match =>
                                {
                                    return item[fieldName]?.ToString() ?? "";
                                }
                                return "";
                            });
                            // 将所有 \n 的换行符 替换为 \r\n
                                    string fieldName = match.Groups[1].Value.Trim();
                                    if (msg != null && item.ContainsKey(fieldName))
                                    {
                                        return item[fieldName]?.ToString() ?? "";
                                    }
                                    return "";
                                });
                            }
                            sb.Append(currentLine);
                            if(string.IsNullOrWhiteSpace(concatStr))
                            {
                                concatStr = currentLine;
                            }
                            // 将所有 \n 的换行符 替换为 \r\n
                            sb.Append(concatStr);
                            sb.Append("\r\n"); // 标准换行
                        }
                    }
@@ -24209,12 +24263,151 @@
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = $"模板渲染失败:{ex.Message}";
                objJsonResult.Message = $"模板渲染失败:{ex}";
                objJsonResult.data = null;
                return objJsonResult;
            }
        }
        private bool getConcatStr(Regex rectRegex, Regex fieldRegex,
            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;
            }
            if(!fieldRegexResult.Success)
            {
                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);
            var fieldName = fieldRegexResult.Groups[5].Value;
            JToken fieldValueToken = "";
            fieldObject.TryGetValue(fieldName, out fieldValueToken);
            if (string.IsNullOrWhiteSpace(fieldValueToken?.ToString()))
            {
                return false;
            }
            string fieldValue = fieldValueToken.ToString();
            if (fieldValue.Length * fieldWidth > rectWidth)
            {
                // 字符串长度大于矩形的最大长度 则文本需要从矩形的Y位置开始渲染
                fieldLocationY = rectLocationY;
                // 获取换行所需的行数
                int lineCount = int.Parse(Math.Ceiling((float)fieldValue.Length / fieldWidth).ToString());
                // 计算换行后的最大行数 * 字符高度 是否大于矩形最大高度
                if(lineCount * fieldHeight > rectHeight)
                {
                    fieldHeight = int.Parse(Math.Floor((float)rectHeight / lineCount).ToString());
                    fieldWidth = fieldHeight;
                }
                // 计算每行应该放多少字符(正确算法)
                int maxCharsPerLine = (int)Math.Ceiling((double)fieldValue.Length / lineCount);
                for (int i = 0; i < lineCount; i++)
                {
                    // 起始位置
                    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 false;
        }
        #endregion
        [HttpGet]
        [Route("Web/getSysParameter")]
        public object getSysParameter(string HClientID)
        {
            var HValue = oSystemParameter.GetSingleSystemParameter("uni-app_savelogin", ref DBUtility.ClsPub.sExeReturnInfo);
            if (HValue == "3" && HClientID != null)
            {
                DataSet ds = oCN.RunProcReturn("select * from Gy_UserClientIdRelation where HClientID= '" + HClientID + "'", "Gy_UserClientIdRelation");
                if (ds != null && ds.Tables.Count > 0)
                {
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = "根据设备自动登录";
                    objJsonResult.data = HValue;
                    return objJsonResult;
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "系统参数不存在,请联系管理";
                    objJsonResult.data = 0;
                    return objJsonResult;
                }
            }
            else if (HValue == "2" || HValue == "1")
            {
                objJsonResult.code = "1";
                objJsonResult.count = 1;
                objJsonResult.Message = "保存成功";
                objJsonResult.data = HValue;
                return objJsonResult;
            }
            else
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "系统参数不存在,请联系管理";
                objJsonResult.data = 0;
                return objJsonResult;
            }
        }
    }
}