315c343ed5
ci-pangolin / Lint — shellcheck (push) Successful in 7s
ci-pangolin / OpenAPI Sync Check (push) Successful in 17s
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Successful in 6s
ci-pangolin / Flutter — analyze + test (push) Failing after 14s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (push) Successful in 5s
ci-pangolin / Codegen Drift — token 生成物未漂移 (push) Successful in 6s
ci-pangolin / Go — build + test (push) Successful in 11s
ci-pangolin / E2E Smoke — L4 进程级端到端 (push) Successful in 14s
ci-pangolin / Go — integration (mysql/redis testcontainers) (push) Failing after 4m3s
ci-pangolin / Golden — 视觉回归 (components + auth) (push) Successful in 14s
① 默认名:DeviceIdentity 改「平台简写(Win/Mac/Linux/iOS/Andr)·主机名」,主机名截
到 8、总长≈12(替代裸主机名,更短)。
② 重命名:新端点 POST /v1/me/devices/{uuid}/rename(Service.RenameDevice 校验归属+
截 64);store.UpdateName;客户端 account_api.renameDevice + provider.rename +
「我的设备」⋯ 菜单加「重命名」+ 输入弹窗;l10n(zh/en)。
③ 版本 1.0.13。
测试:server RenameDevice(归属/空名/404)+ 客户端 rename widget;全量 go/flutter 绿。
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
109 lines
4.2 KiB
Dart
109 lines
4.2 KiB
Dart
// account_providers.dart — 账户域 Riverpod 装配。
|
|
//
|
|
// 此处只做依赖装配:把 authProvider 的令牌/刷新接到统一 ApiClient,
|
|
// 再产出 AccountApi。具体数据 provider(meProvider/devicesProvider/usage…)
|
|
// 在后续阶段加入本文件。
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import '../models/device.dart';
|
|
import '../models/device_usage.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)。未登录返回空。
|
|
/// autoDispose:离开统计页即销毁,重进自动重取(配合下拉刷新/连接态变化 invalidate),
|
|
/// 根治「本月流量取一次就冻住」。后端每 60s 才更新,无需更勤刷新。
|
|
/// 用量曲线 key:days + 可选设备 uuid(null = 全部/账户级)。
|
|
typedef UsageKey = ({int days, String? device});
|
|
|
|
final usageProvider = FutureProvider.autoDispose.family<List<UsagePoint>, UsageKey>((ref, key) async {
|
|
if (!ref.watch(authProvider).isLoggedIn) return const [];
|
|
return ref.read(accountApiProvider).usage(days: key.days, device: key.device);
|
|
});
|
|
|
|
/// 最近 N 天按设备用量(GET /v1/usage/devices?days=N)。统计页设备下拉/明细用。
|
|
/// 未登录返回空。autoDispose 同 usageProvider。
|
|
final deviceUsageProvider =
|
|
FutureProvider.autoDispose.family<List<DeviceUsage>, int>((ref, days) async {
|
|
if (!ref.watch(authProvider).isLoggedIn) return const [];
|
|
return ref.read(accountApiProvider).deviceUsage(days: days);
|
|
});
|
|
|
|
/// 已登录设备列表(GET /v1/me/devices)。
|
|
class DevicesNotifier extends AsyncNotifier<List<Device>> {
|
|
@override
|
|
Future<List<Device>> build() async {
|
|
if (!ref.watch(authProvider).isLoggedIn) return const [];
|
|
return ref.read(accountApiProvider).devices();
|
|
}
|
|
|
|
/// 清除登录信息(DELETE:移除设备 + 吊销会话/凭证)后刷新列表。
|
|
Future<void> remove(String uuid) async {
|
|
await ref.read(accountApiProvider).removeDevice(uuid);
|
|
ref.invalidateSelf();
|
|
await future;
|
|
}
|
|
|
|
/// 强制退出(吊销该设备会话,设备保留)后刷新列表。
|
|
Future<void> forceLogout(String uuid) async {
|
|
await ref.read(accountApiProvider).forceLogout(uuid);
|
|
ref.invalidateSelf();
|
|
await future;
|
|
}
|
|
|
|
/// 重命名设备后刷新列表。
|
|
Future<void> rename(String uuid, String name) async {
|
|
await ref.read(accountApiProvider).renameDevice(uuid, name);
|
|
ref.invalidateSelf();
|
|
await future;
|
|
}
|
|
}
|
|
|
|
final devicesProvider =
|
|
AsyncNotifierProvider<DevicesNotifier, List<Device>>(DevicesNotifier.new);
|