using System; using Newtonsoft.Json.Linq; using System.Web.Http; namespace ThCardReader { public class EntryController : ApiController { public JObject Get(string param) { JObject result = new JObject(); if (param == null) { result.Add("code", 1001); result.Add("message", "参数为空!"); return result; } string[] temps = param.Split(new char[] { '_' }); // 检查两段式前缀(如 jiangsu_face、jiangsu_qrcode) string type; if (temps.Length >= 2 && temps[0].Equals("jiangsu") && (temps[1].Equals("face") || temps[1].Equals("qrcode"))) { type = temps[0] + "_" + temps[1]; // 组合成 jiangsu_face 或 jiangsu_qrcode } else { type = temps[0]; // 单段前缀 } if (type.Equals("qrcode")) { result = NationalEcBusiness.ReadQrCode(temps[1]); } else if (type.Equals("qrcode2")) { result = NationalEcBusiness.ReadQrCode2(temps[1]); } else if (type.Equals("face")) { result = NationalEcBusiness.ReadFace(temps[1]); } else if (type.Equals("sicard")) { result = SsCardBusiness.ReadSiCard(); } else if (type.Equals("idcard")) { result = SsCardBusiness.ReadIdCard(); } else if (type.Equals("idcard2")) { result = NationalEcBusiness.ReadIdCard(temps[1]); } else if (type.Equals("huashi")) { // 华视读卡器调用 string action = temps.Length > 1 ? temps[1] : "readcard"; // 检查是否是业务代码格式(5位数字),如果是则默认为readcard操作 if (temps.Length > 1 && IsBusinessCode(temps[1])) { action = "readcard"; // 业务代码不影响华视读卡器操作,默认读卡 } if (action.Equals("init")) { int port = temps.Length > 2 ? int.Parse(temps[2]) : 1001; result = HuaShiIdCardBusiness.Initialize(port); } else if (action.Equals("readcard")) { // URL调用时启用自动关闭,模拟SSCard.dll的无状态行为 var huashiResult = HuaShiIdCardBusiness.ReadIdCard("", true); result = ConvertHuaShiDataToStandardFormat(huashiResult); } else if (action.Equals("continuous")) { // URL调用时启用自动关闭,模拟SSCard.dll的无状态行为 var huashiResult = HuaShiIdCardBusiness.ReadIdCardContinuous("", true); result = ConvertHuaShiDataToStandardFormat(huashiResult); } else if (action.Equals("status")) { result = HuaShiIdCardBusiness.GetDeviceStatus(); } else if (action.Equals("close")) { result = HuaShiIdCardBusiness.Close(); } else { result.Add("code", 1001); result.Add("message", "华视读卡器操作【" + action + "】错误!允许的操作为【init、readcard、continuous、status、close】"); } } else if (type.Equals("jiangsu_face")) { // 江苏医保刷脸授权获取医保身份 - 使用NationECCode.dll与泰和完全一致 // URL格式: http://localhost:8321/readcard/entry?param=jiangsu_face_${nowbiztype} string businessType = temps.Length > 2 ? temps[2] : "01101"; // 从URL参数获取业务类型(第3段) // 验证业务类型格式 if (!JiangSuSocialCardBusiness.IsValidBusinessType(businessType)) { result.Add("success", false); result.Add("code", 1001); result.Add("message", $"江苏医保刷脸业务类型【{businessType}】无效!支持的业务类型:01101(门诊挂号)、01301(门诊结算)、02121(药店购药)等"); result.Add("type", "face"); result.Add("device", "江苏医保刷脸认证器(NationECCode.dll)"); result.Add("businessType", businessType); } else { result = JiangSuSocialCardBusiness.ReadFace(businessType); // 使用NationECCode.dll与泰和完全一致 // 转换为前端期望的格式 result = ConvertJiangSuFaceAuthToStandardFormat(result); } } else if (type.Equals("jiangsu_qrcode")) { // 江苏医保电子凭证解码(独立入口) string businessType = temps.Length > 2 ? temps[2] : "01101"; // 从URL参数获取业务类型(第3段) // 验证业务类型格式 if (!JiangSuSocialCardBusiness.IsValidBusinessType(businessType)) { result.Add("success", false); result.Add("code", 1001); result.Add("message", $"江苏医保电子凭证业务类型【{businessType}】无效!支持的业务类型:01101(门诊挂号)、01301(门诊结算)、02121(药店购药)等"); result.Add("type", "qrcode"); result.Add("device", "江苏医保电子凭证解码器"); } else { result = JiangSuSocialCardBusiness.ReadElectronicCertificate(businessType); // 转换为前端期望的格式 result = ConvertJiangSuElectronicCertificateToStandardFormat(result); } } else if (type.Equals("jiangsu")) { // 江苏医保社保卡读取 string action = temps.Length > 1 ? temps[1] : "readcard"; // 检查是否是业务代码格式(5位数字),如果是则默认为readcard操作 if (temps.Length > 1 && IsBusinessCode(temps[1])) { action = "readcard"; // 业务代码不影响江苏医保读卡操作,默认读卡 } if (action.Equals("init")) { result = JiangSuSocialCardBusiness.Initialize(); } else if (action.Equals("readcard")) { result = JiangSuSocialCardBusiness.ReadSocialCard(false); // 转换为前端期望的格式 result = ConvertJiangSuDataToStandardFormat(result); } else if (action.Equals("readcard_pin")) { result = JiangSuSocialCardBusiness.ReadSocialCard(true); result = ConvertJiangSuDataToStandardFormat(result); } else if (action.Equals("verifypin")) { result = JiangSuSocialCardBusiness.VerifyCardPIN(); } else if (action.Equals("changepin")) { result = JiangSuSocialCardBusiness.ChangeCardPIN(); } else if (action.Equals("status")) { result = JiangSuSocialCardBusiness.GetDeviceStatus(); } else if (action.Equals("reset")) { result = JiangSuSocialCardBusiness.ResetSystemState(); } else if (action.Equals("getpersoninfo")) { result = JiangSuSocialCardBusiness.GetPersonInfo("socialcard", ""); } else if (action.Equals("checkdll")) { result = JiangSuSocialCardBusiness.CheckDllExists(); } else if (action.Equals("qrcode") || action.Equals("ec")) { // 江苏医保电子凭证解码 string businessType = temps.Length > 2 ? temps[2] : "01101"; // 默认门诊挂号 // 验证业务类型格式 if (!JiangSuSocialCardBusiness.IsValidBusinessType(businessType)) { result.Add("success", false); result.Add("code", 1001); result.Add("message", $"江苏医保电子凭证业务类型【{businessType}】无效!支持的业务类型:01101(门诊挂号)、01301(门诊结算)、02121(药店购药)等"); result.Add("type", "qrcode"); result.Add("device", "江苏医保电子凭证解码器"); } else { result = JiangSuSocialCardBusiness.ReadElectronicCertificate(businessType); // 转换为前端期望的格式(电子凭证不需要特殊转换,保持原格式) result = ConvertJiangSuElectronicCertificateToStandardFormat(result); } } else { result.Add("success", false); result.Add("code", 1001); result.Add("message", "江苏医保操作【" + action + "】错误!允许的操作为【init、readcard、readcard_pin、verifypin、changepin、status、reset、getpersoninfo、checkdll、qrcode、ec】"); } } else { result.Add("code", 1001); result.Add("message", "读卡类别【" + type + "】错误!允许的类别为【sicard、idcard、idcard2、qrcode、qrcode2、face、huashi、jiangsu、jiangsu_qrcode、jiangsu_face】"); } return result; } /// /// 判断字符串是否是业务代码格式(5位数字) /// /// 要检查的字符串 /// 是否是业务代码 private bool IsBusinessCode(string code) { if (string.IsNullOrEmpty(code) || code.Length != 5) { return false; } // 检查是否是5位数字 foreach (char c in code) { if (!char.IsDigit(c)) { return false; } } return true; } /// /// 将华视读卡器的数据格式转换为前端期望的"^"分隔字符串格式 /// /// 华视读卡器返回的JSON数据 /// 转换后的标准格式 private JObject ConvertHuaShiDataToStandardFormat(JObject huashiResult) { JObject result = new JObject(); try { // 如果华视读卡器返回错误,直接返回错误信息 if ((int)huashiResult["code"] != 200) { return huashiResult; } // 提取华视读卡器返回的数据 var data = huashiResult["data"]; if (data == null) { result.Add("code", 1001); result.Add("message", "华视读卡器返回数据为空"); return result; } // 按照前端期望的顺序组装数据: // str1[0] = IDNumber (身份证号) // str1[1] = Name (姓名) // str1[2] = Sex (性别) // str1[3] = Nation (民族) // str1[4] = Birthday (生日) // str1[5] = Address (地址) // str1[6] = IDIssued (签发机关) // str1[7] = IssuedData + ValidDate (签发日期+有效期,连续) // str1[8] = Base64Photo (照片Base64) string idNumber = data["idCode"]?.ToString() ?? ""; string name = data["name"]?.ToString() ?? ""; string sex = data["sex"]?.ToString() ?? ""; string nation = data["nation"]?.ToString() ?? ""; string birthday = data["birthday"]?.ToString() ?? ""; string address = data["address"]?.ToString() ?? ""; string department = data["department"]?.ToString() ?? ""; string startDate = data["startDate"]?.ToString() ?? ""; string endDate = data["endDate"]?.ToString() ?? ""; // 组合签发日期和有效期 (前端会用substring分割) string dateInfo = startDate + endDate; // 获取照片的Base64数据 string base64Photo = data["photoBase64"]?.ToString() ?? ""; // 按照前端期望的顺序用"^"连接 string formattedData = string.Join("^", new string[] { idNumber, // [0] 身份证号 name, // [1] 姓名 sex, // [2] 性别 nation, // [3] 民族 birthday, // [4] 生日 address, // [5] 地址 department, // [6] 签发机关 dateInfo, // [7] 签发日期+有效期 base64Photo, // [8] 照片Base64 "", // [9] 预留空位,确保split后数组有足够元素 "" // [10] 预留空位 }); result.Add("code", 200); result.Add("message", "华视读卡器读取成功(已转换为标准格式)"); result.Add("data", formattedData); result.Add("device", "华视电子身份证读卡器"); result.Add("originalType", "huashi_idcard"); } catch (System.Exception ex) { result.Add("code", 1001); result.Add("message", $"华视数据格式转换异常: {ex.Message}"); } return result; } /// /// 将江苏医保社保卡的数据格式转换为前端期望的"^"分隔字符串格式 /// /// 江苏医保返回的JSON数据 /// 转换后的标准格式 private JObject ConvertJiangSuDataToStandardFormat(JObject jiangsuResult) { JObject result = new JObject(); try { // 如果江苏医保返回错误,直接返回错误信息 if (!(bool)jiangsuResult["success"] || (int)jiangsuResult["code"] != 200) { // 保持原有的success和code字段,但调整为前端期望的格式 result.Add("code", jiangsuResult["code"]); result.Add("message", jiangsuResult["message"]); result.Add("device", "江苏医保社保卡读卡器"); return result; } // 提取江苏医保返回的数据 var data = jiangsuResult["data"]; if (data == null) { result.Add("code", 1001); result.Add("message", "江苏医保返回数据为空"); return result; } // 按照前端期望的顺序组装社保卡数据: // str1[0] = CardNumber (社保卡号) // str1[1] = Name (姓名) // str1[2] = IDNumber (身份证号) // str1[3] = Sex (性别) // str1[4] = Nation (民族) // str1[5] = Birthday (生日) // str1[6] = Address (地址) // str1[7] = IssueDate (发卡日期) // str1[8] = ValidDate (有效期) // str1[9] = Department (发卡机构) // str1[10] = Photo (照片Base64) string cardNumber = data["cardNumber"]?.ToString() ?? ""; string name = data["name"]?.ToString() ?? ""; string idNumber = data["idNumber"]?.ToString() ?? ""; string sex = data["sex"]?.ToString() ?? ""; string nation = data["nation"]?.ToString() ?? ""; string birthday = data["birthday"]?.ToString() ?? ""; string address = data["address"]?.ToString() ?? ""; string issueDate = data["issueDate"]?.ToString() ?? ""; string validDate = data["validDate"]?.ToString() ?? ""; string department = data["department"]?.ToString() ?? ""; string photo = data["photo"]?.ToString() ?? ""; // 按照前端期望的顺序用"^"连接(社保卡格式) string formattedData = string.Join("^", new string[] { cardNumber, // [0] 社保卡号 name, // [1] 姓名 idNumber, // [2] 身份证号 sex, // [3] 性别 nation, // [4] 民族 birthday, // [5] 生日 address, // [6] 地址 issueDate, // [7] 发卡日期 validDate, // [8] 有效期 department, // [9] 发卡机构 photo, // [10] 照片Base64 "", // [11] 预留空位 "" // [12] 预留空位 }); result.Add("code", 200); result.Add("message", "江苏医保社保卡读取成功(已转换为标准格式)"); result.Add("data", formattedData); result.Add("device", "江苏医保社保卡读卡器"); result.Add("originalType", "jiangsu_socialcard"); } catch (System.Exception ex) { result.Add("code", 1001); result.Add("message", $"江苏医保数据格式转换异常: {ex.Message}"); } return result; } /// /// 将江苏医保电子凭证解码结果转换为前端期望的格式 /// /// 江苏医保电子凭证解码返回的JSON数据 /// 转换后的标准格式 private JObject ConvertJiangSuElectronicCertificateToStandardFormat(JObject jiangsuEcResult) { JObject result = new JObject(); try { // 如果江苏医保电子凭证解码返回错误,直接返回错误信息 if (!(bool)jiangsuEcResult["success"] || (int)jiangsuEcResult["code"] != 200) { // 保持原有的字段,确保前端能正确处理 result.Add("code", jiangsuEcResult["code"]); result.Add("message", jiangsuEcResult["message"]); result.Add("device", jiangsuEcResult["device"]); result.Add("type", jiangsuEcResult["type"]); // 添加其他可能的错误信息字段 if (jiangsuEcResult["jiangsuErrorCode"] != null) result.Add("jiangsuErrorCode", jiangsuEcResult["jiangsuErrorCode"]); if (jiangsuEcResult["jiangsuErrorMessage"] != null) result.Add("jiangsuErrorMessage", jiangsuEcResult["jiangsuErrorMessage"]); if (jiangsuEcResult["suggestion"] != null) result.Add("suggestion", jiangsuEcResult["suggestion"]); return result; } // 电子凭证解码成功,保持原格式返回(前端直接使用JSON格式) result.Add("code", jiangsuEcResult["code"]); result.Add("message", jiangsuEcResult["message"]); result.Add("device", jiangsuEcResult["device"]); result.Add("type", jiangsuEcResult["type"]); result.Add("originalType", jiangsuEcResult["originalType"]); result.Add("timestamp", jiangsuEcResult["timestamp"]); // 保持电子凭证的JSON数据格式(前端期望JSON而不是^分隔的字符串) result.Add("data", jiangsuEcResult["data"]); // 添加兼容字段:让前端可以统一处理 result.Add("readCardResult", jiangsuEcResult["readCardResult"]); // 保留江苏医保相关信息 if (jiangsuEcResult["businessType"] != null) result.Add("businessType", jiangsuEcResult["businessType"]); if (jiangsuEcResult["interfaceVersion"] != null) result.Add("interfaceVersion", jiangsuEcResult["interfaceVersion"]); if (jiangsuEcResult["jiangsuOriginalData"] != null) result.Add("jiangsuOriginalData", jiangsuEcResult["jiangsuOriginalData"]); } catch (System.Exception ex) { result.Add("code", 1001); result.Add("message", $"江苏医保电子凭证数据格式转换异常: {ex.Message}"); result.Add("device", "江苏医保电子凭证解码器"); result.Add("type", "qrcode"); result.Add("exception", ex.GetType().Name); } return result; } /// /// 将江苏医保刷脸授权结果转换为前端期望的格式 /// /// 江苏医保刷脸授权返回的JSON数据 /// 转换后的标准格式 private JObject ConvertJiangSuFaceAuthToStandardFormat(JObject jiangsuFaceAuthResult) { JObject result = new JObject(); try { // 刷脸授权直接返回原JSON格式,不需要转换为^分隔字符串 // 因为刷脸返回的是结构化的医保身份信息,前端期望JSON格式 result.Add("code", jiangsuFaceAuthResult["code"]); result.Add("message", jiangsuFaceAuthResult["message"]); result.Add("type", "face"); result.Add("data", jiangsuFaceAuthResult["data"]?.ToString()); result.Add("sign", jiangsuFaceAuthResult["sign"]); // 保留江苏医保特有字段 if (jiangsuFaceAuthResult["device"] != null) result.Add("device", jiangsuFaceAuthResult["device"]); if (jiangsuFaceAuthResult["businessType"] != null) result.Add("businessType", jiangsuFaceAuthResult["businessType"]); if (jiangsuFaceAuthResult["timestamp"] != null) result.Add("timestamp", jiangsuFaceAuthResult["timestamp"]); if (jiangsuFaceAuthResult["authNo"] != null) result.Add("authNo", jiangsuFaceAuthResult["authNo"]); if (jiangsuFaceAuthResult["interfaceVersion"] != null) result.Add("interfaceVersion", jiangsuFaceAuthResult["interfaceVersion"]); } catch (System.Exception ex) { result.Add("code", 1001); result.Add("message", $"江苏医保刷脸授权数据格式转换异常: {ex.Message}"); result.Add("device", "江苏医保刷脸认证器"); result.Add("type", "face"); result.Add("exception", ex.GetType().Name); } return result; } /// /// 江苏工伤联网结算接口 - POST方式 /// POST /api/entry/workinjury /// [HttpPost] [Route("api/entry/workinjury")] [System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptions] [System.Security.SecurityCritical] public JObject PostWorkInjury([FromBody] JObject request) { // 立即创建基础响应,确保总是有返回值 JObject result = new JObject(); result["debug_entry_time"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); result["debug_method_entered"] = true; result["debug_stage"] = "方法入口"; try { // 添加调试信息确保响应不为空 result["debug_timestamp"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); result["debug_step"] = "Controller方法开始执行"; result["debug_stage"] = "开始处理"; // 检查请求体 if (request == null) { result["success"] = false; result["code"] = 1001; result["message"] = "请求体不能为空"; result["device"] = "江苏工伤联网接口"; result["debug_step"] = "请求体为空"; // 添加传入参数(即使是null) result["request_parameters"] = null; return result; } result["debug_step"] = "请求体检查通过"; result["debug_request"] = request; // 获取操作类型 string action = request["action"]?.ToString()?.ToLower() ?? ""; result["debug_action"] = action; result["debug_step"] = "解析action参数"; if (string.IsNullOrEmpty(action)) { result["success"] = false; result["code"] = 1002; result["message"] = "action参数不能为空"; result["device"] = "江苏工伤联网接口"; result["debug_step"] = "action参数为空"; // 添加传入参数 result["request_parameters"] = request; return result; } result["debug_step"] = $"准备执行action: {action}"; // 根据操作类型处理不同的请求 switch (action) { case "init": result["debug_step"] = "执行初始化操作"; result["debug_stage"] = "准备调用HandleWorkInjuryInit"; // 添加立即返回测试 - 如果问题在DLL调用,我们先确保基础流程正常 if (request["test_mode"] != null && request["test_mode"].ToString() == "immediate_return") { result["success"] = true; result["code"] = 200; result["message"] = "测试模式:立即返回成功"; result["device"] = "江苏工伤联网接口"; result["debug_step"] = "测试模式立即返回"; result["test_config"] = request["config"]; // 添加传入参数 result["request_parameters"] = request; break; } try { var initResult = HandleWorkInjuryInit(request); if (initResult != null) { // 替换为HandleWorkInjuryInit的结果并保留debug信息 PreserveDebugInfo(result, initResult); result = initResult; result["debug_controller_step"] = "初始化操作完成"; // 添加传入参数 result["request_parameters"] = request; } else { result["success"] = false; result["code"] = 1007; result["message"] = "HandleWorkInjuryInit返回null"; result["device"] = "江苏工伤联网接口"; result["debug_step"] = "HandleWorkInjuryInit返回null"; // 添加传入参数 result["request_parameters"] = request; } } catch (System.Exception initEx) { result["success"] = false; result["code"] = 1008; result["message"] = $"调用HandleWorkInjuryInit异常: {initEx.Message}"; result["device"] = "江苏工伤联网接口"; result["debug_step"] = "HandleWorkInjuryInit调用异常"; result["debug_error"] = initEx.Message; // 添加传入参数 result["request_parameters"] = request; } break; case "transaction": result["debug_step"] = "执行交易操作"; var transactionResult = HandleWorkInjuryTransaction(request); if (transactionResult != null) { // 替换为HandleWorkInjuryTransaction的结果并保留debug信息 PreserveDebugInfo(result, transactionResult); result = transactionResult; result["debug_controller_step"] = "交易操作完成"; // 添加传入参数 result["request_parameters"] = request; } break; case "health": result["debug_step"] = "执行健康检查"; var healthResult = JiangSuWorkInjuryBusiness.HealthCheck(); if (healthResult != null) { PreserveDebugInfo(result, healthResult); result = healthResult; result["debug_controller_step"] = "健康检查完成"; // 添加传入参数 result["request_parameters"] = request; } break; case "stats": var statsResult = JiangSuWorkInjuryBusiness.GetTransactionStatistics(); if (statsResult != null) { PreserveDebugInfo(result, statsResult); result = statsResult; // 添加传入参数 result["request_parameters"] = request; } break; case "signin_status": var signinStatusResult = JiangSuWorkInjuryBusiness.GetSignInStatus(); if (signinStatusResult != null) { PreserveDebugInfo(result, signinStatusResult); result = signinStatusResult; // 添加传入参数 result["request_parameters"] = request; } break; case "batch_upload": var batchUploadResult = HandleWorkInjuryBatchUpload(request); if (batchUploadResult != null) { PreserveDebugInfo(result, batchUploadResult); result = batchUploadResult; // 添加传入参数 result["request_parameters"] = request; } break; case "complete_process": var completeProcessResult = HandleWorkInjuryCompleteProcess(request); if (completeProcessResult != null) { PreserveDebugInfo(result, completeProcessResult); result = completeProcessResult; // 添加传入参数 result["request_parameters"] = request; } break; case "retry": var retryResult = HandleWorkInjuryRetry(request); if (retryResult != null) { PreserveDebugInfo(result, retryResult); result = retryResult; // 添加传入参数 result["request_parameters"] = request; } break; case "reverse": var reverseResult = HandleWorkInjuryReverse(request); if (reverseResult != null) { PreserveDebugInfo(result, reverseResult); result = reverseResult; // 添加传入参数 result["request_parameters"] = request; } break; default: result["success"] = false; result["code"] = 1003; result["message"] = $"不支持的操作类型: {action}"; result["device"] = "江苏工伤联网接口"; result["supportedActions"] = "init, transaction, health, stats, signin_status, batch_upload, complete_process, retry, reverse"; // 添加传入参数 result["request_parameters"] = request; break; } } catch (System.AccessViolationException avEx) { // 保存原有的debug信息 string debugEntryTime = result["debug_entry_time"]?.ToString(); bool? debugMethodEntered = result["debug_method_entered"]?.ToObject(); string debugStage = result["debug_stage"]?.ToString(); string debugTimestamp = result["debug_timestamp"]?.ToString(); string debugStep = result["debug_step"]?.ToString(); JToken debugRequest = result["debug_request"]; string debugAction = result["debug_action"]?.ToString(); result = new JObject(); // 重新创建以确保不为空 result["success"] = false; result["code"] = 9001; result["message"] = "DLL访问冲突异常 - 可能是JSSiInterface.dll问题"; result["device"] = "江苏工伤联网接口"; result["exception"] = "AccessViolationException"; result["debug_step"] = "Controller捕获访问冲突异常"; result["debug_error"] = avEx.Message; result["debug_stacktrace"] = avEx.StackTrace; // 恢复原有的debug信息 if (!string.IsNullOrEmpty(debugEntryTime)) result["debug_entry_time"] = debugEntryTime; if (debugMethodEntered.HasValue) result["debug_method_entered"] = debugMethodEntered.Value; if (!string.IsNullOrEmpty(debugStage)) result["debug_stage"] = debugStage; if (!string.IsNullOrEmpty(debugTimestamp)) result["debug_timestamp"] = debugTimestamp; if (debugRequest != null) result["debug_request"] = debugRequest; if (!string.IsNullOrEmpty(debugAction)) result["debug_action"] = debugAction; // 添加传入参数 result["request_parameters"] = request; } catch (System.Runtime.InteropServices.SEHException sehEx) { // 保存原有的debug信息 string debugEntryTime = result["debug_entry_time"]?.ToString(); bool? debugMethodEntered = result["debug_method_entered"]?.ToObject(); string debugStage = result["debug_stage"]?.ToString(); string debugTimestamp = result["debug_timestamp"]?.ToString(); string debugStep = result["debug_step"]?.ToString(); JToken debugRequest = result["debug_request"]; string debugAction = result["debug_action"]?.ToString(); result = new JObject(); // 重新创建以确保不为空 result["success"] = false; result["code"] = 9002; result["message"] = "结构化异常处理错误 - DLL调用失败"; result["device"] = "江苏工伤联网接口"; result["exception"] = "SEHException"; result["debug_step"] = "Controller捕获SEH异常"; result["debug_error"] = sehEx.Message; result["debug_stacktrace"] = sehEx.StackTrace; // 恢复原有的debug信息 if (!string.IsNullOrEmpty(debugEntryTime)) result["debug_entry_time"] = debugEntryTime; if (debugMethodEntered.HasValue) result["debug_method_entered"] = debugMethodEntered.Value; if (!string.IsNullOrEmpty(debugStage)) result["debug_stage"] = debugStage; if (!string.IsNullOrEmpty(debugTimestamp)) result["debug_timestamp"] = debugTimestamp; if (debugRequest != null) result["debug_request"] = debugRequest; if (!string.IsNullOrEmpty(debugAction)) result["debug_action"] = debugAction; // 添加传入参数 result["request_parameters"] = request; } catch (System.Exception ex) { // 保存原有的debug信息 string debugEntryTime = result["debug_entry_time"]?.ToString(); bool? debugMethodEntered = result["debug_method_entered"]?.ToObject(); string debugStage = result["debug_stage"]?.ToString(); string debugTimestamp = result["debug_timestamp"]?.ToString(); string debugStep = result["debug_step"]?.ToString(); JToken debugRequest = result["debug_request"]; string debugAction = result["debug_action"]?.ToString(); result = new JObject(); // 重新创建以确保不为空 result["success"] = false; result["code"] = 1000; result["message"] = $"工伤联网接口处理异常: {ex.Message}"; result["device"] = "江苏工伤联网接口"; result["exception"] = ex.GetType().Name; result["debug_step"] = "Controller捕获一般异常"; result["debug_error"] = ex.Message; result["debug_stacktrace"] = ex.StackTrace; result["debug_exception_details"] = ex.ToString(); // 恢复原有的debug信息 if (!string.IsNullOrEmpty(debugEntryTime)) result["debug_entry_time"] = debugEntryTime; if (debugMethodEntered.HasValue) result["debug_method_entered"] = debugMethodEntered.Value; if (!string.IsNullOrEmpty(debugStage)) result["debug_stage"] = debugStage; if (!string.IsNullOrEmpty(debugTimestamp)) result["debug_timestamp"] = debugTimestamp; if (debugRequest != null) result["debug_request"] = debugRequest; if (!string.IsNullOrEmpty(debugAction)) result["debug_action"] = debugAction; // 添加传入参数 result["request_parameters"] = request; } // 确保返回的result不为null并且有基本结构 if (result == null) { result = new JObject(); result["success"] = false; result["code"] = 9999; result["message"] = "未知错误 - result为null"; result["device"] = "江苏工伤联网接口"; result["debug_step"] = "result为null保护"; // 添加传入参数 result["request_parameters"] = request; } // 强制确保必要字段存在 if (result["success"] == null) { result["success"] = false; result["debug_forced_success"] = "强制添加success字段"; } if (result["code"] == null) { result["code"] = 8000; result["debug_forced_code"] = "强制添加code字段"; } if (result["device"] == null) { result["device"] = "江苏工伤联网接口"; result["debug_forced_device"] = "强制添加device字段"; } // 确保有时间戳 if (result["debug_timestamp"] == null) { result["debug_final_timestamp"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); } // 添加最终保护标记 result["debug_method_exit"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); result["debug_return_confirmed"] = true; return result; } /// /// 处理工伤联网初始化请求 /// private JObject HandleWorkInjuryInit(JObject request) { // 立即创建基础响应,确保总是有返回值 JObject result = new JObject(); result["debug_handler_entry"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); result["debug_handler_stage"] = "HandleWorkInjuryInit进入"; try { result["debug_handler_stage"] = "解析配置参数"; var configObj = request["config"]; JObject initResult = null; if (configObj != null) { result["debug_handler_stage"] = "使用自定义配置"; // 使用自定义配置 var config = new JiangSuWorkInjuryBusiness.WorkInjuryConfig { FixmedinsCode = configObj["fixmedinsCode"]?.ToString() ?? "SQ201348", FixmedinsName = configObj["fixmedinsName"]?.ToString() ?? "沭阳铭和医院", ReceiverSysCode = configObj["receiverSysCode"]?.ToString() ?? "JSYTH", InterfaceVersion = configObj["interfaceVersion"]?.ToString() ?? "V2.1", OperatorType = configObj["operatorType"]?.ToString() ?? "1", DefaultOperator = configObj["defaultOperator"]?.ToString() ?? "001", DefaultOperatorName = configObj["defaultOperatorName"]?.ToString() ?? "系统管理员", LogPath = configObj["logPath"]?.ToString() ?? "logs/workinjury/" }; result["debug_handler_stage"] = "准备调用Initialize(config)"; initResult = JiangSuWorkInjuryBusiness.Initialize(config); } else { result["debug_handler_stage"] = "使用默认配置"; initResult = JiangSuWorkInjuryBusiness.Initialize(); } result["debug_handler_stage"] = "Initialize调用完成"; // 确保返回的结果不为空 if (initResult == null) { result["success"] = false; result["code"] = 1005; result["message"] = "初始化返回结果为null"; result["device"] = "江苏工伤联网接口"; result["debug_handler_stage"] = "初始化结果为null"; return result; } // 将调试信息合并到初始化结果中 initResult["debug_handler_complete"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); initResult["debug_handler_stage"] = "成功返回"; return initResult; } catch (System.AccessViolationException avEx) { result["success"] = false; result["code"] = 1006; result["message"] = $"工伤联网初始化访问冲突异常: {avEx.Message}"; result["device"] = "江苏工伤联网接口"; result["exception"] = "AccessViolationException"; result["debug_handler_stage"] = "HandleWorkInjuryInit捕获访问冲突"; result["debug_error"] = avEx.Message; return result; } catch (System.Exception ex) { result["success"] = false; result["code"] = 1004; result["message"] = $"工伤联网初始化处理异常: {ex.Message}"; result["device"] = "江苏工伤联网接口"; result["exception"] = ex.GetType().Name; result["debug_handler_stage"] = "HandleWorkInjuryInit捕获一般异常"; result["debug_error"] = ex.Message; result["debug_stacktrace"] = ex.StackTrace; return result; } } /// /// 处理工伤联网交易请求 /// private JObject HandleWorkInjuryTransaction(JObject request) { try { string transactionName = request["transactionName"]?.ToString() ?? ""; if (string.IsNullOrEmpty(transactionName)) { var result = new JObject(); result["success"] = false; result["code"] = 1005; result["message"] = "transactionName参数不能为空"; result["device"] = "江苏工伤联网接口"; return result; } object businessParams = request["businessParams"]?.ToObject(); string identifyMode = request["identifyMode"]?.ToString() ?? "1"; string qrCodeInfo = request["qrCodeInfo"]?.ToString() ?? ""; string operatorId = request["operatorId"]?.ToString() ?? ""; string operatorName = request["operatorName"]?.ToString() ?? ""; return JiangSuWorkInjuryBusiness.ProcessWorkInjuryTransaction( transactionName, businessParams, identifyMode, qrCodeInfo, operatorId, operatorName); } catch (System.Exception ex) { var result = new JObject(); result["success"] = false; result["code"] = 1006; result["message"] = $"工伤联网交易处理异常: {ex.Message}"; result["device"] = "江苏工伤联网接口"; return result; } } /// /// 处理工伤联网批量上传请求 /// private JObject HandleWorkInjuryBatchUpload(JObject request) { try { var prescriptionsArray = request["prescriptions"]?.ToObject(); if (prescriptionsArray == null || prescriptionsArray.Length == 0) { var result = new JObject(); result["success"] = false; result["code"] = 1007; result["message"] = "prescriptions参数不能为空"; result["device"] = "江苏工伤联网接口"; return result; } string patientId = request["patientId"]?.ToString() ?? ""; string visitNo = request["visitNo"]?.ToString() ?? ""; return JiangSuWorkInjuryBusiness.BatchUploadPrescriptions(prescriptionsArray, patientId, visitNo); } catch (System.Exception ex) { var result = new JObject(); result["success"] = false; result["code"] = 1008; result["message"] = $"工伤联网批量上传处理异常: {ex.Message}"; result["device"] = "江苏工伤联网接口"; return result; } } /// /// 处理工伤联网完整流程请求 /// private JObject HandleWorkInjuryCompleteProcess(JObject request) { try { object patientInfo = request["patientInfo"]?.ToObject(); object[] feeDetails = request["feeDetails"]?.ToObject(); return JiangSuWorkInjuryBusiness.CompleteWorkInjuryProcess(patientInfo, feeDetails); } catch (System.Exception ex) { var result = new JObject(); result["success"] = false; result["code"] = 1009; result["message"] = $"工伤联网完整流程处理异常: {ex.Message}"; result["device"] = "江苏工伤联网接口"; return result; } } /// /// 处理工伤联网智能重试请求 /// private JObject HandleWorkInjuryRetry(JObject request) { try { string transactionName = request["transactionName"]?.ToString() ?? ""; if (string.IsNullOrEmpty(transactionName)) { var result = new JObject(); result["success"] = false; result["code"] = 1010; result["message"] = "transactionName参数不能为空"; result["device"] = "江苏工伤联网接口"; return result; } object businessParams = request["businessParams"]?.ToObject(); int maxRetries = request["maxRetries"]?.ToObject() ?? 3; int baseDelayMs = request["baseDelayMs"]?.ToObject() ?? 1000; return JiangSuWorkInjuryBusiness.SmartRetryTransaction(transactionName, businessParams, maxRetries, baseDelayMs); } catch (System.Exception ex) { var result = new JObject(); result["success"] = false; result["code"] = 1011; result["message"] = $"工伤联网智能重试处理异常: {ex.Message}"; result["device"] = "江苏工伤联网接口"; return result; } } /// /// 处理工伤联网冲正请求 /// private JObject HandleWorkInjuryReverse(JObject request) { try { string originalMessageId = request["originalMessageId"]?.ToString() ?? ""; if (string.IsNullOrEmpty(originalMessageId)) { var result = new JObject(); result["success"] = false; result["code"] = 1012; result["message"] = "originalMessageId参数不能为空"; result["device"] = "江苏工伤联网接口"; return result; } return JiangSuWorkInjuryBusiness.ReverseTransactionByRecord(originalMessageId); } catch (System.Exception ex) { var result = new JObject(); result["success"] = false; result["code"] = 1013; result["message"] = $"工伤联网冲正处理异常: {ex.Message}"; result["device"] = "江苏工伤联网接口"; return result; } } /// /// 保留调试信息到结果中 /// private void PreserveDebugInfo(JObject target, JObject source) { if (source["debug_entry_time"] != null) target["debug_entry_time"] = source["debug_entry_time"]; if (source["debug_method_entered"] != null) target["debug_method_entered"] = source["debug_method_entered"]; if (source["debug_stage"] != null) target["debug_stage"] = source["debug_stage"]; if (source["debug_timestamp"] != null) target["debug_timestamp"] = source["debug_timestamp"]; if (source["debug_step"] != null) target["debug_step"] = source["debug_step"]; if (source["debug_request"] != null) target["debug_request"] = source["debug_request"]; if (source["debug_action"] != null) target["debug_action"] = source["debug_action"]; if (source["debug_error"] != null) target["debug_error"] = source["debug_error"]; if (source["debug_stacktrace"] != null) target["debug_stacktrace"] = source["debug_stacktrace"]; if (source["debug_exception_details"] != null) target["debug_exception_details"] = source["debug_exception_details"]; if (source["debug_controller_step"] != null) target["debug_controller_step"] = source["debug_controller_step"]; if (source["debug_handler_entry"] != null) target["debug_handler_entry"] = source["debug_handler_entry"]; if (source["debug_handler_stage"] != null) target["debug_handler_stage"] = source["debug_handler_stage"]; if (source["debug_handler_complete"] != null) target["debug_handler_complete"] = source["debug_handler_complete"]; if (source["test_config"] != null) target["test_config"] = source["test_config"]; } } }