ConfigHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. /// 空轮缓存库WMS的webapi 接口
  216. /// </summary>
  217. public string KLHCWMSWebAPIUrl { get; set; }
  218. /// <summary>
  219. /// 分拣库WCS的webapi 接口
  220. /// </summary>
  221. public string FJWCSWebAPIUrl { get; set; }
  222. /// <summary>
  223. /// 空轮缓存库WCS的webapi 接口
  224. /// </summary>
  225. public string KLHCWCSWebAPIUrl { get; set; }
  226. public string PTWMSWebAPIUrl { get; set; }
  227. public string PTWCSWebAPIUrl { get; set; }
  228. public string SXWMSWebAPIUrl { get; set; }
  229. public string SXWCSWebAPIUrl { get; set; }
  230. public string CPWMSWebAPIUrl { get; set; }
  231. public string CPWCSWebAPIUrl { get; set; }
  232. /// <summary>
  233. /// 是否启用单点登录
  234. /// </summary>
  235. public bool IsSSO { get; set; }
  236. /// <summary>
  237. /// JWT认证密钥
  238. /// </summary>
  239. public string JwtSecret { get; set; }
  240. /// <summary>
  241. /// JWT认证过期时间
  242. /// </summary>
  243. public int JwtExp { get; set; }
  244. /// <summary>
  245. /// 附件存放地址(主要是考虑到api 和 web 两部分)
  246. /// </summary>
  247. public string AnnexesFile { get; set; }
  248. /// <summary>
  249. /// 葡萄城AR报表服务地址
  250. /// </summary>
  251. public string AReportsUrl { get; set; }
  252. /// <summary>
  253. /// 葡萄城AR报表目录
  254. /// </summary>
  255. public string AReportsPath { get; set; }
  256. }
  257. public class RedisConfigs
  258. {
  259. public string WriteServerList { get; set; }
  260. public string ReadServerList { get; set; }
  261. public int MaxWritePoolSize { get; set; }
  262. public int MaxReadPoolSize { get; set; }
  263. public bool AutoStart { get; set; }
  264. public int LocalCacheTime { get; set; }
  265. public bool RecordeLog { get; set; }
  266. }
  267. public class ConnectionConfigs
  268. {
  269. public List<ConnectionConfig> Connections { get; set; }
  270. /// <summary>
  271. ///
  272. /// </summary>
  273. public ConnectionConfig WMSConnectionConfig { get; set; }
  274. /// <summary>
  275. ///
  276. /// </summary>
  277. public ConnectionConfig WCSConnectionConfig { get; set; }
  278. public string WMSRedisConnect { get; set; }
  279. }
  280. public class ConnectionConfig
  281. {
  282. public string ConnectionString { get; set; }
  283. public string DbSetNo { get; set; }
  284. public int DataBaseType { get; set; }
  285. public int InitKey { get; set; }
  286. public bool IsAutoCloseConn { get; set; }
  287. public string ConfigId { get; set; }
  288. }
  289. public class WCSRedis
  290. {
  291. public string HJRedis { get; set; }
  292. public string PTRedis { get; set; }
  293. public string FJRedis { get; set; }
  294. public string SXRedis { get; set; }
  295. public string CPRedis { get; set; }
  296. }
  297. public class DeviceEffectiveUrls
  298. {
  299. public string HJUrls { get; set; }
  300. public string PTUrls { get; set; }
  301. public string FJUrls { get; set; }
  302. public string SXUrls { get; set; }
  303. public string CPUrls { get; set; }
  304. }
  305. public static class JsCssHelper
  306. {
  307. private static readonly JavaScriptCompressor javaScriptCompressor = new JavaScriptCompressor();
  308. private static readonly CssCompressor cssCompressor = new CssCompressor();
  309. #region Js 文件操作
  310. /// <summary>
  311. /// 读取js文件内容并压缩
  312. /// </summary>
  313. /// <param name="filePathlist"></param>
  314. /// <returns></returns>
  315. public static string ReadJSFile(string[] filePathlist)
  316. {
  317. StringBuilder jsStr = new StringBuilder();
  318. try
  319. {
  320. string rootPath = ConfigHelper.GetValue<string>("baseDir");
  321. foreach (var filePath in filePathlist)
  322. {
  323. string path = rootPath + filePath;
  324. if (DirFileHelper.IsExistFile(path))
  325. {
  326. string content = File.ReadAllText(path, Encoding.Default);
  327. if (ConfigHelper.GetValue<string>("env") != "dev")
  328. {
  329. string _content;
  330. try
  331. {
  332. _content = javaScriptCompressor.Compress(content);
  333. }
  334. catch (Exception)
  335. {
  336. _content = content;
  337. }
  338. content = _content;
  339. }
  340. jsStr.Append(content);
  341. }
  342. }
  343. return jsStr.ToString();
  344. }
  345. catch (Exception)
  346. {
  347. return "";
  348. }
  349. }
  350. #endregion
  351. #region Css 文件操作
  352. /// <summary>
  353. /// 读取css 文件内容并压缩
  354. /// </summary>
  355. /// <param name="filePathlist"></param>
  356. /// <returns></returns>
  357. public static string ReadCssFile(string[] filePathlist)
  358. {
  359. StringBuilder cssStr = new StringBuilder();
  360. try
  361. {
  362. string rootPath = ConfigHelper.GetValue<string>("baseDir");
  363. foreach (var filePath in filePathlist)
  364. {
  365. string path = rootPath + filePath;
  366. if (DirFileHelper.IsExistFile(path))
  367. {
  368. string content = File.ReadAllText(path, Encoding.Default);
  369. content = cssCompressor.Compress(content);
  370. cssStr.Append(content);
  371. }
  372. }
  373. return cssStr.ToString();
  374. }
  375. catch (Exception)
  376. {
  377. return cssStr.ToString();
  378. }
  379. }
  380. #endregion
  381. #region js 操作
  382. /// <summary>
  383. /// 压缩js
  384. /// </summary>
  385. /// <param name="strJs"></param>
  386. /// <returns></returns>
  387. public static string CompressJS(string strJs)
  388. {
  389. if (ConfigHelper.GetValue<string>("env") != "dev")
  390. {
  391. strJs = javaScriptCompressor.Compress(strJs);
  392. }
  393. return strJs;
  394. }
  395. /// <summary>
  396. /// 压缩Css
  397. /// </summary>
  398. /// <param name="strCss"></param>
  399. /// <returns></returns>
  400. public static string CompressCss(string strCss)
  401. {
  402. if (ConfigHelper.GetValue<string>("env") != "dev")
  403. {
  404. strCss = cssCompressor.Compress(strCss);
  405. }
  406. return strCss;
  407. }
  408. #endregion
  409. #region css 操作
  410. #endregion
  411. #region 读取文件
  412. /// <summary>
  413. /// 读取js文件
  414. /// </summary>
  415. /// <param name="filePath">文件夹目录</param>
  416. /// <returns></returns>
  417. public static string ReadJS(string filePath)
  418. {
  419. StringBuilder str = new StringBuilder();
  420. try
  421. {
  422. string rootPath = AppContext.BaseDirectory;
  423. rootPath = rootPath.Replace("\\", "/");
  424. string path = rootPath + filePath;
  425. if (DirFileHelper.IsExistFile(path))
  426. {
  427. string content = File.ReadAllText(path, Encoding.UTF8);
  428. if (ConfigHelper.GetValue<string>("env") != "dev")
  429. {
  430. content = javaScriptCompressor.Compress(content);
  431. }
  432. str.Append(content);
  433. }
  434. return str.ToString();
  435. }
  436. catch (Exception)
  437. {
  438. return "";
  439. }
  440. }
  441. /// <summary>
  442. /// 读取css文件
  443. /// </summary>
  444. /// <param name="filePath"></param>
  445. /// <returns></returns>
  446. public static string ReadCss(string filePath)
  447. {
  448. StringBuilder str = new StringBuilder();
  449. try
  450. {
  451. string rootPath = AppContext.BaseDirectory;
  452. rootPath = rootPath.Replace("\\", "/");
  453. string path = rootPath + filePath;
  454. if (DirFileHelper.IsExistFile(path))
  455. {
  456. string content = File.ReadAllText(path, Encoding.UTF8);
  457. content = cssCompressor.Compress(content);
  458. str.Append(content);
  459. }
  460. return str.ToString();
  461. }
  462. catch (Exception)
  463. {
  464. return "";
  465. }
  466. }
  467. #endregion
  468. }
  469. public static class DirFileHelper
  470. {
  471. /// <summary>
  472. /// 检测指定文件是否存在,如果存在则返回true。
  473. /// </summary>
  474. /// <param name="filePath">文件的绝对路径</param>
  475. public static bool IsExistFile(string filePath)
  476. {
  477. return File.Exists(filePath);
  478. }
  479. }
  480. }