Statics.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using CP.Business;
  2. using CP.Cache;
  3. using Newtonsoft.Json;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Web;
  10. namespace CP.Web
  11. {
  12. public class Statics
  13. {
  14. #region 返回静态文件输出
  15. /// <summary>
  16. /// 返回静态文件输出
  17. /// </summary>
  18. /// <param name="name">名称</param>
  19. /// <param name="type">类型-防止重名</param>
  20. /// <returns></returns>
  21. public static string GetStatics(string name, string type = "")
  22. {
  23. if (string.IsNullOrEmpty(name))
  24. return "";
  25. StringBuilder sb = new StringBuilder();
  26. var names = name.Split(':');
  27. if (names != null && names.Length > 0)
  28. {
  29. string br = "\r\n";
  30. for (int i = 0; i < names.Length; i++)
  31. {
  32. if (i == names.Length - 1)
  33. br = "";
  34. var item = StaticFile.GetStaticFile(names[i], type);
  35. if (item != null && !string.IsNullOrEmpty(item.url))
  36. {
  37. switch (item.type)
  38. {
  39. case "js":
  40. sb.Append("<script type=\"text/javascript\" src=\"" + item.url + "?v=" + item.ver + "\"></script>" + br);
  41. break;
  42. case "css":
  43. sb.Append("<link href=\"" + item.url + "?v=" + item.ver + "\" rel=\"stylesheet\" type=\"text/css\" />" + br);
  44. break;
  45. }
  46. }
  47. }
  48. }
  49. return sb.ToString();
  50. }
  51. #endregion
  52. }
  53. public class StaticFile
  54. {
  55. /// <summary>
  56. /// 缓存
  57. /// </summary>
  58. static WMCache cache = WMCache.GetCacheService();
  59. /// <summary>
  60. /// cache中的所有数据
  61. /// </summary>
  62. /// <returns></returns>
  63. public static List<StaticFileInfo> GetStaticFileList()
  64. {
  65. string key = CacheKeys.Static_File;
  66. List<StaticFileInfo> list = cache.GetObject<List<StaticFileInfo>>(key) as List<StaticFileInfo>;
  67. if (list == null)
  68. {
  69. list = StaticFileData.GetStaticFileList();
  70. cache.AddObject(key, list, (int)CacheTime.System);
  71. }
  72. return list;
  73. }
  74. /// <summary>
  75. /// 某个文件
  76. /// </summary>
  77. /// <param name="name"></param>
  78. /// <returns></returns>
  79. public static StaticFileInfo GetStaticFile(string name, string type)
  80. {
  81. StaticFileInfo info = new StaticFileInfo();
  82. var list = GetStaticFileList();
  83. foreach (var item in list)
  84. {
  85. if (!string.IsNullOrWhiteSpace(type))
  86. {
  87. if (item.name.Trim().Equals(name, StringComparison.CurrentCultureIgnoreCase)
  88. && item.type.Equals(type, StringComparison.CurrentCultureIgnoreCase))
  89. return item;
  90. }
  91. else
  92. {
  93. if (item.name.Trim().Equals(name, StringComparison.CurrentCultureIgnoreCase))
  94. return item;
  95. }
  96. }
  97. return info;
  98. }
  99. }
  100. public class StaticFileInfo
  101. {
  102. /// <summary>
  103. /// 名称
  104. /// </summary>
  105. public string name { get; set; }
  106. /// <summary>
  107. /// 地址
  108. /// </summary>
  109. public string url { get; set; }
  110. /// <summary>
  111. /// 版本号
  112. /// </summary>
  113. public string ver { get; set; }
  114. /// <summary>
  115. /// 类型/css/js
  116. /// </summary>
  117. public string type { get; set; }
  118. }
  119. public class StaticFileData
  120. {
  121. /// <summary>
  122. /// get 文件中的static文件配置
  123. /// </summary>
  124. /// <returns></returns>
  125. public static List<StaticFileInfo> GetStaticFileList()
  126. {
  127. List<StaticFileInfo> list = new List<StaticFileInfo>();
  128. string result = string.Empty;
  129. try
  130. {
  131. string path = System.Web.HttpContext.Current.Server.MapPath("/") + "Config/data/static.config";
  132. result = File.ReadAllText(path);
  133. }
  134. catch (Exception ex)
  135. {
  136. throw new Exception("static.config配置错误:" + ex.Message);
  137. }
  138. if (!string.IsNullOrEmpty(result))
  139. {
  140. try
  141. {
  142. list = JsonConvert.DeserializeObject<List<StaticFileInfo>>(result);
  143. }
  144. catch (Exception ex)
  145. {
  146. throw new Exception("反序列化static.config出错:" + ex.Message);
  147. }
  148. }
  149. return list;
  150. }
  151. }
  152. }