fix 登录授权的问题

This commit is contained in:
glz
2026-06-30 09:15:52 +08:00
parent 2fcff63ee2
commit c342f543f7
5 changed files with 94 additions and 70 deletions

View File

@ -24,7 +24,6 @@ public class WeChatAuthService(
IPetService petService)
: BaseRepository<WxUser>, IWeChatAuthService
{
private const string TokenKeyPrefix = "InteractiveMagazine:WeChatAuth:Token";
private const string AccessTokenCacheKey = "InteractiveMagazine:WeChat:AccessToken";
private const string Code2SessionUrl = "https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code";
private const string GetAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}";
@ -85,10 +84,10 @@ public class WeChatAuthService(
UpdatedAt = DateTime.Now
};
var wxUserId = await wxUserRepository.Insertable(wxUser).ExecuteReturnIdentityAsync();
wxUser.Id = wxUserId;
await wxUserRepository.InsertAsync(wxUser);
logger.LogInformation("WxUser 创建成功WxUserId: {WxUserId}, OpenId: {OpenId}", wxUserId, wxResponse.OpenId);
logger.LogInformation("WxUser 创建成功WxUserId: {WxUserId}, OpenId: {OpenId}", wxUser.Id, wxResponse.OpenId);
}
else
{
@ -201,8 +200,8 @@ public class WeChatAuthService(
var token = JwtHelper.GenerateToken(wxUserId, refreshedUser.Id, refreshedUser.Name ?? string.Empty, jwtSettings);
// 清除旧 Redis Token写入新 Token
await RedisHelper.DelAsync($"{TokenKeyPrefix}:{currentUserId}");
await RedisHelper.SetAsync($"{TokenKeyPrefix}:{refreshedUser.Id}", token, TimeSpan.FromMinutes(jwtSettings.ExpiryMinutes));
await RedisHelper.DelAsync(JwtHelper.BuildWeChatTokenKey(wxUserId, currentUserId));
await RedisHelper.SetAsync(JwtHelper.BuildWeChatTokenKey(wxUserId, refreshedUser.Id), token, TimeSpan.FromMinutes(jwtSettings.ExpiryMinutes));
return new WeChatSwitchUserOutput
{
@ -327,7 +326,7 @@ public class WeChatAuthService(
var jwtSettings = GetJwtSettings();
var token = JwtHelper.GenerateToken(wxUser.Id, userId, userName, jwtSettings);
await RedisHelper.SetAsync($"{TokenKeyPrefix}:{wxUser.Id}{userId}", token, TimeSpan.FromMinutes(jwtSettings.ExpiryMinutes));
await RedisHelper.SetAsync(JwtHelper.BuildWeChatTokenKey(wxUser.Id, userId), token, TimeSpan.FromMinutes(jwtSettings.ExpiryMinutes));
return new WeChatLoginOutput
{