WxNoticeHelper.cs 6.2 KB

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