27 lines
928 B
Dart
27 lines
928 B
Dart
/// 解析用户积分余额(`credit` / `credits`)。
|
|
///
|
|
/// 换皮映射下余额多为 `credit`(文档 wire `export` → `credit`)。
|
|
/// `credits`/`padding` 在部分接口里可能是 int 余额,也可能是嵌套的积分配置
|
|
/// `Map`(如 fast_login 里 `padding: {}`),不能把 Map 当数字解析。
|
|
int? parseUserCreditsBalance(Map<String, dynamic> json) {
|
|
int? toInt(dynamic value) {
|
|
if (value == null) return null;
|
|
if (value is int) return value;
|
|
if (value is num) return value.toInt();
|
|
if (value is String && value.isNotEmpty) {
|
|
final i = int.tryParse(value);
|
|
if (i != null) return i;
|
|
final d = double.tryParse(value);
|
|
if (d != null) return d.round();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
final credit = toInt(json['credit']);
|
|
if (credit != null) return credit;
|
|
|
|
final credits = json['credits'];
|
|
if (credits is Map) return null;
|
|
return toInt(credits);
|
|
}
|