using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; using NPOI.SS.Formula.Functions; using System.Collections.Concurrent; using System.Data; using System.Text; using Yahoo.Yui.Compressor; namespace WMS.Util { public static class ConfigHelper { /// /// 缓存数据(配置信息) /// private static readonly ConcurrentDictionary _setting = new ConcurrentDictionary(); /// /// 缓存数据 /// private static readonly ConcurrentDictionary _cache = new ConcurrentDictionary(); private static IConfiguration configuration; /// /// 获取配置信息 /// /// 数据类型 /// 键 /// public static T GetAppSettings(string key) where T : class, new() { if (configuration == null) { string fileName = "appsettings.json"; if (GetValue("env") == "dev") { fileName = "appsettings.Development.json"; } var directory = ConfigHelper.GetValue("baseDir"); var filePath = $"{directory}/{fileName}"; var builder = new ConfigurationBuilder(); builder.AddJsonFile(filePath, false, true); configuration = builder.Build(); } // 获取bin目录路径 //if (!File.Exists(filePath)) //{ // var length = directory.IndexOf("/bin", StringComparison.Ordinal); // filePath = $"{directory.Substring(0, length)}/{fileName}"; //} //IConfiguration configuration; var appconfig = new ServiceCollection() .AddOptions() .Configure(configuration.GetSection(key)) .BuildServiceProvider() .GetService>() .Value; return appconfig; } /// /// 获取配置信息 /// /// public static ServerOp GetConfig() { return GetAppSettings("ServerOp"); } public static RedisConfigs GetRedisConfigs() { return GetAppSettings("RedisConfigs"); } public static ConnectionConfigs GetConnectionConfigs() { return GetAppSettings("ConnectionConfigs"); } public static ConnectionConfigs GetQuestDBConnectionConfigs() { return GetAppSettings("QuestDBConnectionConfigs"); } public static WCSRedis GetWCSRedis() { return GetAppSettings("WCSRedis"); } public static DeviceEffectiveUrls GetDeviceEffectiveUrls() { return GetAppSettings("DeviceEffectiveUrls"); } /// /// 设置信息 /// /// 键 /// 值 public static void SetValue(string key, object value) { _setting.GetOrAdd(key, value); } /// /// 获取数据值 /// /// 类型 /// 键值 /// public static T GetValue(string key) where T : class { _setting.TryGetValue(key, out object result); return result as T; } /// /// 设置信息 /// /// 键 /// 值 public static void SetCache(string key, string value) { _cache.GetOrAdd(key, value); } /// /// 获取数据值 /// /// 键值 /// public static string GetCache(string key) { _cache.TryGetValue(key, out string result); return result; } } public class ServerOp { /// /// 软件名称 /// public string SoftName { get; set; } /// /// 软件版本号 /// public string Version { get; set; } /// /// 数据库连接 /// public string dbConn { get; set; } /// /// 数据库类型 SqlServer,Oracle,MySql /// public string dbType { get; set; } /// /// 缓存前缀 /// public string RedisPrev { get; set; } /// /// 缓存地址 /// public string RedisExchangeHosts { get; set; } /// /// 服务目录 /// public string VirtualPath { get; set; } /// /// 皮肤配置 /// public string UItheme { get; set; } /// /// IM地址 /// public string IMUrl { get; set; } /// /// 即时服务是否打开 /// public bool IMOpen { get; set; } /// /// 发出邮箱设置邮箱主机 /// public string MailHost { get; set; } /// /// 发出邮箱的名称 /// public string MailName { get; set; } /// /// 发出邮箱的地址 /// public string MailUserName { get; set; } /// /// 发出邮箱的密码 /// public string MailPassword { get; set; } /// /// 企业号ID /// public string CorpId { get; set; } /// /// 企业号管理密钥 /// public string CorpSecret { get; set; } /// /// 企业应用的id,整型。可在应用的设置页面查看 /// public string CorpAppId { get; set; } /// /// 百度编辑器图片地址 /// public string UeditorImg { get; set; } /// /// 合金WMS的webapi 接口 /// public string HJWMSWebAPIUrl { get; set; } /// /// 合金WCS的webapi 接口 取消 完成 任务。 /// public string HJWCSWebAPIUrl { get; set; } /// /// 分拣库WMS的webapi 接口 /// public string FJWMSWebAPIUrl { get; set; } /// /// 分拣库WCS的webapi 接口 /// public string FJWCSWebAPIUrl { get; set; } public string PTWMSWebAPIUrl { get; set; } public string PTWCSWebAPIUrl { get; set; } public string SXWMSWebAPIUrl { get; set; } public string SXWCSWebAPIUrl { get; set; } public string CPWMSWebAPIUrl { get; set; } public string CPWCSWebAPIUrl { get; set; } /// /// 是否启用单点登录 /// public bool IsSSO { get; set; } /// /// JWT认证密钥 /// public string JwtSecret { get; set; } /// /// JWT认证过期时间 /// public int JwtExp { get; set; } /// /// 附件存放地址(主要是考虑到api 和 web 两部分) /// public string AnnexesFile { get; set; } /// /// 葡萄城AR报表服务地址 /// public string AReportsUrl { get; set; } /// /// 葡萄城AR报表目录 /// public string AReportsPath { get; set; } } public class RedisConfigs { public string WriteServerList { get; set; } public string ReadServerList { get; set; } public int MaxWritePoolSize { get; set; } public int MaxReadPoolSize { get; set; } public bool AutoStart { get; set; } public int LocalCacheTime { get; set; } public bool RecordeLog { get; set; } } public class ConnectionConfigs { public List Connections { get; set; } /// /// /// public ConnectionConfig WMSConnectionConfig { get; set; } /// /// /// public ConnectionConfig WCSConnectionConfig { get; set; } public string WMSRedisConnect { get; set; } } public class ConnectionConfig { public string ConnectionString { get; set; } public string DbSetNo { get; set; } public int DataBaseType { get; set; } public int InitKey { get; set; } public bool IsAutoCloseConn { get; set; } public string ConfigId { get; set; } } public class WCSRedis { public string HJRedis { get; set; } public string PTRedis { get; set; } public string FJRedis { get; set; } public string SXRedis { get; set; } public string CPRedis { get; set; } } public class DeviceEffectiveUrls { public string HJUrls { get; set; } public string PTUrls { get; set; } public string FJUrls { get; set; } public string SXUrls { get; set; } public string CPUrls { get; set; } } public static class JsCssHelper { private static readonly JavaScriptCompressor javaScriptCompressor = new JavaScriptCompressor(); private static readonly CssCompressor cssCompressor = new CssCompressor(); #region Js 文件操作 /// /// 读取js文件内容并压缩 /// /// /// public static string ReadJSFile(string[] filePathlist) { StringBuilder jsStr = new StringBuilder(); try { string rootPath = ConfigHelper.GetValue("baseDir"); foreach (var filePath in filePathlist) { string path = rootPath + filePath; if (DirFileHelper.IsExistFile(path)) { string content = File.ReadAllText(path, Encoding.Default); if (ConfigHelper.GetValue("env") != "dev") { string _content; try { _content = javaScriptCompressor.Compress(content); } catch (Exception) { _content = content; } content = _content; } jsStr.Append(content); } } return jsStr.ToString(); } catch (Exception) { return ""; } } #endregion #region Css 文件操作 /// /// 读取css 文件内容并压缩 /// /// /// public static string ReadCssFile(string[] filePathlist) { StringBuilder cssStr = new StringBuilder(); try { string rootPath = ConfigHelper.GetValue("baseDir"); foreach (var filePath in filePathlist) { string path = rootPath + filePath; if (DirFileHelper.IsExistFile(path)) { string content = File.ReadAllText(path, Encoding.Default); content = cssCompressor.Compress(content); cssStr.Append(content); } } return cssStr.ToString(); } catch (Exception) { return cssStr.ToString(); } } #endregion #region js 操作 /// /// 压缩js /// /// /// public static string CompressJS(string strJs) { if (ConfigHelper.GetValue("env") != "dev") { strJs = javaScriptCompressor.Compress(strJs); } return strJs; } /// /// 压缩Css /// /// /// public static string CompressCss(string strCss) { if (ConfigHelper.GetValue("env") != "dev") { strCss = cssCompressor.Compress(strCss); } return strCss; } #endregion #region css 操作 #endregion #region 读取文件 /// /// 读取js文件 /// /// 文件夹目录 /// public static string ReadJS(string filePath) { StringBuilder str = new StringBuilder(); try { string rootPath = AppContext.BaseDirectory; rootPath = rootPath.Replace("\\", "/"); string path = rootPath + filePath; if (DirFileHelper.IsExistFile(path)) { string content = File.ReadAllText(path, Encoding.UTF8); if (ConfigHelper.GetValue("env") != "dev") { content = javaScriptCompressor.Compress(content); } str.Append(content); } return str.ToString(); } catch (Exception) { return ""; } } /// /// 读取css文件 /// /// /// public static string ReadCss(string filePath) { StringBuilder str = new StringBuilder(); try { string rootPath = AppContext.BaseDirectory; rootPath = rootPath.Replace("\\", "/"); string path = rootPath + filePath; if (DirFileHelper.IsExistFile(path)) { string content = File.ReadAllText(path, Encoding.UTF8); content = cssCompressor.Compress(content); str.Append(content); } return str.ToString(); } catch (Exception) { return ""; } } #endregion } public static class DirFileHelper { /// /// 检测指定文件是否存在,如果存在则返回true。 /// /// 文件的绝对路径 public static bool IsExistFile(string filePath) { return File.Exists(filePath); } } }