This commit is contained in:
tanglong 2025-06-10 16:32:54 +08:00
parent 687fdcd67a
commit ff846fe72f

View File

@ -430,8 +430,15 @@ namespace YD_WeChatApplet.Services
SchoolCode = userInfo.PhoneNo SchoolCode = userInfo.PhoneNo
}); });
var userAuth = await _userContext.User_Auth.CountAsync(x => x.Self_Id == userInfo.User_Id && x.Bind_Id == userInfo.User_Id); // 异步启动一个后台任务,执行数据库插入逻辑,不影响主流程
if (userAuth == 0) _ = Task.Run(async () =>
{
try
{
var authCount = await _userContext.User_Auth
.CountAsync(x => x.Self_Id == userInfo.User_Id && x.Bind_Id == userInfo.User_Id);
if (authCount == 0)
{ {
await _userContext.User_Auth.AddAsync(new User_Auth() await _userContext.User_Auth.AddAsync(new User_Auth()
{ {
@ -443,6 +450,13 @@ namespace YD_WeChatApplet.Services
await _userContext.SaveChangesAsync(); await _userContext.SaveChangesAsync();
} }
}
catch (Exception ex)
{
// 记录异常,避免后台线程未捕获异常引发崩溃
Console.WriteLine($"[UserAuth Insert Error]: {ex.Message}");
}
});
return new UserInfoDto() return new UserInfoDto()
{ {
@ -456,6 +470,7 @@ namespace YD_WeChatApplet.Services
Token = token Token = token
}; };
} }
private async Task<WeChatSession> GetWeChatSession(string code) private async Task<WeChatSession> GetWeChatSession(string code)
{ {
string url = $"https://api.weixin.qq.com/sns/jscode2session?appid={AppSettings.WeChat.Appid}&secret={AppSettings.WeChat.Secret}&js_code={code}&grant_type=authorization_code"; string url = $"https://api.weixin.qq.com/sns/jscode2session?appid={AppSettings.WeChat.Appid}&secret={AppSettings.WeChat.Secret}&js_code={code}&grant_type=authorization_code";