123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- 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
- {
- /// <summary>
- /// 缓存数据(配置信息)
- /// </summary>
- private static readonly ConcurrentDictionary<string, object> _setting =
- new ConcurrentDictionary<string, object>();
- /// <summary>
- /// 缓存数据
- /// </summary>
- private static readonly ConcurrentDictionary<string, string> _cache =
- new ConcurrentDictionary<string, string>();
- private static IConfiguration configuration;
- /// <summary>
- /// 获取配置信息
- /// </summary>
- /// <typeparam name="T">数据类型</typeparam>
- /// <param name="key">键</param>
- /// <returns></returns>
- public static T GetAppSettings<T>(string key) where T : class, new()
- {
- if (configuration == null)
- {
- string fileName = "appsettings.json";
- if (GetValue<string>("env") == "dev")
- {
- fileName = "appsettings.Development.json";
- }
- var directory = ConfigHelper.GetValue<string>("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<T>(configuration.GetSection(key))
- .BuildServiceProvider()
- .GetService<IOptions<T>>()
- .Value;
- return appconfig;
- }
- /// <summary>
- /// 获取配置信息
- /// </summary>
- /// <returns></returns>
- public static ServerOp GetConfig()
- {
- return GetAppSettings<ServerOp>("ServerOp");
- }
- public static RedisConfigs GetRedisConfigs()
- {
- return GetAppSettings<RedisConfigs>("RedisConfigs");
- }
- public static ConnectionConfigs GetConnectionConfigs()
- {
- return GetAppSettings<ConnectionConfigs>("ConnectionConfigs");
- }
- public static ConnectionConfigs GetQuestDBConnectionConfigs()
- {
- return GetAppSettings<ConnectionConfigs>("QuestDBConnectionConfigs");
- }
-
- public static WCSRedis GetWCSRedis()
- {
- return GetAppSettings<WCSRedis>("WCSRedis");
- }
- public static DeviceEffectiveUrls GetDeviceEffectiveUrls()
- {
- return GetAppSettings<DeviceEffectiveUrls>("DeviceEffectiveUrls");
- }
- /// <summary>
- /// 设置信息
- /// </summary>
- /// <param name="key">键</param>
- /// <param name="value">值</param>
- public static void SetValue(string key, object value)
- {
- _setting.GetOrAdd(key, value);
- }
- /// <summary>
- /// 获取数据值
- /// </summary>
- /// <typeparam name="T">类型</typeparam>
- /// <param name="key">键值</param>
- /// <returns></returns>
- public static T GetValue<T>(string key) where T : class
- {
- _setting.TryGetValue(key, out object result);
- return result as T;
- }
- /// <summary>
- /// 设置信息
- /// </summary>
- /// <param name="key">键</param>
- /// <param name="value">值</param>
- public static void SetCache(string key, string value)
- {
- _cache.GetOrAdd(key, value);
- }
- /// <summary>
- /// 获取数据值
- /// </summary>
- /// <param name="key">键值</param>
- /// <returns></returns>
- public static string GetCache(string key)
- {
- _cache.TryGetValue(key, out string result);
- return result;
- }
- }
- public class ServerOp
- {
- /// <summary>
- /// 软件名称
- /// </summary>
- public string SoftName { get; set; }
- /// <summary>
- /// 软件版本号
- /// </summary>
- public string Version { get; set; }
- /// <summary>
- /// 数据库连接
- /// </summary>
- public string dbConn { get; set; }
- /// <summary>
- /// 数据库类型 SqlServer,Oracle,MySql
- /// </summary>
- public string dbType { get; set; }
- /// <summary>
- /// 缓存前缀
- /// </summary>
- public string RedisPrev { get; set; }
- /// <summary>
- /// 缓存地址
- /// </summary>
- public string RedisExchangeHosts { get; set; }
- /// <summary>
- /// 服务目录
- /// </summary>
- public string VirtualPath { get; set; }
- /// <summary>
- /// 皮肤配置
- /// </summary>
- public string UItheme { get; set; }
- /// <summary>
- /// IM地址
- /// </summary>
- public string IMUrl { get; set; }
- /// <summary>
- /// 即时服务是否打开
- /// </summary>
- public bool IMOpen { get; set; }
- /// <summary>
- /// 发出邮箱设置邮箱主机
- /// </summary>
- public string MailHost { get; set; }
- /// <summary>
- /// 发出邮箱的名称
- /// </summary>
- public string MailName { get; set; }
- /// <summary>
- /// 发出邮箱的地址
- /// </summary>
- public string MailUserName { get; set; }
- /// <summary>
- /// 发出邮箱的密码
- /// </summary>
- public string MailPassword { get; set; }
- /// <summary>
- /// 企业号ID
- /// </summary>
- public string CorpId { get; set; }
- /// <summary>
- /// 企业号管理密钥
- /// </summary>
- public string CorpSecret { get; set; }
- /// <summary>
- /// 企业应用的id,整型。可在应用的设置页面查看
- /// </summary>
- public string CorpAppId { get; set; }
- /// <summary>
- /// 百度编辑器图片地址
- /// </summary>
- public string UeditorImg { get; set; }
- /// <summary>
- /// 合金WMS的webapi 接口
- /// </summary>
- public string HJWMSWebAPIUrl { get; set; }
- /// <summary>
- /// 合金WCS的webapi 接口 取消 完成 任务。
- /// </summary>
- public string HJWCSWebAPIUrl { get; set; }
- /// <summary>
- /// 分拣库WMS的webapi 接口
- /// </summary>
- public string FJWMSWebAPIUrl { get; set; }
- /// <summary>
- /// 分拣库WCS的webapi 接口
- /// </summary>
- 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; }
- /// <summary>
- /// 是否启用单点登录
- /// </summary>
- public bool IsSSO { get; set; }
- /// <summary>
- /// JWT认证密钥
- /// </summary>
- public string JwtSecret { get; set; }
- /// <summary>
- /// JWT认证过期时间
- /// </summary>
- public int JwtExp { get; set; }
- /// <summary>
- /// 附件存放地址(主要是考虑到api 和 web 两部分)
- /// </summary>
- public string AnnexesFile { get; set; }
- /// <summary>
- /// 葡萄城AR报表服务地址
- /// </summary>
- public string AReportsUrl { get; set; }
- /// <summary>
- /// 葡萄城AR报表目录
- /// </summary>
- 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<ConnectionConfig> Connections { get; set; }
- /// <summary>
- ///
- /// </summary>
- public ConnectionConfig WMSConnectionConfig { get; set; }
- /// <summary>
- ///
- /// </summary>
- 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 文件操作
- /// <summary>
- /// 读取js文件内容并压缩
- /// </summary>
- /// <param name="filePathlist"></param>
- /// <returns></returns>
- public static string ReadJSFile(string[] filePathlist)
- {
- StringBuilder jsStr = new StringBuilder();
- try
- {
- string rootPath = ConfigHelper.GetValue<string>("baseDir");
- foreach (var filePath in filePathlist)
- {
- string path = rootPath + filePath;
- if (DirFileHelper.IsExistFile(path))
- {
- string content = File.ReadAllText(path, Encoding.Default);
- if (ConfigHelper.GetValue<string>("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 文件操作
- /// <summary>
- /// 读取css 文件内容并压缩
- /// </summary>
- /// <param name="filePathlist"></param>
- /// <returns></returns>
- public static string ReadCssFile(string[] filePathlist)
- {
- StringBuilder cssStr = new StringBuilder();
- try
- {
- string rootPath = ConfigHelper.GetValue<string>("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 操作
- /// <summary>
- /// 压缩js
- /// </summary>
- /// <param name="strJs"></param>
- /// <returns></returns>
- public static string CompressJS(string strJs)
- {
- if (ConfigHelper.GetValue<string>("env") != "dev")
- {
- strJs = javaScriptCompressor.Compress(strJs);
- }
- return strJs;
- }
- /// <summary>
- /// 压缩Css
- /// </summary>
- /// <param name="strCss"></param>
- /// <returns></returns>
- public static string CompressCss(string strCss)
- {
- if (ConfigHelper.GetValue<string>("env") != "dev")
- {
- strCss = cssCompressor.Compress(strCss);
- }
- return strCss;
- }
- #endregion
- #region css 操作
- #endregion
- #region 读取文件
- /// <summary>
- /// 读取js文件
- /// </summary>
- /// <param name="filePath">文件夹目录</param>
- /// <returns></returns>
- 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<string>("env") != "dev")
- {
- content = javaScriptCompressor.Compress(content);
- }
- str.Append(content);
- }
- return str.ToString();
- }
- catch (Exception)
- {
- return "";
- }
- }
- /// <summary>
- /// 读取css文件
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- 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
- {
- /// <summary>
- /// 检测指定文件是否存在,如果存在则返回true。
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- public static bool IsExistFile(string filePath)
- {
- return File.Exists(filePath);
- }
- }
- }
|