You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
7.8 KiB

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.IO;
  11. using System.Net;
  12. using System.Text;
  13. namespace Basic.Tools
  14. {
  15. /// <summary>
  16. /// 发送模板消息通知会员
  17. /// </summary>
  18. public class SendInfo
  19. {
  20. public static void SendTheInfo(string type, string openid, string appid, string secret, string strContent)
  21. {
  22. string access_token = GetResult("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret);
  23. access_token = access_token.Remove(0, access_token.IndexOf("\":\"")).Split('"')[2];
  24. string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={0}", access_token);
  25. string content = "";
  26. string[] ArrayCon = strContent.Split('|');
  27. if (type == "下单成功通知")
  28. {
  29. content = "{\"touser\":\"" + openid + "\"," +
  30. "\"template_id\":\"TKbAdcfIzfikwyOMX4VGsa9CTLwCWHhCKUhDcYWUbkg\"," +
  31. "\"url\":\"http://m.ivhua.com/\"," +
  32. "\"topcolor\":\"#d4a93a\"," +
  33. "\"data\":{" +
  34. "\"first\": {" +
  35. "\"value\":\"" + ArrayCon[0] + "\"," +
  36. "\"color\":\"#0A0A0A\"" +
  37. "}," +
  38. "\"keyword1\": {" +
  39. "\"value\":\"" + ArrayCon[1] + "\"," +
  40. "\"color\":\"#0A0A0A\"" +
  41. "}," +
  42. "\"keyword2\":{" +
  43. "\"value\":\"" + ArrayCon[2] + "\"," +
  44. "\"color\":\"#0A0A0A\"" +
  45. "}," +
  46. "\"keyword3\": {" +
  47. "\"value\":\"" + ArrayCon[3] + "\"," +
  48. "\"color\":\"#0A0A0A\"" +
  49. "}," +
  50. "\"keyword4\": {" +
  51. "\"value\":\"" + ArrayCon[4] + "\"," +
  52. "\"color\":\"#0A0A0A\"" +
  53. "}," +
  54. "\"keyword5\": {" +
  55. "\"value\":\"" + ArrayCon[5] + "\"," +
  56. "\"color\":\"#0A0A0A\"" +
  57. "}," +
  58. "\"remark\": {" +
  59. "\"value\":\"" + ArrayCon[6] + "\"," +
  60. "\"color\":\"#173177\"" +
  61. "} " +
  62. "}" +
  63. "}";
  64. }
  65. if (type == "订单发货通知")
  66. {
  67. content = "{\"touser\":\"" + openid + "\"," +
  68. "\"template_id\":\"q6-u6t8YK0e1m3MYLIfJdR5LOuqC0jCrbd7QXWADSW4\"," +
  69. "\"url\":\"http://m.ivhua.com/\"," +
  70. "\"topcolor\":\"#d4a93a\"," +
  71. "\"data\":{" +
  72. "\"first\": {" +
  73. "\"value\":\"" + ArrayCon[0] + "\"," +
  74. "\"color\":\"#0A0A0A\"" +
  75. "}," +
  76. "\"keyword1\": {" +
  77. "\"value\":\"" + ArrayCon[1] + "\"," +
  78. "\"color\":\"#0A0A0A\"" +
  79. "}," +
  80. "\"keyword2\":{" +
  81. "\"value\":\"" + ArrayCon[2] + "\"," +
  82. "\"color\":\"#0A0A0A\"" +
  83. "}," +
  84. "\"keyword3\": {" +
  85. "\"value\":\"" + ArrayCon[3] + "\"," +
  86. "\"color\":\"#0A0A0A\"" +
  87. "}," +
  88. "\"keyword4\": {" +
  89. "\"value\":\"" + ArrayCon[4] + "\"," +
  90. "\"color\":\"#0A0A0A\"" +
  91. "}," +
  92. "\"keyword5\": {" +
  93. "\"value\":\"" + ArrayCon[5] + "\"," +
  94. "\"color\":\"#0A0A0A\"" +
  95. "}," +
  96. "\"remark\": {" +
  97. "\"value\":\"" + ArrayCon[6] + "\"," +
  98. "\"color\":\"#173177\"" +
  99. "} " +
  100. "}" +
  101. "}";
  102. }
  103. SendPostHttpRequest(url, "application/x-www-form-urlencoded", content);
  104. }
  105. private static string GetResult(string theurl)
  106. {
  107. string postString = theurl.Split('?')[1];//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来
  108. byte[] postData = Encoding.UTF8.GetBytes(postString);//编码,尤其是汉字,事先要看下抓取网页的编码方式
  109. string url = theurl.Split('?')[0];//地址
  110. WebClient webClient = new WebClient();
  111. webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");//采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可
  112. byte[] responseData = webClient.UploadData(url, "POST", postData);//得到返回字符流
  113. string srcString = Encoding.UTF8.GetString(responseData);
  114. return srcString;
  115. }
  116. private static string SendPostHttpRequest(string url, string contentType, string requestData)
  117. {
  118. WebRequest request = (WebRequest)HttpWebRequest.Create(url);
  119. request.Method = "POST";
  120. byte[] postBytes = null;
  121. request.ContentType = contentType;
  122. postBytes = Encoding.UTF8.GetBytes(requestData);
  123. request.ContentLength = postBytes.Length;
  124. using (Stream outstream = request.GetRequestStream())
  125. {
  126. outstream.Write(postBytes, 0, postBytes.Length);
  127. }
  128. string result = string.Empty;
  129. using (WebResponse response = request.GetResponse())
  130. {
  131. if (response != null)
  132. {
  133. using (Stream stream = response.GetResponseStream())
  134. {
  135. using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
  136. {
  137. result = reader.ReadToEnd();
  138. }
  139. }
  140. }
  141. }
  142. return result;
  143. }
  144. }
  145. }