import '../core/network/api_client.dart'; import '../core/network/endpoints.dart'; /// 钱包信息(《03-接口文档》4.4) class WalletInfo { const WalletInfo({ this.points = 0, this.totalRecharge = 0, this.totalEarned = 0, this.totalConsumed = 0, this.rewardProgress = const CommentRewardProgress(), }); final int points; final int totalRecharge; final int totalEarned; final int totalConsumed; final CommentRewardProgress rewardProgress; factory WalletInfo.fromJson(Map json) => WalletInfo( points: (json['points'] as num?)?.toInt() ?? 0, totalRecharge: (json['total_recharge'] as num?)?.toInt() ?? 0, totalEarned: (json['total_earned'] as num?)?.toInt() ?? 0, totalConsumed: (json['total_consumed'] as num?)?.toInt() ?? 0, rewardProgress: CommentRewardProgress.fromJson( (json['comment_reward_progress'] as Map?) ?? const {}, ), ); } /// 评论奖励进度 class CommentRewardProgress { const CommentRewardProgress({ this.current = 0, this.target = 10, this.todayRewarded = 0, this.dailyLimit = 5, }); final int current; final int target; final int todayRewarded; final int dailyLimit; factory CommentRewardProgress.fromJson(Map json) => CommentRewardProgress( current: (json['current'] as num?)?.toInt() ?? 0, target: (json['target'] as num?)?.toInt() ?? 10, todayRewarded: (json['today_rewarded'] as num?)?.toInt() ?? 0, dailyLimit: (json['daily_limit'] as num?)?.toInt() ?? 5, ); } /// 积分明细记录(《03-接口文档》4.5) class PointsRecord { const PointsRecord({ required this.id, required this.changeAmount, required this.balanceAfter, required this.bizType, this.remark = '', this.createdAt = '', }); final int id; /// 正=获得,负=消耗 final int changeAmount; final int balanceAfter; /// 1充值 2观看 3评论奖励 4会员 5赠送 6调整 7扣回 final int bizType; final String remark; final String createdAt; static const Map bizTypeLabels = { 1: '充值', 2: '观看', 3: '评论奖励', 4: '会员', 5: '赠送', 6: '调整', 7: '扣回', }; String get bizTypeLabel => bizTypeLabels[bizType] ?? '其他'; bool get isExpense => changeAmount < 0; factory PointsRecord.fromJson(Map json) => PointsRecord( id: (json['id'] as num).toInt(), changeAmount: (json['change_amount'] as num).toInt(), balanceAfter: (json['balance_after'] as num).toInt(), bizType: (json['biz_type'] as num).toInt(), remark: (json['remark'] as String?) ?? '', createdAt: (json['created_at'] as String?) ?? '', ); } /// 积分明细分页 class PointsRecordsPage { const PointsRecordsPage({required this.total, required this.items}); final int total; final List items; factory PointsRecordsPage.fromJson(Map json) => PointsRecordsPage( total: (json['total'] as num?)?.toInt() ?? 0, items: ((json['list'] as List?) ?? const []) .whereType>() .map(PointsRecord.fromJson) .toList(), ); } /// 钱包服务(《03-接口文档》4.4/4.5) class WalletService { WalletService._(); /// 4.4 钱包信息(需登录) static Future fetchWallet() async { final data = await ApiClient.get>(Endpoints.meWallet); return WalletInfo.fromJson(data); } /// 4.5 积分明细(需登录) static Future fetchPointsRecords({ int page = 1, int pageSize = 20, int? bizType, }) async { final query = {'page': page, 'page_size': pageSize}; if (bizType != null) query['biz_type'] = bizType; final data = await ApiClient.get>( Endpoints.mePointsRecords, queryParameters: query, ); return PointsRecordsPage.fromJson(data); } }