Files
pangolin/client/lib/services/account_api.dart
T
wangjia f2499a64e1
ci-pangolin / Lint — shellcheck (push) Successful in 7s
ci-pangolin / OpenAPI Sync Check (push) Successful in 16s
ci-pangolin / Redline Scan — 脱敏 (UI 文案) (push) Successful in 6s
ci-pangolin / Flutter — analyze + test (push) Successful in 24s
ci-pangolin / Portable SQL — 可移植性 (mysql/sqlite) (push) Successful in 4s
ci-pangolin / Codegen Drift — token 生成物未漂移 (push) Successful in 5s
ci-pangolin / Go — build + test (push) Successful in 10s
ci-pangolin / E2E Smoke — L4 进程级端到端 (push) Successful in 14s
ci-pangolin / Go — integration (mysql/redis testcontainers) (push) Failing after 4m8s
ci-pangolin / Golden — 视觉回归 (components + auth) (push) Successful in 14s
fix(usage): 统计按客户端本地日聚合 —— 修「29号却显示28号数据」时区 bug
根因:服务端用量全按 UTC 预切天,UTC+8 用户在中国 0-8 点时 UTC 还是前一天,
「今天」就显示前一天数据。

改法(用户拍板:小时桶+查询带时区偏移):
- migration 000017 `usage_hourly`(UTC 纪元小时整数桶,稀疏存储,空小时不落行)。
- ReportUsage 同时累加 usage_hourly(按 windowEnd 的 UTC 小时);usage_daily 保留作配额/今日。
- `UsageCurve(days, offsetMin)`:读 UTC 小时桶,按客户端时区偏移**重新聚合成本地日**曲线。
- `/v1/usage?tz_offset=分钟`;客户端 `usage()` 带 `DateTime.now().timeZoneOffset.inMinutes`。
- 客户端早已「取曲线最后一点当今日」,故只改服务端聚合 + 带偏移即可。

测试:store 时区分桶证明(UTC 18:00 在 UTC vs UTC+8 落不同本地日,锁住 bug 修复)+
migration v17 + usage_hourly 表;集成测试改喂 hourly;全量 go/flutter test 绿。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-29 07:02:04 +08:00

92 lines
3.6 KiB
Dart

// account_api.dart — 账户域端点封装(受 JWT 保护,走 ApiClient)。
//
// 覆盖:/v1/me、/v1/plans、/v1/me/devices(列表+删除)、/v1/usage、
// /v1/redeem、/v1/ads/unlock。错误统一 AuthApiException(由 ApiClient 抛)。
import '../models/device.dart';
import '../models/device_usage.dart';
import '../models/me.dart';
import '../models/plan.dart';
import '../models/usage_point.dart';
import 'api_client.dart';
/// 兑换结果(POST /v1/redeem)。
class RedeemResult {
const RedeemResult({
required this.plan,
required this.durationDays,
this.idempotent = false,
this.expiresAt,
});
final String plan;
final int durationDays;
final bool idempotent;
final DateTime? expiresAt;
factory RedeemResult.fromJson(Map<String, dynamic> m) {
final exp = m['expires_at'] as String?;
return RedeemResult(
plan: m['plan'] as String? ?? '',
durationDays: (m['duration_days'] as num?)?.toInt() ?? 0,
idempotent: m['idempotent'] as bool? ?? false,
expiresAt: (exp != null && exp.isNotEmpty) ? DateTime.tryParse(exp) : null,
);
}
}
class AccountApi {
AccountApi(this._c);
final ApiClient _c;
/// GET /v1/me — 账户聚合视图。
Future<Me> me() async => Me.fromJson(await _c.getJson('/v1/me'));
/// GET /v1/plans — 套餐定义。
Future<List<Plan>> plans() async {
final body = await _c.getJson('/v1/plans');
final raw = (body['plans'] as List<dynamic>?) ?? const [];
return raw.map((e) => Plan.fromJson(e as Map<String, dynamic>)).toList();
}
/// GET /v1/me/devices — 已登录设备列表。
Future<List<Device>> devices() async {
final body = await _c.getJson('/v1/me/devices');
final raw = (body['devices'] as List<dynamic>?) ?? const [];
return raw.map((e) => Device.fromJson(e as Map<String, dynamic>)).toList();
}
/// DELETE /v1/me/devices/{uuid} — 清除登录信息(移除设备 + 吊销会话/凭证)。
Future<void> removeDevice(String uuid) => _c.delete('/v1/me/devices/$uuid');
/// POST /v1/me/devices/{uuid}/logout — 强制退出(吊销该设备会话,设备留列表)。
Future<void> forceLogout(String uuid) async {
await _c.postJson('/v1/me/devices/$uuid/logout');
}
/// GET /v1/usage?days=N&tz_offset=M — 最近 N 天用量(默认 7,后端范围 [1,90])。
/// 带本机时区偏移(分钟,如 UTC+8=480),服务端按「本地日」聚合 → 「今天」与本地日历一致
/// (修「29 号却显示 28 号数据」的时区 bug)。
Future<List<UsagePoint>> usage({int days = 7}) async {
final tz = DateTime.now().timeZoneOffset.inMinutes;
final body = await _c.getJson('/v1/usage?days=$days&tz_offset=$tz');
final raw = (body['points'] as List<dynamic>?) ?? const [];
return raw.map((e) => UsagePoint.fromJson(e as Map<String, dynamic>)).toList();
}
/// GET /v1/usage/devices?days=N — 最近 N 天按设备用量(默认 7,范围 [1,90])。
Future<List<DeviceUsage>> deviceUsage({int days = 7}) async {
final body = await _c.getJson('/v1/usage/devices?days=$days');
final raw = (body['devices'] as List<dynamic>?) ?? const [];
return raw.map((e) => DeviceUsage.fromJson(e as Map<String, dynamic>)).toList();
}
/// POST /v1/redeem — 兑换码。
Future<RedeemResult> redeem(String code) async =>
RedeemResult.fromJson(await _c.postJson('/v1/redeem', {'code': code}));
/// POST /v1/ads/unlock — 看广告解锁今日免费额度。
Future<void> adUnlock({required String deviceId, required String adToken}) =>
_c.postJson('/v1/ads/unlock', {'device_id': deviceId, 'ad_token': adToken});
}