bb39b36d84
- account_providers:新增 meProvider(GET /v1/me,登录态变化自动重取)、 plansProvider、usageProvider(family by days)。 - isFreePlanProvider 改为派生自 meProvider.plan(去掉可写演示开关)。 - quotaProvider:总额取 plans free.daily_minutes,今日剩余取 me.quota_today_min (后端已算好);adUnlocked 保留本地态(ad SDK 未接)。 - account_page:邮箱/套餐/到期接 me;删「演示:免费版视角」开关 + kDemoEmail; 协议标签 WireGuard→REALITY/Hysteria2。 - 重写 quota_controller_test 为 provider 驱动;account/quota golden 重生成。 flutter analyze 0 error;113 tests passed。统计页(weekly/月流量/延迟)随 P4 一并接(其均延迟依赖 P4 实测探针)。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
61 lines
2.3 KiB
Dart
61 lines
2.3 KiB
Dart
// account_providers.dart — 账户域 Riverpod 装配。
|
|
//
|
|
// 此处只做依赖装配:把 authProvider 的令牌/刷新接到统一 ApiClient,
|
|
// 再产出 AccountApi。具体数据 provider(meProvider/devicesProvider/usage…)
|
|
// 在后续阶段加入本文件。
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../models/me.dart';
|
|
import '../models/plan.dart';
|
|
import '../models/usage_point.dart';
|
|
import '../services/account_api.dart';
|
|
import '../services/api_client.dart';
|
|
import '../services/api_config.dart';
|
|
import 'auth_provider.dart';
|
|
|
|
/// 统一鉴权 HTTP 客户端:令牌取自 authProvider,401 时触发 AuthNotifier.refresh。
|
|
final apiClientProvider = Provider<ApiClient>((ref) {
|
|
return ApiClient(
|
|
baseUrl: kApiBaseUrl,
|
|
getToken: () => ref.read(authProvider).accessToken,
|
|
refresh: () => ref.read(authProvider.notifier).refresh(),
|
|
);
|
|
});
|
|
|
|
/// 账户域端点封装。
|
|
final accountApiProvider = Provider<AccountApi>(
|
|
(ref) => AccountApi(ref.watch(apiClientProvider)),
|
|
);
|
|
|
|
// ── 数据 provider ───────────────────────────────────────────────────────────
|
|
|
|
/// 当前账户聚合视图(GET /v1/me)。登录态变化自动重取;未登录返回默认免费档。
|
|
class MeNotifier extends AsyncNotifier<Me> {
|
|
@override
|
|
Future<Me> build() async {
|
|
final auth = ref.watch(authProvider);
|
|
if (!auth.isLoggedIn) return const Me(email: '', plan: 'free');
|
|
return ref.read(accountApiProvider).me();
|
|
}
|
|
|
|
/// 手动刷新(如连接/兑换后)。
|
|
Future<void> refresh() async {
|
|
state = const AsyncLoading();
|
|
state = await AsyncValue.guard(() => ref.read(accountApiProvider).me());
|
|
}
|
|
}
|
|
|
|
final meProvider = AsyncNotifierProvider<MeNotifier, Me>(MeNotifier.new);
|
|
|
|
/// 套餐定义(GET /v1/plans)。未登录返回空。
|
|
final plansProvider = FutureProvider<List<Plan>>((ref) async {
|
|
if (!ref.watch(authProvider).isLoggedIn) return const [];
|
|
return ref.read(accountApiProvider).plans();
|
|
});
|
|
|
|
/// 最近 N 天用量(GET /v1/usage?days=N)。未登录返回空。
|
|
final usageProvider = FutureProvider.family<List<UsagePoint>, int>((ref, days) async {
|
|
if (!ref.watch(authProvider).isLoggedIn) return const [];
|
|
return ref.read(accountApiProvider).usage(days: days);
|
|
});
|