Article.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Xml;
  4. using System.Text;
  5. using CB.Cache;
  6. using CB.Common;
  7. namespace CB.Data.Synchronize
  8. {
  9. /// <summary>
  10. /// 站外文章显示
  11. /// </summary>
  12. public class Article
  13. {
  14. /// <summary>
  15. /// 调用站外文章
  16. /// </summary>
  17. /// <param name="topSize">topSize</param>
  18. /// <param name="pageSize">pageSize</param>
  19. /// <param name="cid">文章分类</param>
  20. /// <param name="wap">是否WAP站</param>
  21. /// <returns></returns>
  22. public static IList<ArticleInfo> GetArticleList(int topSize, int pageSize, int cid, bool wap = false)
  23. {
  24. string key = string.Format("{0}-{1}-{2}", CacheKeys.SyncArticleList, pageSize, cid);
  25. var cache = CBCache.GetCacheService();
  26. IList<ArticleInfo> list = cache.GetObject(key) as IList<ArticleInfo>;
  27. if (null == list)
  28. {
  29. list = GetArticleList(pageSize, cid, wap);
  30. cache.AddObject(key, list);
  31. }
  32. if (null == list)
  33. return null;
  34. IList<ArticleInfo> r = new List<ArticleInfo>();
  35. int count = list.Count;
  36. count = topSize > count ? count : topSize;
  37. for (int i = 0; i < topSize; i++)
  38. {
  39. r.Add(list[i]);
  40. }
  41. return r;
  42. }
  43. private static IList<ArticleInfo> GetArticleList(int pageSize, int cid, bool wap)
  44. {
  45. if (string.IsNullOrEmpty(CB.Config.BaseConfigs.GetConfig().SynArticleUrl))
  46. return null;
  47. string userName = "55128";
  48. if (wap) userName = "55128wap";
  49. string passKey = "d3d3LjU1MTI4LmNu";
  50. string paras = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("topSize={0}|cid={1}",
  51. pageSize, cid)));
  52. string sign = Utils.MD5(passKey + userName + paras);
  53. string reqParas = string.Format("username={0}&paras={1}&sign={2}", userName, paras, sign);
  54. string str = Utils.GetHttpWebResponse(CB.Config.BaseConfigs.GetConfig().SynArticleUrl, reqParas);
  55. if (string.IsNullOrEmpty(str))
  56. return null;
  57. XmlDocument doc = new XmlDocument();
  58. try
  59. {
  60. doc.LoadXml(str);
  61. }
  62. catch { return null; }
  63. XmlNodeList nodeList = doc.GetElementsByTagName("data");
  64. if (null == nodeList || 0 == nodeList.Count)
  65. return null;
  66. IList<ArticleInfo> list = new List<ArticleInfo>();
  67. ArticleInfo entity = null;
  68. XmlNode n = null;
  69. foreach (XmlNode node in nodeList)
  70. {
  71. entity = new ArticleInfo();
  72. n = node.SelectSingleNode("title");
  73. if (null != n)
  74. entity.Title = n.InnerText.Trim();
  75. n = node.SelectSingleNode("time");
  76. if (null != n)
  77. entity.Time = TypeConverter.StrToDateTime(n.InnerText, DateTime.MinValue);
  78. n = node.SelectSingleNode("url");
  79. if (null != n)
  80. entity.Url = n.InnerText.Trim();
  81. list.Add(entity);
  82. }
  83. return list;
  84. }
  85. /// <summary>
  86. /// 站外文章实体
  87. /// </summary>
  88. public class ArticleInfo
  89. {
  90. /// <summary>
  91. /// 标题
  92. /// </summary>
  93. public string Title { get; set; }
  94. /// <summary>
  95. /// 文章链接
  96. /// </summary>
  97. public string Url { get; set; }
  98. /// <summary>
  99. /// 文章时间
  100. /// </summary>
  101. public DateTime Time { get; set; }
  102. }
  103. }
  104. }