HaoDaExtHelper.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. RedisDbconn.Instance.AddList("WeChatResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"ApplymentId\":\"" + applyment_id + "\"}");
  55. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
  56. LogHelper.Instance.WriteLog("end", "提交微信商户意愿申请单");
  57. }
  58. else if (result.Contains("\"message\":"))
  59. {
  60. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
  61. LogHelper.Instance.WriteLog("false", "提交微信商户意愿申请单");
  62. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  63. JsonData jsonObj = JsonMapper.ToObject(result);
  64. info.Status = -1;
  65. merchant.Status = -1;
  66. info.WeChatRemark = jsonObj["message"].ToString();
  67. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交微信商户意愿申请单");
  68. LogHelper.Instance.WriteLog("end", "提交微信商户意愿申请单");
  69. db.SaveChanges();
  70. }
  71. db.Dispose();
  72. }
  73. catch (Exception ex)
  74. {
  75. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "微信开户意愿申请异常");
  76. }
  77. }
  78. public void StartAlipay()
  79. {
  80. Thread th = new Thread(StartAlipayDo);
  81. th.IsBackground = true;
  82. th.Start();
  83. }
  84. private void StartAlipayDo()
  85. {
  86. while (true)
  87. {
  88. string content = RedisDbconn.Instance.RPop<string>("AlipayForHaoDaQueue");
  89. if (!string.IsNullOrEmpty(content))
  90. {
  91. AlipayAddInfo(int.Parse(content));
  92. }
  93. else
  94. {
  95. Thread.Sleep(10000);
  96. }
  97. }
  98. }
  99. public void AlipayAddInfo(int Id)
  100. {
  101. try
  102. {
  103. WebCMSEntities db = new WebCMSEntities();
  104. MerchantAddInfo info = db.MerchantAddInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantAddInfo();
  105. string result = AlipayFunctionForHD.Instance.AuthOrderCreate(info, info.AlipayAuthToken);
  106. JsonData jsonObj = JsonMapper.ToObject(result);
  107. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
  108. LogHelper.Instance.WriteLog(result, "提交支付宝商户意愿申请单");
  109. if (jsonObj["alipay_merchant_indirect_authorder_create_response"]["code"].ToString() == "10000")
  110. {
  111. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
  112. LogHelper.Instance.WriteLog("success", "提交支付宝商户意愿申请单");
  113. string order_no = jsonObj["alipay_merchant_indirect_authorder_create_response"]["order_no"].ToString();
  114. RedisDbconn.Instance.AddList("AlipayResultForHaoDaQueue", "{\"MerchantId\":\"" + Id + "\",\"order_no\":\"" + order_no + "\"}");
  115. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
  116. LogHelper.Instance.WriteLog("end", "提交支付宝商户意愿申请单");
  117. }
  118. else
  119. {
  120. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
  121. LogHelper.Instance.WriteLog("false", "提交支付宝商户意愿申请单");
  122. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == Id) ?? new MerchantInfo();
  123. info.QueryCount = -1;
  124. merchant.QueryCount = -1;
  125. info.AlipayRemark = jsonObj["alipay_merchant_indirect_authorder_create_response"]["msg"].ToString();
  126. LogHelper.Instance.WriteLog(DateTime.Now.ToString(), "提交支付宝商户意愿申请单");
  127. LogHelper.Instance.WriteLog("end", "提交支付宝商户意愿申请单");
  128. db.SaveChanges();
  129. }
  130. db.Dispose();
  131. }
  132. catch (Exception ex)
  133. {
  134. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "支付宝开户意愿申请异常");
  135. }
  136. }
  137. }