feat(auth): add comprehensive logging to WechatPhoneLogin method
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
- Add ILogger<LoginService> dependency injection to LoginService - Add logging at method entry point with OpenId and PhoneNumber parameters - Add logging for phone number query results and user lookup - Add logging for WeChat record queries by UserId - Add logging for OpenId duplicate detection and reassignment logic - Add logging for new WeChat record creation - Add logging for new user registration flow - Add logging for successful login completion - Improve debugging and monitoring capabilities for WeChat phone login authentication flow
This commit is contained in:
parent
442f32a043
commit
96279745cb
|
|
@ -12,6 +12,7 @@ using LiveForum.Model.Enum;
|
||||||
using LiveForum.Model.Enum.Users;
|
using LiveForum.Model.Enum.Users;
|
||||||
|
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
|
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
|
|
@ -38,6 +39,7 @@ namespace LiveForum.Service.Auth
|
||||||
private readonly IUserInfoService _userInfoService;
|
private readonly IUserInfoService _userInfoService;
|
||||||
private readonly IBaseRepository<T_IdentityGroups> _identityGroupsRepository;
|
private readonly IBaseRepository<T_IdentityGroups> _identityGroupsRepository;
|
||||||
private readonly IBaseRepository<T_UserIdentityGroups> _userIdentityGroupsRepository;
|
private readonly IBaseRepository<T_UserIdentityGroups> _userIdentityGroupsRepository;
|
||||||
|
private readonly ILogger<LoginService> _logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 注意,要先创建私有对象,在构造函数中进行赋值
|
/// 注意,要先创建私有对象,在构造函数中进行赋值
|
||||||
|
|
@ -53,6 +55,7 @@ namespace LiveForum.Service.Auth
|
||||||
/// <param name="userInfoService">用户信息服务</param>
|
/// <param name="userInfoService">用户信息服务</param>
|
||||||
/// <param name="identityGroupsRepository">身份组仓储类</param>
|
/// <param name="identityGroupsRepository">身份组仓储类</param>
|
||||||
/// <param name="userIdentityGroupsRepository">用户身份组关联仓储类</param>
|
/// <param name="userIdentityGroupsRepository">用户身份组关联仓储类</param>
|
||||||
|
/// <param name="logger">日志记录器</param>
|
||||||
public LoginService(IJwtAuthManager jwtAuthManager,
|
public LoginService(IJwtAuthManager jwtAuthManager,
|
||||||
IBaseRepository<T_Users> userRepository,
|
IBaseRepository<T_Users> userRepository,
|
||||||
IBaseRepository<T_UserTokens> userTokensRepository,
|
IBaseRepository<T_UserTokens> userTokensRepository,
|
||||||
|
|
@ -63,7 +66,8 @@ namespace LiveForum.Service.Auth
|
||||||
IOptionsSnapshot<AppSettings> appSettingsSnapshot,
|
IOptionsSnapshot<AppSettings> appSettingsSnapshot,
|
||||||
IUserInfoService userInfoService,
|
IUserInfoService userInfoService,
|
||||||
IBaseRepository<T_IdentityGroups> identityGroupsRepository,
|
IBaseRepository<T_IdentityGroups> identityGroupsRepository,
|
||||||
IBaseRepository<T_UserIdentityGroups> userIdentityGroupsRepository)
|
IBaseRepository<T_UserIdentityGroups> userIdentityGroupsRepository,
|
||||||
|
ILogger<LoginService> logger)
|
||||||
{
|
{
|
||||||
_jwtAuthManager = jwtAuthManager;
|
_jwtAuthManager = jwtAuthManager;
|
||||||
_userRepository = userRepository;
|
_userRepository = userRepository;
|
||||||
|
|
@ -76,6 +80,7 @@ namespace LiveForum.Service.Auth
|
||||||
_userInfoService = userInfoService;
|
_userInfoService = userInfoService;
|
||||||
_identityGroupsRepository = identityGroupsRepository;
|
_identityGroupsRepository = identityGroupsRepository;
|
||||||
_userIdentityGroupsRepository = userIdentityGroupsRepository;
|
_userIdentityGroupsRepository = userIdentityGroupsRepository;
|
||||||
|
_logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -535,6 +540,8 @@ namespace LiveForum.Service.Auth
|
||||||
if (string.IsNullOrEmpty(openId)) throw new ArgumentNullException(nameof(openId));
|
if (string.IsNullOrEmpty(openId)) throw new ArgumentNullException(nameof(openId));
|
||||||
if (string.IsNullOrEmpty(phoneNumber)) throw new ArgumentNullException(nameof(phoneNumber));
|
if (string.IsNullOrEmpty(phoneNumber)) throw new ArgumentNullException(nameof(phoneNumber));
|
||||||
|
|
||||||
|
_logger.LogInformation("[WechatPhoneLogin] 开始处理,OpenId={OpenId}, PhoneNumber={PhoneNumber}", openId, phoneNumber);
|
||||||
|
|
||||||
var now = DateTime.Now;
|
var now = DateTime.Now;
|
||||||
var clientIp = _httpContextAccessor.GetClientIpAddress();
|
var clientIp = _httpContextAccessor.GetClientIpAddress();
|
||||||
T_Users user = null;
|
T_Users user = null;
|
||||||
|
|
@ -545,6 +552,8 @@ namespace LiveForum.Service.Auth
|
||||||
.Where(x => x.PhoneNumber == phoneNumber)
|
.Where(x => x.PhoneNumber == phoneNumber)
|
||||||
.FirstAsync();
|
.FirstAsync();
|
||||||
|
|
||||||
|
_logger.LogInformation("[WechatPhoneLogin] 手机号查询结果: UserId={UserId}", user?.Id);
|
||||||
|
|
||||||
if (user != null)
|
if (user != null)
|
||||||
{
|
{
|
||||||
// 找到手机号用户,确保绑定/更新 OpenID
|
// 找到手机号用户,确保绑定/更新 OpenID
|
||||||
|
|
@ -553,6 +562,9 @@ namespace LiveForum.Service.Auth
|
||||||
.Where(x => x.UserId == user.Id)
|
.Where(x => x.UserId == user.Id)
|
||||||
.FirstAsync();
|
.FirstAsync();
|
||||||
|
|
||||||
|
_logger.LogInformation("[WechatPhoneLogin] 按UserId={UserId}查微信记录: WechatLoginId={WechatLoginId}, ExistingOpenId={ExistingOpenId}",
|
||||||
|
user.Id, wechatLogin?.Id, wechatLogin?.OpenId);
|
||||||
|
|
||||||
if (wechatLogin == null)
|
if (wechatLogin == null)
|
||||||
{
|
{
|
||||||
// 当前用户没有微信记录,检查该 OpenId 是否已被其他用户占用
|
// 当前用户没有微信记录,检查该 OpenId 是否已被其他用户占用
|
||||||
|
|
@ -560,9 +572,14 @@ namespace LiveForum.Service.Auth
|
||||||
.Where(x => x.OpenId == openId)
|
.Where(x => x.OpenId == openId)
|
||||||
.FirstAsync();
|
.FirstAsync();
|
||||||
|
|
||||||
|
_logger.LogInformation("[WechatPhoneLogin] 当前用户无微信记录,按OpenId查重: ExistingId={ExistingId}, ExistingUserId={ExistingUserId}",
|
||||||
|
existingByOpenId?.Id, existingByOpenId?.UserId);
|
||||||
|
|
||||||
if (existingByOpenId != null)
|
if (existingByOpenId != null)
|
||||||
{
|
{
|
||||||
// OpenId 已存在,将其重新关联到当前手机号用户
|
// OpenId 已存在,将其重新关联到当前手机号用户
|
||||||
|
_logger.LogInformation("[WechatPhoneLogin] OpenId已被UserId={OldUserId}占用,重新关联到UserId={NewUserId}",
|
||||||
|
existingByOpenId.UserId, user.Id);
|
||||||
existingByOpenId.UserId = user.Id;
|
existingByOpenId.UserId = user.Id;
|
||||||
existingByOpenId.SessionKey = sessionKey;
|
existingByOpenId.SessionKey = sessionKey;
|
||||||
existingByOpenId.LastLoginTime = now;
|
existingByOpenId.LastLoginTime = now;
|
||||||
|
|
@ -573,6 +590,7 @@ namespace LiveForum.Service.Auth
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("[WechatPhoneLogin] OpenId不存在,新建微信记录,UserId={UserId}, OpenId={OpenId}", user.Id, openId);
|
||||||
wechatLogin = new T_WechatMiniProgramLogins
|
wechatLogin = new T_WechatMiniProgramLogins
|
||||||
{
|
{
|
||||||
OpenId = openId,
|
OpenId = openId,
|
||||||
|
|
@ -604,6 +622,9 @@ namespace LiveForum.Service.Auth
|
||||||
.Where(x => x.OpenId == openId)
|
.Where(x => x.OpenId == openId)
|
||||||
.FirstAsync();
|
.FirstAsync();
|
||||||
|
|
||||||
|
_logger.LogInformation("[WechatPhoneLogin] 手机号未匹配用户,按OpenId查询: WechatLoginId={WechatLoginId}, UserId={UserId}",
|
||||||
|
wechatLogin?.Id, wechatLogin?.UserId);
|
||||||
|
|
||||||
if (wechatLogin != null)
|
if (wechatLogin != null)
|
||||||
{
|
{
|
||||||
user = await _userRepository.Select
|
user = await _userRepository.Select
|
||||||
|
|
@ -627,6 +648,8 @@ namespace LiveForum.Service.Auth
|
||||||
// 3. 新用户注册
|
// 3. 新用户注册
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
|
_logger.LogInformation("[WechatPhoneLogin] 手机号和OpenId均未匹配,创建新用户。OpenId={OpenId}, PhoneNumber={PhoneNumber}", openId, phoneNumber);
|
||||||
|
|
||||||
var appSettings = _appSettingsSnapshot.Value;
|
var appSettings = _appSettingsSnapshot.Value;
|
||||||
var defaultNickName = string.IsNullOrEmpty(appSettings.UserDefaultName) ? "用户" : appSettings.UserDefaultName;
|
var defaultNickName = string.IsNullOrEmpty(appSettings.UserDefaultName) ? "用户" : appSettings.UserDefaultName;
|
||||||
var defaultAvatar = string.IsNullOrEmpty(appSettings.UserDefaultIcon) ? "" : appSettings.UserDefaultIcon;
|
var defaultAvatar = string.IsNullOrEmpty(appSettings.UserDefaultIcon) ? "" : appSettings.UserDefaultIcon;
|
||||||
|
|
@ -651,6 +674,7 @@ namespace LiveForum.Service.Auth
|
||||||
LastLoginTime = now
|
LastLoginTime = now
|
||||||
};
|
};
|
||||||
await _userRepository.InsertAsync(user);
|
await _userRepository.InsertAsync(user);
|
||||||
|
_logger.LogInformation("[WechatPhoneLogin] 新用户创建成功,UserId={UserId}", user.Id);
|
||||||
|
|
||||||
// 新用户自动关联默认身份组
|
// 新用户自动关联默认身份组
|
||||||
await AssignDefaultIdentityGroupAsync(user.Id);
|
await AssignDefaultIdentityGroupAsync(user.Id);
|
||||||
|
|
@ -667,6 +691,7 @@ namespace LiveForum.Service.Auth
|
||||||
UpdatedAt = now,
|
UpdatedAt = now,
|
||||||
};
|
};
|
||||||
await _wechatMiniProgramLoginsRepository.InsertAsync(wechatLogin);
|
await _wechatMiniProgramLoginsRepository.InsertAsync(wechatLogin);
|
||||||
|
_logger.LogInformation("[WechatPhoneLogin] 新用户微信记录创建成功,WechatLoginId={WechatLoginId}", wechatLogin.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新用户最后登录信息
|
// 更新用户最后登录信息
|
||||||
|
|
@ -674,6 +699,8 @@ namespace LiveForum.Service.Auth
|
||||||
user.LastLoginIp = clientIp;
|
user.LastLoginIp = clientIp;
|
||||||
await _userRepository.UpdateAsync(user);
|
await _userRepository.UpdateAsync(user);
|
||||||
|
|
||||||
|
_logger.LogInformation("[WechatPhoneLogin] 登录成功,UserId={UserId}, WechatLoginId={WechatLoginId}", user.Id, wechatLogin?.Id);
|
||||||
|
|
||||||
// 生成JWT Token
|
// 生成JWT Token
|
||||||
var claims = new List<Claim>
|
var claims = new List<Claim>
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user