AppConfig.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using System;
  2. using System.Configuration;
  3. namespace BeiJingComService.Infrastructure
  4. {
  5. public static class AppConfig
  6. {
  7. // ===== HTTP 通道 =====
  8. public static int HttpPort => GetInt("HttpPort", 5001);
  9. public static int MaxRequestSize => GetInt("MaxRequestSize", 4194304);
  10. public static string AccessToken => Get("AccessToken", "");
  11. public static string CorsAllowOrigin => Get("CorsAllowOrigin", "*");
  12. // ===== 本地 COM(首信医保 MedicareCom.Outpatient) =====
  13. /// <summary>COM 组件 ProgID,首信医保为 MedicareCom.Outpatient。</summary>
  14. public static string ComProgId => Get("ComProgId", "MedicareCom.Outpatient");
  15. /// <summary>次要 COM 组件 ProgID(中药订单共享 EachCommunicationCom.Outpatient);留空表示不启用。</summary>
  16. public static string ComProgIdSecondary => Get("ComProgIdSecondary", "");
  17. /// <summary>单笔 COM 调用超时(毫秒)。</summary>
  18. public static int ComTimeoutMs => GetInt("ComTimeoutMs", 60000);
  19. /// <summary>
  20. /// COM 会话空闲关阈值(毫秒):队列空闲超过该时间即主动 close,实现"用完即关"。
  21. /// 阈值越小读卡器释放越及时;小于该阈值的连续读卡仍复用会话(更快)。
  22. /// </summary>
  23. public static int ComIdleTimeoutMs => GetInt("ComIdleTimeoutMs", 2000);
  24. /// <summary>
  25. /// Round59:未插卡(0xfffffffe)返回耗时超过该值(毫秒)即判定读卡器亚健康,主动关闭会话。
  26. /// 默认 2000,0 = 禁用(回到只有空闲关闭的老逻辑)。
  27. /// 依据:三天日志 18 次未插卡中,返回 &lt;1 秒的 10 次全部平安,
  28. /// 返回 &gt;3 秒的 7 次里 2 次紧接 f8、3 次导致 close 失败。
  29. /// </summary>
  30. public static int NoCardSlowCloseMs => GetInt("NoCardSlowCloseMs", 2000);
  31. // ===== COM 弹框置顶(Round57) =====
  32. /// <summary>
  33. /// 总开关。false = 不装 CBT 钩子、不起看门狗,完全恢复 1.0.0.3 的弹框行为(一级回滚开关)。
  34. /// </summary>
  35. public static bool ComDialogTopmost => GetBool("ComDialogTopmost", true);
  36. /// <summary>看门狗轮询间隔(毫秒);0 = 只留钩子、不启用看门狗。</summary>
  37. public static int ComDialogTopmostWatchMs => GetInt("ComDialogTopmostWatchMs", 200);
  38. /// <summary>
  39. /// true = 弹框出现时额外抢前台焦点(AttachThreadInput + SetForegroundWindow)。
  40. /// 默认关:抢焦点会打断操作员正在输入的窗口;置顶本身不需要焦点。
  41. /// </summary>
  42. public static bool ComDialogForeground => GetBool("ComDialogForeground", false);
  43. /// <summary>
  44. /// 只置顶这些窗口类名(逗号分隔),留空 = 不限类名(推荐)。
  45. /// 仅作现场排障的收窄手段;不要默认填 "#32770"(VB6 窗体类名不是它)。
  46. /// </summary>
  47. public static string ComDialogTopmostClasses => Get("ComDialogTopmostClasses", "");
  48. // ===== 扫码预捕获(Round54) =====
  49. /// <summary>
  50. /// 总开关。false 时完全不装低级键盘钩子;/api/scan/take 仍可访问但恒返回未命中。
  51. /// 现场遇到安全软件拦截钩子时改这里,重启生效。
  52. /// </summary>
  53. public static bool ScanCaptureEnabled => GetBool("ScanCaptureEnabled", true);
  54. /// <summary>
  55. /// 捕获码的存活时间(毫秒)。业务侧取用时超过该时间视为过期。
  56. /// 等待弹框实测最长约 1 秒,默认 5000 已留 5 倍余量;建议 3000~8000,不要超过 10000。
  57. /// </summary>
  58. public static int ScanCaptureTtlMs => GetInt("ScanCaptureTtlMs", 5000);
  59. /// <summary>
  60. /// 判定「这是扫码枪」的主判据:整串从首字符到回车的最大总耗时(毫秒)。
  61. /// 人工敲 18 位身份证最快也要 1.5 秒以上,300ms 足以区分。
  62. /// </summary>
  63. public static int ScanCaptureMaxDurationMs => GetInt("ScanCaptureMaxDurationMs", 300);
  64. /// <summary>判定:相邻字符最大间隔(毫秒),超过即清空重来。</summary>
  65. public static int ScanCaptureMaxKeyGapMs => GetInt("ScanCaptureMaxKeyGapMs", 50);
  66. /// <summary>判定:有效串最小长度。</summary>
  67. public static int ScanCaptureMinLength => GetInt("ScanCaptureMinLength", 8);
  68. // ===== 宿主 =====
  69. public static string ServiceName => Get("ServiceName", "BeiJingComService");
  70. public static bool AutoStart => GetBool("AutoStart", true);
  71. // ===== 日志 =====
  72. public static string LogDirectory => Get("LogDirectory", "Log");
  73. public static int LogRetainDays => GetInt("LogRetainDays", 30);
  74. // ===== 调用原文日志 =====
  75. /// <summary>是否把每次调用的入参/出参原文写入 yyyy-MM-dd_Trade.log。关闭后仍保留摘要日志。</summary>
  76. public static bool TradeLogEnabled => GetBool("TradeLogEnabled", true);
  77. /// <summary>原文落盘前是否按字段名脱敏(身份证、卡号、姓名、手机号等)。正式环境建议保持 true。</summary>
  78. public static bool TradeLogMaskSensitive => GetBool("TradeLogMaskSensitive", true);
  79. /// <summary>需脱敏的字段名,逗号分隔,不区分大小写。配置变更需重启程序生效。</summary>
  80. public static string TradeLogSensitiveFields => Get("TradeLogSensitiveFields", DEFAULT_SENSITIVE_FIELDS);
  81. /// <summary>单条原文最多落盘的字符数,0 表示不截断。</summary>
  82. public static int TradeLogMaxChars => GetInt("TradeLogMaxChars", 0);
  83. private const string DEFAULT_SENSITIVE_FIELDS =
  84. "xm,brxm,czyxm,name,sfzh,zjhm,idcard,identity,grbh,ylzh,sbzh,kh,cardno,card_no,sjhm,lxdh,phone,mobile,csrq,birthday,dz,address";
  85. private static string Get(string key, string defaultValue)
  86. {
  87. var value = ConfigurationManager.AppSettings[key];
  88. return string.IsNullOrWhiteSpace(value) ? defaultValue : value;
  89. }
  90. private static int GetInt(string key, int defaultValue)
  91. {
  92. var value = ConfigurationManager.AppSettings[key];
  93. if (string.IsNullOrWhiteSpace(value)) return defaultValue;
  94. if (!int.TryParse(value, out int result))
  95. throw new ConfigurationErrorsException($"AppSettings 配置项 {key} 不是有效的整数: {value}");
  96. return result;
  97. }
  98. private static bool GetBool(string key, bool defaultValue)
  99. {
  100. var value = ConfigurationManager.AppSettings[key];
  101. if (string.IsNullOrWhiteSpace(value)) return defaultValue;
  102. if (!bool.TryParse(value, out bool result))
  103. throw new ConfigurationErrorsException($"AppSettings 配置项 {key} 不是有效的布尔值: {value}");
  104. return result;
  105. }
  106. }
  107. }