XiuNotifyHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using BizCom;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using System.Web.Caching;
  9. using System.Web.Script.Serialization;
  10. namespace SiteCore
  11. {
  12. public class XiuNotifyHelper
  13. {
  14. static object miniAtObj = new Object();
  15. static string tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
  16. static string sendUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";
  17. public static string mini_appid = "wxee687601b460ac2f";
  18. public static string mini_appsecret = "ed1195446aa6fadc7c84d726de8ea699";
  19. static string mini_sendUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=";
  20. public static void AddRunCacheTime(string cKey, object cValue, double cTime)
  21. {
  22. if (HttpRuntime.Cache[cKey] == null && cValue != null)//HttpContext.Current.Cache.Remove(cKey);
  23. HttpRuntime.Cache.Add(cKey, cValue, null, DateTime.Now.AddMinutes(cTime), TimeSpan.Zero, CacheItemPriority.Normal, null);
  24. }
  25. public static object GetRunCache(string cKey)
  26. {
  27. return HttpRuntime.Cache[cKey];
  28. }
  29. public static void RemoveRunCache(string cKey)
  30. {
  31. if (HttpRuntime.Cache[cKey] != null)
  32. HttpRuntime.Cache.Remove(cKey);
  33. }
  34. public static object getMiniAt(HttpHelper http)
  35. {
  36. object cat = GetRunCache("xiuminiAt");
  37. if (cat == null)
  38. {
  39. lock (miniAtObj)
  40. {
  41. //http = new HttpHelper();
  42. HttpItem item = new HttpItem()
  43. {
  44. URL = string.Format(tokenUrl, mini_appid, mini_appsecret)
  45. };
  46. HttpResult hResult = http.GetHtml(item);
  47. string html = hResult.Html;
  48. JavaScriptSerializer jss = new JavaScriptSerializer();
  49. Dictionary<string, object> jObj = jss.Deserialize<Dictionary<string, object>>(html);
  50. if (!jObj.ContainsKey("errcode"))
  51. {
  52. string at = jObj["access_token"].ToString();
  53. AddRunCacheTime("xiuminiAt", at, 100);
  54. cat = at;
  55. }
  56. }
  57. }
  58. return cat;
  59. }
  60. public static void sendReceiveMsg(string openid, string formid, SXiuRecord entity)
  61. {
  62. //if (sArr.Length < 7 || sArr[6] == "") return;
  63. //if (!lst.Contains("'" + sArr[6] + "'")) lst.Add("'" + sArr[6] + "'");
  64. //if (sArr[6] == "the formId is a mock one") return;
  65. HttpHelper http = new HttpHelper();
  66. HttpResult hResult = null;
  67. object cat = getMiniAt(http);
  68. if (cat != null)
  69. {
  70. DateTime cTime = DateTime.Now;
  71. //订单号{ { keyword1.DATA} }
  72. //订单信息{ { keyword2.DATA} }
  73. //确认时间{ { keyword3.DATA} }
  74. //联系人{ { keyword4.DATA} }
  75. //备注{ { keyword5.DATA} }
  76. //订单内容{ { keyword6.DATA} }
  77. //订单状态{ { keyword7.DATA} }
  78. var tdata = new
  79. {
  80. keyword1 = new TemplateItem(entity.Code, "#333333"),
  81. keyword2 = new TemplateItem(entity.XiuTypeInfo, "#0E7700"),
  82. keyword3 = new TemplateItem("如有需要会电话联系您", "#2148CD"),
  83. keyword4 = new TemplateItem("请保持手机通讯通畅", "#000000"),
  84. keyword5 = new TemplateItem("已受理", "#000000"),
  85. };
  86. var msgData = new MiniTemplateData()
  87. {
  88. touser = openid,
  89. template_id = "xPqCAfPQFr3D_6sI_eFppuJKCVxsgpIOO97V2ExdVdk",
  90. form_id = formid,
  91. page = "pages/orders/orders",
  92. data = tdata
  93. };
  94. // miniprogram= minidata,
  95. string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData);
  96. HttpItem item = new HttpItem()
  97. {
  98. URL = mini_sendUrl + cat,
  99. Method = "POST",
  100. ContentType = "application/json",
  101. Postdata = msg,
  102. PostEncoding = Encoding.UTF8,
  103. Encoding = Encoding.UTF8
  104. };
  105. //item.Encoding = Encoding.UTF8;
  106. hResult = http.GetHtml(item);
  107. string html = hResult.Html;
  108. }
  109. }
  110. }
  111. }