PosPushHelper.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 = AppConfig.Base.JavaPushHost + "v1/kxs/statServer/sp/binding";
  86. if(dataType == "un_bind") url = AppConfig.Base.JavaPushHost + "v1/kxs/statServer/sp/unbind";
  87. if(dataType == "trade") url = AppConfig.Base.JavaPushHost + "v1/kxs/statServer/sp/trade";
  88. if(dataType == "deposit") url = AppConfig.Base.JavaPushHost + "v1/kxs/statServer/sp/cash";
  89. if(dataType == "active") url = AppConfig.Base.JavaPushHost + "v1/kxs/statServer/sp/extActData";
  90. if(dataType == "qr_bind") url = AppConfig.Base.JavaPushHost + "v1/kxs/statServer/sp/saveHaoDaQrCodeMchInfo";
  91. if(dataType == "lkb_trade") url = AppConfig.Base.JavaPushHost + "v1/kxs/statServer/sp/lkbTrade";
  92. function.WriteLog("url:" + url, "统一推送日志");
  93. string encryptContent = AesEncrypt(dataContent);
  94. encryptContent = Convert.ToBase64String(Encoding.UTF8.GetBytes(encryptContent));
  95. function.WriteLog("encryptContent:" + encryptContent, "统一推送日志");
  96. string res = PostWebRequest(url, encryptContent, new Dictionary<string, string>());
  97. function.WriteLog("res:" + res + "\n\n\n", "统一推送日志");
  98. if(!res.Contains("\"status\""))
  99. {
  100. SaveToDb(dataContent, encryptContent, dataType, res);
  101. return;
  102. }
  103. JsonData backObj = JsonMapper.ToObject(res);
  104. if(backObj["status"].ToString() != "1")
  105. {
  106. SaveToDb(dataContent, encryptContent, dataType, res);
  107. return;
  108. }
  109. SaveToDb(dataContent, encryptContent, dataType, res, 1);
  110. }
  111. public void SaveToDb(string content, string encryptContent, string dataType, string backContent, int status = 0)
  112. {
  113. WebCMSEntities db = new WebCMSEntities();
  114. db.PushPosRecord.Add(new PushPosRecord()
  115. {
  116. CreateDate = DateTime.Now,
  117. Content = content,
  118. DataType = dataType,
  119. EncryptContent = encryptContent,
  120. BackContent = backContent,
  121. Status = status,
  122. });
  123. db.SaveChanges();
  124. db.Dispose();
  125. }
  126. public string AesEncrypt(string str)
  127. {
  128. if (string.IsNullOrEmpty(str)) return null;
  129. Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
  130. string key = "CBTU1dD4Kd5pyiGWTsI10jRQ3SvKusSV";
  131. string iv = "DYgjCEIMVrj2W9xN";
  132. System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
  133. {
  134. Key = Encoding.UTF8.GetBytes(key),
  135. IV = Encoding.UTF8.GetBytes(iv),
  136. Mode = System.Security.Cryptography.CipherMode.CBC,
  137. Padding = System.Security.Cryptography.PaddingMode.PKCS7
  138. };
  139. System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
  140. Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
  141. return Convert.ToBase64String(resultArray, 0, resultArray.Length);
  142. }
  143. public string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
  144. {
  145. string ret = string.Empty;
  146. try
  147. {
  148. byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
  149. // 设置提交的相关参数
  150. HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
  151. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
  152. request.Method = "POST";
  153. request.KeepAlive = false;
  154. request.AllowAutoRedirect = true;
  155. request.ContentType = "application/json";
  156. foreach (string key in headers.Keys)
  157. {
  158. request.Headers.Add(key, headers[key]);
  159. }
  160. 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)";
  161. request.ContentLength = postData.Length;
  162. // 提交请求数据
  163. Stream outputStream = request.GetRequestStream();
  164. outputStream.Write(postData, 0, postData.Length);
  165. outputStream.Close();
  166. HttpWebResponse response;
  167. Stream responseStream;
  168. StreamReader reader;
  169. string srcString;
  170. response = request.GetResponse() as HttpWebResponse;
  171. responseStream = response.GetResponseStream();
  172. reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  173. srcString = reader.ReadToEnd();
  174. ret = srcString; //返回值赋值
  175. reader.Close();
  176. }
  177. catch (WebException ex)
  178. {
  179. HttpWebResponse response = (HttpWebResponse)ex.Response;
  180. Stream myResponseStream = response.GetResponseStream();
  181. //获取响应内容
  182. StreamReader myStreamReader = new StreamReader(myResponseStream);
  183. ret = myStreamReader.ReadToEnd();
  184. myResponseStream.Close();
  185. }
  186. catch (Exception ex)
  187. {
  188. ret = "fail";
  189. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "POST请求异常");
  190. }
  191. return ret;
  192. }
  193. }
  194. }