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

View File

@ -430,19 +430,33 @@ 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 () =>
{ {
await _userContext.User_Auth.AddAsync(new User_Auth() try
{ {
Self_Id = userInfo.User_Id, var authCount = await _userContext.User_Auth
Bind_Id = userInfo.User_Id, .CountAsync(x => x.Self_Id == userInfo.User_Id && x.Bind_Id == userInfo.User_Id);
Role_Id = userInfo.Role_Id,
Token = token
});
await _userContext.SaveChangesAsync(); if (authCount == 0)
} {
await _userContext.User_Auth.AddAsync(new User_Auth()
{
Self_Id = userInfo.User_Id,
Bind_Id = userInfo.User_Id,
Role_Id = userInfo.Role_Id,
Token = token
});
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";