From 8d94d9e4bb43dfb6f5a202021ac2e13b884c45cc Mon Sep 17 00:00:00 2001 From: hailin Date: Tue, 9 Dec 2025 18:19:13 -0800 Subject: [PATCH] fix(mobile): sync avatar from API when local storage is empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When recovering an account via mnemonic, the avatar SVG might not be saved to local storage. Now the profile page checks if avatarUrl is returned from the /me API and updates both the display and local storage. - Add updateLocalAvatarSvg() method to AccountService - Update _loadMeData() to sync avatar from API response 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../lib/core/services/account_service.dart | 8 ++++++++ .../profile/presentation/pages/profile_page.dart | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/frontend/mobile-app/lib/core/services/account_service.dart b/frontend/mobile-app/lib/core/services/account_service.dart index beb4ea7f..3c88f568 100644 --- a/frontend/mobile-app/lib/core/services/account_service.dart +++ b/frontend/mobile-app/lib/core/services/account_service.dart @@ -689,6 +689,14 @@ class AccountService { return result; } + /// 更新本地存储的头像 SVG + /// 当从 API 获取到头像后,同步保存到本地存储 + Future updateLocalAvatarSvg(String avatarSvg) async { + debugPrint('$_tag updateLocalAvatarSvg() - 更新本地头像 SVG (长度: ${avatarSvg.length})'); + await _secureStorage.write(key: StorageKeys.avatarSvg, value: avatarSvg); + debugPrint('$_tag updateLocalAvatarSvg() - 保存成功'); + } + /// 获取推荐码 Future getReferralCode() async { debugPrint('$_tag getReferralCode() - 获取推荐码'); diff --git a/frontend/mobile-app/lib/features/profile/presentation/pages/profile_page.dart b/frontend/mobile-app/lib/features/profile/presentation/pages/profile_page.dart index dfb7633c..39ff49f3 100644 --- a/frontend/mobile-app/lib/features/profile/presentation/pages/profile_page.dart +++ b/frontend/mobile-app/lib/features/profile/presentation/pages/profile_page.dart @@ -198,6 +198,12 @@ class _ProfilePageState extends ConsumerState { if (meData.referralCode.isNotEmpty) { _referralCode = meData.referralCode; } + // 如果本地没有头像但API返回了头像,更新显示 + if (_avatarSvg == null && meData.avatarUrl != null && meData.avatarUrl!.isNotEmpty) { + _avatarSvg = meData.avatarUrl; + // 同时保存到本地存储,避免下次还需要从API获取 + accountService.updateLocalAvatarSvg(meData.avatarUrl!); + } }); } } catch (e) { @@ -209,6 +215,7 @@ class _ProfilePageState extends ConsumerState { /// 加载推荐数据 (from referral-service) Future _loadReferralData() async { try { + debugPrint('[ProfilePage] 开始加载推荐数据...'); final referralService = ref.read(referralServiceProvider); // 并行加载推荐信息和直推列表 @@ -220,6 +227,8 @@ class _ProfilePageState extends ConsumerState { final referralInfo = results[0] as ReferralInfoResponse; final directReferrals = results[1] as DirectReferralsResponse; + debugPrint('[ProfilePage] 推荐数据加载成功: directReferralCount=${referralInfo.directReferralCount}, totalTeamCount=${referralInfo.totalTeamCount}'); + if (mounted) { setState(() { _directReferralCount = referralInfo.directReferralCount; @@ -237,8 +246,9 @@ class _ProfilePageState extends ConsumerState { }).toList(); }); } - } catch (e) { + } catch (e, stackTrace) { debugPrint('[ProfilePage] 加载推荐数据失败: $e'); + debugPrint('[ProfilePage] 堆栈: $stackTrace'); // 失败时保持默认数据 } }