UpdateSignUrlService.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.Models;
  5. using Library;
  6. using LitJson;
  7. using System.Text.RegularExpressions;
  8. using System.Threading;
  9. /// <summary>
  10. /// 更新签约码
  11. /// </summary>
  12. namespace MySystem
  13. {
  14. public class UpdateSignUrlService
  15. {
  16. public readonly static UpdateSignUrlService Instance = new UpdateSignUrlService();
  17. private UpdateSignUrlService()
  18. { }
  19. public void Start()
  20. {
  21. Thread th = new Thread(StartListen);
  22. th.IsBackground = true;
  23. th.Start();
  24. }
  25. public void StartListen()
  26. {
  27. while (true)
  28. {
  29. string content = RedisDbconn.Instance.RPop<string>("UpdateSignUrlQueue");
  30. if (!string.IsNullOrEmpty(content))
  31. {
  32. JsonData json = JsonMapper.ToObject(content);
  33. string result = UpdateSignUrl(int.Parse(json["MerchantId"].ToString()), int.Parse(json["Kind"].ToString()));
  34. if (result == "wait")
  35. {
  36. Thread.Sleep(10000);
  37. RedisDbconn.Instance.AddList("UpdateSignUrlQueue", content);
  38. }
  39. }
  40. else
  41. {
  42. Thread.Sleep(2000);
  43. }
  44. }
  45. }
  46. public string UpdateSignUrl(int MerchantId, int Kind)
  47. {
  48. try
  49. {
  50. WebCMSEntities db = new WebCMSEntities();
  51. MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
  52. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  53. if (Kind == 1) //支付宝
  54. {
  55. string content = AlipayFunction.Instance.GetBatchNo(AddInfo.IdCardName, AddInfo.MobilePhone, AddInfo.CreateMan);
  56. JsonData json = JsonMapper.ToObject(content);
  57. if (json["alipay_open_agent_create_response"]["code"].ToString() == "10000")
  58. {
  59. AddInfo.AlipayRemark = "";
  60. AddInfo.QueryCount = 0;
  61. merchant.QueryCount = 0;
  62. db.SaveChanges();
  63. string batch_no = json["alipay_open_agent_create_response"]["batch_no"].ToString();
  64. string Qualifications = AddInfo.Qualifications;
  65. if (!string.IsNullOrEmpty(Qualifications))
  66. {
  67. Qualifications = function.getPath("/" + Qualifications);
  68. }
  69. content = AlipayFunction.Instance.CommonSign(batch_no, AddInfo.UpdateMan, AddInfo.LicenseNumber, AddInfo.LicenseCopy, AddInfo.LicenseCopy, AddInfo.StoreEntrancePic, AddInfo.IndoorPic);
  70. json = JsonMapper.ToObject(content);
  71. if (json["alipay_open_agent_facetoface_sign_response"]["code"].ToString() == "10000")
  72. {
  73. content = AlipayFunction.Instance.AgentConfirm(batch_no);
  74. json = JsonMapper.ToObject(content);
  75. if (json["alipay_open_agent_confirm_response"]["code"].ToString() == "10000")
  76. {
  77. AddInfo.SeoTitle = batch_no;
  78. db.SaveChanges();
  79. RedisDbconn.Instance.AddList("AlipaySignQueue", new MerchantSign()
  80. {
  81. BusinessCode = batch_no,
  82. MerchantAddInfoId = MerchantId,
  83. Status = ""
  84. });
  85. }
  86. else
  87. {
  88. AddInfo.AlipayRemark = json["alipay_open_agent_confirm_response"]["sub_msg"].ToString();
  89. AddInfo.QueryCount = -1;
  90. merchant.QueryCount = -1;
  91. db.SaveChanges();
  92. }
  93. }
  94. else
  95. {
  96. AddInfo.AlipayRemark = json["alipay_open_agent_facetoface_sign_response"]["sub_msg"].ToString();
  97. AddInfo.QueryCount = -1;
  98. merchant.QueryCount = -1;
  99. db.SaveChanges();
  100. }
  101. }
  102. else
  103. {
  104. AddInfo.AlipayRemark = json["alipay_open_agent_create_response"]["sub_msg"].ToString();
  105. AddInfo.QueryCount = -1;
  106. merchant.QueryCount = -1;
  107. db.SaveChanges();
  108. }
  109. }
  110. if (Kind == 2) //微信
  111. {
  112. string BusinessCode = AddInfo.BusinessCode;
  113. if (string.IsNullOrEmpty(BusinessCode))
  114. {
  115. BusinessCode = "LKB" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(5);
  116. AddInfo.BusinessCode = BusinessCode;
  117. }
  118. string result = WeChatFunction.Instance.MerchantCreate(AddInfo, merchant);
  119. if (result.Contains("\"applyment_id\":"))
  120. {
  121. JsonData jsonObj = JsonMapper.ToObject(result);
  122. AddInfo.ApplymentId = jsonObj["applyment_id"].ToString();
  123. AddInfo.WeChatRemark = "";
  124. AddInfo.Status = 0;
  125. merchant.Status = 0;
  126. db.SaveChanges();
  127. List<MerchantSign> signs = RedisDbconn.Instance.GetList<MerchantSign>("MerchantSignList", 1, 10000000);
  128. bool op = signs.Any(m => m.BusinessCode == BusinessCode);
  129. if (!op)
  130. {
  131. RedisDbconn.Instance.AddList("WeChatSignQueue", new MerchantSign()
  132. {
  133. BusinessCode = BusinessCode,
  134. MerchantAddInfoId = MerchantId,
  135. Status = ""
  136. });
  137. }
  138. signs.Clear();
  139. AddInfo.ApplymentId = AddInfo.ApplymentId;
  140. db.SaveChanges();
  141. }
  142. else if (result.Contains("\"message\":"))
  143. {
  144. JsonData jsonObj = JsonMapper.ToObject(result);
  145. AddInfo.Status = -1;
  146. merchant.Status = -1;
  147. AddInfo.WeChatRemark = jsonObj["message"].ToString();
  148. db.SaveChanges();
  149. }
  150. }
  151. }
  152. catch (Exception ex)
  153. {
  154. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "更新签约码队列异常");
  155. }
  156. return "";
  157. }
  158. }
  159. }