134 lines
4.2 KiB
C#
134 lines
4.2 KiB
C#
using System.Collections.Specialized;
|
||
using YiDa_WinForm;
|
||
|
||
namespace MQTT_WinformV1
|
||
{
|
||
using System.Configuration;
|
||
|
||
/// <summary>
|
||
/// 统一配置读取辅助类(
|
||
/// </summary>
|
||
public class AppConfigHelper
|
||
{
|
||
#region 私有字段(宜搭配置集合,仅初始化一次)
|
||
|
||
private static readonly NameValueCollection _yiDaConfigCollection;
|
||
|
||
#endregion
|
||
|
||
#region 静态构造函数(程序启动时执行,加载所有配置,仅执行一次)
|
||
|
||
static AppConfigHelper()
|
||
{
|
||
// 初始化宜搭配置集合
|
||
_yiDaConfigCollection = (NameValueCollection)ConfigurationManager.GetSection("YiDaConfig");
|
||
if (_yiDaConfigCollection == null)
|
||
{
|
||
_yiDaConfigCollection = new NameValueCollection();
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 数据库配置(MySQL 连接字符串)
|
||
|
||
/// <summary>
|
||
/// 获取 MySQL 连接字符串(对应 App.config 中的 MySqlConnection)
|
||
/// </summary>
|
||
public static string MySqlConnectionString
|
||
{
|
||
get
|
||
{
|
||
ConnectionStringSettings connectionSettings = ConfigurationManager.ConnectionStrings["MySqlConnection"];
|
||
|
||
// 容错处理:数据库配置缺失抛出明确异常,影响核心功能需提醒
|
||
if (connectionSettings == null || string.IsNullOrEmpty(connectionSettings.ConnectionString))
|
||
{
|
||
throw new ConfigurationErrorsException("App.config 中未配置有效的 MySqlConnection 连接字符串");
|
||
}
|
||
|
||
return connectionSettings.ConnectionString;
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 钉钉配置
|
||
|
||
/// <summary>
|
||
/// 钉钉 BaseAddress 地址
|
||
/// </summary>
|
||
public static string DingDing_BaseAddress => GetYiDaConfigValue("DingDingRobotWebHook");
|
||
|
||
/// <summary>
|
||
/// 钉钉机器人 WebHook 地址
|
||
/// </summary>
|
||
public static string DingDing_RobotWebHook => GetYiDaConfigValue("DingDingRobotWebHook");
|
||
|
||
/// <summary>
|
||
/// 钉钉机器人加签 Secret
|
||
/// </summary>
|
||
public static string DingDing_RobotSecret => GetYiDaConfigValue("DingDingRobotSecret");
|
||
|
||
#endregion
|
||
|
||
#region 宜搭配置(所有宜搭相关配置项)
|
||
|
||
/// <summary>
|
||
/// 宜搭 TokenAppKey
|
||
/// </summary>
|
||
public static string YiDa_TokenAppKey => GetYiDaConfigValue("TokenAppKey");
|
||
|
||
/// <summary>
|
||
/// 宜搭 TokenAppSecret
|
||
/// </summary>
|
||
public static string YiDa_TokenAppSecret => GetYiDaConfigValue("TokenAppSecret");
|
||
|
||
/// <summary>
|
||
/// 宜搭 AppType
|
||
/// </summary>
|
||
public static string YiDa_AppType => GetYiDaConfigValue("AppType");
|
||
|
||
/// <summary>
|
||
/// 宜搭 SystemToken
|
||
/// </summary>
|
||
public static string YiDa_SystemToken => GetYiDaConfigValue("SystemToken");
|
||
|
||
/// <summary>
|
||
/// 宜搭 UserId
|
||
/// </summary>
|
||
public static string YiDa_UserId => GetYiDaConfigValue("UserId");
|
||
|
||
/// <summary>
|
||
/// 宜搭 FormUuid
|
||
/// </summary>
|
||
public static string YiDa_FormUuid => GetYiDaConfigValue("FormUuid");
|
||
|
||
/// <summary>
|
||
/// 宜搭 ProcessCode
|
||
/// </summary>
|
||
public static string YiDa_ProcessCode => GetYiDaConfigValue("ProcessCode");
|
||
|
||
#endregion
|
||
|
||
#region 私有辅助方法(宜搭配置通用读取)
|
||
|
||
/// <summary>
|
||
/// 通用宜搭配置值读取方法(带容错,配置项不存在返回空字符串)
|
||
/// </summary>
|
||
/// <param name="key">配置项的 key</param>
|
||
/// <returns>配置项的值,不存在返回空字符串</returns>
|
||
private static string GetYiDaConfigValue(string key)
|
||
{
|
||
string value = _yiDaConfigCollection[key];
|
||
if (string.IsNullOrEmpty(value))
|
||
{
|
||
LogHelper.AppendLog($"警告:宜搭配置项 {key} 未配置");
|
||
return string.Empty;
|
||
}
|
||
return value;
|
||
}
|
||
|
||
#endregion
|
||
}
|
||
} |