WxPush.sub.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web.Script.Serialization;
  5. namespace SiteCore
  6. {
  7. public partial class WxPush
  8. {
  9. private static readonly object miniAtObj = new object();
  10. public static string mini_appid = "wx968c99ff1498e09f";
  11. public static string mini_appsecret = "153afb0877ee60fecd7f5580c9182ee6";
  12. static string mini_sendUrl = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=";
  13. public static object getMiniAt(HttpHelper http)
  14. {
  15. object cat = WebCache.GetRunCache("miniAt");
  16. if (cat == null)
  17. {
  18. lock (miniAtObj)
  19. {
  20. //http = new HttpHelper();
  21. HttpItem item = new HttpItem()
  22. {
  23. URL = string.Format(tokenUrl, mini_appid, mini_appsecret)
  24. };
  25. HttpResult hResult = http.GetHtml(item);
  26. string html = hResult.Html;
  27. JavaScriptSerializer jss = new JavaScriptSerializer();
  28. Dictionary<string, object> jObj = jss.Deserialize<Dictionary<string, object>>(html);
  29. if (!jObj.ContainsKey("errcode"))
  30. {
  31. string at = jObj["access_token"].ToString();
  32. WebCache.AddRunCacheTime("miniAt", at, 100);
  33. cat = at;
  34. }
  35. }
  36. }
  37. return cat;
  38. }
  39. /// <summary>
  40. /// 通知工地订单已受理
  41. /// </summary>
  42. public static void NotifyGdHasGetMsg(string openid, string txt1, string txt2, string date1)
  43. {
  44. HttpHelper http = new HttpHelper();
  45. HttpResult hResult = null;
  46. object cat = getMiniAt(http);
  47. if (cat != null)
  48. {
  49. DateTime cTime = DateTime.Now;
  50. //txt1 = "部位:" + txt1;
  51. if (txt2.Length > 20) txt2 = txt2.Substring(0, 20);
  52. var tdata = new
  53. {
  54. thing1 = new TemplateItem(txt1),
  55. thing2 = new TemplateItem(txt2),
  56. date3 = new TemplateItem(date1)
  57. };
  58. var msgData = new MiniTemplateData()
  59. {
  60. touser = openid,
  61. template_id = "mJfJKxxb7XDDgWWlM-XMr9OPF2FRb_Z5gClsGs805AU",
  62. page = "pages/index",
  63. data = tdata
  64. };
  65. // miniprogram= minidata,
  66. string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData);
  67. HttpItem item = new HttpItem()
  68. {
  69. URL = mini_sendUrl + cat,
  70. Method = "POST",
  71. ContentType = "application/json",
  72. Postdata = msg,
  73. PostEncoding = Encoding.UTF8,
  74. Encoding = Encoding.UTF8
  75. };
  76. //item.Encoding = Encoding.UTF8;
  77. hResult = http.GetHtml(item);
  78. string html = hResult.Html;
  79. }
  80. }
  81. //任务报名通知
  82. public static void sendTaskReceiveMsg(string openid, string form_id, object tid, string btime, string bname, string bphone, string bprice, string bmemo)
  83. {
  84. HttpHelper http = new HttpHelper();
  85. HttpResult hResult = null;
  86. object cat = getMiniAt(http);
  87. if (cat != null)
  88. {
  89. DateTime cTime = DateTime.Now;
  90. var tdata = new
  91. {
  92. keyword1 = new TemplateItem(btime, "#333333"),
  93. keyword2 = new TemplateItem(bname, "#0E7700"),
  94. keyword3 = new TemplateItem(bprice, "#0E7700"),
  95. keyword4 = new TemplateItem(bphone, "#333333"),
  96. keyword5 = new TemplateItem(bmemo, "#333333")
  97. };
  98. var msgData = new MiniTemplateData()
  99. {
  100. touser = openid,
  101. template_id = "3FJ8OG_oqQxCq97ZYwUsIOWTh7VkgpxHtuh0Hc9IKY4",
  102. form_id = form_id,
  103. page = "pages/bounty/bountyDetail?r=1&tid=" + tid,
  104. data = tdata
  105. };
  106. // miniprogram= minidata,
  107. string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData);
  108. HttpItem item = new HttpItem()
  109. {
  110. URL = mini_sendUrl + cat,
  111. Method = "POST",
  112. ContentType = "application/json",
  113. Postdata = msg,
  114. PostEncoding = Encoding.UTF8,
  115. Encoding = Encoding.UTF8
  116. };
  117. //item.Encoding = Encoding.UTF8;
  118. hResult = http.GetHtml(item);
  119. string html = hResult.Html;
  120. }
  121. }
  122. //任务选中/取消选中通知
  123. public static void sendTaskChioceMsg(string openid, string form_id, object tid, string title, string con)
  124. {
  125. HttpHelper http = new HttpHelper();
  126. HttpResult hResult = null;
  127. object cat = getMiniAt(http);
  128. if (cat != null)
  129. {
  130. DateTime cTime = DateTime.Now;
  131. var tdata = new
  132. {
  133. keyword1 = new TemplateItem(title, "#333333"),
  134. keyword2 = new TemplateItem(con + " " + cTime.ToString("yyyy-MM-dd HH:mm:ss"), "#0E7700")
  135. };
  136. var msgData = new MiniTemplateData()
  137. {
  138. touser = openid,
  139. template_id = "rpvN7kE6vEWq8e4P1rvpWCGtPbxJUjXRmzKUO71zT4E",
  140. form_id = form_id,
  141. page = "pages/bounty/bountyDetail?r=1&tid=" + tid,
  142. data = tdata
  143. };
  144. // miniprogram= minidata,
  145. string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData);
  146. HttpItem item = new HttpItem()
  147. {
  148. URL = mini_sendUrl + cat,
  149. Method = "POST",
  150. ContentType = "application/json",
  151. Postdata = msg,
  152. PostEncoding = Encoding.UTF8,
  153. Encoding = Encoding.UTF8
  154. };
  155. //item.Encoding = Encoding.UTF8;
  156. hResult = http.GetHtml(item);
  157. string html = hResult.Html;
  158. }
  159. }
  160. }
  161. public class MiniTemplateData
  162. {
  163. public string touser { get; set; }
  164. public string template_id { get; set; }
  165. public string page { get; set; }
  166. public object form_id { get; set; }
  167. public object data { get; set; }
  168. //public string emphasis_keyword { get; set; }
  169. }
  170. }