// Author: fruit.com.cn import 'json_numbers.dart'; /// 金币钱包与流水:对应 `GET /app/wallet` 的 `wallet` / `ledger`。 class CoinWallet { const CoinWallet({ required this.coinBalance, required this.totalRechargedCoin, required this.totalConsumedCoin, required this.totalRefundedCoin, }); final int coinBalance; final int totalRechargedCoin; final int totalConsumedCoin; final int totalRefundedCoin; factory CoinWallet.fromJson(Map json) { return CoinWallet( coinBalance: jsonToInt(json['coinBalance']), totalRechargedCoin: jsonToInt(json['totalRechargedCoin']), totalConsumedCoin: jsonToInt(json['totalConsumedCoin']), totalRefundedCoin: jsonToInt(json['totalRefundedCoin']), ); } } /// 一笔金币变动;[changeAmount] 正数入账、负数出账。 class CoinLedgerEntry { const CoinLedgerEntry({ required this.id, required this.changeAmount, required this.balanceAfter, required this.bizType, required this.remark, required this.createdAt, }); final String id; final int changeAmount; final int balanceAfter; final String bizType; final String remark; final DateTime? createdAt; factory CoinLedgerEntry.fromJson(Map json) { return CoinLedgerEntry( id: (json['id'] ?? '').toString(), changeAmount: jsonToInt(json['changeAmount']), balanceAfter: jsonToInt(json['balanceAfter']), bizType: json['bizType'] as String? ?? '', remark: json['remark'] as String? ?? '', createdAt: DateTime.tryParse(json['createdAt'] as String? ?? ''), ); } } /// 钱包页一次渲染所需的余额与最近流水。 class WalletSnapshot { const WalletSnapshot({required this.wallet, this.ledger = const []}); final CoinWallet wallet; final List ledger; factory WalletSnapshot.fromJson(Map json) { final wallet = json['wallet']; return WalletSnapshot( wallet: wallet is Map ? CoinWallet.fromJson(wallet) : const CoinWallet( coinBalance: 0, totalRechargedCoin: 0, totalConsumedCoin: 0, totalRefundedCoin: 0, ), ledger: (json['ledger'] as List? ?? const []) .whereType>() .map(CoinLedgerEntry.fromJson) .toList(), ); } } /// 金币解锁结果:`POST /app/episodes/{id}/unlock`。 class UnlockResult { const UnlockResult({ required this.unlocked, required this.accessType, required this.coinCost, required this.coinBalance, }); final bool unlocked; final String accessType; final int coinCost; final int coinBalance; factory UnlockResult.fromJson(Map json) { return UnlockResult( unlocked: json['unlocked'] == true, accessType: json['accessType'] as String? ?? '', coinCost: jsonToInt(json['coinCost']), coinBalance: jsonToInt(json['coinBalance']), ); } }