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配置测试方式
//
//
//
//
//
///
/// TV走势图API接口规范
///
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("
");
context.Response.Write("EncryptSource=" + _s);
context.Response.Write("
");
var _array = _u.Split('|');
var _encryptKey = _array[0].Replace("-", "");
if (2 >= _array.Length)
_encryptKey += _array[1];
context.Response.Write("加密KEY=" + _encryptKey);
context.Response.Write("
");
if (16 == _encryptKey.Length)
{
context.Response.Write("AES加密流程:key为固定16位.然后直接AES.Encrypt('被加密字符串','密钥')");
context.Response.Write("
");
context.Response.Write(Encrypt.AES.Encrypt(_s, _encryptKey));
context.Response.Write("
");
}
}
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 常量
///
/// 暂无资料
///
private const string NoDataMsg = "暂无资料";
///
/// 错误
///
private const int ErrorCode = 1;
///
/// 正常
///
private const int SuccessCode = 0;
///
/// 异常
///
private const int UnknownCode = 99;
///
/// 默认异常信息
///
private const string UnknownErrorMsg = "未知错误";
#endregion
#region 常用方法及验证
///
/// 基本数据校验
///
///
/// xml有Data节点时返回对应XmlNode值,否则为NULL
///
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;
}
///
/// 数据验证
///
///
///
///
///
///
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
///
/// 获取配置信息
///
///
///
public static string GetAppConfig(XmlNode node, string userName)
{
var result = new ViewResult() { 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();
}
///
/// 获取区域列表
///
///
///
public static string GetAreaList()
{
var result = new ViewResult() { 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();
}
///
/// 获取彩种列表
///
///
///
public static string GetLotteryList(XmlNode node)
{
var result = new ViewResult() { 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();
}
///
/// 获取走势图列表
///
///
///
public static string GetChartList(XmlNode node)
{
var result = new ViewResult() { 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();
}
///
/// 保存用户信息
///
///
///
public static string SaveUserInfo(XmlNode node, string userName)
{
var result = new ViewResult() { 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();
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();
}
///
/// 获取APP开机图和背景图
///
///
///
public static string GetAppImage()
{
var result = new ViewResult() { 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();
}
}
}