PosRePushHelper.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using System;
  2. using System.Linq;
  3. using System.Data;
  4. using System.Threading;
  5. using Library;
  6. using LitJson;
  7. using MySystem.Models.Push;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using System.IO;
  11. using System.Net;
  12. namespace MySystem
  13. {
  14. public class PosRePushHelper
  15. {
  16. public readonly static PosRePushHelper Instance = new PosRePushHelper();
  17. private PosRePushHelper()
  18. {
  19. }
  20. public void Start()//启动
  21. {
  22. Thread thread = new Thread(threadStart);
  23. thread.IsBackground = true;
  24. thread.Start();
  25. }
  26. private void threadStart()
  27. {
  28. while (true)
  29. {
  30. try
  31. {
  32. DoSomeThing();
  33. }
  34. catch (Exception ex)
  35. {
  36. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "重推机具数据异常");
  37. }
  38. Thread.Sleep(5000);
  39. }
  40. }
  41. //要执行的方法
  42. public void DoSomeThing()
  43. {
  44. string url = "";
  45. WebCMSEntities db = new WebCMSEntities();
  46. DateTime check = DateTime.Now.AddMinutes(-3);
  47. List<PushPosRecord> list = db.PushPosRecord.Where(m => m.CreateDate >= check && m.Status < 1).OrderBy(m => m.Id).Take(200).ToList();
  48. foreach(PushPosRecord sub in list)
  49. {
  50. string dataType = sub.DataType;
  51. if(dataType == "bind") url = "https://apigateway.kexiaoshuang.com/v1/kxs/statServer/sp/binding";
  52. if(dataType == "un_bind") url = "https://apigateway.kexiaoshuang.com/v1/kxs/statServer/sp/unbind";
  53. if(dataType == "trade") url = "https://apigateway.kexiaoshuang.com/v1/kxs/statServer/sp/trade";
  54. if(dataType == "deposit") url = "https://apigateway.kexiaoshuang.com/v1/kxs/statServer/sp/cash";
  55. if(dataType == "active") url = "https://apigateway.kexiaoshuang.com/v1/kxs/statServer/sp/extActData";
  56. string res = PostWebRequest(url, sub.EncryptContent, new Dictionary<string, string>());
  57. if(res.Contains("\"status\""))
  58. {
  59. JsonData backObj = JsonMapper.ToObject(res);
  60. if(backObj["status"].ToString() == "1")
  61. {
  62. PushPosRecord edit = db.PushPosRecord.FirstOrDefault(m => m.Id == sub.Id);
  63. if(edit != null)
  64. {
  65. edit.Status = 1;
  66. edit.BackContent = res;
  67. }
  68. }
  69. else
  70. {
  71. PushPosRecord edit = db.PushPosRecord.FirstOrDefault(m => m.Id == sub.Id);
  72. if(edit != null)
  73. {
  74. edit.Status = 2;
  75. edit.BackContent = res;
  76. }
  77. }
  78. }
  79. else
  80. {
  81. PushPosRecord edit = db.PushPosRecord.FirstOrDefault(m => m.Id == sub.Id);
  82. if(edit != null)
  83. {
  84. edit.Status = 3;
  85. edit.BackContent = res;
  86. }
  87. }
  88. }
  89. db.SaveChanges();
  90. db.Dispose();
  91. }
  92. public void SaveToDb(string content, string encryptContent, string dataType)
  93. {
  94. WebCMSEntities db = new WebCMSEntities();
  95. db.PushPosRecord.Add(new PushPosRecord()
  96. {
  97. CreateDate = DateTime.Now,
  98. Content = content,
  99. DataType = dataType,
  100. EncryptContent = encryptContent,
  101. });
  102. db.SaveChanges();
  103. db.Dispose();
  104. }
  105. public string AesEncrypt(string str)
  106. {
  107. if (string.IsNullOrEmpty(str)) return null;
  108. Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
  109. string key = "CBTU1dD4Kd5pyiGWTsI10jRQ3SvKusSV";
  110. string iv = "DYgjCEIMVrj2W9xN";
  111. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  112. {
  113. Key = Encoding.UTF8.GetBytes(key),
  114. IV = Encoding.UTF8.GetBytes(iv),
  115. Mode = System.Security.Cryptography.CipherMode.CBC,
  116. Padding = System.Security.Cryptography.PaddingMode.PKCS7
  117. };
  118. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
  119. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  120. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  121. }
  122. public string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
  123. {
  124. string ret = string.Empty;
  125. try
  126. {
  127. byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
  128. // 设置提交的相关参数
  129. HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
  130. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
  131. request.Method = "POST";
  132. request.KeepAlive = false;
  133. request.AllowAutoRedirect = true;
  134. request.ContentType = "application/json";
  135. foreach (string key in headers.Keys)
  136. {
  137. request.Headers.Add(key, headers[key]);
  138. }
  139. request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)";
  140. request.ContentLength = postData.Length;
  141. // 提交请求数据
  142. Stream outputStream = request.GetRequestStream();
  143. outputStream.Write(postData, 0, postData.Length);
  144. outputStream.Close();
  145. HttpWebResponse response;
  146. Stream responseStream;
  147. StreamReader reader;
  148. string srcString;
  149. response = request.GetResponse() as HttpWebResponse;
  150. responseStream = response.GetResponseStream();
  151. reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  152. srcString = reader.ReadToEnd();
  153. ret = srcString; //返回值赋值
  154. reader.Close();
  155. }
  156. catch (WebException ex)
  157. {
  158. HttpWebResponse response = (HttpWebResponse)ex.Response;
  159. Stream myResponseStream = response.GetResponseStream();
  160. //获取响应内容
  161. StreamReader myStreamReader = new StreamReader(myResponseStream);
  162. ret = myStreamReader.ReadToEnd();
  163. myResponseStream.Close();
  164. }
  165. catch (Exception ex)
  166. {
  167. ret = "fail";
  168. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "POST请求异常");
  169. }
  170. return ret;
  171. }
  172. }
  173. }