123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- using CP.Business;
- using CP.Cache;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Web;
- namespace CP.Web
- {
- public class Statics
- {
- #region 返回静态文件输出
- /// <summary>
- /// 返回静态文件输出
- /// </summary>
- /// <param name="name">名称</param>
- /// <param name="type">类型-防止重名</param>
- /// <returns></returns>
- public static string GetStatics(string name, string type = "")
- {
- if (string.IsNullOrEmpty(name))
- return "";
- StringBuilder sb = new StringBuilder();
- var names = name.Split(':');
- if (names != null && names.Length > 0)
- {
- string br = "\r\n";
- for (int i = 0; i < names.Length; i++)
- {
- if (i == names.Length - 1)
- br = "";
- var item = StaticFile.GetStaticFile(names[i], type);
- if (item != null && !string.IsNullOrEmpty(item.url))
- {
- switch (item.type)
- {
- case "js":
- sb.Append("<script type=\"text/javascript\" src=\"" + item.url + "?v=" + item.ver + "\"></script>" + br);
- break;
- case "css":
- sb.Append("<link href=\"" + item.url + "?v=" + item.ver + "\" rel=\"stylesheet\" type=\"text/css\" />" + br);
- break;
- }
- }
- }
- }
- return sb.ToString();
- }
- #endregion
- }
- public class StaticFile
- {
- /// <summary>
- /// 缓存
- /// </summary>
- static WMCache cache = WMCache.GetCacheService();
- /// <summary>
- /// cache中的所有数据
- /// </summary>
- /// <returns></returns>
- public static List<StaticFileInfo> GetStaticFileList()
- {
- string key = CacheKeys.Static_File;
- List<StaticFileInfo> list = cache.GetObject<List<StaticFileInfo>>(key) as List<StaticFileInfo>;
- if (list == null)
- {
- list = StaticFileData.GetStaticFileList();
- cache.AddObject(key, list, (int)CacheTime.System);
- }
- return list;
- }
- /// <summary>
- /// 某个文件
- /// </summary>
- /// <param name="name"></param>
- /// <returns></returns>
- public static StaticFileInfo GetStaticFile(string name, string type)
- {
- StaticFileInfo info = new StaticFileInfo();
- var list = GetStaticFileList();
- foreach (var item in list)
- {
- if (!string.IsNullOrWhiteSpace(type))
- {
- if (item.name.Trim().Equals(name, StringComparison.CurrentCultureIgnoreCase)
- && item.type.Equals(type, StringComparison.CurrentCultureIgnoreCase))
- return item;
- }
- else
- {
- if (item.name.Trim().Equals(name, StringComparison.CurrentCultureIgnoreCase))
- return item;
- }
- }
- return info;
- }
- }
- public class StaticFileInfo
- {
- /// <summary>
- /// 名称
- /// </summary>
- public string name { get; set; }
- /// <summary>
- /// 地址
- /// </summary>
- public string url { get; set; }
- /// <summary>
- /// 版本号
- /// </summary>
- public string ver { get; set; }
- /// <summary>
- /// 类型/css/js
- /// </summary>
- public string type { get; set; }
- }
- public class StaticFileData
- {
- /// <summary>
- /// get 文件中的static文件配置
- /// </summary>
- /// <returns></returns>
- public static List<StaticFileInfo> GetStaticFileList()
- {
- List<StaticFileInfo> list = new List<StaticFileInfo>();
- string result = string.Empty;
- try
- {
- string path = System.Web.HttpContext.Current.Server.MapPath("/") + "Config/data/static.config";
- result = File.ReadAllText(path);
- }
- catch (Exception ex)
- {
- throw new Exception("static.config配置错误:" + ex.Message);
- }
- if (!string.IsNullOrEmpty(result))
- {
- try
- {
- list = JsonConvert.DeserializeObject<List<StaticFileInfo>>(result);
- }
- catch (Exception ex)
- {
- throw new Exception("反序列化static.config出错:" + ex.Message);
- }
- }
- return list;
- }
- }
- }
|