WxPush.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using BizCom;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Web.Script.Serialization;
  6. namespace SiteCore
  7. {
  8. public partial class WxPush
  9. {
  10. //public static string
  11. public static string appid = "wx20432512482565e9";
  12. public static string appsecret = "07492bc9cbbc5b303e5e135b4dccd6f0";
  13. public static string hy_appid = "wx0669fa53c6f74698";
  14. public static string hy_appsecret = "678776d4eec59ce289eb88ff36f3b4ef";
  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. static object atObj = new Object();
  18. private static object getAt(HttpHelper http)
  19. {
  20. object cat = WebCache.GetRunCache("hyAt");
  21. if (cat == null)
  22. {
  23. lock (atObj)
  24. {
  25. //http = new HttpHelper();
  26. HttpItem item = new HttpItem()
  27. {
  28. URL = string.Format(tokenUrl, hy_appid, hy_appsecret)
  29. };
  30. HttpResult hResult = http.GetHtml(item);
  31. string html = hResult.Html;
  32. XLog.SaveLog(0, "getAt:" + html);
  33. JavaScriptSerializer jss = new JavaScriptSerializer();
  34. Dictionary<string, object> jObj = jss.Deserialize<Dictionary<string, object>>(html);
  35. if (!jObj.ContainsKey("errcode"))
  36. {
  37. string at = jObj["access_token"].ToString();
  38. WebCache.AddRunCacheTime("hyAt", at, 100);
  39. cat = at;
  40. }
  41. }
  42. }
  43. return cat;
  44. }
  45. public static void sendMonitorMsg(string title, string openid)
  46. {
  47. HttpHelper http = new HttpHelper();
  48. HttpResult hResult = null;
  49. object cat = getAt(http);
  50. if (cat != null)
  51. {
  52. var tdata = new
  53. {
  54. first = new TemplateItem(title, "#333333"),
  55. keyword1 = new TemplateItem(title, "#333333"),
  56. keyword2 = new TemplateItem(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), "#0E7700"),
  57. remark = new TemplateItem("请注意查看", "#333333")
  58. };
  59. //var minidata = new
  60. //{
  61. // appid = "wx20432512482565e9",
  62. // pagepath = "pages/lab/lab?remind=1"
  63. //};
  64. var msgData = new TemplateData()
  65. {
  66. touser = openid,
  67. template_id = "Tsp5iFvM_L1Ok10nta3TvQzjnrZvas7PKIWbdX1t08M",
  68. url = "",
  69. data = tdata
  70. };
  71. // miniprogram= minidata,
  72. string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData);
  73. HttpItem item = new HttpItem()
  74. {
  75. URL = sendUrl + cat,
  76. Method = "POST",
  77. ContentType = "application/json",
  78. Postdata = msg,
  79. PostEncoding = Encoding.UTF8,
  80. Encoding = Encoding.UTF8
  81. };
  82. //item.Encoding = Encoding.UTF8;
  83. hResult = http.GetHtml(item);
  84. string html = hResult.Html;
  85. XLog.SaveLog(0, html);
  86. }
  87. }
  88. public static void sendSignDispatchMsg(string openid, string key1, string key2, string memo)
  89. {
  90. HttpHelper http = new HttpHelper();
  91. HttpResult hResult = null;
  92. object cat = getAt(http);
  93. if (cat != null)
  94. {
  95. var tdata = new
  96. {
  97. first = new TemplateItem("您的电子小票已签收", "#333333"),
  98. keyword1 = new TemplateItem(key1, "#333333"),
  99. keyword2 = new TemplateItem(key2, "#0E7700"),
  100. keyword3 = new TemplateItem(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), "#0E7700"),
  101. remark = new TemplateItem(memo, "#333333")
  102. };
  103. var minidata = new
  104. {
  105. appid = mini_appid,
  106. pagepath = "pages/index"
  107. };
  108. var msgData = new TemplateData()
  109. {
  110. touser = openid,
  111. template_id = "uDUEkjJt4-j2m9PhIB6bTXeLZrAvvNdi7a2xMBXzH1I",
  112. url = "",
  113. miniprogram = minidata,
  114. data = tdata
  115. };
  116. string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData);
  117. HttpItem item = new HttpItem()
  118. {
  119. URL = sendUrl + cat,
  120. Method = "POST",
  121. ContentType = "application/json",
  122. Postdata = msg,
  123. PostEncoding = Encoding.UTF8,
  124. Encoding = Encoding.UTF8
  125. };
  126. //item.Encoding = Encoding.UTF8;
  127. hResult = http.GetHtml(item);
  128. string html = hResult.Html;
  129. }
  130. }
  131. #region xiu
  132. public static void sendXiuPersonMsg(string openids, string cust, string con, string notify)
  133. {
  134. if (openids == "") return;
  135. string[] oArr = openids.Split(',');
  136. for (int i = 0; i < oArr.Length; i++)
  137. {
  138. sendXiuPersonMsg(oArr[i], "有新的维修单待受理", cust, con, notify, "请注意查看");
  139. }
  140. }
  141. public static void sendXiuPersonMsg(string openid, string first, string cust, string con, string notify, string remark)
  142. {
  143. HttpHelper http = new HttpHelper();
  144. HttpResult hResult = null;
  145. object cat = getAt(http);
  146. if (cat != null)
  147. {
  148. var tdata = new
  149. {
  150. first = new TemplateItem(first, "#333333"),
  151. keyword1 = new TemplateItem(cust, "#333333"),
  152. keyword2 = new TemplateItem(con, "#0E7700"),
  153. keyword3 = new TemplateItem(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), "#0E7700"),
  154. keyword4 = new TemplateItem(notify, "#0E7700"),
  155. remark = new TemplateItem(remark, "#333333")
  156. };
  157. //var minidata = new
  158. //{
  159. // appid = "wx20432512482565e9",
  160. // pagepath = "pages/lab/lab?remind=1"
  161. //};
  162. var msgData = new TemplateData()
  163. {
  164. touser = openid,
  165. template_id = "0sbCmaWrhC-5v_jL7z0-BR7PtrKJ75E74E2WJuu4Wk8",
  166. url = "",
  167. data = tdata
  168. };
  169. // miniprogram= minidata,
  170. string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData);
  171. HttpItem item = new HttpItem()
  172. {
  173. URL = sendUrl + cat,
  174. Method = "POST",
  175. ContentType = "application/json",
  176. Postdata = msg,
  177. PostEncoding = Encoding.UTF8,
  178. Encoding = Encoding.UTF8
  179. };
  180. //item.Encoding = Encoding.UTF8;
  181. hResult = http.GetHtml(item);
  182. string html = hResult.Html;
  183. XLog.SaveLog(0, html);
  184. }
  185. }
  186. public static void sendXiuCorpMsg(string openids, string type, string state, string log)
  187. {
  188. if (openids == "") return;
  189. string[] oArr = openids.Split(',');
  190. for (int i = 0; i < oArr.Length; i++)
  191. {
  192. sendXiuCorpMsg(oArr[i], "您的售后服务有新的进展", type, state, log, "请注意查看");
  193. }
  194. }
  195. public static void sendXiuCorpMsg(string openid, string first, string type, string state, string log, string remark)
  196. {
  197. HttpHelper http = new HttpHelper();
  198. HttpResult hResult = null;
  199. object cat = getAt(http);
  200. if (cat != null)
  201. {
  202. var tdata = new
  203. {
  204. first = new TemplateItem(first, "#333333"),
  205. HandleType = new TemplateItem(type, "#333333"),
  206. Status = new TemplateItem(state, "#0E7700"),
  207. RowCreateDate = new TemplateItem(DateTime.Now.ToString("yyyy-MM-dd HH:mm"), "#0E7700"),
  208. LogType = new TemplateItem(log, "#0E7700"),
  209. remark = new TemplateItem(remark, "#333333")
  210. };
  211. //var minidata = new
  212. //{
  213. // appid = "wx20432512482565e9",
  214. // pagepath = "pages/lab/lab?remind=1"
  215. //};
  216. var msgData = new TemplateData()
  217. {
  218. touser = openid,
  219. template_id = "_OsQ7mmYeEjq3GsZDZVVnZDh5iIw_jbp7nUlkTTfwsk",
  220. url = "",
  221. data = tdata
  222. };
  223. // miniprogram= minidata,
  224. string msg = Newtonsoft.Json.JsonConvert.SerializeObject(msgData);
  225. HttpItem item = new HttpItem()
  226. {
  227. URL = sendUrl + cat,
  228. Method = "POST",
  229. ContentType = "application/json",
  230. Postdata = msg,
  231. PostEncoding = Encoding.UTF8,
  232. Encoding = Encoding.UTF8
  233. };
  234. //item.Encoding = Encoding.UTF8;
  235. hResult = http.GetHtml(item);
  236. string html = hResult.Html;
  237. XLog.SaveLog(0, html);
  238. }
  239. }
  240. #endregion
  241. }
  242. }
  243. public class TemplateData
  244. {
  245. public string touser { get; set; }
  246. public string template_id { get; set; }
  247. public string topcolor { get; set; }
  248. public string url { get; set; }
  249. public object data { get; set; }
  250. public object miniprogram { get; set; }
  251. }
  252. public class TemplateItem
  253. {
  254. public string value { get; set; }
  255. public string color { get; set; }
  256. public TemplateItem(string _value)
  257. {
  258. value = _value;
  259. }
  260. public TemplateItem(string _value, string _color)
  261. {
  262. value = _value;
  263. color = _color;
  264. }
  265. }