OpenCaiApiServices.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. //组装参数
  48. HttpItem item = new HttpItem
  49. {
  50. Url = url,
  51. Method = "GET",
  52. ContentType = "application/json",
  53. Timeout = 90 * 1000,
  54. ReadWriteTimeout = 90 * 1000,
  55. Encoding = Encoding.UTF8
  56. };
  57. //开始请求
  58. HttpResult result = helper.GetHtml(item);
  59. if (result.StatusCode == HttpStatusCode.OK)
  60. {
  61. string jsonStr = result.Html;
  62. if (!string.IsNullOrEmpty(jsonStr))
  63. {
  64. if (jsonStr.IndexOf("请求频率过快", StringComparison.Ordinal) < 0)
  65. {
  66. baseJson = jsonStr.JsonToEntity<OpenCaiBaseJson>();
  67. }
  68. else
  69. {
  70. log.Debug(type ?? typeof(OpenCaiApiServices), "调用了接口,请求参数:" + openCaiApiArg.TryToJson() + ",请求地址:"+ url + "\r\n");
  71. }
  72. }
  73. }
  74. else
  75. {
  76. log.Error(typeof(OpenCaiApiServices), "请求接口[" + url + "]失败,状态码:" + result.StatusCode);
  77. }
  78. }
  79. catch (Exception e)
  80. {
  81. log.Error(typeof(OpenCaiApiServices), e);
  82. }
  83. //finally
  84. //{
  85. // log.Debug(type, "调用了接口,请求参数:" + openCaiApiArg.TryToJson() + "\r\n");
  86. //}
  87. }
  88. return baseJson;
  89. }
  90. /// <summary>
  91. /// 组装请求参数
  92. /// </summary>
  93. /// <param name="openCaiApiArg"></param>
  94. /// <returns></returns>
  95. private static string GetRequsetArg(OpenCaiApiArg openCaiApiArg)
  96. {
  97. StringBuilder builder = new StringBuilder();
  98. if (!string.IsNullOrEmpty(openCaiApiArg.token))
  99. {
  100. builder.Append(string.Format("token={0}&", openCaiApiArg.token));
  101. }
  102. if (!string.IsNullOrEmpty(openCaiApiArg.code))
  103. {
  104. builder.Append(string.Format("code={0}&", openCaiApiArg.code));
  105. }
  106. if (openCaiApiArg.rows > 0)
  107. {
  108. builder.Append(string.Format("rows={0}&", openCaiApiArg.rows));
  109. }
  110. if (!string.IsNullOrEmpty(openCaiApiArg.format))
  111. {
  112. builder.Append(string.Format("format={0}&", openCaiApiArg.format));
  113. }
  114. if (!string.IsNullOrEmpty(openCaiApiArg.date))
  115. {
  116. builder.Append(string.Format("date={0}&", openCaiApiArg.date));
  117. }
  118. if (!string.IsNullOrEmpty(openCaiApiArg.callback))
  119. {
  120. builder.Append(string.Format("callback={0}", openCaiApiArg.callback));
  121. }
  122. return StringHelper.DelLastChar(builder.ToString(), "&");
  123. }
  124. #region Attribute
  125. /// <summary>
  126. /// 日志对象
  127. /// </summary>
  128. private static LogHelper log = null;
  129. /// <summary>
  130. /// 邮件接口
  131. /// </summary>
  132. private static IEmail email = null;
  133. /// <summary>
  134. /// HttpHelper
  135. /// </summary>
  136. private static HttpHelper helper = null;
  137. /// <summary>
  138. /// 开彩网接口地址
  139. /// </summary>
  140. private static string ApiHost = String.Empty;
  141. #endregion
  142. }
  143. }