OpenAPI.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using System.Web;
  6. using System.Xml;
  7. using CB.TVUCenter.View;
  8. namespace CB.TVUCenter
  9. {
  10. //web.config配置测试方式
  11. //<system.webServer>
  12. // <handlers>
  13. // <add name="openapi" path="openapi/*.aspx" verb="GET,POST" type="CB.TVUCenter.OpenAPI" resourceType="Unspecified" requireAccess="Read" preCondition="integratedMode" />
  14. // </handlers>
  15. //</system.webServer>
  16. /// <summary>
  17. /// TV走势图API接口规范
  18. /// </summary>
  19. public class OpenAPI : IHttpHandler
  20. {
  21. public bool IsReusable
  22. {
  23. get { return false; }
  24. }
  25. public void ProcessRequest(HttpContext context)
  26. {
  27. string urlPath = context.Request.Url.AbsolutePath.ToLower();
  28. if (!Regex.IsMatch(urlPath, @"openapi/\w+.aspx", RegexOptions.IgnoreCase))
  29. return;
  30. string fileName = System.IO.Path.GetFileNameWithoutExtension(urlPath);
  31. string param = CB.Common.WRequest.GetFormString("p");
  32. #region Debug 测试相关
  33. if (Config.TVConfigs.GetConfig().Debug)
  34. {
  35. context.Response.ContentType = "text/xml";
  36. if ("testaes" == fileName)
  37. {
  38. string _u = "", _s = "";
  39. if ("POST" == context.Request.HttpMethod.ToUpper())
  40. {
  41. _u = CB.Common.WRequest.GetFormString("UserName").Trim();
  42. _s = CB.Common.WRequest.GetFormString("EncryptSource").Trim();
  43. }
  44. else
  45. {
  46. _u = CB.Common.WRequest.GetQueryString("UserName").Trim();
  47. _s = CB.Common.WRequest.GetQueryString("EncryptSource").Trim();
  48. }
  49. if (!string.IsNullOrEmpty(_s) && !string.IsNullOrEmpty(_u))
  50. {
  51. context.Response.Write("UserName=" + _u);
  52. context.Response.Write("<br />");
  53. context.Response.Write("EncryptSource=" + _s);
  54. context.Response.Write("<br />");
  55. var _array = _u.Split('|');
  56. var _encryptKey = _array[0].Replace("-", "");
  57. if (2 >= _array.Length)
  58. _encryptKey += _array[1];
  59. context.Response.Write("加密KEY=" + _encryptKey);
  60. context.Response.Write("<br />");
  61. if (16 == _encryptKey.Length)
  62. {
  63. context.Response.Write("AES加密流程:key为固定16位.然后直接AES.Encrypt('被加密字符串','密钥')");
  64. context.Response.Write("<br />");
  65. context.Response.Write(Encrypt.AES.Encrypt(_s, _encryptKey));
  66. context.Response.Write("<br />");
  67. }
  68. }
  69. return;
  70. }
  71. }
  72. #endregion
  73. XmlNode node = null;
  74. string userName = "";
  75. if (CheckAuthority(param, ref node, ref userName))
  76. {
  77. switch (fileName)
  78. {
  79. case "getappconfig":
  80. context.Response.Write(GetAppConfig(node, userName));
  81. break;
  82. case "getarealist":
  83. context.Response.Write(GetAreaList());
  84. break;
  85. case "getlotterylist":
  86. context.Response.Write(GetLotteryList(node));
  87. break;
  88. case "getchartlist":
  89. context.Response.Write(GetChartList(node));
  90. break;
  91. case "saveuserinfo":
  92. context.Response.Write(SaveUserInfo(node, userName));
  93. break;
  94. case "getappimage":
  95. context.Response.Write(GetAppImage());
  96. break;
  97. }
  98. return;
  99. }
  100. }
  101. #region 常量
  102. /// <summary>
  103. /// 暂无资料
  104. /// </summary>
  105. private const string NoDataMsg = "暂无资料";
  106. /// <summary>
  107. /// 错误
  108. /// </summary>
  109. private const int ErrorCode = 1;
  110. /// <summary>
  111. /// 正常
  112. /// </summary>
  113. private const int SuccessCode = 0;
  114. /// <summary>
  115. /// 异常
  116. /// </summary>
  117. private const int UnknownCode = 99;
  118. /// <summary>
  119. /// 默认异常信息
  120. /// </summary>
  121. private const string UnknownErrorMsg = "未知错误";
  122. #endregion
  123. #region 常用方法及验证
  124. /// <summary>
  125. /// 基本数据校验
  126. /// </summary>
  127. /// <param name="xml"></param>
  128. /// <param name="requestData">xml有Data节点时返回对应XmlNode值,否则为NULL</param>
  129. /// <returns></returns>
  130. private static bool CheckAuthority(string xml, ref XmlNode requestData, ref string realUserName)
  131. {
  132. if (string.IsNullOrEmpty(xml))
  133. {
  134. CB.Common.FileUtil.WriteLog("XML文档为空!");
  135. return false;
  136. }
  137. XmlDocument doc = new XmlDocument();
  138. try
  139. { doc.LoadXml(xml); }
  140. catch
  141. {
  142. CB.Common.FileUtil.WriteLog("XML文档解析失败!");
  143. return false;
  144. }
  145. var root = doc.SelectSingleNode("Channel");
  146. if (null != root)
  147. {
  148. string userName = "", sign = "", data = "";
  149. var node = root.SelectSingleNode("UserName");
  150. if (null != node)
  151. userName = node.InnerText.Trim();
  152. node = root.SelectSingleNode("Sign");
  153. if (null != node)
  154. sign = node.InnerText.Trim();
  155. requestData = root.SelectSingleNode("Data");
  156. if (null != requestData)
  157. data = requestData.InnerXml.Trim();
  158. if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(sign))
  159. {
  160. CB.Common.FileUtil.WriteLog("UserName节点或者Sign节点值为空!");
  161. return false;
  162. }
  163. if (CheckSign(userName, data, sign, ref realUserName))
  164. {
  165. return !Data.OnlineUsers.CheckUserLock(new Entity.OnlineUserInfo()
  166. {
  167. UserName = realUserName,
  168. Sign = sign,
  169. RequestTime = DateTime.Now
  170. });
  171. }
  172. }
  173. CB.Common.FileUtil.WriteLog("XML文档无Channel节点!");
  174. return false;
  175. }
  176. /// <summary>
  177. /// 数据验证
  178. /// </summary>
  179. /// <param name="userName"></param>
  180. /// <param name="data"></param>
  181. /// <param name="sign"></param>
  182. /// <param name="realUserName"></param>
  183. /// <returns></returns>
  184. private static bool CheckSign(string userName, string data, string sign, ref string realUserName)
  185. {
  186. var array = userName.Split('|');
  187. realUserName = array[0].Trim();
  188. var encryptKey = array[0].Replace("-", "");
  189. if (2 >= array.Length)
  190. encryptKey += array[1].Trim();
  191. if (16 != encryptKey.Length)
  192. {
  193. CB.Common.FileUtil.WriteLog("加密Key长度不正确!");
  194. return false;
  195. }
  196. var encryptStr = string.IsNullOrEmpty(data) ? userName + Config.TVConfigs.GetConfig().ExEncryptKey : userName + data;
  197. //return true;//用户测试取消验证 正式测试则去掉
  198. return sign == Encrypt.AES.Encrypt(encryptStr, encryptKey);
  199. }
  200. #endregion
  201. /// <summary>
  202. /// 获取配置信息
  203. /// </summary>
  204. /// <param name="xml"></param>
  205. /// <returns></returns>
  206. public static string GetAppConfig(XmlNode node, string userName)
  207. {
  208. var result = new ViewResult<View.ConfigInfo>() { Code = ErrorCode };
  209. if (string.IsNullOrEmpty(userName))
  210. return result.ToString();
  211. result.Entity = new View.ConfigInfo()
  212. {
  213. EncryptKey = Config.TVConfigs.GetConfig().TrendChartEncryptKey,
  214. ShowByBrowser = Config.TVConfigs.GetConfig().ShowByBrowser,
  215. AreaVersion = Config.TVConfigs.GetConfig().AreaVersion,
  216. ImgVersion = Config.TVConfigs.GetConfig().ImgVersion
  217. };
  218. var user = CB.Data.TVUserService.GetTVUserByMac(userName);
  219. if (null != user)
  220. {
  221. var area = CB.Data.Caches.GetAreaInfo(user.AreaId);
  222. var lottery = CB.Data.Caches.GetLotteryInfo(user.LotteryId);
  223. var chart = CB.Data.Caches.GetTrendChartInfo(user.ChartId);
  224. var vUser = new View.UserInfo()
  225. {
  226. Uid = user.Uid,
  227. Direction = user.Direction,
  228. };
  229. if (null != area)
  230. {
  231. vUser.AreaId = user.AreaId;
  232. vUser.AreaName = area.Name;
  233. }
  234. if (null != lottery)
  235. {
  236. vUser.LotteryId = user.LotteryId;
  237. vUser.LotteryName = lottery.Name;
  238. }
  239. if (null != chart)
  240. {
  241. vUser.ChartId = user.ChartId;
  242. vUser.ChartName = chart.Name;
  243. }
  244. result.Entity.UserInfo = vUser;
  245. }
  246. result.Code = SuccessCode;
  247. return result.ToString();
  248. }
  249. /// <summary>
  250. /// 获取区域列表
  251. /// </summary>
  252. /// <param name="xml"></param>
  253. /// <returns></returns>
  254. public static string GetAreaList()
  255. {
  256. var result = new ViewResult<View.AreaListInfo>() { Code = ErrorCode };
  257. var list = CB.Data.Caches.GetAreaList();
  258. if (null == list || 0 >= list.Count)
  259. {
  260. result.Message = "未找到数据!";
  261. return result.ToString();
  262. }
  263. result.Entity = new View.AreaListInfo();
  264. result.Entity.AreaList = list.Select(item => new View.AreaInfo()
  265. {
  266. Aid = item.Aid,
  267. AreaName = item.Name
  268. }).ToList();
  269. result.Code = SuccessCode;
  270. return result.ToString();
  271. }
  272. /// <summary>
  273. /// 获取彩种列表
  274. /// </summary>
  275. /// <param name="xml"></param>
  276. /// <returns></returns>
  277. public static string GetLotteryList(XmlNode node)
  278. {
  279. var result = new ViewResult<View.LotteryListInfo>() { Code = ErrorCode };
  280. if (null == node)
  281. {
  282. result.Message = "Data节点不存在!";
  283. return result.ToString();
  284. }
  285. var n = node.SelectSingleNode("Aid");
  286. int aid = null == n ? 0 : CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
  287. var list = CB.Data.Caches.GetLotteryList(aid);
  288. if (null == list || 0 >= list.Count)
  289. {
  290. result.Message = "未查找到数据!";
  291. return result.ToString();
  292. }
  293. result.Entity = new LotteryListInfo();
  294. result.Entity.LotteryList = list.Select(item => new View.LotteryInfo() { Lid = item.Cid, LotteryName = item.Name }).ToList();
  295. result.Code = SuccessCode;
  296. return result.ToString();
  297. }
  298. /// <summary>
  299. /// 获取走势图列表
  300. /// </summary>
  301. /// <param name="xml"></param>
  302. /// <returns></returns>
  303. public static string GetChartList(XmlNode node)
  304. {
  305. var result = new ViewResult<View.ChartListInfo>() { Code = ErrorCode };
  306. if (null == node)
  307. {
  308. result.Message = "Data节点不存在!";
  309. return result.ToString();
  310. }
  311. var n = node.SelectSingleNode("Lid");
  312. int cid = null == n ? 0 : CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
  313. var list = CB.Data.Caches.GetTrendChartList(cid, 2001);
  314. if (null == list || 0 >= list.Count)
  315. {
  316. result.Message = "未找到有效数据";
  317. return result.ToString();
  318. }
  319. result.Entity = new View.ChartListInfo();
  320. result.Entity.ChartList = list.Select(item => new View.ChartInfo()
  321. {
  322. Cid = item.Cid,
  323. ChartId = item.Id,
  324. ChartName = item.Name
  325. }).ToList();
  326. result.Code = SuccessCode;
  327. return result.ToString();
  328. }
  329. /// <summary>
  330. /// 保存用户信息
  331. /// </summary>
  332. /// <param name="xml"></param>
  333. /// <returns></returns>
  334. public static string SaveUserInfo(XmlNode node, string userName)
  335. {
  336. var result = new ViewResult<View.ChartServerInfo>() { Code = ErrorCode };
  337. if (null == node)
  338. {
  339. result.Message = "没找到Data节点";
  340. return result.ToString();
  341. }
  342. var user = new CB.Entity.TVUserInfo() { MacAddr = userName };
  343. var n = node.SelectSingleNode("Uid");
  344. if (null != n)
  345. user.Uid = CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
  346. n = node.SelectSingleNode("AreaId");
  347. if (null != n)
  348. user.AreaId = CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
  349. n = node.SelectSingleNode("LotteryId");
  350. if (null != n)
  351. user.LotteryId = CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
  352. n = node.SelectSingleNode("ChartId");
  353. if (null != n)
  354. user.ChartId = CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
  355. n = node.SelectSingleNode("Direction");
  356. if (null != n)
  357. user.Direction = CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
  358. n = node.SelectSingleNode("OSInfo");
  359. if (null != n)
  360. user.OSInfo = n.InnerText.Trim();
  361. if (0 >= user.Uid || 0 >= user.AreaId || 0 >= user.ChartId || 0 >= user.LotteryId)
  362. {
  363. result.Message = "Data节点存在无效数据";
  364. return result.ToString();
  365. }
  366. if (CB.Data.TVUserService.Update(user))
  367. {
  368. var list = CB.Data.TVServerService.GetTVServer(user.LotteryId);
  369. if (null != list && 0 < list.Count)
  370. {
  371. var urls = new List<string>();
  372. foreach (var item in list)
  373. {
  374. urls.Add(item.Url);
  375. }
  376. result.Entity = new View.ChartServerInfo() { Urls = urls };
  377. result.Code = SuccessCode;
  378. return result.ToString();
  379. }
  380. result.Message = "未找到数据";
  381. return result.ToString();
  382. }
  383. result.Code = UnknownCode;
  384. result.Message = UnknownErrorMsg;
  385. return result.ToString();
  386. }
  387. /// <summary>
  388. /// 获取APP开机图和背景图
  389. /// </summary>
  390. /// <param name="xml"></param>
  391. /// <returns></returns>
  392. public static string GetAppImage()
  393. {
  394. var result = new ViewResult<View.ImgConfig>() { Code = ErrorCode };
  395. result.Code = SuccessCode;
  396. result.Entity = new View.ImgConfig()
  397. {
  398. InitImg = Config.TVConfigs.GetConfig().InitImg,
  399. InitVerticalImg = Config.TVConfigs.GetConfig().InitVerticalImg,
  400. BackgroundImg = Config.TVConfigs.GetConfig().BackgroundImg,
  401. BackgroundVerticalImg = Config.TVConfigs.GetConfig().BackgroundVerticalImg
  402. };
  403. return result.ToString();
  404. }
  405. }
  406. }