线程
This commit is contained in:
parent
687fdcd67a
commit
ff846fe72f
@ -430,19 +430,33 @@ namespace YD_WeChatApplet.Services
|
||||
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,
|
||||
Bind_Id = userInfo.User_Id,
|
||||
Role_Id = userInfo.Role_Id,
|
||||
Token = token
|
||||
});
|
||||
var authCount = await _userContext.User_Auth
|
||||
.CountAsync(x => x.Self_Id == userInfo.User_Id && x.Bind_Id == userInfo.User_Id);
|
||||
|
||||
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()
|
||||
{
|
||||
@ -456,6 +470,7 @@ namespace YD_WeChatApplet.Services
|
||||
Token = token
|
||||
};
|
||||
}
|
||||
|
||||
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";
|
||||
|
Loading…
x
Reference in New Issue
Block a user