PosPushHelper.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 PosPushHelper
  15. {
  16. public readonly static PosPushHelper Instance = new PosPushHelper();
  17. private PosPushHelper()
  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. string content = RedisDbconn.Instance.RPop<string>("KxsPosDataQueue");
  31. if (!string.IsNullOrEmpty(content))
  32. {
  33. try
  34. {
  35. DoSomeThing(content);
  36. }
  37. catch (Exception ex)
  38. {
  39. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex, "推送机具数据异常");
  40. }
  41. }
  42. else
  43. {
  44. Thread.Sleep(5000);
  45. }
  46. }
  47. }
  48. public void StartTest()//启动
  49. {
  50. Thread thread = new Thread(threadStartTest);
  51. thread.IsBackground = true;
  52. thread.Start();
  53. }
  54. private void threadStartTest()
  55. {
  56. while (true)
  57. {
  58. string content = RedisDbconnTest.Instance.RPop<string>("KxsPosDataQueue");
  59. if (!string.IsNullOrEmpty(content))
  60. {
  61. try
  62. {
  63. DoSomeThing(content);
  64. }
  65. catch (Exception ex)
  66. {
  67. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + content + "\n" + ex, "推送机具数据异常");
  68. }
  69. }
  70. else
  71. {
  72. Thread.Sleep(5000);
  73. }
  74. }
  75. }
  76. //要执行的方法
  77. public void DoSomeThing(string content)
  78. {
  79. string url = "";
  80. function.WriteLog(DateTime.Now.ToString() + "\n" + content, "统一推送日志");
  81. content = content.Replace("null", "\"\"");
  82. JsonData jsonObj = JsonMapper.ToObject(content);
  83. string dataType = jsonObj["data_type"].ToString();
  84. string dataContent = jsonObj["data_content"].ToJson();
  85. if(dataType == "bind") url = "https://apigateway.kexiaoshuang.com/v1/kxs/statServer/sp/binding";
  86. if(dataType == "un_bind") url = "https://apigateway.kexiaoshuang.com/v1/kxs/statServer/sp/unbind";
  87. if(dataType == "trade") url = "https://apigateway.kexiaoshuang.com/v1/kxs/statServer/sp/trade";
  88. if(dataType == "deposit") url = "https://apigateway.kexiaoshuang.com/v1/kxs/statServer/sp/cash";
  89. if(dataType == "active") url = "https://apigateway.kexiaoshuang.com/v1/kxs/statServer/sp/extActData";
  90. if(dataType == "qr_bind") url = "https://apigateway.kexiaoshuang.com/v1/kxs/statServer/sp/saveHaoDaQrCodeMchInfo";
  91. function.WriteLog("url:" + url, "统一推送日志");
  92. string encryptContent = AesEncrypt(dataContent);
  93. encryptContent = Convert.ToBase64String(Encoding.UTF8.GetBytes(encryptContent));
  94. function.WriteLog("encryptContent:" + encryptContent, "统一推送日志");
  95. string res = PostWebRequest(url, encryptContent, new Dictionary<string, string>());
  96. function.WriteLog("res:" + res + "\n\n\n", "统一推送日志");
  97. if(!res.Contains("\"status\""))
  98. {
  99. SaveToDb(dataContent, encryptContent, dataType, res);
  100. return;
  101. }
  102. JsonData backObj = JsonMapper.ToObject(res);
  103. if(backObj["status"].ToString() != "1")
  104. {
  105. SaveToDb(dataContent, encryptContent, dataType, res);
  106. return;
  107. }
  108. SaveToDb(dataContent, encryptContent, dataType, res, 1);
  109. }
  110. public void SaveToDb(string content, string encryptContent, string dataType, string backContent, int status = 0)
  111. {
  112. WebCMSEntities db = new WebCMSEntities();
  113. db.PushPosRecord.Add(new PushPosRecord()
  114. {
  115. CreateDate = DateTime.Now,
  116. Content = content,
  117. DataType = dataType,
  118. EncryptContent = encryptContent,
  119. BackContent = backContent,
  120. Status = status,
  121. });
  122. db.SaveChanges();
  123. db.Dispose();
  124. }
  125. public string AesEncrypt(string str)
  126. {
  127. if (string.IsNullOrEmpty(str)) return null;
  128. Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
  129. string key = "CBTU1dD4Kd5pyiGWTsI10jRQ3SvKusSV";
  130. string iv = "DYgjCEIMVrj2W9xN";
  131. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  132. {
  133. Key = Encoding.UTF8.GetBytes(key),
  134. IV = Encoding.UTF8.GetBytes(iv),
  135. Mode = System.Security.Cryptography.CipherMode.CBC,
  136. Padding = System.Security.Cryptography.PaddingMode.PKCS7
  137. };
  138. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
  139. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  140. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  141. }
  142. public string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
  143. {
  144. string ret = string.Empty;
  145. try
  146. {
  147. byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
  148. // 设置提交的相关参数
  149. HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
  150. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
  151. request.Method = "POST";
  152. request.KeepAlive = false;
  153. request.AllowAutoRedirect = true;
  154. request.ContentType = "application/json";
  155. foreach (string key in headers.Keys)
  156. {
  157. request.Headers.Add(key, headers[key]);
  158. }
  159. 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)";
  160. request.ContentLength = postData.Length;
  161. // 提交请求数据
  162. Stream outputStream = request.GetRequestStream();
  163. outputStream.Write(postData, 0, postData.Length);
  164. outputStream.Close();
  165. HttpWebResponse response;
  166. Stream responseStream;
  167. StreamReader reader;
  168. string srcString;
  169. response = request.GetResponse() as HttpWebResponse;
  170. responseStream = response.GetResponseStream();
  171. reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  172. srcString = reader.ReadToEnd();
  173. ret = srcString; //返回值赋值
  174. reader.Close();
  175. }
  176. catch (WebException ex)
  177. {
  178. HttpWebResponse response = (HttpWebResponse)ex.Response;
  179. Stream myResponseStream = response.GetResponseStream();
  180. //获取响应内容
  181. StreamReader myStreamReader = new StreamReader(myResponseStream);
  182. ret = myStreamReader.ReadToEnd();
  183. myResponseStream.Close();
  184. }
  185. catch (Exception ex)
  186. {
  187. ret = "fail";
  188. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "POST请求异常");
  189. }
  190. return ret;
  191. }
  192. }
  193. }