OpenCaiApiServices.cs 5.8 KB

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