From 262e593ff6a16da28ecab29d1d9020f33a3e610d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Sat, 9 Jul 2022 19:02:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Middleware/GlobalExceptionMiddleware.cs | 3 +- ZR.Admin.WebApi/appsettings.json | 7 + ZR.Common/WxNoticeHelper.cs | 157 ++++++++++++++++++ ZR.Tasks/TaskScheduler/JobBase.cs | 2 + 4 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 ZR.Common/WxNoticeHelper.cs diff --git a/ZR.Admin.WebApi/Middleware/GlobalExceptionMiddleware.cs b/ZR.Admin.WebApi/Middleware/GlobalExceptionMiddleware.cs index c1b224e..7298fcf 100644 --- a/ZR.Admin.WebApi/Middleware/GlobalExceptionMiddleware.cs +++ b/ZR.Admin.WebApi/Middleware/GlobalExceptionMiddleware.cs @@ -10,6 +10,7 @@ using System.Text.Encodings.Web; using System.Text.Json; using System.Threading.Tasks; using ZR.Admin.WebApi.Extensions; +using ZR.Common; using ZR.Model.System; using ZR.Service.System.IService; @@ -119,7 +120,7 @@ namespace ZR.Admin.WebApi.Middleware Logger.Log(ei); context.Response.ContentType = "text/json;charset=utf-8"; await context.Response.WriteAsync(responseResult, System.Text.Encoding.UTF8); - + WxNoticeHelper.SendMsg("系统出错", sysOperLog.errorMsg); SysOperLogService.InsertOperlog(sysOperLog); } diff --git a/ZR.Admin.WebApi/appsettings.json b/ZR.Admin.WebApi/appsettings.json index da86d84..d41e5e2 100644 --- a/ZR.Admin.WebApi/appsettings.json +++ b/ZR.Admin.WebApi/appsettings.json @@ -32,6 +32,13 @@ "bucketName": "bucketName", "domainUrl": "http://xxx.xxx.com" //访问资源域名 }, + //企业微信通知配置 + "WxCorp": { + "AgentID": "", + "CorpID": "", + "CorpSecret": "", + "SendUser": "@all" + }, "gen": { "conn": "Data Source=LAPTOP-STKF2M8H\\SQLEXPRESS;User ID=sa;Password=zradmin123;Initial Catalog=ZrAdmin;Integrated Security=SSPI", "dbType": 1, //MySql = 0, SqlServer = 1 diff --git a/ZR.Common/WxNoticeHelper.cs b/ZR.Common/WxNoticeHelper.cs new file mode 100644 index 0000000..0f7fa3a --- /dev/null +++ b/ZR.Common/WxNoticeHelper.cs @@ -0,0 +1,157 @@ +using Infrastructure; +using System.Collections.Generic; +using System.Text.Json; + +namespace ZR.Common +{ + public class WxNoticeHelper + { + //CorpID 企业ID + //AGENTID 应用的ID + //Secret 应用的ID对应的密钥 + private static readonly string AGENTID = AppSettings.App(new string[] { "WxCorp", "AgentID" }); + private static readonly string CORPID = AppSettings.App(new string[] { "WxCorp", "CorpID" }); + private static readonly string CORPSECRET = AppSettings.App(new string[] { "WxCorp", "CorpSecret" }); + private static readonly string SEND_USER = AppSettings.App(new string[] { "WxCorp", "SendUser" }); + private static readonly string SendUrl = "https://qyapi.weixin.qq.com/cgi-bin/message/send"; + private static readonly string GetTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"; + + /// + /// 消息类型 + /// + public enum MsgType { markdown, text, textcard, interactive_taskcard } + + /// + /// 发送消息公共模板方法 + /// + /// 微信微信好友id,默认@all发给所有关注该应用的用户 + /// 标题 + /// 内容 + /// 消息类型 + /// + public static (int, string) SendMsg(string title, string content, string toUser = "", MsgType msgType = MsgType.text) + { + if (string.IsNullOrEmpty(toUser)) + { + toUser = SEND_USER; + } + if (string.IsNullOrEmpty(title)) + { + return (0, "title不能为空"); + } + if (string.IsNullOrEmpty(CORPID)) + { + return (0, "请完成企业微信通知配置"); + } + GetTokenResult tokenResult = GetAccessToken(); + + if (tokenResult == null || tokenResult.errcode != 0) + { + return (0, tokenResult?.errmsg); + } + + Dictionary dic = null; + switch (msgType) + { + case MsgType.markdown: + dic = GetMarkdown(title, content, toUser); + break; + case MsgType.text: + dic = GetText(title, content, toUser); + break; + case MsgType.textcard: + break; + case MsgType.interactive_taskcard: + break; + default: + dic = GetText(title, content, toUser); + break; + } + string postData = JsonSerializer.Serialize(dic); + string msgUrl = $"{SendUrl}?access_token={tokenResult.access_token}"; + + //返回结果 + //{"errcode":0,"errmsg":"ok","invaliduser":""} + string msgResult = HttpHelper.HttpPost(msgUrl, postData, "contentType/json"); + GetTokenResult getTokenResult = JsonSerializer.Deserialize(msgResult); + + return (getTokenResult?.errcode == 0 ? 100 : 0, getTokenResult?.errmsg); + } + public static (int, string) SendMsg(string title, string content, string toUser) + { + return SendMsg(title, content, toUser, MsgType.markdown); + } + + /// + /// 获取访问token + /// + /// + /// {"errcode":0,"errmsg":"ok","access_token":"iCbcfE1OjfRhV0_io-CzqTNC0lnrudeW3oF5rhJKfmINaxLClLa1FoqAY_wEXtodYh_DTnrtAwZfzeb-NRXvwiOoqUTHx3i6QKLYcfBtF8y-xd5mvaeaf3e9mvTAPhmX0lkm1cLTwRLmoa1IwzgQ-QZEZcuIcntWdEMGseVYok3BwCGpC87bt6nNdgnekZdFVRp1uuaxoctDGlXpoQlQsA","expires_in":7200} + /// + private static GetTokenResult GetAccessToken() + { + string getTokenUrl = $"{GetTokenUrl}?corpid={CORPID}&corpsecret={CORPSECRET}"; + string getTokenResult = HttpHelper.HttpGet(getTokenUrl); + + GetTokenResult tokenResult = JsonSerializer.Deserialize(getTokenResult); + return tokenResult; + } + + /// + /// 发送text + /// + /// + /// + /// + /// + private static Dictionary GetText(string title, string content, string toUser = "") + { + Dictionary dic = new() + { + { "msgtype", "text" }, + { "touser", toUser }, + { "agentid", AGENTID }, + { "text", new Dictionary + { + { "content",$"{title}\n\n{content}" + } + }} + }; + return dic; + } + + /// + /// 发送markdown + /// + /// 要发送的标题 + /// 发送的内容 + /// 指定接收消息的成员,成员ID列表(多个接收者用‘|’分隔,最多支持1000个)。 特殊情况:指定为”@all”,则向该企业应用的全部成员发送 + /// + private static Dictionary GetMarkdown(string title, string content, string toUser = "") + { + Dictionary dic = new() + { + { "touser", toUser }, + { "msgtype", "markdown" }, + { "agentid", AGENTID }, + { "enable_duplicate_check",1} + }; + + dic.Add("markdown", new Dictionary + { + { "content", $"**{title}**\n\n{content}" } + }); + return dic; + } + + public class GetTokenResult + { + /// + /// 0、正常 + /// + public int errcode { get; set; } + public string errmsg { get; set; } + public string access_token { get; set; } + } + } +} diff --git a/ZR.Tasks/TaskScheduler/JobBase.cs b/ZR.Tasks/TaskScheduler/JobBase.cs index 0f3d973..52419e0 100644 --- a/ZR.Tasks/TaskScheduler/JobBase.cs +++ b/ZR.Tasks/TaskScheduler/JobBase.cs @@ -4,6 +4,7 @@ using Quartz; using System; using System.Diagnostics; using System.Threading.Tasks; +using ZR.Common; using ZR.Model.System; using ZR.Service.System.IService; @@ -47,6 +48,7 @@ namespace ZR.Tasks }; status = 1; logMsg = $"Job Run Fail,Exception:{ex.Message}"; + WxNoticeHelper.SendMsg("任务执行出错", logMsg); } var logModel = new SysTasksLog()