PosChannelHelper.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.Data;
  6. using MySystem;
  7. using MySystem.PxcModels;
  8. using Library;
  9. using LitJson;
  10. using System.Security.Cryptography;
  11. using System.Text;
  12. using System.IO;
  13. using System.Net;
  14. public class PosChannelHelper
  15. {
  16. public readonly static PosChannelHelper Instance = new PosChannelHelper();
  17. private PosChannelHelper()
  18. {
  19. }
  20. public void Start()
  21. {
  22. Thread th = new Thread(DoWorks);
  23. th.IsBackground = true;
  24. th.Start();
  25. }
  26. private void DoWorks()
  27. {
  28. while (true)
  29. {
  30. try
  31. {
  32. WebCMSEntities db = new WebCMSEntities();
  33. DoSomething(db);
  34. db.Dispose();
  35. Thread.Sleep(600000);
  36. }
  37. catch (Exception ex)
  38. {
  39. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "补下单机具券奖励异常");
  40. }
  41. function.WriteLog(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "\n\n", "补下单机具券奖励日志");
  42. }
  43. }
  44. public void DoSomething(WebCMSEntities db)
  45. {
  46. List<PosChannelSet> mains = db.PosChannelSet.Where(m => m.Status == 0).ToList();
  47. foreach(PosChannelSet main in mains)
  48. {
  49. int PassCount = 0;
  50. string PosSnList = "";
  51. List<PosChannelSetRecord> list = db.PosChannelSetRecord.Where(m => m.RecordId == main.Id).ToList();
  52. foreach(PosChannelSetRecord sub in list)
  53. {
  54. string jsonString = QueryTermAcqInfo(sub.PosSn);
  55. JsonData jsonData = JsonMapper.ToObject(jsonString);
  56. if(jsonData["resultCode"].ToString() == "1")
  57. {
  58. // string acqInstId = jsonData["data"]["acqInstId"].ToString();
  59. // if(sub.ChannelNo == acqInstId)
  60. // {
  61. PosChannelSetRecord edit = db.PosChannelSetRecord.FirstOrDefault(m => m.Id == sub.Id);
  62. if(edit != null)
  63. {
  64. edit.Status = 1;
  65. db.SaveChanges();
  66. PosSnList += edit.PosSn + ",";
  67. PassCount += 1;
  68. }
  69. // }
  70. }
  71. }
  72. if(PassCount >= list.Count)
  73. {
  74. PosChannelSet edit = db.PosChannelSet.FirstOrDefault(m => m.Id == main.Id);
  75. if(edit != null)
  76. {
  77. edit.Status = 1;
  78. db.SaveChanges();
  79. Users user = db.Users.FirstOrDefault(m => m.Id == edit.UserId) ?? new Users();
  80. string ChannelName = "";
  81. if(edit.ChannelNo == "48130000") ChannelName = "环讯";
  82. if(edit.ChannelNo == "48310000") ChannelName = "易生";
  83. if(edit.ChannelNo == "48330000") ChannelName = "海科";
  84. if(edit.ChannelNo == "48470000") ChannelName = "中付";
  85. RedisDbconn.Instance.AddList("MsgPersonalQueue", Newtonsoft.Json.JsonConvert.SerializeObject(new MsgPersonal()
  86. {
  87. UserId = edit.UserId, //创客
  88. Title = "机具通道变更通知", //标题
  89. Content = "<div class='f16' style='padding:12px 16px;'>尊敬的" + user.RealName + "盟主您好:<br />您名下的机具SN:<br />" + PosSnList.TrimEnd(',').Replace(",", "<br />") + ",通道已更改为" + ChannelName + ",请知悉!</div>", //内容
  90. CreateDate = DateTime.Now,
  91. }));
  92. }
  93. }
  94. }
  95. }
  96. #region 盒易付
  97. //测试环境
  98. // string BoxRequestUrl = "https://openapi-test.iboxpay.com/api";
  99. // string BoxAppId = "8840021411446784";
  100. // string BoxAppSecret = "xgwGnmYGjVy0GOWOWkQtRk8Hk8ur5tCV";
  101. // string BoxPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjMQxp24mjxDTr13uPW0y+tiO1yXFGw7P/pPQ0oZKK7F6KstEaus7pLEywBZ5XRXE5jgkhR2TS7Ne7djJfbpn5yFc6pPlz3ZsOfBVeB88NEwhx6xzCGX2eqSSkO33n8w2G0xc2ss5HpYBarT00NBZWhrwOXpdRPYLOYHKVU3Rl+FA9xDw/wYfoWvrr+JSfHRGn/ENMmQFEdckAbPauKaQMrZD2kz+PRrhq56eWnCuVQPcaz/jroVT8qQEgkg2IsNy+DwfLOIqm8IySEpxnQ5wN/KvsQJc2wXDQNf9F5kvWwjoqSSP0qJS+oPRXET+zJb+WTk2y5M6AYoC9NodwsC4NwIDAQAB";
  102. // string BrhCode = "039001";
  103. //生产环境
  104. string BoxRequestUrl = "https://openapi.iboxpay.com/api";
  105. string BoxAppId = "AP5879017839106039808";
  106. string BoxAppSecret = "DGtftIyl6R3nzwPr5YuV5fnmkfwufXRj";
  107. string BoxPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjmjoQirIYZBD9Qon2HkF4j/NAINXtJ7Lzq/WXxTF7t7mg7LNARt0+ZZaeWx8caq2fv5zdsGyyoInL23cBtDI5KmFfK69iA0ygQMK0WbiKqsUB1OpPbT3+9zLuadIJAznjA223lY6CIjTpdLZhaRjImNVqc60bdkx6YsQcA+xW+3r1JH4PPHb7yBEbkKIX8OhyX7U4p0TkbDkAobbjHr5YB9gmYLoSFJMOPfTtSExkv7/Y7IVR9poZAHcr3teFoAiXW3RzxelRtnXxIkl/6AUOKoL5fhr/UTUN+Q18uzNljYWr6SwnTI3EmtzgykaewWtZvV85Xdhe/BjiQ5Xor7YbwIDAQAB";
  108. string BrhCode = "039034";
  109. public string BoxServiceFee(string SnList, string ServiceFee)
  110. {
  111. function.WriteLog(DateTime.Now.ToString(), "设置盒易付机具服务费");
  112. function.WriteLog(SnList, "设置盒易付机具服务费");
  113. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  114. Dictionary<string, string> headdic = new Dictionary<string, string>();
  115. string batchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  116. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  117. headdic.Add("appid", BoxAppId);//开发者id
  118. headdic.Add("appsecret", BoxAppSecret);//开发者密码
  119. headdic.Add("X-Up-AppId", BoxAppId);
  120. headdic.Add("X-Timestamp", timestamp);
  121. headdic.Add("X-Sign-Type", "SHA-256");
  122. reqdic.Add("snList", SnList.Split(',').ToList());//终端列表
  123. if(ServiceFee != "0")
  124. {
  125. ServiceFee += "00";
  126. }
  127. reqdic.Add("depositGear", ServiceFee);//押金档位
  128. reqdic.Add("modelId", "MHN10916");//费率,固定0.6
  129. reqdic.Add("brhCode", BrhCode);//机构号
  130. reqdic.Add("batchNo", batchNo);//批次号
  131. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  132. headdic.Add("X-Sign", SHA256Sign(timestamp + BoxAppSecret + req));
  133. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  134. function.WriteLog("请求头\n" + head, "设置盒易付机具服务费");
  135. function.WriteLog("请求参数\n" + req, "设置盒易付机具服务费");
  136. function.WriteLog("请求地址:" + BoxRequestUrl + "/inst/register/terms", "设置盒易付机具服务费");
  137. string result = PostWebRequest(BoxRequestUrl + "/inst/register/terms", req, headdic);
  138. function.WriteLog("返回\n" + result + "\n\n", "设置盒易付机具服务费");
  139. return result;
  140. }
  141. public string BoxFee(string MerNo, string Fee)
  142. {
  143. function.WriteLog(DateTime.Now.ToString(), "设置盒易付机具费率");
  144. function.WriteLog(MerNo, "设置盒易付机具费率");
  145. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  146. Dictionary<string, string> headdic = new Dictionary<string, string>();
  147. string batchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  148. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  149. headdic.Add("appid", BoxAppId);//开发者id
  150. headdic.Add("appsecret", BoxAppSecret);//开发者密码
  151. headdic.Add("X-Up-AppId", BoxAppId);
  152. headdic.Add("X-Timestamp", timestamp);
  153. headdic.Add("X-Sign-Type", "SHA-256");
  154. reqdic.Add("mchtNo", MerNo);//终端列表
  155. reqdic.Add("brhCode", BrhCode);//机构号
  156. reqdic.Add("tNDiscId", Fee == "0.6" ? "MHN10916" : "MHN10917");//费率模型
  157. reqdic.Add("effectiveFlag", "1");//立即生效
  158. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  159. headdic.Add("X-Sign", SHA256Sign(timestamp + BoxAppSecret + req));
  160. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  161. function.WriteLog("请求头\n" + head, "设置盒易付机具费率");
  162. function.WriteLog("请求参数\n" + req, "设置盒易付机具费率");
  163. function.WriteLog("请求地址:" + BoxRequestUrl + "/inst/mcht/update_disc", "设置盒易付机具费率");
  164. string result = PostWebRequest(BoxRequestUrl + "/inst/mcht/update_disc", req, headdic);
  165. function.WriteLog("返回\n" + result + "\n\n", "设置盒易付机具费率");
  166. return result;
  167. }
  168. //查询终端收单机构
  169. public string QueryTermAcqInfo(string SnNo)
  170. {
  171. function.WriteLog(DateTime.Now.ToString(), "查询终端收单机构");
  172. function.WriteLog(SnNo, "查询终端收单机构");
  173. Dictionary<string, object> reqdic = new Dictionary<string, object>();
  174. Dictionary<string, string> headdic = new Dictionary<string, string>();
  175. string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  176. headdic.Add("appid", BoxAppId);//开发者id
  177. headdic.Add("appsecret", BoxAppSecret);//开发者密码
  178. headdic.Add("X-Up-AppId", BoxAppId);
  179. headdic.Add("X-Timestamp", timestamp);
  180. headdic.Add("X-Sign-Type", "SHA-256");
  181. reqdic.Add("tusn", SnNo); //机具号
  182. string req = Newtonsoft.Json.JsonConvert.SerializeObject(reqdic);
  183. headdic.Add("X-Sign", SHA256Sign(timestamp + BoxAppSecret + req));
  184. string head = Newtonsoft.Json.JsonConvert.SerializeObject(headdic);
  185. function.WriteLog("请求头\n" + head, "查询终端收单机构");
  186. function.WriteLog("请求参数\n" + req, "查询终端收单机构");
  187. function.WriteLog("请求地址:" + BoxRequestUrl + "/inst/term/queryTermAcqInfo", "查询终端收单机构");
  188. string result = PostWebRequest(BoxRequestUrl + "/inst/term/queryTermAcqInfo", req, headdic);
  189. function.WriteLog("返回\n" + result + "\n\n", "查询终端收单机构");
  190. return result;
  191. }
  192. public string SHA256Sign(string toSignStr)
  193. {
  194. byte[] toSignByte = Encoding.UTF8.GetBytes(toSignStr);
  195. SHA256 sha256 = SHA256.Create();
  196. byte[] signByte = sha256.ComputeHash(toSignByte);
  197. string sign = Convert.ToBase64String(signByte);
  198. return sign;
  199. }
  200. public bool VerifySign(string toSignStr, string signStr)
  201. {
  202. byte[] toSignByte = Encoding.Default.GetBytes(toSignStr);
  203. byte[] signByte = Convert.FromBase64String(signStr);
  204. var toKey = Convert.FromBase64String(BoxPublicKey);
  205. var rsaroot = RSA.Create();
  206. rsaroot.ImportSubjectPublicKeyInfo(toKey, out _);
  207. var publicKeyParameters = rsaroot.ExportParameters(false);
  208. using (var rsa = RSA.Create())
  209. {
  210. rsa.ImportParameters(publicKeyParameters);
  211. var sha256 = SHA256.Create();
  212. var hash = sha256.ComputeHash(toSignByte);
  213. return rsa.VerifyHash(hash, signByte, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  214. }
  215. }
  216. public string SignData(string toSignStr)
  217. {
  218. byte[] toSignByte = Encoding.UTF8.GetBytes(toSignStr);
  219. var toKey = Convert.FromBase64String(BoxPublicKey);
  220. var rsaroot = RSA.Create();
  221. rsaroot.ImportSubjectPublicKeyInfo(toKey, out _);
  222. var publicKeyParameters = rsaroot.ExportParameters(false);
  223. using (var rsa = RSA.Create())
  224. {
  225. rsa.ImportParameters(publicKeyParameters);
  226. var sha256 = SHA256.Create();
  227. var hash = sha256.ComputeHash(toSignByte);
  228. byte[] endByte = rsa.SignData(toSignByte, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
  229. return Convert.ToBase64String(endByte);
  230. }
  231. }
  232. public string PostWebRequest(string postUrl, string paramData, Dictionary<string, string> headers)
  233. {
  234. string ret = string.Empty;
  235. try
  236. {
  237. function.WriteLog(DateTime.Now.ToString(), "请求开店宝API日志");
  238. function.WriteLog(postUrl, "请求开店宝API日志");
  239. function.WriteLog(paramData, "请求开店宝API日志");
  240. byte[] postData = System.Text.Encoding.UTF8.GetBytes(paramData);
  241. // 设置提交的相关参数
  242. HttpWebRequest request = WebRequest.Create(postUrl) as HttpWebRequest;
  243. System.Text.Encoding myEncoding = System.Text.Encoding.UTF8;
  244. request.Method = "POST";
  245. request.KeepAlive = false;
  246. request.AllowAutoRedirect = true;
  247. request.ContentType = "application/json";
  248. foreach (string key in headers.Keys)
  249. {
  250. request.Headers.Add(key, headers[key]);
  251. }
  252. 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)";
  253. request.ContentLength = postData.Length;
  254. // 提交请求数据
  255. Stream outputStream = request.GetRequestStream();
  256. outputStream.Write(postData, 0, postData.Length);
  257. outputStream.Close();
  258. HttpWebResponse response;
  259. Stream responseStream;
  260. StreamReader reader;
  261. string srcString;
  262. response = request.GetResponse() as HttpWebResponse;
  263. responseStream = response.GetResponseStream();
  264. reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
  265. srcString = reader.ReadToEnd();
  266. ret = srcString; //返回值赋值
  267. reader.Close();
  268. function.WriteLog(srcString, "请求开店宝API日志");
  269. }
  270. catch (WebException ex)
  271. {
  272. HttpWebResponse response = (HttpWebResponse)ex.Response;
  273. Stream myResponseStream = response.GetResponseStream();
  274. //获取响应内容
  275. StreamReader myStreamReader = new StreamReader(myResponseStream);
  276. ret = myStreamReader.ReadToEnd();
  277. myResponseStream.Close();
  278. }
  279. catch (Exception ex)
  280. {
  281. ret = "fail";
  282. function.WriteLog(DateTime.Now.ToString() + "\r\n" + ex.ToString(), "请求开店宝API异常");
  283. }
  284. return ret;
  285. }
  286. #endregion
  287. }