PosRePushHelper.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 = "http://gateway.kexiaoshuang.com/v1/kxs/statServer/sp/binding";
  52. if(dataType == "un_bind") url = "http://gateway.kexiaoshuang.com/v1/kxs/statServer/sp/unbind";
  53. if(dataType == "trade") url = "http://gateway.kexiaoshuang.com/v1/kxs/statServer/sp/trade";
  54. if(dataType == "deposit") url = "http://gateway.kexiaoshuang.com/v1/kxs/statServer/sp/cash";
  55. if(dataType == "active") url = "http://gateway.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. }
  67. }
  68. else
  69. {
  70. PushPosRecord edit = db.PushPosRecord.FirstOrDefault(m => m.Id == sub.Id);
  71. if(edit != null)
  72. {
  73. edit.Status = -1;
  74. }
  75. }
  76. }
  77. else
  78. {
  79. PushPosRecord edit = db.PushPosRecord.FirstOrDefault(m => m.Id == sub.Id);
  80. if(edit != null)
  81. {
  82. edit.Status = -2;
  83. }
  84. }
  85. }
  86. db.SaveChanges();
  87. db.Dispose();
  88. }
  89. public void SaveToDb(string content, string encryptContent, string dataType)
  90. {
  91. WebCMSEntities db = new WebCMSEntities();
  92. db.PushPosRecord.Add(new PushPosRecord()
  93. {
  94. CreateDate = DateTime.Now,
  95. Content = content,
  96. DataType = dataType,
  97. EncryptContent = encryptContent,
  98. });
  99. db.SaveChanges();
  100. db.Dispose();
  101. }
  102. public string AesEncrypt(string str)
  103. {
  104. if (string.IsNullOrEmpty(str)) return null;
  105. Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
  106. string key = "CBTU1dD4Kd5pyiGWTsI10jRQ3SvKusSV";
  107. string iv = "DYgjCEIMVrj2W9xN";
  108. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  109. {
  110. Key = Encoding.UTF8.GetBytes(key),
  111. IV = Encoding.UTF8.GetBytes(iv),
  112. Mode = System.Security.Cryptography.CipherMode.CBC,
  113. Padding = System.Security.Cryptography.PaddingMode.PKCS7
  114. };
  115. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
  116. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  117. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  118. }
  119. public string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
  120. {
  121. string ret = string.Empty;
  122. try
  123. {
  124. byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
  125. // 设置提交的相关参数
  126. HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
  127. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
  128. request.Method = "POST";
  129. request.KeepAlive = false;
  130. request.AllowAutoRedirect = true;
  131. request.ContentType = "application/json";
  132. foreach (string key in headers.Keys)
  133. {
  134. request.Headers.Add(key, headers[key]);
  135. }
  136. 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)";
  137. request.ContentLength = postData.Length;
  138. // 提交请求数据
  139. Stream outputStream = request.GetRequestStream();
  140. outputStream.Write(postData, 0, postData.Length);
  141. outputStream.Close();
  142. HttpWebResponse response;
  143. Stream responseStream;
  144. StreamReader reader;
  145. string srcString;
  146. response = request.GetResponse() as HttpWebResponse;
  147. responseStream = response.GetResponseStream();
  148. reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  149. srcString = reader.ReadToEnd();
  150. ret = srcString; //返回值赋值
  151. reader.Close();
  152. }
  153. catch (WebException ex)
  154. {
  155. HttpWebResponse response = (HttpWebResponse)ex.Response;
  156. Stream myResponseStream = response.GetResponseStream();
  157. //获取响应内容
  158. StreamReader myStreamReader = new StreamReader(myResponseStream);
  159. ret = myStreamReader.ReadToEnd();
  160. myResponseStream.Close();
  161. }
  162. catch (Exception ex)
  163. {
  164. ret = "fail";
  165. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "POST请求异常");
  166. }
  167. return ret;
  168. }
  169. }
  170. }