WxNoticeHelper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Infrastructure;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using ZR.Common.Model;
  5. namespace ZR.Common
  6. {
  7. public class WxNoticeHelper
  8. {
  9. //CorpID 企业ID
  10. //AGENTID 应用的ID
  11. //Secret 应用的ID对应的密钥
  12. private static readonly string AGENTID = AppSettings.App(new string[] { "WxCorp", "AgentID" });
  13. private static readonly string CORPID = AppSettings.App(new string[] { "WxCorp", "CorpID" });
  14. private static readonly string CORPSECRET = AppSettings.App(new string[] { "WxCorp", "CorpSecret" });
  15. private static readonly string SEND_USER = AppSettings.App(new string[] { "WxCorp", "SendUser" });
  16. private static readonly string SendUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
  17. private static readonly string GetTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
  18. /// <summary>
  19. /// 消息类型
  20. /// </summary>
  21. public enum MsgType { markdown, text, textcard, interactive_taskcard }
  22. /// <summary>
  23. /// 发送消息公共模板方法
  24. /// </summary>
  25. /// <param name="toUser">微信微信好友id,默认@all发给所有关注该应用的用户</param>
  26. /// <param name="title">标题</param>
  27. /// <param name="content">内容</param>
  28. /// <param name="msgType">消息类型</param>
  29. /// <returns></returns>
  30. public static (int, string) SendMsg(string title, string content, string toUser = "", MsgType msgType = MsgType.text)
  31. {
  32. if (string.IsNullOrEmpty(toUser))
  33. {
  34. toUser = SEND_USER;
  35. }
  36. if (string.IsNullOrEmpty(title))
  37. {
  38. return (0, "title不能为空");
  39. }
  40. if (string.IsNullOrEmpty(CORPID))
  41. {
  42. System.Console.WriteLine("如需微信接收异常消息,请完成企业微信配置");
  43. return (0, "请完成企业微信通知配置");
  44. }
  45. WxTokenResult tokenResult = GetAccessToken();
  46. if (tokenResult == null || tokenResult.errcode != 0)
  47. {
  48. return (0, tokenResult?.errmsg);
  49. }
  50. Dictionary<string, object> dic = null;
  51. switch (msgType)
  52. {
  53. case MsgType.markdown:
  54. dic = GetMarkdown(title, content, toUser);
  55. break;
  56. case MsgType.text:
  57. dic = GetText(title, content, toUser);
  58. break;
  59. case MsgType.textcard:
  60. break;
  61. case MsgType.interactive_taskcard:
  62. break;
  63. default:
  64. dic = GetText(title, content, toUser);
  65. break;
  66. }
  67. string postData = JsonSerializer.Serialize(dic);
  68. string msgUrl = $"{SendUrl}?access_token={tokenResult.access_token}";
  69. //返回结果
  70. //{"errcode":0,"errmsg":"ok","invaliduser":""}
  71. string msgResult = HttpHelper.HttpPost(msgUrl, postData, "contentType/json");
  72. WxTokenResult getTokenResult = JsonSerializer.Deserialize<WxTokenResult>(msgResult);
  73. System.Console.WriteLine(msgResult);
  74. return (getTokenResult?.errcode == 0 ? 100 : 0, getTokenResult?.errmsg);
  75. }
  76. public static (int, string) SendMsg(string title, string content, string toUser)
  77. {
  78. return SendMsg(title, content, toUser, MsgType.markdown);
  79. }
  80. /// <summary>
  81. /// 获取访问token
  82. /// </summary>
  83. /// <returns>
  84. /// {"errcode":0,"errmsg":"ok","access_token":"iCbcfE1OjfRhV0_io-CzqTNC0lnrudeW3oF5rhJKfmINaxLClLa1FoqAY_wEXtodYh_DTnrtAwZfzeb-NRXvwiOoqUTHx3i6QKLYcfBtF8y-xd5mvaeaf3e9mvTAPhmX0lkm1cLTwRLmoa1IwzgQ-QZEZcuIcntWdEMGseVYok3BwCGpC87bt6nNdgnekZdFVRp1uuaxoctDGlXpoQlQsA","expires_in":7200}
  85. /// </returns>
  86. private static WxTokenResult GetAccessToken()
  87. {
  88. string getTokenUrl = $"{GetTokenUrl}?corpid={CORPID}&corpsecret={CORPSECRET}";
  89. string getTokenResult = HttpHelper.HttpGet(getTokenUrl);
  90. System.Console.WriteLine(getTokenResult);
  91. WxTokenResult tokenResult = JsonSerializer.Deserialize<WxTokenResult>(getTokenResult);
  92. return tokenResult;
  93. }
  94. /// <summary>
  95. /// 发送text
  96. /// </summary>
  97. /// <param name="title"></param>
  98. /// <param name="content"></param>
  99. /// <param name="toUser"></param>
  100. /// <returns></returns>
  101. private static Dictionary<string, object> GetText(string title, string content, string toUser = "")
  102. {
  103. Dictionary<string, object> dic = new()
  104. {
  105. { "msgtype", "text" },
  106. { "touser", toUser },
  107. { "agentid", AGENTID },
  108. { "text", new Dictionary<string, string>
  109. {
  110. { "content",$"{title}\n\n{content}"
  111. }
  112. }}
  113. };
  114. return dic;
  115. }
  116. /// <summary>
  117. /// 发送markdown
  118. /// </summary>
  119. /// <param name="title">要发送的标题</param>
  120. /// <param name="content">发送的内容</param>
  121. /// <param name="toUser">指定接收消息的成员,成员ID列表(多个接收者用‘|’分隔,最多支持1000个)。 特殊情况:指定为”@all”,则向该企业应用的全部成员发送</param>
  122. /// <returns></returns>
  123. private static Dictionary<string, object> GetMarkdown(string title, string content, string toUser = "")
  124. {
  125. Dictionary<string, object> dic = new()
  126. {
  127. { "touser", toUser },
  128. { "msgtype", "markdown" },
  129. { "agentid", AGENTID },
  130. { "enable_duplicate_check", 1 },
  131. {
  132. "markdown",
  133. new Dictionary<string, string>
  134. {
  135. { "content", $"**{title}**\n\n{content}" }
  136. }
  137. }
  138. };
  139. return dic;
  140. }
  141. }
  142. }