WebAPI/Controllers/WebAPIController.cs
@@ -20,6 +20,7 @@
using WebAPI.Models;
using WebAPI.Service;
using SyntacticSugar.constant;
using System.Text.RegularExpressions;
namespace WebAPI.Controllers
{
@@ -20968,7 +20969,18 @@
                {
                    objjson.code = "0";
                    objjson.count = 0;
                    objjson.Message = "获取失败,明细查无数据";
                    if(Type == "DJ")
                    {
                        objjson.Message = "获取失败,模具无绑定的点检规程";
                    }
                    else if (Type == "BY")
                    {
                        objjson.Message = "获取失败,模具无绑定的保养规程";
                    }
                    else
                    {
                        objjson.Message = "获取失败,明细查无数据";
                    }
                    objjson.data = null;
                    return objjson;
                }
@@ -21169,12 +21181,12 @@
                SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
                ds = oCN.RunProcReturn("select ManagerFlag from gy_czygl where czymc='" + user.Trim() + "'", "gy_czygl");
                if (!(bool)ds.Tables[0].Rows[0]["ManagerFlag"])  //是否管理员
                {
                    //不是管理员
                    sWhere += " and 制单人 = '" + user + "'";
                }
                //ds = oCN.RunProcReturn("select ManagerFlag from gy_czygl where czymc='" + user.Trim() + "'", "gy_czygl");
                //if (!(bool)ds.Tables[0].Rows[0]["ManagerFlag"])  //是否管理员
                //{
                //    //不是管理员
                //    sWhere += " and 制单人 = '" + user + "'";
                //}
                string sql = string.Format(@"select * from " + HView + " where 1 = 1 " + sWhere + " order by 日期 desc, hmainid desc");
@@ -23993,5 +24005,216 @@
            }
        }
        #endregion
        #region  APP登录权限验证接口
        /// <summary>
        /// APP登录权限验证接口
        /// </summary>
        [Route("Auth/CheckAppLoginAuth")]
        [HttpPost]
        public object CheckAppLoginAuth()
        {
            try
            {
                // 获取请求参数
                string orgID = HttpContext.Current.Request.Form["orgID"] ?? "";
                string orgName = HttpContext.Current.Request.Form["orgName"] ?? "";
                string serverUrl = HttpContext.Current.Request.Form["serverUrl"] ?? "";
                string phoneType = HttpContext.Current.Request.Form["phoneType"] ?? "";
                string userAccount = HttpContext.Current.Request.Form["userAccount"] ?? "";
                string other = HttpContext.Current.Request.Form["other"] ?? "";
                // 参数验证
                if (string.IsNullOrEmpty(userAccount))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "账号不能为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                if (string.IsNullOrEmpty(orgID))
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "组织ID不能为空!";
                    objJsonResult.data = null;
                    return objJsonResult;
                }
                // 参数安全处理(防止注入)
                orgID = orgID.Replace("'", "''");
                orgName = orgName.Replace("'", "''");
                serverUrl = serverUrl.Replace("'", "''");
                phoneType = phoneType.Replace("'", "''");
                userAccount = userAccount.Replace("'", "''");
                // 执行固定存储过程
                string sql = $"EXEC SP_CheckAppLoginAuth '{orgID}', '{orgName}', '{serverUrl}', '{phoneType}','{userAccount}','{other}'";
                ds = oCN.RunProcReturn(sql, "SP_CheckAppLoginAuth");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    int result = Convert.ToInt32(ds.Tables[0].Rows[0]["Result"]);
                    string message = ds.Tables[0].Rows[0]["Message"].ToString();
                    objJsonResult.code = "1";
                    objJsonResult.count = 1;
                    objJsonResult.Message = message;
                    objJsonResult.data = ds.Tables[0];
                }
                else
                {
                    objJsonResult.code = "0";
                    objJsonResult.count = 0;
                    objJsonResult.Message = "验证接口返回异常!";
                    objJsonResult.data = null;
                }
                return objJsonResult;
            }
            catch (Exception e)
            {
                objJsonResult.code = "0";
                objJsonResult.count = 0;
                objJsonResult.Message = "Exception!" + e.ToString();
                objJsonResult.data = null;
                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
    }
}