// 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/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 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() async => Me.fromJson(await _c.getJson('/v1/me')); /// GET /v1/plans — 套餐定义。 Future> plans() async { final body = await _c.getJson('/v1/plans'); final raw = (body['plans'] as List?) ?? const []; return raw.map((e) => Plan.fromJson(e as Map)).toList(); } /// GET /v1/me/devices — 已登录设备列表。 Future> devices() async { final body = await _c.getJson('/v1/me/devices'); final raw = (body['devices'] as List?) ?? const []; return raw.map((e) => Device.fromJson(e as Map)).toList(); } /// DELETE /v1/me/devices/{uuid} — 移除设备。 Future removeDevice(String uuid) => _c.delete('/v1/me/devices/$uuid'); /// GET /v1/usage?days=N — 最近 N 天用量(默认 7,后端范围 [1,90])。 Future> usage({int days = 7}) async { final body = await _c.getJson('/v1/usage?days=$days'); final raw = (body['points'] as List?) ?? const []; return raw.map((e) => UsagePoint.fromJson(e as Map)).toList(); } /// POST /v1/redeem — 兑换码。 Future redeem(String code) async => RedeemResult.fromJson(await _c.postJson('/v1/redeem', {'code': code})); /// POST /v1/ads/unlock — 看广告解锁今日免费额度。 Future adUnlock({required String deviceId, required String adToken}) => _c.postJson('/v1/ads/unlock', {'device_id': deviceId, 'ad_token': adToken}); }