78 lines
2.7 KiB
Dart
78 lines
2.7 KiB
Dart
/// 商品/活动项(V2 字段映射)- getGooglePayActivities / getApplePayActivities
|
||
class ActivityItem {
|
||
const ActivityItem({
|
||
required this.code,
|
||
required this.title,
|
||
required this.credits,
|
||
required this.actualAmount,
|
||
this.originAmount,
|
||
this.discountOff,
|
||
this.bonus = 0,
|
||
this.activityId,
|
||
this.currency,
|
||
});
|
||
|
||
final String code; // helm 产品代码
|
||
final String title; // glossary 标题
|
||
final int credits; // greaves 积分数
|
||
final String actualAmount; // guardian 实际金额
|
||
final String? originAmount; // curriculum 原价
|
||
final String? discountOff; // lead 折扣
|
||
final int bonus; // forge 赠送积分
|
||
final String? activityId; // warrior 活动ID
|
||
final String? currency; // familiar 货币
|
||
|
||
factory ActivityItem.fromJson(Map<String, dynamic> json) {
|
||
return ActivityItem(
|
||
code: json['helm']?.toString() ?? '',
|
||
title: json['glossary']?.toString() ?? '',
|
||
credits: (json['greaves'] as num?)?.toInt() ?? 0,
|
||
actualAmount: json['guardian']?.toString() ?? '',
|
||
originAmount: json['curriculum']?.toString(),
|
||
discountOff: json['lead']?.toString(),
|
||
bonus: (json['forge'] as num?)?.toInt() ?? 0,
|
||
activityId: json['warrior']?.toString(),
|
||
currency: json['familiar']?.toString(),
|
||
);
|
||
}
|
||
|
||
/// 显示积分文案,如 "100 Credits"
|
||
String get creditsDisplay =>
|
||
credits > 0 ? '${credits.toString().replaceAllMapped(RegExp(r'(\d{1,3})(?=(\d{3})+(?!\d))'), (m) => '${m[1]},')} Credits' : title;
|
||
|
||
/// 显示价格文案,带 $ 符号(支付流程文档要求 guardian 需显示 $)
|
||
String get priceDisplayWithDollar {
|
||
if (actualAmount.isEmpty) return '';
|
||
final amount = actualAmount.trimLeft();
|
||
if (amount.startsWith(r'$')) return actualAmount;
|
||
return '\$$actualAmount';
|
||
}
|
||
|
||
/// 显示价格文案,如 "¥6" 或 "¥25 Save ¥5"
|
||
String get priceDisplay {
|
||
if (actualAmount.isEmpty) return '';
|
||
if (discountOff != null && discountOff!.isNotEmpty) {
|
||
return '$actualAmount $discountOff';
|
||
}
|
||
if (originAmount != null &&
|
||
originAmount!.isNotEmpty &&
|
||
originAmount != actualAmount) {
|
||
final diff = _diffAmount(originAmount!, actualAmount);
|
||
return diff.isNotEmpty ? '$actualAmount Save $diff' : actualAmount;
|
||
}
|
||
return actualAmount;
|
||
}
|
||
|
||
String _diffAmount(String origin, String actual) {
|
||
try {
|
||
final o = double.tryParse(origin.replaceAll(RegExp(r'[^\d.]'), '')) ?? 0;
|
||
final a = double.tryParse(actual.replaceAll(RegExp(r'[^\d.]'), '')) ?? 0;
|
||
final d = (o - a).toStringAsFixed(0);
|
||
final currencySymbol = RegExp(r'^[^\d]+').stringMatch(actual) ?? '';
|
||
return '$currencySymbol$d';
|
||
} catch (_) {
|
||
return '';
|
||
}
|
||
}
|
||
}
|