优化代码

This commit is contained in:
不做码农 2023-05-12 08:02:58 +08:00
parent a04aaa947e
commit ec92b3fa3a
3 changed files with 27 additions and 12 deletions

View File

@ -44,7 +44,7 @@ namespace ZR.Admin.WebApi.Extensions
{ {
c.SwaggerDoc("v1", new OpenApiInfo c.SwaggerDoc("v1", new OpenApiInfo
{ {
Title = "ZrAdmin.NET Api - .NET6", Title = "ZrAdmin.NET Api",
Version = "v1", Version = "v1",
Description = "", Description = "",
}); });

View File

@ -4,6 +4,7 @@ using Infrastructure.Model;
using IPTools.Core; using IPTools.Core;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using NLog; using NLog;
using System.Diagnostics;
using System.Text.Encodings.Web; using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
using ZR.Admin.WebApi.Extensions; using ZR.Admin.WebApi.Extensions;
@ -117,7 +118,18 @@ namespace ZR.Admin.WebApi.Middleware
Logger.Log(ei); Logger.Log(ei);
context.Response.ContentType = "text/json;charset=utf-8"; context.Response.ContentType = "text/json;charset=utf-8";
await context.Response.WriteAsync(responseResult, System.Text.Encoding.UTF8); await context.Response.WriteAsync(responseResult, System.Text.Encoding.UTF8);
WxNoticeHelper.SendMsg("系统出错", sysOperLog.ErrorMsg);
// 获取异常堆栈
var traceFrame = new StackTrace(true)?.GetFrame(0);
// 获取出错的文件名
var exceptionFileName = traceFrame?.GetFileName();
// 获取出错的行号
var exceptionFileLineNumber = traceFrame?.GetFileLineNumber();
string errorMsg = $"用户名:{sysOperLog.OperName}\n" +
$"错误信息:{sysOperLog.ErrorMsg}\n"+
$"错误行号:{exceptionFileLineNumber}\n" +
$"{traceFrame}#{exceptionFileName}";
WxNoticeHelper.SendMsg("系统出错", errorMsg);
SysOperLogService.InsertOperlog(sysOperLog); SysOperLogService.InsertOperlog(sysOperLog);
} }

View File

@ -41,6 +41,7 @@ namespace ZR.Common
} }
if (string.IsNullOrEmpty(CORPID)) if (string.IsNullOrEmpty(CORPID))
{ {
System.Console.WriteLine("请完成企业微信配置");
return (0, "请完成企业微信通知配置"); return (0, "请完成企业微信通知配置");
} }
GetTokenResult tokenResult = GetAccessToken(); GetTokenResult tokenResult = GetAccessToken();
@ -74,7 +75,7 @@ namespace ZR.Common
//{"errcode":0,"errmsg":"ok","invaliduser":""} //{"errcode":0,"errmsg":"ok","invaliduser":""}
string msgResult = HttpHelper.HttpPost(msgUrl, postData, "contentType/json"); string msgResult = HttpHelper.HttpPost(msgUrl, postData, "contentType/json");
GetTokenResult getTokenResult = JsonSerializer.Deserialize<GetTokenResult>(msgResult); GetTokenResult getTokenResult = JsonSerializer.Deserialize<GetTokenResult>(msgResult);
System.Console.WriteLine(msgResult);
return (getTokenResult?.errcode == 0 ? 100 : 0, getTokenResult?.errmsg); return (getTokenResult?.errcode == 0 ? 100 : 0, getTokenResult?.errmsg);
} }
public static (int, string) SendMsg(string title, string content, string toUser) public static (int, string) SendMsg(string title, string content, string toUser)
@ -92,7 +93,7 @@ namespace ZR.Common
{ {
string getTokenUrl = $"{GetTokenUrl}?corpid={CORPID}&corpsecret={CORPSECRET}"; string getTokenUrl = $"{GetTokenUrl}?corpid={CORPID}&corpsecret={CORPSECRET}";
string getTokenResult = HttpHelper.HttpGet(getTokenUrl); string getTokenResult = HttpHelper.HttpGet(getTokenUrl);
System.Console.WriteLine(getTokenResult);
GetTokenResult tokenResult = JsonSerializer.Deserialize<GetTokenResult>(getTokenResult); GetTokenResult tokenResult = JsonSerializer.Deserialize<GetTokenResult>(getTokenResult);
return tokenResult; return tokenResult;
} }
@ -134,13 +135,15 @@ namespace ZR.Common
{ "touser", toUser }, { "touser", toUser },
{ "msgtype", "markdown" }, { "msgtype", "markdown" },
{ "agentid", AGENTID }, { "agentid", AGENTID },
{ "enable_duplicate_check",1} { "enable_duplicate_check", 1 },
}; {
"markdown",
dic.Add("markdown", new Dictionary<string, string> new Dictionary<string, string>
{ {
{ "content", $"**{title}**\n\n{content}" } { "content", $"**{title}**\n\n{content}" }
}); }
}
};
return dic; return dic;
} }