HaoDaExtHelper.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using System.Linq;
  6. using Microsoft.Extensions.Hosting;
  7. using MySystem;
  8. using MySystem.Models;
  9. using LitJson;
  10. using Library;
  11. using MySystem.Models.Main;
  12. public class HaoDaExtHelper
  13. {
  14. public readonly static HaoDaExtHelper Instance = new HaoDaExtHelper();
  15. private HaoDaExtHelper()
  16. {
  17. }
  18. public void StartWeChat()
  19. {
  20. Thread th = new Thread(StartWeChatDo);
  21. th.IsBackground = true;
  22. th.Start();
  23. }
  24. private void StartWeChatDo()
  25. {
  26. while (true)
  27. {
  28. string content = RedisDbconn.Instance.RPop<string>("WeChatForHaoDaQueue");
  29. if (!string.IsNullOrEmpty(content))
  30. {
  31. WeChatAddInfo(int.Parse(content));
  32. }
  33. else
  34. {
  35. Thread.Sleep(10000);
  36. }
  37. }
  38. }
  39. public void WeChatAddInfo(int Id)
  40. {
  41. try
  42. {
  43. WebCMSEntities db = new WebCMSEntities();
  44. MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
  45. string result = WeChatFunctionForHD.Instance.MerchantApply(info);
  46. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
  47. LogHelper.Instance.WriteLog(result, "提交微信商户意愿申请单");
  48. if (result.Contains("\"applyment_id\":"))
  49. {
  50. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
  51. LogHelper.Instance.WriteLog("success", "提交微信商户意愿申请单");
  52. JsonData json = JsonMapper.ToObject(result);
  53. string applyment_id = json["applyment_id"].ToString();
  54. info.ApplymentId = applyment_id;
  55. db.SaveChanges();
  56. // RedisDbconn.Instance.AddList("WeChatResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"ApplymentId\":\"" + applyment_id + "\"}");
  57. // RedisDbconn.Instance.AddList("WeChatAuthResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\"}");
  58. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
  59. LogHelper.Instance.WriteLog("end", "提交微信商户意愿申请单");
  60. }
  61. else if (result.Contains("\"message\":"))
  62. {
  63. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
  64. LogHelper.Instance.WriteLog("false", "提交微信商户意愿申请单");
  65. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  66. JsonData jsonObj = JsonMapper.ToObject(result);
  67. info.Status = -1;
  68. merchant.Status = -1;
  69. info.WeChatRemark = jsonObj["message"].ToString();
  70. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
  71. LogHelper.Instance.WriteLog("end", "提交微信商户意愿申请单");
  72. db.SaveChanges();
  73. }
  74. db.Dispose();
  75. }
  76. catch (Exception ex)
  77. {
  78. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "微信开户意愿申请异常");
  79. }
  80. }
  81. public void StartAlipay()
  82. {
  83. Thread th = new Thread(StartAlipayDo);
  84. th.IsBackground = true;
  85. th.Start();
  86. }
  87. private void StartAlipayDo()
  88. {
  89. while (true)
  90. {
  91. string content = RedisDbconn.Instance.RPop<string>("AlipayForHaoDaQueue");
  92. if (!string.IsNullOrEmpty(content))
  93. {
  94. AlipayAddInfo(int.Parse(content));
  95. }
  96. else
  97. {
  98. Thread.Sleep(10000);
  99. }
  100. }
  101. }
  102. public void AlipayAddInfo(int Id)
  103. {
  104. try
  105. {
  106. WebCMSEntities db = new WebCMSEntities();
  107. MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
  108. string result = AlipayFunctionForHD.Instance.AuthOrderCreate(info, info.AlipayAuthToken);
  109. JsonData jsonObj = JsonMapper.ToObject(result);
  110. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
  111. LogHelper.Instance.WriteLog(result, "提交支付宝商户意愿申请单");
  112. if (jsonObj["alipay_merchant_indirect_authorder_create_response"]["code"].ToString() == "10000")
  113. {
  114. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
  115. LogHelper.Instance.WriteLog("success", "提交支付宝商户意愿申请单");
  116. string order_no = jsonObj["alipay_merchant_indirect_authorder_create_response"]["order_no"].ToString();
  117. info.SeoTitle = order_no;
  118. db.SaveChanges();
  119. // RedisDbconn.Instance.AddList("AlipayResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"order_no\":\"" + order_no + "\"}");
  120. // RedisDbconn.Instance.AddList("AlipayAuthResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\"}");
  121. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
  122. LogHelper.Instance.WriteLog("end", "提交支付宝商户意愿申请单");
  123. }
  124. else
  125. {
  126. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
  127. LogHelper.Instance.WriteLog("false", "提交支付宝商户意愿申请单");
  128. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  129. info.QueryCount = -1;
  130. merchant.QueryCount = -1;
  131. string msg = jsonObj["alipay_merchant_indirect_authorder_create_response"]["msg"].ToString();
  132. if(result.Contains("\"sub_msg\""))
  133. {
  134. msg = jsonObj["alipay_merchant_indirect_authorder_create_response"]["sub_msg"].ToString();
  135. }
  136. info.AlipayRemark = msg;
  137. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
  138. LogHelper.Instance.WriteLog("end", "提交支付宝商户意愿申请单");
  139. db.SaveChanges();
  140. }
  141. db.Dispose();
  142. }
  143. catch (Exception ex)
  144. {
  145. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "支付宝开户意愿申请异常");
  146. }
  147. }
  148. }