当前位置:Gxlcms > JavaScript > 微信开发 消息推送实现代码

微信开发 消息推送实现代码

时间:2021-07-01 10:21:17 帮助过:6人阅读

最近做微信公共号的开发,有个需求是这样的消息推送,以文本的形式把编辑的消息发送给微信企业号中的某一个应用组,这里做下笔记,以下是整理内容:

  1. //定义数据模型
  2. public class Access_token
  3. {
  4. public Access_token()
  5. {
  6. //
  7. // TODO: 在此处添加构造函数逻辑
  8. //
  9. }
  10. string _access_token;
  11. string _expires_in;
  12. ///
  13. /// 获取到的凭证
  14. ///
  15. public string access_token
  16. {
  17. get { return _access_token; }
  18. set { _access_token = value; }
  19. }
  20. ///
  21. /// 凭证有效时间,单位:秒
  22. ///
  23. public string expires_in
  24. {
  25. get { return _expires_in; }
  26. set { _expires_in = value; }
  27. }
  28. }<br>
  1. public ActionResult index(string returnUrl)
  2. {
  3. GetAccess_token();
  4. IsExistAccess_Token();
  5. return View();
  6. }
  7. public static Access_token GetAccess_token()
  8. {
  9. string AppUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?";
  10. string AppID = "应用组的CorpID";//在设置-》权限管理-》系统管理组
  11. string AppSecret = "应用组的Secret";//在设置-》权限管理-》系统管理组
  12. WebClient webClient = new WebClient();
  13. Byte[] bytes = webClient.DownloadData(string.Format("{0}corpid={1}&corpsecret={2}", AppUrl, AppID, AppSecret));
  14. string result = Encoding.GetEncoding("utf-8").GetString(bytes);
  15. JObject jObj = JObject.Parse(result);
  16. string token = jObj["access_token"].ToString();
  17. string expires_in = jObj["expires_in"].ToString();
  18. Access_token mode = new Access_token();
  19. mode.access_token = token;
  20. mode.expires_in = expires_in;
  21. return mode;
  22. }
  23. ///

根据当前日期 判断Access_Token 是否超期 如果超期返回新的Access_Token 否则返回之前的Access_Token

  1. public static string IsExistAccess_Token()
  2. {
  3. string Token = string.Empty;
  4. DateTime YouXRQ;
  5. string strPath = "../../weixin/XMLFile.xml";
  6. // 读取XML文件中的数据,并显示出来
  7. //string filepath = System.Web.Hosting.HostingEnvironment.MapPath(strPath);
  8. string filepath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
  9. StreamReader str = new StreamReader(filepath, System.Text.Encoding.UTF8);
  10. XmlDocument xml = new XmlDocument();
  11. xml.Load(str);
  12. str.Close();
  13. str.Dispose();
  14. Token = xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText;
  15. YouXRQ = Convert.ToDateTime(xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText);
  16. if (DateTime.Now > YouXRQ)
  17. {
  18. DateTime _youxrq = DateTime.Now;
  19. Access_token mode = GetAccess_token();
  20. xml.SelectSingleNode("xml").SelectSingleNode("Access_Token").InnerText = mode.access_token;
  21. _youxrq = _youxrq.AddSeconds(int.Parse(mode.expires_in));
  22. xml.SelectSingleNode("xml").SelectSingleNode("Access_YouXRQ").InnerText = _youxrq.ToString();
  23. xml.Save(filepath);
  24. Token = mode.access_token;
  25. }
  26. object text = new
  27. {
  28. toparty = "1",
  29. agentid = "2",
  30. msgtype = "text",
  31. text = new
  32. {
  33. content = "项目名称:"+来保网+""
  34. }
  35. };
  36. string wcr= btnSend(Token, text);
  37. return wcr;
  38. }
  39. public static string btnSend(string Token, object text)
  40. {
  41. string url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + Token;
  42. WebRequest req = WebRequest.Create(url);
  43. JavaScriptSerializer aa = new JavaScriptSerializer();
  44. string postData = aa.Serialize(text);
  45. byte[] requestBytes = Encoding.UTF8.GetBytes(postData);
  46. req.Method = "POST";
  47. req.ContentType = "application/x-www-form-urlencoded";
  48. req.ContentLength = requestBytes.Length;
  49. Stream requestStream = req.GetRequestStream();
  50. requestStream.Write(requestBytes, 0, requestBytes.Length);
  51. requestStream.Close();
  52. HttpWebResponse res = (HttpWebResponse)req.GetResponse();
  53. StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.Default);
  54. string backstr = sr.ReadToEnd();
  55. sr.Close();
  56. res.Close();
  57. WeChatReturn WCR = aa.Deserialize(backstr);
  58. return WCR.errmsg;
  59. }

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

人气教程排行