feat(auth): add comprehensive logging to WechatPhoneLogin method
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:
zpc 2026-03-26 22:22:54 +08:00
parent 442f32a043
commit 96279745cb

View File

@ -12,6 +12,7 @@ using LiveForum.Model.Enum;
using LiveForum.Model.Enum.Users;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Security.Claims;
@ -38,6 +39,7 @@ namespace LiveForum.Service.Auth
private readonly IUserInfoService _userInfoService;
private readonly IBaseRepository<T_IdentityGroups> _identityGroupsRepository;
private readonly IBaseRepository<T_UserIdentityGroups> _userIdentityGroupsRepository;
private readonly ILogger<LoginService> _logger;
/// <summary>
/// 注意,要先创建私有对象,在构造函数中进行赋值
@ -53,6 +55,7 @@ namespace LiveForum.Service.Auth
/// <param name="userInfoService">用户信息服务</param>
/// <param name="identityGroupsRepository">身份组仓储类</param>
/// <param name="userIdentityGroupsRepository">用户身份组关联仓储类</param>
/// <param name="logger">日志记录器</param>
public LoginService(IJwtAuthManager jwtAuthManager,
IBaseRepository<T_Users> userRepository,
IBaseRepository<T_UserTokens> userTokensRepository,
@ -63,7 +66,8 @@ namespace LiveForum.Service.Auth
IOptionsSnapshot<AppSettings> appSettingsSnapshot,
IUserInfoService userInfoService,
IBaseRepository<T_IdentityGroups> identityGroupsRepository,
IBaseRepository<T_UserIdentityGroups> userIdentityGroupsRepository)
IBaseRepository<T_UserIdentityGroups> userIdentityGroupsRepository,
ILogger<LoginService> logger)
{
_jwtAuthManager = jwtAuthManager;
_userRepository = userRepository;
@ -76,6 +80,7 @@ namespace LiveForum.Service.Auth
_userInfoService = userInfoService;
_identityGroupsRepository = identityGroupsRepository;
_userIdentityGroupsRepository = userIdentityGroupsRepository;
_logger = logger;
}
/// <summary>
@ -535,6 +540,8 @@ namespace LiveForum.Service.Auth
if (string.IsNullOrEmpty(openId)) throw new ArgumentNullException(nameof(openId));
if (string.IsNullOrEmpty(phoneNumber)) throw new ArgumentNullException(nameof(phoneNumber));
_logger.LogInformation("[WechatPhoneLogin] 开始处理OpenId={OpenId}, PhoneNumber={PhoneNumber}", openId, phoneNumber);
var now = DateTime.Now;
var clientIp = _httpContextAccessor.GetClientIpAddress();
T_Users user = null;
@ -545,6 +552,8 @@ namespace LiveForum.Service.Auth
.Where(x => x.PhoneNumber == phoneNumber)
.FirstAsync();
_logger.LogInformation("[WechatPhoneLogin] 手机号查询结果: UserId={UserId}", user?.Id);
if (user != null)
{
// 找到手机号用户,确保绑定/更新 OpenID
@ -553,6 +562,9 @@ namespace LiveForum.Service.Auth
.Where(x => x.UserId == user.Id)
.FirstAsync();
_logger.LogInformation("[WechatPhoneLogin] 按UserId={UserId}查微信记录: WechatLoginId={WechatLoginId}, ExistingOpenId={ExistingOpenId}",
user.Id, wechatLogin?.Id, wechatLogin?.OpenId);
if (wechatLogin == null)
{
// 当前用户没有微信记录,检查该 OpenId 是否已被其他用户占用
@ -560,9 +572,14 @@ namespace LiveForum.Service.Auth
.Where(x => x.OpenId == openId)
.FirstAsync();
_logger.LogInformation("[WechatPhoneLogin] 当前用户无微信记录按OpenId查重: ExistingId={ExistingId}, ExistingUserId={ExistingUserId}",
existingByOpenId?.Id, existingByOpenId?.UserId);
if (existingByOpenId != null)
{
// OpenId 已存在,将其重新关联到当前手机号用户
_logger.LogInformation("[WechatPhoneLogin] OpenId已被UserId={OldUserId}占用重新关联到UserId={NewUserId}",
existingByOpenId.UserId, user.Id);
existingByOpenId.UserId = user.Id;
existingByOpenId.SessionKey = sessionKey;
existingByOpenId.LastLoginTime = now;
@ -573,6 +590,7 @@ namespace LiveForum.Service.Auth
}
else
{
_logger.LogInformation("[WechatPhoneLogin] OpenId不存在新建微信记录UserId={UserId}, OpenId={OpenId}", user.Id, openId);
wechatLogin = new T_WechatMiniProgramLogins
{
OpenId = openId,
@ -604,6 +622,9 @@ namespace LiveForum.Service.Auth
.Where(x => x.OpenId == openId)
.FirstAsync();
_logger.LogInformation("[WechatPhoneLogin] 手机号未匹配用户按OpenId查询: WechatLoginId={WechatLoginId}, UserId={UserId}",
wechatLogin?.Id, wechatLogin?.UserId);
if (wechatLogin != null)
{
user = await _userRepository.Select
@ -627,6 +648,8 @@ namespace LiveForum.Service.Auth
// 3. 新用户注册
if (user == null)
{
_logger.LogInformation("[WechatPhoneLogin] 手机号和OpenId均未匹配创建新用户。OpenId={OpenId}, PhoneNumber={PhoneNumber}", openId, phoneNumber);
var appSettings = _appSettingsSnapshot.Value;
var defaultNickName = string.IsNullOrEmpty(appSettings.UserDefaultName) ? "用户" : appSettings.UserDefaultName;
var defaultAvatar = string.IsNullOrEmpty(appSettings.UserDefaultIcon) ? "" : appSettings.UserDefaultIcon;
@ -651,6 +674,7 @@ namespace LiveForum.Service.Auth
LastLoginTime = now
};
await _userRepository.InsertAsync(user);
_logger.LogInformation("[WechatPhoneLogin] 新用户创建成功UserId={UserId}", user.Id);
// 新用户自动关联默认身份组
await AssignDefaultIdentityGroupAsync(user.Id);
@ -667,6 +691,7 @@ namespace LiveForum.Service.Auth
UpdatedAt = now,
};
await _wechatMiniProgramLoginsRepository.InsertAsync(wechatLogin);
_logger.LogInformation("[WechatPhoneLogin] 新用户微信记录创建成功WechatLoginId={WechatLoginId}", wechatLogin.Id);
}
// 更新用户最后登录信息
@ -674,6 +699,8 @@ namespace LiveForum.Service.Auth
user.LastLoginIp = clientIp;
await _userRepository.UpdateAsync(user);
_logger.LogInformation("[WechatPhoneLogin] 登录成功UserId={UserId}, WechatLoginId={WechatLoginId}", user.Id, wechatLogin?.Id);
// 生成JWT Token
var claims = new List<Claim>
{