BaseController.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. using CP.Business;
  2. using CP.Common;
  3. using CP.Kjh.Models;
  4. using CP.Model;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Configuration;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Web;
  12. using System.Web.Mvc;
  13. namespace CP.Kjh.Controllers
  14. {
  15. public class BaseController : Controller
  16. {
  17. /// <summary>
  18. /// 重新基类在Action执行之前的事情
  19. /// </summary>
  20. /// <param name="filterContext">重写方法的参数</param>
  21. protected override void OnActionExecuting(ActionExecutingContext filterContext)
  22. {
  23. //base.OnActionExecuting(filterContext);
  24. ////原始地址
  25. //var aurl = System.Web.HttpContext.Current.Request.RawUrl;
  26. //var url = GetOldUrl(aurl);
  27. //if (!url.IsEmpty())
  28. //{
  29. // filterContext.Result = Content("<script type='text/javascript'>top.location = \"" + url + "\";</script>");
  30. //}
  31. }
  32. static readonly string _Apiurl = ConfigurationManager.AppSettings["api55128"].ToString();
  33. static readonly string _ApiKey = ConfigurationManager.AppSettings["ApiKey"].ToString();
  34. WebHeaderCollection GetHeaders()
  35. {
  36. WebHeaderCollection webHeader = new WebHeaderCollection();
  37. webHeader["ApiKey"] = _ApiKey;
  38. return webHeader;
  39. }
  40. public List<ApiModel> GetToday()
  41. {
  42. string path = $"{_Apiurl}/api/today";
  43. string json = HttpHelp.HttpGet(path, 10, GetHeaders());
  44. if (!string.IsNullOrEmpty(json))
  45. {
  46. var info = JsonConvert.DeserializeObject<ApiList<ApiModel>>(json);
  47. return info.body.data;
  48. }
  49. return new List<ApiModel>();
  50. }
  51. public ApiModel GetOne(string ename, int qi=0)
  52. {
  53. string path = $"{_Apiurl}/api/one?ename={ename}&qi={qi}";
  54. string json = HttpHelp.HttpGet(path, 10, GetHeaders());
  55. if (!string.IsNullOrEmpty(json))
  56. {
  57. var info = JsonConvert.DeserializeObject<ApiOne<ApiModel>>(json);
  58. return info.body.data;
  59. }
  60. return new ApiModel();
  61. }
  62. public List<ApiModel> GetList(string ename, int n = 30)
  63. {
  64. string path = $"{_Apiurl}/api/list?ename={ename}&n={n}&y={0}&sqi={0}&eqi={0}";
  65. string json = HttpHelp.HttpGet(path, 10, GetHeaders());
  66. if (!string.IsNullOrEmpty(json))
  67. {
  68. var info = JsonConvert.DeserializeObject<ApiList<ApiModel>>(json);
  69. return info.body.data;
  70. }
  71. return new List<ApiModel>();
  72. }
  73. public List<ApiModel> GetList(string ename, int? y, int? sqi, int? eqi, int n = 30)
  74. {
  75. string path = $"{_Apiurl}/api/list?ename={ename}&n={n}&y={(y.HasValue ? y : 0)}&sqi={(sqi.HasValue ? sqi : 0)}&eqi={(eqi.HasValue ? eqi : 0)}";
  76. string json = HttpHelp.HttpGet(path, 10, GetHeaders());
  77. if (!string.IsNullOrEmpty(json))
  78. {
  79. var info = JsonConvert.DeserializeObject<ApiList<ApiModel>>(json);
  80. return info.body.data;
  81. }
  82. return new List<ApiModel>();
  83. }
  84. public List<ApiModel> GetLsList(string ename, string type,int qi)
  85. {
  86. string path = $"{_Apiurl}/api/list?ename={ename}&type={type}&qi={qi}";
  87. string json = HttpHelp.HttpGet(path,10, GetHeaders());
  88. if (!string.IsNullOrEmpty(json))
  89. {
  90. var info = JsonConvert.DeserializeObject<ApiList<ApiModel>>(json);
  91. return info.body.data;
  92. }
  93. return new List<ApiModel>();
  94. }
  95. public string GetOldUrl(string url)
  96. {
  97. url = url.ToLower();
  98. var name = url.Contains("kjxq") ? ViewEnum.KJXQ :
  99. url.Contains("kjls") ? ViewEnum.KJLS :
  100. url.Contains("kjh") ? ViewEnum.KJH :
  101. url.Contains("sjh") ? ViewEnum.SJH :
  102. url.Contains("index.html") ? ViewEnum.INDEX :
  103. url.Contains("qgc.html") ? ViewEnum.QGCINDEX :
  104. url.Contains("dfc.html") ? ViewEnum.DFCINDEX :
  105. url.Contains("gpc.html") ? ViewEnum.GPCINDEX :
  106. ViewEnum.NO;
  107. var list = url.Split('/')[url.Split('/').Length - 1].Split('.')[0].Split('-').ToList();
  108. if (list[0].IsEmpty() || name == ViewEnum.NO || url.IsEmpty() || !url.Contains("/k/"))
  109. return "";
  110. var oldName = "";
  111. if (!new List<ViewEnum> {ViewEnum.INDEX, ViewEnum.QGCINDEX, ViewEnum.DFCINDEX, ViewEnum.GPCINDEX }.Contains(name))
  112. {
  113. if (name == ViewEnum.KJH || name == ViewEnum.SJH)
  114. oldName = RouteHelper.RouteList.SingleOrDefault(p => p.ViewNewName == list[0]).OldName;
  115. else
  116. oldName = RouteHelper.RouteList.SingleOrDefault(p => p.ViewNewName == list[1]).OldName;
  117. }
  118. switch (name)
  119. {
  120. case ViewEnum.KJXQ:
  121. ;
  122. if (list.Count > 2)
  123. return $"/{oldName}-kjjg-{list[2]}.htm";
  124. else
  125. return $"/xq_{oldName}.aspx";
  126. case ViewEnum.KJLS:
  127. if (list.Count > 2)
  128. {
  129. if (list[0] == "gpc")
  130. return $"/{oldName}-history-{list[2].ToDateTime1().ToString("yyyy-MM-dd")}.htm";
  131. if (list[2].Contains("q"))
  132. return $"/history_{oldName}.aspx?selectyear=按年份&newterm={list[2].Remove(0, 1)}";
  133. return $"/{oldName}-history-{list[2].Remove(0, 1)}.aspx";
  134. }
  135. else
  136. return $"/history_{oldName}.aspx";
  137. case ViewEnum.KJH:
  138. var newName = RouteHelper.RouteList.SingleOrDefault(p => p.ViewNewName == list[0]).NewName;
  139. if (list.Count == 2)
  140. return $"/{oldName}-kaijihao-{list[1].Remove(0, 1)}.htm";
  141. if (list.Count == 3)
  142. return $"/{oldName}-kaijihao-{list[1]}-{list[2]}.htm";
  143. return $"/{oldName}{(newName == QGEnum.fcsd.ToString() ? "kjh.aspx" : "-kaijihao")}";
  144. case ViewEnum.SJH:
  145. newName = RouteHelper.RouteList.SingleOrDefault(p => p.ViewNewName == list[0]).NewName;
  146. if (list.Count == 2)
  147. return $"/{oldName}-shijihao-{list[1].Remove(0, 1)}.htm";
  148. if (list.Count == 3)
  149. return $"/{oldName}-shijihao-{list[1]}-{list[2]}.htm";
  150. return $"/{oldName}sjh.aspx";
  151. case ViewEnum.INDEX:
  152. return "/index.aspx";
  153. case ViewEnum.QGCINDEX:
  154. return "/qgc.aspx";
  155. case ViewEnum.DFCINDEX:
  156. return "/dfc.aspx";
  157. case ViewEnum.GPCINDEX:
  158. return "/gpc.aspx";
  159. }
  160. return "";
  161. }
  162. }
  163. }