fg_yida_2/YiDa_WinForm/Helper/AppConfigHelper.cs

127 lines
4.0 KiB
C#
Raw Normal View History

2026-01-29 20:29:12 +08:00
using System.Collections.Specialized;
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)
{
return _yiDaConfigCollection[key] ?? string.Empty;
}
#endregion
}
}