PageFunc.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. using CP.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Web;
  8. using System.Xml;
  9. namespace CP.Web
  10. {
  11. /// <summary>
  12. /// 资源文件管理 by JNswins
  13. /// </summary>
  14. public class ResourcesConfigs
  15. {
  16. private ResourcesConfigs() { }
  17. private static string configPath = AppDomain.CurrentDomain.BaseDirectory + "Config\\Resources.config";
  18. private static ResourcesConfigInfo _config = null;
  19. private static object lockObject = new object();
  20. static ResourcesConfigs()
  21. {
  22. LoadConfig();
  23. }
  24. /// <summary>
  25. /// 加载配置
  26. /// </summary>
  27. public static void LoadConfig()
  28. {
  29. var config = new ResourcesConfigInfo();
  30. if (File.Exists(configPath))
  31. {
  32. var doc = new XmlDocument();
  33. doc.Load(configPath);
  34. var node = doc.SelectSingleNode("Resources/ResourcesPath");
  35. if (null != node)
  36. config.ResourcesPath = node.InnerText.Trim();
  37. var list = doc.SelectNodes("Resources/FileList/File");
  38. if (null != list && 0 < list.Count)
  39. {
  40. config.FileList = new List<ResourcesFile>();
  41. foreach (XmlNode item in list)
  42. {
  43. var entity = new ResourcesFile();
  44. node = item.SelectSingleNode("FileName");
  45. if (null != node)
  46. entity.FileName = node.InnerText.Trim();
  47. node = item.SelectSingleNode("FileUrl");
  48. if (null != node)
  49. entity.FileUrl = node.InnerText.Trim();
  50. node = item.SelectSingleNode("Version");
  51. if (null != node)
  52. entity.Version = TypeConverter.StrToInt(node.InnerText.Trim());
  53. node = item.SelectSingleNode("IsLocal");
  54. if (null != node)
  55. entity.IsLocal = TypeConverter.StrToBool(node.InnerText.Trim(), false);
  56. node = item.SelectSingleNode("FileType");
  57. if (null != node)
  58. entity.FileType = (ResourcesFileType)Enum.Parse(typeof(ResourcesFileType), node.InnerText.Trim(), true);
  59. config.FileList.Add(entity);
  60. }
  61. }
  62. }
  63. _config = config;
  64. }
  65. /// <summary>
  66. /// 重新加载资源配置
  67. /// </summary>
  68. public static void ResetLoadConfig(string configPath = "")
  69. {
  70. lock (lockObject)
  71. {
  72. var config = GetConfig();
  73. var newConfig = new ResourcesConfigInfo();
  74. if (null != config)
  75. newConfig.ResourcesPath = config.ResourcesPath;
  76. if (!string.IsNullOrEmpty(configPath) && File.Exists(configPath))
  77. newConfig.ResourcesPath = configPath;
  78. if (!string.IsNullOrEmpty(newConfig.ResourcesPath))
  79. {
  80. var list = new List<ResourcesFile>();
  81. string[] files = null;
  82. string path = "";
  83. #region 原有远端资源文件
  84. if (null != config && null != config.FileList && 0 < config.FileList.Count)
  85. {
  86. foreach (var item in config.FileList)
  87. {
  88. if (!item.IsLocal)
  89. list.Add(item);
  90. }
  91. }
  92. #endregion
  93. #region 本地JS资源文件
  94. path = AppDomain.CurrentDomain.BaseDirectory + newConfig.ResourcesPath.Replace("/", "\\") + "js\\";
  95. if (Directory.Exists(path))
  96. {
  97. files = Directory.GetFiles(path);
  98. foreach (var item in files)
  99. {
  100. list.Add(new ResourcesFile()
  101. {
  102. FileName = System.IO.Path.GetFileName(item),
  103. FileUrl = "",
  104. Version = TypeConverter.StrToInt(File.GetLastWriteTime(item).ToString("yyyyMMddHH")),
  105. IsLocal = true,
  106. FileType = ResourcesFileType.JS
  107. });
  108. }
  109. }
  110. #endregion
  111. #region 本地CSS资源
  112. path = AppDomain.CurrentDomain.BaseDirectory + newConfig.ResourcesPath.Replace("/", "\\") + "css\\";
  113. if (Directory.Exists(path))
  114. {
  115. files = Directory.GetFiles(path);
  116. foreach (var item in files)
  117. {
  118. list.Add(new ResourcesFile()
  119. {
  120. FileName = System.IO.Path.GetFileName(item),
  121. FileUrl = "",
  122. Version = TypeConverter.StrToInt(File.GetLastWriteTime(item).ToString("yyyyMMddHH")),
  123. IsLocal = true,
  124. FileType = ResourcesFileType.CSS
  125. });
  126. }
  127. }
  128. #endregion
  129. newConfig.FileList = list;
  130. _config = newConfig;
  131. SaveConfig(newConfig);
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// 保存配置文件
  137. /// </summary>
  138. /// <param name="config"></param>
  139. /// <returns></returns>
  140. public static bool SaveConfig(ResourcesConfigInfo config)
  141. {
  142. if (null == config)
  143. throw new ArgumentNullException("ResourcesConfigInfo 对象为空!");
  144. var sp = new StringBuilder(2000);
  145. sp.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\r\n");
  146. sp.Append("<Resources>\r\n");
  147. sp.Append(" <ResourcesPath>" + config.ResourcesPath + "</ResourcesPath>\r\n");
  148. sp.Append(" <FileList>\r\n");
  149. if (null != config.FileList && 0 < config.FileList.Count)
  150. {
  151. foreach (var item in config.FileList)
  152. {
  153. sp.AppendFormat(" <File>\r\n <FileName>{0}</FileName>\r\n <FileUrl>{1}</FileUrl>\r\n <Version>{2}</Version>\r\n <IsLocal>{3}</IsLocal>\r\n <FileType>{4}</FileType>\r\n </File>\r\n",
  154. item.FileName, item.FileUrl, item.Version, item.IsLocal, item.FileType);
  155. }
  156. }
  157. sp.Append(" </FileList>\r\n");
  158. sp.Append("</Resources>");
  159. using (StreamWriter writer = new StreamWriter(configPath, false, System.Text.Encoding.UTF8, sp.Length))
  160. {
  161. writer.Write(sp.ToString());
  162. }
  163. return true;
  164. }
  165. /// <summary>
  166. /// 返回配置
  167. /// </summary>
  168. /// <returns></returns>
  169. public static ResourcesConfigInfo GetConfig()
  170. {
  171. if (null == _config)
  172. LoadConfig();
  173. return _config;
  174. }
  175. /// <summary>
  176. /// 批量获取资源文件HTML代码
  177. /// </summary>
  178. /// <param name="files">文件名数组</param>
  179. /// <returns></returns>
  180. public static string GetMultiResourcesFilesHtml(string[] files)
  181. {
  182. if (null == files || 0 >= files.Length)
  183. return "";
  184. var config = GetConfig();
  185. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  186. return "";
  187. var sp = new StringBuilder(1000);
  188. for (int i = 0; i < files.Length; i++)
  189. {
  190. if (-1 == files[i].IndexOf('.'))
  191. continue;
  192. ResourcesFileType fileType;
  193. foreach (var item in config.FileList)
  194. {
  195. if (Enum.TryParse(System.IO.Path.GetExtension(files[i]).TrimStart('.'), true, out fileType))
  196. {
  197. if (item.FileName.ToLower() == files[i].ToLower() && item.FileType == fileType)
  198. {
  199. sp.AppendLine(item.GetFileHtml(config.ResourcesPath));
  200. }
  201. }
  202. }
  203. }
  204. return sp.ToString();
  205. }
  206. /// <summary>
  207. /// 批量获取资源文件HTML代码,多个文件请以","分隔
  208. /// </summary>
  209. /// <param name="files">资源文件以","分隔</param>
  210. /// <returns></returns>
  211. public static string GetMultiResourcesFilesHtml(string files)
  212. {
  213. if (string.IsNullOrWhiteSpace(files))
  214. return "";
  215. return GetMultiResourcesFilesHtml(files.Split(','));
  216. }
  217. /// <summary>
  218. /// 翻页列表
  219. /// </summary>
  220. /// <param name="pageSize"></param>
  221. /// <param name="pageIndex"></param>
  222. /// <param name="recordCount"></param>
  223. /// <returns></returns>
  224. public static IList<ResourcesFile> ToPaging(int pageSize, int pageIndex, out int recordCount)
  225. {
  226. recordCount = 0;
  227. var config = GetConfig();
  228. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  229. return null;
  230. recordCount = config.FileList.Count;
  231. int end = (pageIndex - 1) * pageSize + pageSize;
  232. int start = pageIndex == 1 ? 0 : (pageIndex - 1) * pageSize;
  233. if (start > recordCount)
  234. return null;
  235. end = end > recordCount ? recordCount : end;
  236. IList<ResourcesFile> list = new List<ResourcesFile>();
  237. for (int i = start; i < end; i++)
  238. {
  239. list.Add(config.FileList[i]);
  240. }
  241. return list;
  242. }
  243. /// <summary>
  244. /// 添加资源文件
  245. /// </summary>
  246. /// <param name="file"></param>
  247. /// <returns></returns>
  248. public static bool AddResourcesFiles(ResourcesFile file)
  249. {
  250. lock (lockObject)
  251. {
  252. var config = GetConfig();
  253. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  254. return true;
  255. bool isContains = false;
  256. foreach (var item in config.FileList)
  257. {
  258. if (item.FileUrl == file.FileUrl)
  259. isContains = true;
  260. }
  261. if (!isContains)
  262. {
  263. config.FileList.Add(file);
  264. SaveConfig(config);
  265. return true;
  266. }
  267. return false;
  268. }
  269. }
  270. /// <summary>
  271. /// 移除资源文件
  272. /// </summary>
  273. /// <param name="fileName"></param>
  274. /// <param name="fileType"></param>
  275. /// <returns></returns>
  276. public static bool RemoveResourcesFile(string fileName, ResourcesFileType fileType)
  277. {
  278. lock (lockObject)
  279. {
  280. var config = GetConfig();
  281. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  282. return true;
  283. foreach (var item in config.FileList)
  284. {
  285. if (fileName == item.FileName && fileType == item.FileType)
  286. {
  287. config.FileList.Remove(item);
  288. SaveConfig(config);
  289. return true;
  290. }
  291. }
  292. return false;
  293. }
  294. }
  295. /// <summary>
  296. /// 更新文件版本号
  297. /// </summary>
  298. /// <param name="fileName">文件名</param>
  299. /// <param name="fileType">文件类型</param>
  300. /// <param name="saveFile">是否保存文件</param>
  301. /// <returns></returns>
  302. public static bool UpdateFileVersion(string fileName, ResourcesFileType fileType, bool saveFile = true)
  303. {
  304. var config = GetConfig();
  305. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  306. return true;
  307. foreach (var item in config.FileList)
  308. {
  309. if (item.IsLocal && fileName == item.FileName && fileType == item.FileType)
  310. {
  311. string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
  312. string.Format("{0}{1}\\{2}", config.ResourcesPath.Replace("/", "\\"), item.FileType.ToString(), item.FileName));
  313. if (File.Exists(filePath))
  314. item.Version = TypeConverter.StrToInt(File.GetLastWriteTime(filePath).ToString("yyyyMMddHH"));
  315. if (saveFile)
  316. SaveConfig(config);
  317. return true;
  318. }
  319. }
  320. return false;
  321. }
  322. /// <summary>
  323. /// 更新所有文件版本号
  324. /// </summary>
  325. /// <param name="fileName"></param>
  326. /// <param name="fileType"></param>
  327. /// <returns></returns>
  328. public static bool UpdateAllFileVersion()
  329. {
  330. var config = GetConfig();
  331. if (null == config || null == config.FileList || 0 >= config.FileList.Count)
  332. return true;
  333. foreach (var item in config.FileList)
  334. {
  335. if (item.IsLocal)
  336. {
  337. string filePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
  338. string.Format("{0}{1}\\{2}", config.ResourcesPath.Replace("/", "\\"), item.FileType.ToString(), item.FileName));
  339. if (File.Exists(filePath))
  340. item.Version = TypeConverter.StrToInt(File.GetLastWriteTime(filePath).ToString("yyyyMMddHH"));
  341. }
  342. }
  343. return SaveConfig(config);
  344. }
  345. }
  346. /// <summary>
  347. /// 资源配置文件 By:JNswins
  348. /// </summary>
  349. public class ResourcesConfigInfo
  350. {
  351. /// <summary>
  352. /// 资源文件路劲
  353. /// </summary>
  354. public string ResourcesPath { get; set; }
  355. /// <summary>
  356. /// 资源文件
  357. /// </summary>
  358. public IList<ResourcesFile> FileList { get; set; }
  359. }
  360. /// <summary>
  361. /// 资源文件
  362. /// </summary>
  363. public class ResourcesFile
  364. {
  365. /// <summary>
  366. /// 文件名称
  367. /// </summary>
  368. public string FileName { get; set; }
  369. /// <summary>
  370. /// 文件地址
  371. /// </summary>
  372. public string FileUrl { get; set; }
  373. /// <summary>
  374. /// 版本
  375. /// </summary>
  376. public int Version { get; set; }
  377. /// <summary>
  378. /// 是否本地资源文件
  379. /// </summary>
  380. public bool IsLocal { get; set; }
  381. /// <summary>
  382. /// 资源文件类型
  383. /// </summary>
  384. public ResourcesFileType FileType { get; set; }
  385. /// <summary>
  386. /// 返回文件地址
  387. /// </summary>
  388. /// <param name="path">资源文件配置路径</param>
  389. /// <returns></returns>
  390. public string GetFileUrl(string path)
  391. {
  392. if (!IsLocal)
  393. return FileUrl;
  394. return string.Format("{0}{1}/{2}?v={3}", path, this.FileType.ToString().ToLower(), this.FileName.ToLower(), this.Version);
  395. }
  396. /// <summary>
  397. /// 返回HTML代码
  398. /// </summary>
  399. /// <param name="path"></param>
  400. /// <returns></returns>
  401. public string GetFileHtml(string path)
  402. {
  403. switch (this.FileType)
  404. {
  405. case ResourcesFileType.CSS:
  406. return string.Format("<link type=\"text/css\" href=\"{0}\" rel=\"stylesheet\"/>", GetFileUrl(path));
  407. case ResourcesFileType.JS:
  408. return string.Format("<script type=\"text/javascript\" src=\"{0}\"></script>", GetFileUrl(path));
  409. }
  410. return "";
  411. }
  412. }
  413. /// <summary>
  414. /// 资源文件类型
  415. /// </summary>
  416. public enum ResourcesFileType
  417. {
  418. CSS = 1,
  419. JS = 2,
  420. IMG = 3
  421. }
  422. }