ConfigHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. using Microsoft.Extensions.Configuration;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using Microsoft.Extensions.Options;
  4. using NPOI.SS.Formula.Functions;
  5. using System.Collections.Concurrent;
  6. using System.Data;
  7. using System.Text;
  8. using Yahoo.Yui.Compressor;
  9. namespace WMS.Util
  10. {
  11. public static class ConfigHelper
  12. {
  13. /// <summary>
  14. /// 缓存数据(配置信息)
  15. /// </summary>
  16. private static readonly ConcurrentDictionary<string, object> _setting =
  17. new ConcurrentDictionary<string, object>();
  18. /// <summary>
  19. /// 缓存数据
  20. /// </summary>
  21. private static readonly ConcurrentDictionary<string, string> _cache =
  22. new ConcurrentDictionary<string, string>();
  23. private static IConfiguration configuration;
  24. /// <summary>
  25. /// 获取配置信息
  26. /// </summary>
  27. /// <typeparam name="T">数据类型</typeparam>
  28. /// <param name="key">键</param>
  29. /// <returns></returns>
  30. public static T GetAppSettings<T>(string key) where T : class, new()
  31. {
  32. if (configuration == null)
  33. {
  34. string fileName = "appsettings.json";
  35. if (GetValue<string>("env") == "dev")
  36. {
  37. fileName = "appsettings.Development.json";
  38. }
  39. var directory = ConfigHelper.GetValue<string>("baseDir");
  40. var filePath = $"{directory}/{fileName}";
  41. var builder = new ConfigurationBuilder();
  42. builder.AddJsonFile(filePath, false, true);
  43. configuration = builder.Build();
  44. }
  45. // 获取bin目录路径
  46. //if (!File.Exists(filePath))
  47. //{
  48. // var length = directory.IndexOf("/bin", StringComparison.Ordinal);
  49. // filePath = $"{directory.Substring(0, length)}/{fileName}";
  50. //}
  51. //IConfiguration configuration;
  52. var appconfig = new ServiceCollection()
  53. .AddOptions()
  54. .Configure<T>(configuration.GetSection(key))
  55. .BuildServiceProvider()
  56. .GetService<IOptions<T>>()
  57. .Value;
  58. return appconfig;
  59. }
  60. /// <summary>
  61. /// 获取配置信息
  62. /// </summary>
  63. /// <returns></returns>
  64. public static ServerOp GetConfig()
  65. {
  66. return GetAppSettings<ServerOp>("ServerOp");
  67. }
  68. public static RedisConfigs GetRedisConfigs()
  69. {
  70. return GetAppSettings<RedisConfigs>("RedisConfigs");
  71. }
  72. public static ConnectionConfigs GetConnectionConfigs()
  73. {
  74. return GetAppSettings<ConnectionConfigs>("ConnectionConfigs");
  75. }
  76. public static ConnectionConfigs GetQuestDBConnectionConfigs()
  77. {
  78. return GetAppSettings<ConnectionConfigs>("QuestDBConnectionConfigs");
  79. }
  80. public static WCSRedis GetWCSRedis()
  81. {
  82. return GetAppSettings<WCSRedis>("WCSRedis");
  83. }
  84. public static DeviceEffectiveUrls GetDeviceEffectiveUrls()
  85. {
  86. return GetAppSettings<DeviceEffectiveUrls>("DeviceEffectiveUrls");
  87. }
  88. /// <summary>
  89. /// 设置信息
  90. /// </summary>
  91. /// <param name="key">键</param>
  92. /// <param name="value">值</param>
  93. public static void SetValue(string key, object value)
  94. {
  95. _setting.GetOrAdd(key, value);
  96. }
  97. /// <summary>
  98. /// 获取数据值
  99. /// </summary>
  100. /// <typeparam name="T">类型</typeparam>
  101. /// <param name="key">键值</param>
  102. /// <returns></returns>
  103. public static T GetValue<T>(string key) where T : class
  104. {
  105. _setting.TryGetValue(key, out object result);
  106. return result as T;
  107. }
  108. /// <summary>
  109. /// 设置信息
  110. /// </summary>
  111. /// <param name="key">键</param>
  112. /// <param name="value">值</param>
  113. public static void SetCache(string key, string value)
  114. {
  115. _cache.GetOrAdd(key, value);
  116. }
  117. /// <summary>
  118. /// 获取数据值
  119. /// </summary>
  120. /// <param name="key">键值</param>
  121. /// <returns></returns>
  122. public static string GetCache(string key)
  123. {
  124. _cache.TryGetValue(key, out string result);
  125. return result;
  126. }
  127. }
  128. public class ServerOp
  129. {
  130. /// <summary>
  131. /// 软件名称
  132. /// </summary>
  133. public string SoftName { get; set; }
  134. /// <summary>
  135. /// 软件版本号
  136. /// </summary>
  137. public string Version { get; set; }
  138. /// <summary>
  139. /// 数据库连接
  140. /// </summary>
  141. public string dbConn { get; set; }
  142. /// <summary>
  143. /// 数据库类型 SqlServer,Oracle,MySql
  144. /// </summary>
  145. public string dbType { get; set; }
  146. /// <summary>
  147. /// 缓存前缀
  148. /// </summary>
  149. public string RedisPrev { get; set; }
  150. /// <summary>
  151. /// 缓存地址
  152. /// </summary>
  153. public string RedisExchangeHosts { get; set; }
  154. /// <summary>
  155. /// 服务目录
  156. /// </summary>
  157. public string VirtualPath { get; set; }
  158. /// <summary>
  159. /// 皮肤配置
  160. /// </summary>
  161. public string UItheme { get; set; }
  162. /// <summary>
  163. /// IM地址
  164. /// </summary>
  165. public string IMUrl { get; set; }
  166. /// <summary>
  167. /// 即时服务是否打开
  168. /// </summary>
  169. public bool IMOpen { get; set; }
  170. /// <summary>
  171. /// 发出邮箱设置邮箱主机
  172. /// </summary>
  173. public string MailHost { get; set; }
  174. /// <summary>
  175. /// 发出邮箱的名称
  176. /// </summary>
  177. public string MailName { get; set; }
  178. /// <summary>
  179. /// 发出邮箱的地址
  180. /// </summary>
  181. public string MailUserName { get; set; }
  182. /// <summary>
  183. /// 发出邮箱的密码
  184. /// </summary>
  185. public string MailPassword { get; set; }
  186. /// <summary>
  187. /// 企业号ID
  188. /// </summary>
  189. public string CorpId { get; set; }
  190. /// <summary>
  191. /// 企业号管理密钥
  192. /// </summary>
  193. public string CorpSecret { get; set; }
  194. /// <summary>
  195. /// 企业应用的id,整型。可在应用的设置页面查看
  196. /// </summary>
  197. public string CorpAppId { get; set; }
  198. /// <summary>
  199. /// 百度编辑器图片地址
  200. /// </summary>
  201. public string UeditorImg { get; set; }
  202. /// <summary>
  203. /// 合金WMS的webapi 接口
  204. /// </summary>
  205. public string HJWMSWebAPIUrl { get; set; }
  206. /// <summary>
  207. /// 合金WCS的webapi 接口 取消 完成 任务。
  208. /// </summary>
  209. public string HJWCSWebAPIUrl { get; set; }
  210. /// <summary>
  211. /// 分拣库WMS的webapi 接口
  212. /// </summary>
  213. public string FJWMSWebAPIUrl { get; set; }
  214. /// <summary>
  215. /// 分拣库WCS的webapi 接口
  216. /// </summary>
  217. public string FJWCSWebAPIUrl { get; set; }
  218. public string PTWMSWebAPIUrl { get; set; }
  219. public string PTWCSWebAPIUrl { get; set; }
  220. public string SXWMSWebAPIUrl { get; set; }
  221. public string SXWCSWebAPIUrl { get; set; }
  222. public string CPWMSWebAPIUrl { get; set; }
  223. public string CPWCSWebAPIUrl { get; set; }
  224. /// <summary>
  225. /// 是否启用单点登录
  226. /// </summary>
  227. public bool IsSSO { get; set; }
  228. /// <summary>
  229. /// JWT认证密钥
  230. /// </summary>
  231. public string JwtSecret { get; set; }
  232. /// <summary>
  233. /// JWT认证过期时间
  234. /// </summary>
  235. public int JwtExp { get; set; }
  236. /// <summary>
  237. /// 附件存放地址(主要是考虑到api 和 web 两部分)
  238. /// </summary>
  239. public string AnnexesFile { get; set; }
  240. /// <summary>
  241. /// 葡萄城AR报表服务地址
  242. /// </summary>
  243. public string AReportsUrl { get; set; }
  244. /// <summary>
  245. /// 葡萄城AR报表目录
  246. /// </summary>
  247. public string AReportsPath { get; set; }
  248. }
  249. public class RedisConfigs
  250. {
  251. public string WriteServerList { get; set; }
  252. public string ReadServerList { get; set; }
  253. public int MaxWritePoolSize { get; set; }
  254. public int MaxReadPoolSize { get; set; }
  255. public bool AutoStart { get; set; }
  256. public int LocalCacheTime { get; set; }
  257. public bool RecordeLog { get; set; }
  258. }
  259. public class ConnectionConfigs
  260. {
  261. public List<ConnectionConfig> Connections { get; set; }
  262. /// <summary>
  263. ///
  264. /// </summary>
  265. public ConnectionConfig WMSConnectionConfig { get; set; }
  266. /// <summary>
  267. ///
  268. /// </summary>
  269. public ConnectionConfig WCSConnectionConfig { get; set; }
  270. public string WMSRedisConnect { get; set; }
  271. }
  272. public class ConnectionConfig
  273. {
  274. public string ConnectionString { get; set; }
  275. public string DbSetNo { get; set; }
  276. public int DataBaseType { get; set; }
  277. public int InitKey { get; set; }
  278. public bool IsAutoCloseConn { get; set; }
  279. public string ConfigId { get; set; }
  280. }
  281. public class WCSRedis
  282. {
  283. public string HJRedis { get; set; }
  284. public string PTRedis { get; set; }
  285. public string FJRedis { get; set; }
  286. public string SXRedis { get; set; }
  287. public string CPRedis { get; set; }
  288. }
  289. public class DeviceEffectiveUrls
  290. {
  291. public string HJUrls { get; set; }
  292. public string PTUrls { get; set; }
  293. public string FJUrls { get; set; }
  294. public string SXUrls { get; set; }
  295. public string CPUrls { get; set; }
  296. }
  297. public static class JsCssHelper
  298. {
  299. private static readonly JavaScriptCompressor javaScriptCompressor = new JavaScriptCompressor();
  300. private static readonly CssCompressor cssCompressor = new CssCompressor();
  301. #region Js 文件操作
  302. /// <summary>
  303. /// 读取js文件内容并压缩
  304. /// </summary>
  305. /// <param name="filePathlist"></param>
  306. /// <returns></returns>
  307. public static string ReadJSFile(string[] filePathlist)
  308. {
  309. StringBuilder jsStr = new StringBuilder();
  310. try
  311. {
  312. string rootPath = ConfigHelper.GetValue<string>("baseDir");
  313. foreach (var filePath in filePathlist)
  314. {
  315. string path = rootPath + filePath;
  316. if (DirFileHelper.IsExistFile(path))
  317. {
  318. string content = File.ReadAllText(path, Encoding.Default);
  319. if (ConfigHelper.GetValue<string>("env") != "dev")
  320. {
  321. string _content;
  322. try
  323. {
  324. _content = javaScriptCompressor.Compress(content);
  325. }
  326. catch (Exception)
  327. {
  328. _content = content;
  329. }
  330. content = _content;
  331. }
  332. jsStr.Append(content);
  333. }
  334. }
  335. return jsStr.ToString();
  336. }
  337. catch (Exception)
  338. {
  339. return "";
  340. }
  341. }
  342. #endregion
  343. #region Css 文件操作
  344. /// <summary>
  345. /// 读取css 文件内容并压缩
  346. /// </summary>
  347. /// <param name="filePathlist"></param>
  348. /// <returns></returns>
  349. public static string ReadCssFile(string[] filePathlist)
  350. {
  351. StringBuilder cssStr = new StringBuilder();
  352. try
  353. {
  354. string rootPath = ConfigHelper.GetValue<string>("baseDir");
  355. foreach (var filePath in filePathlist)
  356. {
  357. string path = rootPath + filePath;
  358. if (DirFileHelper.IsExistFile(path))
  359. {
  360. string content = File.ReadAllText(path, Encoding.Default);
  361. content = cssCompressor.Compress(content);
  362. cssStr.Append(content);
  363. }
  364. }
  365. return cssStr.ToString();
  366. }
  367. catch (Exception)
  368. {
  369. return cssStr.ToString();
  370. }
  371. }
  372. #endregion
  373. #region js 操作
  374. /// <summary>
  375. /// 压缩js
  376. /// </summary>
  377. /// <param name="strJs"></param>
  378. /// <returns></returns>
  379. public static string CompressJS(string strJs)
  380. {
  381. if (ConfigHelper.GetValue<string>("env") != "dev")
  382. {
  383. strJs = javaScriptCompressor.Compress(strJs);
  384. }
  385. return strJs;
  386. }
  387. /// <summary>
  388. /// 压缩Css
  389. /// </summary>
  390. /// <param name="strCss"></param>
  391. /// <returns></returns>
  392. public static string CompressCss(string strCss)
  393. {
  394. if (ConfigHelper.GetValue<string>("env") != "dev")
  395. {
  396. strCss = cssCompressor.Compress(strCss);
  397. }
  398. return strCss;
  399. }
  400. #endregion
  401. #region css 操作
  402. #endregion
  403. #region 读取文件
  404. /// <summary>
  405. /// 读取js文件
  406. /// </summary>
  407. /// <param name="filePath">文件夹目录</param>
  408. /// <returns></returns>
  409. public static string ReadJS(string filePath)
  410. {
  411. StringBuilder str = new StringBuilder();
  412. try
  413. {
  414. string rootPath = AppContext.BaseDirectory;
  415. rootPath = rootPath.Replace("\\", "/");
  416. string path = rootPath + filePath;
  417. if (DirFileHelper.IsExistFile(path))
  418. {
  419. string content = File.ReadAllText(path, Encoding.UTF8);
  420. if (ConfigHelper.GetValue<string>("env") != "dev")
  421. {
  422. content = javaScriptCompressor.Compress(content);
  423. }
  424. str.Append(content);
  425. }
  426. return str.ToString();
  427. }
  428. catch (Exception)
  429. {
  430. return "";
  431. }
  432. }
  433. /// <summary>
  434. /// 读取css文件
  435. /// </summary>
  436. /// <param name="filePath"></param>
  437. /// <returns></returns>
  438. public static string ReadCss(string filePath)
  439. {
  440. StringBuilder str = new StringBuilder();
  441. try
  442. {
  443. string rootPath = AppContext.BaseDirectory;
  444. rootPath = rootPath.Replace("\\", "/");
  445. string path = rootPath + filePath;
  446. if (DirFileHelper.IsExistFile(path))
  447. {
  448. string content = File.ReadAllText(path, Encoding.UTF8);
  449. content = cssCompressor.Compress(content);
  450. str.Append(content);
  451. }
  452. return str.ToString();
  453. }
  454. catch (Exception)
  455. {
  456. return "";
  457. }
  458. }
  459. #endregion
  460. }
  461. public static class DirFileHelper
  462. {
  463. /// <summary>
  464. /// 检测指定文件是否存在,如果存在则返回true。
  465. /// </summary>
  466. /// <param name="filePath">文件的绝对路径</param>
  467. public static bool IsExistFile(string filePath)
  468. {
  469. return File.Exists(filePath);
  470. }
  471. }
  472. }