OpenCaiApiServices.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using SCC.Common;
  5. using SCC.Interface;
  6. using SCC.Models;
  7. namespace SCC.Services
  8. {
  9. /// <summary>
  10. /// 调用开彩网接口服务
  11. /// </summary>
  12. public static class OpenCaiApiServices
  13. {
  14. private static readonly object _lock = new object();
  15. /// <summary>
  16. /// 调用开彩网接口服务
  17. /// </summary>
  18. static OpenCaiApiServices()
  19. {
  20. ApiHost = ConfigHelper.GetConfigValue<string>("OpenCaiApiHostMain") ?? ConfigHelper.GetConfigValue<string>("OpenCaiApiHostBack");
  21. //TODO 检测是否同主机能够通信
  22. helper = new HttpHelper();
  23. log = new LogHelper();
  24. email = IOC.Resolve<IEmail>();
  25. }
  26. /// <summary>
  27. /// 获取接口数据
  28. /// <para>备注:接口支持3种参数模式,分别如下:</para>
  29. /// <para>1、按最新查询,只需要传入code、rows(如有需要)参数即可</para>
  30. /// <para>2、按最新查询(批量),只需要传入code(多个用逗号隔开)、rows(如有需要)参数即可</para>
  31. /// <para>3、按开奖日期查询,只需要传入code、date参数即可</para>
  32. /// </summary>
  33. /// <param name="openCaiApiArg"></param>
  34. /// <param name="type"></param>
  35. /// <returns></returns>
  36. public static OpenCaiBaseJson GetOpenCaiApiData(OpenCaiApiArg openCaiApiArg, Type type = null)
  37. {
  38. OpenCaiBaseJson baseJson = new OpenCaiBaseJson();
  39. lock (_lock)
  40. {
  41. try
  42. {
  43. //按开奖日期查询地址修正
  44. string action = !string.IsNullOrEmpty(openCaiApiArg.date) ? "daily" : "newly";
  45. //最终接口地址
  46. string url = string.Format(ApiHost, GetRequsetArg(openCaiApiArg), action);
  47. //string url = "http://f.apiplus.net/bjpk10.json";
  48. //组装参数
  49. HttpItem item = new HttpItem
  50. {
  51. Url = url,
  52. Method = "GET",
  53. ContentType = "application/json",
  54. Timeout = 90 * 1000,
  55. ReadWriteTimeout = 90 * 1000,
  56. Encoding = Encoding.UTF8
  57. };
  58. //开始请求
  59. HttpResult result = helper.GetHtml(item);
  60. if (result.StatusCode == HttpStatusCode.OK)
  61. {
  62. string jsonStr = result.Html;
  63. if (!string.IsNullOrEmpty(jsonStr))
  64. {
  65. if (jsonStr.IndexOf("请求频率过快", StringComparison.Ordinal) < 0)
  66. {
  67. baseJson = jsonStr.JsonToEntity<OpenCaiBaseJson>();
  68. }
  69. else
  70. {
  71. log.Debug(type ?? typeof(OpenCaiApiServices), "调用了接口,请求参数:" + openCaiApiArg.TryToJson() + ",请求地址:"+ url + "\r\n");
  72. }
  73. }
  74. }
  75. else
  76. {
  77. log.Error(typeof(OpenCaiApiServices), "请求接口[" + url + "]失败,状态码:" + result.StatusCode);
  78. }
  79. }
  80. catch (Exception e)
  81. {
  82. log.Error(typeof(OpenCaiApiServices), e);
  83. }
  84. //finally
  85. //{
  86. // log.Debug(type, "调用了接口,请求参数:" + openCaiApiArg.TryToJson() + "\r\n");
  87. //}
  88. }
  89. return baseJson;
  90. }
  91. /// <summary>
  92. /// 组装请求参数
  93. /// </summary>
  94. /// <param name="openCaiApiArg"></param>
  95. /// <returns></returns>
  96. private static string GetRequsetArg(OpenCaiApiArg openCaiApiArg)
  97. {
  98. StringBuilder builder = new StringBuilder();
  99. if (!string.IsNullOrEmpty(openCaiApiArg.token))
  100. {
  101. builder.Append(string.Format("token={0}&", openCaiApiArg.token));
  102. }
  103. if (!string.IsNullOrEmpty(openCaiApiArg.code))
  104. {
  105. builder.Append(string.Format("code={0}&", openCaiApiArg.code));
  106. }
  107. if (openCaiApiArg.rows > 0)
  108. {
  109. builder.Append(string.Format("rows={0}&", openCaiApiArg.rows));
  110. }
  111. if (!string.IsNullOrEmpty(openCaiApiArg.format))
  112. {
  113. builder.Append(string.Format("format={0}&", openCaiApiArg.format));
  114. }
  115. if (!string.IsNullOrEmpty(openCaiApiArg.date))
  116. {
  117. builder.Append(string.Format("date={0}&", openCaiApiArg.date));
  118. }
  119. if (!string.IsNullOrEmpty(openCaiApiArg.callback))
  120. {
  121. builder.Append(string.Format("callback={0}", openCaiApiArg.callback));
  122. }
  123. return StringHelper.DelLastChar(builder.ToString(), "&");
  124. }
  125. #region Attribute
  126. /// <summary>
  127. /// 日志对象
  128. /// </summary>
  129. private static LogHelper log = null;
  130. /// <summary>
  131. /// 邮件接口
  132. /// </summary>
  133. private static IEmail email = null;
  134. /// <summary>
  135. /// HttpHelper
  136. /// </summary>
  137. private static HttpHelper helper = null;
  138. /// <summary>
  139. /// 开彩网接口地址
  140. /// </summary>
  141. private static string ApiHost = String.Empty;
  142. #endregion
  143. }
  144. }