petsHero-AI/lib/core/user/account_refresh.dart

31 lines
1.1 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import '../api/api_config.dart';
import '../api/services/user_api.dart';
import '../auth/auth_service.dart';
import 'user_state.dart';
/// 刷新用户账户信息并更新 UserState
///
/// [updateProfile] 为 true 时,同时更新 avatar 和 userName用于 Profile 页)
Future<void> refreshAccount({bool updateProfile = false}) async {
final uid = UserState.userId.value;
if (uid == null || uid.isEmpty) return;
try {
await AuthService.loginComplete;
final res = await UserApi.getAccount(
sentinel: ApiConfig.appId,
asset: uid,
);
if (!res.isSuccess || res.data == null) return;
final data = res.data as Map<String, dynamic>?;
final credits = data?['reveal'] as int?;
if (credits != null) UserState.setCredits(credits);
if (updateProfile) {
final avatarUrl = data?['realm'] as String?;
UserState.setAvatar(
avatarUrl != null && avatarUrl.isNotEmpty ? avatarUrl : null);
final name = data?['terminal'] as String?;
UserState.setUserName(name != null && name.isNotEmpty ? name : null);
}
} catch (_) {}
}