优化代码

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
{
Title = "ZrAdmin.NET Api - .NET6",
Title = "ZrAdmin.NET Api",
Version = "v1",
Description = "",
});

View File

@ -4,6 +4,7 @@ using Infrastructure.Model;
using IPTools.Core;
using Microsoft.AspNetCore.Http.Features;
using NLog;
using System.Diagnostics;
using System.Text.Encodings.Web;
using System.Text.Json;
using ZR.Admin.WebApi.Extensions;
@ -117,7 +118,18 @@ 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);
// 获取异常堆栈
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);
}

View File

@ -41,6 +41,7 @@ namespace ZR.Common
}
if (string.IsNullOrEmpty(CORPID))
{
System.Console.WriteLine("请完成企业微信配置");
return (0, "请完成企业微信通知配置");
}
GetTokenResult tokenResult = GetAccessToken();
@ -74,7 +75,7 @@ namespace ZR.Common
//{"errcode":0,"errmsg":"ok","invaliduser":""}
string msgResult = HttpHelper.HttpPost(msgUrl, postData, "contentType/json");
GetTokenResult getTokenResult = JsonSerializer.Deserialize<GetTokenResult>(msgResult);
System.Console.WriteLine(msgResult);
return (getTokenResult?.errcode == 0 ? 100 : 0, getTokenResult?.errmsg);
}
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 getTokenResult = HttpHelper.HttpGet(getTokenUrl);
System.Console.WriteLine(getTokenResult);
GetTokenResult tokenResult = JsonSerializer.Deserialize<GetTokenResult>(getTokenResult);
return tokenResult;
}
@ -131,16 +132,18 @@ namespace ZR.Common
{
Dictionary<string, object> dic = new()
{
{ "touser", toUser },
{ "msgtype", "markdown" },
{ "agentid", AGENTID },
{ "enable_duplicate_check",1}
};
dic.Add("markdown", new Dictionary<string, string>
{ "touser", toUser },
{ "msgtype", "markdown" },
{ "agentid", AGENTID },
{ "enable_duplicate_check", 1 },
{
"markdown",
new Dictionary<string, string>
{
{ "content", $"**{title}**\n\n{content}" }
});
}
}
};
return dic;
}