123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- using System.Web;
- using System.Xml;
- using CB.TVUCenter.View;
- namespace CB.TVUCenter
- {
- //web.config配置测试方式
- //<system.webServer>
- // <handlers>
- // <add name="openapi" path="openapi/*.aspx" verb="GET,POST" type="CB.TVUCenter.OpenAPI" resourceType="Unspecified" requireAccess="Read" preCondition="integratedMode" />
- // </handlers>
- //</system.webServer>
- /// <summary>
- /// TV走势图API接口规范
- /// </summary>
- public class OpenAPI : IHttpHandler
- {
- public bool IsReusable
- {
- get { return false; }
- }
- public void ProcessRequest(HttpContext context)
- {
- string urlPath = context.Request.Url.AbsolutePath.ToLower();
- if (!Regex.IsMatch(urlPath, @"openapi/\w+.aspx", RegexOptions.IgnoreCase))
- return;
- string fileName = System.IO.Path.GetFileNameWithoutExtension(urlPath);
- string param = CB.Common.WRequest.GetFormString("p");
- #region Debug 测试相关
- if (Config.TVConfigs.GetConfig().Debug)
- {
- context.Response.ContentType = "text/xml";
- if ("testaes" == fileName)
- {
- string _u = "", _s = "";
- if ("POST" == context.Request.HttpMethod.ToUpper())
- {
- _u = CB.Common.WRequest.GetFormString("UserName").Trim();
- _s = CB.Common.WRequest.GetFormString("EncryptSource").Trim();
- }
- else
- {
- _u = CB.Common.WRequest.GetQueryString("UserName").Trim();
- _s = CB.Common.WRequest.GetQueryString("EncryptSource").Trim();
- }
- if (!string.IsNullOrEmpty(_s) && !string.IsNullOrEmpty(_u))
- {
- context.Response.Write("UserName=" + _u);
- context.Response.Write("<br />");
- context.Response.Write("EncryptSource=" + _s);
- context.Response.Write("<br />");
- var _array = _u.Split('|');
- var _encryptKey = _array[0].Replace("-", "");
- if (2 >= _array.Length)
- _encryptKey += _array[1];
- context.Response.Write("加密KEY=" + _encryptKey);
- context.Response.Write("<br />");
- if (16 == _encryptKey.Length)
- {
- context.Response.Write("AES加密流程:key为固定16位.然后直接AES.Encrypt('被加密字符串','密钥')");
- context.Response.Write("<br />");
- context.Response.Write(Encrypt.AES.Encrypt(_s, _encryptKey));
- context.Response.Write("<br />");
- }
- }
- return;
- }
- }
- #endregion
- XmlNode node = null;
- string userName = "";
- if (CheckAuthority(param, ref node, ref userName))
- {
- switch (fileName)
- {
- case "getappconfig":
- context.Response.Write(GetAppConfig(node, userName));
- break;
- case "getarealist":
- context.Response.Write(GetAreaList());
- break;
- case "getlotterylist":
- context.Response.Write(GetLotteryList(node));
- break;
- case "getchartlist":
- context.Response.Write(GetChartList(node));
- break;
- case "saveuserinfo":
- context.Response.Write(SaveUserInfo(node, userName));
- break;
- case "getappimage":
- context.Response.Write(GetAppImage());
- break;
- }
- return;
- }
- }
- #region 常量
- /// <summary>
- /// 暂无资料
- /// </summary>
- private const string NoDataMsg = "暂无资料";
- /// <summary>
- /// 错误
- /// </summary>
- private const int ErrorCode = 1;
- /// <summary>
- /// 正常
- /// </summary>
- private const int SuccessCode = 0;
- /// <summary>
- /// 异常
- /// </summary>
- private const int UnknownCode = 99;
- /// <summary>
- /// 默认异常信息
- /// </summary>
- private const string UnknownErrorMsg = "未知错误";
- #endregion
- #region 常用方法及验证
- /// <summary>
- /// 基本数据校验
- /// </summary>
- /// <param name="xml"></param>
- /// <param name="requestData">xml有Data节点时返回对应XmlNode值,否则为NULL</param>
- /// <returns></returns>
- private static bool CheckAuthority(string xml, ref XmlNode requestData, ref string realUserName)
- {
- if (string.IsNullOrEmpty(xml))
- {
- CB.Common.FileUtil.WriteLog("XML文档为空!");
- return false;
- }
- XmlDocument doc = new XmlDocument();
- try
- { doc.LoadXml(xml); }
- catch
- {
- CB.Common.FileUtil.WriteLog("XML文档解析失败!");
- return false;
- }
- var root = doc.SelectSingleNode("Channel");
- if (null != root)
- {
- string userName = "", sign = "", data = "";
- var node = root.SelectSingleNode("UserName");
- if (null != node)
- userName = node.InnerText.Trim();
- node = root.SelectSingleNode("Sign");
- if (null != node)
- sign = node.InnerText.Trim();
- requestData = root.SelectSingleNode("Data");
- if (null != requestData)
- data = requestData.InnerXml.Trim();
- if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(sign))
- {
- CB.Common.FileUtil.WriteLog("UserName节点或者Sign节点值为空!");
- return false;
- }
- if (CheckSign(userName, data, sign, ref realUserName))
- {
- return !Data.OnlineUsers.CheckUserLock(new Entity.OnlineUserInfo()
- {
- UserName = realUserName,
- Sign = sign,
- RequestTime = DateTime.Now
- });
- }
- }
- CB.Common.FileUtil.WriteLog("XML文档无Channel节点!");
- return false;
- }
- /// <summary>
- /// 数据验证
- /// </summary>
- /// <param name="userName"></param>
- /// <param name="data"></param>
- /// <param name="sign"></param>
- /// <param name="realUserName"></param>
- /// <returns></returns>
- private static bool CheckSign(string userName, string data, string sign, ref string realUserName)
- {
- var array = userName.Split('|');
- realUserName = array[0].Trim();
- var encryptKey = array[0].Replace("-", "");
- if (2 >= array.Length)
- encryptKey += array[1].Trim();
- if (16 != encryptKey.Length)
- {
- CB.Common.FileUtil.WriteLog("加密Key长度不正确!");
- return false;
- }
- var encryptStr = string.IsNullOrEmpty(data) ? userName + Config.TVConfigs.GetConfig().ExEncryptKey : userName + data;
- //return true;//用户测试取消验证 正式测试则去掉
- return sign == Encrypt.AES.Encrypt(encryptStr, encryptKey);
- }
- #endregion
- /// <summary>
- /// 获取配置信息
- /// </summary>
- /// <param name="xml"></param>
- /// <returns></returns>
- public static string GetAppConfig(XmlNode node, string userName)
- {
- var result = new ViewResult<View.ConfigInfo>() { Code = ErrorCode };
- if (string.IsNullOrEmpty(userName))
- return result.ToString();
- result.Entity = new View.ConfigInfo()
- {
- EncryptKey = Config.TVConfigs.GetConfig().TrendChartEncryptKey,
- ShowByBrowser = Config.TVConfigs.GetConfig().ShowByBrowser,
- AreaVersion = Config.TVConfigs.GetConfig().AreaVersion,
- ImgVersion = Config.TVConfigs.GetConfig().ImgVersion
- };
- var user = CB.Data.TVUserService.GetTVUserByMac(userName);
- if (null != user)
- {
- var area = CB.Data.Caches.GetAreaInfo(user.AreaId);
- var lottery = CB.Data.Caches.GetLotteryInfo(user.LotteryId);
- var chart = CB.Data.Caches.GetTrendChartInfo(user.ChartId);
- var vUser = new View.UserInfo()
- {
- Uid = user.Uid,
- Direction = user.Direction,
- };
- if (null != area)
- {
- vUser.AreaId = user.AreaId;
- vUser.AreaName = area.Name;
- }
- if (null != lottery)
- {
- vUser.LotteryId = user.LotteryId;
- vUser.LotteryName = lottery.Name;
- }
- if (null != chart)
- {
- vUser.ChartId = user.ChartId;
- vUser.ChartName = chart.Name;
- }
- result.Entity.UserInfo = vUser;
- }
- result.Code = SuccessCode;
- return result.ToString();
- }
- /// <summary>
- /// 获取区域列表
- /// </summary>
- /// <param name="xml"></param>
- /// <returns></returns>
- public static string GetAreaList()
- {
- var result = new ViewResult<View.AreaListInfo>() { Code = ErrorCode };
- var list = CB.Data.Caches.GetAreaList();
- if (null == list || 0 >= list.Count)
- {
- result.Message = "未找到数据!";
- return result.ToString();
- }
- result.Entity = new View.AreaListInfo();
- result.Entity.AreaList = list.Select(item => new View.AreaInfo()
- {
- Aid = item.Aid,
- AreaName = item.Name
- }).ToList();
- result.Code = SuccessCode;
- return result.ToString();
- }
- /// <summary>
- /// 获取彩种列表
- /// </summary>
- /// <param name="xml"></param>
- /// <returns></returns>
- public static string GetLotteryList(XmlNode node)
- {
- var result = new ViewResult<View.LotteryListInfo>() { Code = ErrorCode };
- if (null == node)
- {
- result.Message = "Data节点不存在!";
- return result.ToString();
- }
- var n = node.SelectSingleNode("Aid");
- int aid = null == n ? 0 : CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
- var list = CB.Data.Caches.GetLotteryList(aid);
- if (null == list || 0 >= list.Count)
- {
- result.Message = "未查找到数据!";
- return result.ToString();
- }
- result.Entity = new LotteryListInfo();
- result.Entity.LotteryList = list.Select(item => new View.LotteryInfo() { Lid = item.Cid, LotteryName = item.Name }).ToList();
- result.Code = SuccessCode;
- return result.ToString();
- }
- /// <summary>
- /// 获取走势图列表
- /// </summary>
- /// <param name="xml"></param>
- /// <returns></returns>
- public static string GetChartList(XmlNode node)
- {
- var result = new ViewResult<View.ChartListInfo>() { Code = ErrorCode };
- if (null == node)
- {
- result.Message = "Data节点不存在!";
- return result.ToString();
- }
- var n = node.SelectSingleNode("Lid");
- int cid = null == n ? 0 : CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
- var list = CB.Data.Caches.GetTrendChartList(cid, 2001);
- if (null == list || 0 >= list.Count)
- {
- result.Message = "未找到有效数据";
- return result.ToString();
- }
- result.Entity = new View.ChartListInfo();
- result.Entity.ChartList = list.Select(item => new View.ChartInfo()
- {
- Cid = item.Cid,
- ChartId = item.Id,
- ChartName = item.Name
- }).ToList();
- result.Code = SuccessCode;
- return result.ToString();
- }
- /// <summary>
- /// 保存用户信息
- /// </summary>
- /// <param name="xml"></param>
- /// <returns></returns>
- public static string SaveUserInfo(XmlNode node, string userName)
- {
- var result = new ViewResult<View.ChartServerInfo>() { Code = ErrorCode };
- if (null == node)
- {
- result.Message = "没找到Data节点";
- return result.ToString();
- }
- var user = new CB.Entity.TVUserInfo() { MacAddr = userName };
- var n = node.SelectSingleNode("Uid");
- if (null != n)
- user.Uid = CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
- n = node.SelectSingleNode("AreaId");
- if (null != n)
- user.AreaId = CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
- n = node.SelectSingleNode("LotteryId");
- if (null != n)
- user.LotteryId = CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
- n = node.SelectSingleNode("ChartId");
- if (null != n)
- user.ChartId = CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
- n = node.SelectSingleNode("Direction");
- if (null != n)
- user.Direction = CB.Common.TypeConverter.StrToInt(n.InnerText.Trim());
- n = node.SelectSingleNode("OSInfo");
- if (null != n)
- user.OSInfo = n.InnerText.Trim();
- if (0 >= user.Uid || 0 >= user.AreaId || 0 >= user.ChartId || 0 >= user.LotteryId)
- {
- result.Message = "Data节点存在无效数据";
- return result.ToString();
- }
- if (CB.Data.TVUserService.Update(user))
- {
- var list = CB.Data.TVServerService.GetTVServer(user.LotteryId);
- if (null != list && 0 < list.Count)
- {
- var urls = new List<string>();
- foreach (var item in list)
- {
- urls.Add(item.Url);
- }
- result.Entity = new View.ChartServerInfo() { Urls = urls };
- result.Code = SuccessCode;
- return result.ToString();
- }
- result.Message = "未找到数据";
- return result.ToString();
- }
- result.Code = UnknownCode;
- result.Message = UnknownErrorMsg;
- return result.ToString();
- }
- /// <summary>
- /// 获取APP开机图和背景图
- /// </summary>
- /// <param name="xml"></param>
- /// <returns></returns>
- public static string GetAppImage()
- {
- var result = new ViewResult<View.ImgConfig>() { Code = ErrorCode };
- result.Code = SuccessCode;
- result.Entity = new View.ImgConfig()
- {
- InitImg = Config.TVConfigs.GetConfig().InitImg,
- InitVerticalImg = Config.TVConfigs.GetConfig().InitVerticalImg,
- BackgroundImg = Config.TVConfigs.GetConfig().BackgroundImg,
- BackgroundVerticalImg = Config.TVConfigs.GetConfig().BackgroundVerticalImg
- };
- return result.ToString();
- }
- }
- }
|