debug: add detailed logging for avatar loading
Added debug logs in: - ProfilePage._loadUserData() - local storage data - ProfilePage._loadMeData() - API response and sync conditions - ProfilePage._buildAvatarContent() - avatar rendering decision - UserApplicationService.getMe() - backend avatarUrl value 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
parent
8d94d9e4bb
commit
a3c0d3948d
|
|
@ -492,9 +492,16 @@ export class UserApplicationService {
|
|||
* 获取当前登录用户信息 (GET /api/me)
|
||||
*/
|
||||
async getMe(userId: string): Promise<MeResult> {
|
||||
this.logger.debug(`getMe() - userId: ${userId}`);
|
||||
const account = await this.userRepository.findById(UserId.create(userId));
|
||||
if (!account) throw new ApplicationError('用户不存在');
|
||||
|
||||
this.logger.debug(`getMe() - account found: seq=${account.accountSequence.value}, nickname=${account.nickname}`);
|
||||
this.logger.debug(`getMe() - avatarUrl: ${account.avatarUrl ? `长度=${account.avatarUrl.length}` : 'null'}`);
|
||||
if (account.avatarUrl) {
|
||||
this.logger.debug(`getMe() - avatarUrl前100字符: ${account.avatarUrl.substring(0, 100)}`);
|
||||
}
|
||||
|
||||
const baseUrl = 'https://app.rwadurian.com'; // TODO: 从配置读取
|
||||
const referralLink = `${baseUrl}/invite/${account.referralCode.value}`;
|
||||
|
||||
|
|
|
|||
|
|
@ -143,6 +143,7 @@ class _ProfilePageState extends ConsumerState<ProfilePage> {
|
|||
|
||||
/// 加载用户数据
|
||||
Future<void> _loadUserData() async {
|
||||
debugPrint('[ProfilePage] _loadUserData() - 开始加载本地存储数据...');
|
||||
final accountService = ref.read(accountServiceProvider);
|
||||
|
||||
// 并行加载本地存储数据
|
||||
|
|
@ -162,6 +163,13 @@ class _ProfilePageState extends ConsumerState<ProfilePage> {
|
|||
final localAvatarPath = results[4] as String?;
|
||||
final referralCode = results[5] as String?;
|
||||
|
||||
debugPrint('[ProfilePage] _loadUserData() - 本地存储数据:');
|
||||
debugPrint('[ProfilePage] username: $username');
|
||||
debugPrint('[ProfilePage] serialNum: $serialNum');
|
||||
debugPrint('[ProfilePage] avatarSvg: ${avatarSvg != null ? "长度=${avatarSvg.length}" : "null"}');
|
||||
debugPrint('[ProfilePage] avatarUrl: $avatarUrl');
|
||||
debugPrint('[ProfilePage] localAvatarPath: $localAvatarPath');
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_nickname = username ?? '未设置昵称';
|
||||
|
|
@ -185,9 +193,16 @@ class _ProfilePageState extends ConsumerState<ProfilePage> {
|
|||
/// 从API加载完整用户信息
|
||||
Future<void> _loadMeData() async {
|
||||
try {
|
||||
debugPrint('[ProfilePage] _loadMeData() - 开始加载用户信息...');
|
||||
final accountService = ref.read(accountServiceProvider);
|
||||
final meData = await accountService.getMe();
|
||||
|
||||
debugPrint('[ProfilePage] _loadMeData() - API返回数据:');
|
||||
debugPrint('[ProfilePage] nickname: ${meData.nickname}');
|
||||
debugPrint('[ProfilePage] avatarUrl: ${meData.avatarUrl != null ? "长度=${meData.avatarUrl!.length}" : "null"}');
|
||||
debugPrint('[ProfilePage] avatarUrl内容前100字符: ${meData.avatarUrl?.substring(0, meData.avatarUrl!.length > 100 ? 100 : meData.avatarUrl!.length) ?? "null"}');
|
||||
debugPrint('[ProfilePage] 当前_avatarSvg: ${_avatarSvg != null ? "长度=${_avatarSvg!.length}" : "null"}');
|
||||
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_referrerSerial = meData.inviterSequence?.toString() ?? '无';
|
||||
|
|
@ -199,15 +214,24 @@ class _ProfilePageState extends ConsumerState<ProfilePage> {
|
|||
_referralCode = meData.referralCode;
|
||||
}
|
||||
// 如果本地没有头像但API返回了头像,更新显示
|
||||
debugPrint('[ProfilePage] _loadMeData() - 检查头像同步条件:');
|
||||
debugPrint('[ProfilePage] _avatarSvg == null: ${_avatarSvg == null}');
|
||||
debugPrint('[ProfilePage] meData.avatarUrl != null: ${meData.avatarUrl != null}');
|
||||
debugPrint('[ProfilePage] meData.avatarUrl.isNotEmpty: ${meData.avatarUrl?.isNotEmpty ?? false}');
|
||||
|
||||
if (_avatarSvg == null && meData.avatarUrl != null && meData.avatarUrl!.isNotEmpty) {
|
||||
debugPrint('[ProfilePage] _loadMeData() - 同步头像到本地!');
|
||||
_avatarSvg = meData.avatarUrl;
|
||||
// 同时保存到本地存储,避免下次还需要从API获取
|
||||
accountService.updateLocalAvatarSvg(meData.avatarUrl!);
|
||||
} else {
|
||||
debugPrint('[ProfilePage] _loadMeData() - 不需要同步头像 (条件不满足)');
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
} catch (e, stackTrace) {
|
||||
debugPrint('[ProfilePage] 加载用户信息失败: $e');
|
||||
debugPrint('[ProfilePage] 堆栈: $stackTrace');
|
||||
// 失败时保持现有数据,不影响页面显示
|
||||
}
|
||||
}
|
||||
|
|
@ -543,11 +567,17 @@ class _ProfilePageState extends ConsumerState<ProfilePage> {
|
|||
|
||||
/// 构建头像内容(优先本地文件,其次网络URL,最后SVG)
|
||||
Widget _buildAvatarContent() {
|
||||
debugPrint('[ProfilePage] _buildAvatarContent() - 开始构建头像');
|
||||
debugPrint('[ProfilePage] _localAvatarPath: $_localAvatarPath');
|
||||
debugPrint('[ProfilePage] _avatarUrl: $_avatarUrl');
|
||||
debugPrint('[ProfilePage] _avatarSvg: ${_avatarSvg != null ? "长度=${_avatarSvg!.length}" : "null"}');
|
||||
|
||||
// 1. 优先显示本地缓存的头像文件
|
||||
if (_localAvatarPath != null && _localAvatarPath!.isNotEmpty) {
|
||||
final file = File(_localAvatarPath!);
|
||||
// 同步检查文件是否存在
|
||||
if (file.existsSync()) {
|
||||
debugPrint('[ProfilePage] _buildAvatarContent() - 使用本地文件: $_localAvatarPath');
|
||||
return Image.file(
|
||||
file,
|
||||
width: 80,
|
||||
|
|
@ -555,10 +585,13 @@ class _ProfilePageState extends ConsumerState<ProfilePage> {
|
|||
fit: BoxFit.cover,
|
||||
gaplessPlayback: true, // 防止图片切换时闪烁
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
debugPrint('[ProfilePage] _buildAvatarContent() - 本地文件加载失败: $error');
|
||||
// 本地文件加载失败,尝试网络URL
|
||||
return _buildNetworkOrSvgAvatar();
|
||||
},
|
||||
);
|
||||
} else {
|
||||
debugPrint('[ProfilePage] _buildAvatarContent() - 本地文件不存在');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -568,8 +601,10 @@ class _ProfilePageState extends ConsumerState<ProfilePage> {
|
|||
|
||||
/// 构建网络URL或SVG头像
|
||||
Widget _buildNetworkOrSvgAvatar() {
|
||||
debugPrint('[ProfilePage] _buildNetworkOrSvgAvatar() - 尝试网络URL或SVG');
|
||||
// 尝试显示网络图片URL
|
||||
if (_avatarUrl != null && _avatarUrl!.isNotEmpty) {
|
||||
debugPrint('[ProfilePage] _buildNetworkOrSvgAvatar() - 使用网络URL: $_avatarUrl');
|
||||
return Image.network(
|
||||
_avatarUrl!,
|
||||
width: 80,
|
||||
|
|
@ -589,6 +624,7 @@ class _ProfilePageState extends ConsumerState<ProfilePage> {
|
|||
);
|
||||
},
|
||||
errorBuilder: (context, error, stackTrace) {
|
||||
debugPrint('[ProfilePage] _buildNetworkOrSvgAvatar() - 网络图片加载失败: $error');
|
||||
// 加载失败时显示SVG或默认头像
|
||||
return _buildSvgOrDefaultAvatar();
|
||||
},
|
||||
|
|
@ -596,12 +632,15 @@ class _ProfilePageState extends ConsumerState<ProfilePage> {
|
|||
}
|
||||
|
||||
// 显示SVG或默认头像
|
||||
debugPrint('[ProfilePage] _buildNetworkOrSvgAvatar() - 无网络URL,尝试SVG');
|
||||
return _buildSvgOrDefaultAvatar();
|
||||
}
|
||||
|
||||
/// 构建SVG或默认头像
|
||||
Widget _buildSvgOrDefaultAvatar() {
|
||||
if (_avatarSvg != null) {
|
||||
debugPrint('[ProfilePage] _buildSvgOrDefaultAvatar() - 使用SVG头像,长度=${_avatarSvg!.length}');
|
||||
debugPrint('[ProfilePage] SVG前50字符: ${_avatarSvg!.substring(0, _avatarSvg!.length > 50 ? 50 : _avatarSvg!.length)}');
|
||||
return SvgPicture.string(
|
||||
_avatarSvg!,
|
||||
width: 80,
|
||||
|
|
@ -609,6 +648,7 @@ class _ProfilePageState extends ConsumerState<ProfilePage> {
|
|||
fit: BoxFit.cover,
|
||||
);
|
||||
}
|
||||
debugPrint('[ProfilePage] _buildSvgOrDefaultAvatar() - 无SVG,使用默认图标');
|
||||
return const Icon(
|
||||
Icons.person,
|
||||
size: 40,
|
||||
|
|
|
|||
Loading…
Reference in New Issue