144 lines
4.6 KiB
Dart
144 lines
4.6 KiB
Dart
import 'package:pj_supermarket_cloud/common/index.dart';
|
|
|
|
abstract class JsonTool {
|
|
static bool stringToBoolean(String? value) {
|
|
if (value == null) return false;
|
|
return value == '1';
|
|
}
|
|
|
|
static String? booleanToString(bool? value) {
|
|
if (value == null) return null;
|
|
return value ? '1' : '0';
|
|
}
|
|
|
|
static int? stringToInt(String? value) {
|
|
if (value == null) return null;
|
|
return int.tryParse(value);
|
|
}
|
|
|
|
/// 兼容后端 Long 以字符串或数字返回的单值场景。
|
|
static int anyToInt(Object? value) {
|
|
if (value is int) return value;
|
|
if (value is num) return value.toInt();
|
|
final parsed = int.tryParse(value.toString());
|
|
if (parsed == null) throw const FormatException('数字格式错误');
|
|
return parsed;
|
|
}
|
|
|
|
/// 兼容可选 Long 以字符串、数字或空值返回的场景,空值保持为空。
|
|
static int? anyToNullableInt(Object? value) {
|
|
if (value == null) return null;
|
|
return anyToInt(value);
|
|
}
|
|
|
|
/// 兼容后端 BigDecimal 以字符串或数字返回的可选小数。
|
|
static double? anyToNullableDouble(Object? value) {
|
|
if (value == null) return null;
|
|
if (value is num) return value.toDouble();
|
|
return double.tryParse(value.toString());
|
|
}
|
|
|
|
static String? intToString(int? value) {
|
|
if (value == null) return null;
|
|
return value.toString();
|
|
}
|
|
|
|
static String? anyToString(dynamic value) {
|
|
if (value == null) return null;
|
|
return value.toString();
|
|
}
|
|
|
|
/// 将后端必填 Long 或字符串标识转换为非空字符串;缺失或空值时在模型边界直接拒绝。
|
|
static String anyToRequiredString(Object? value) {
|
|
final result = anyToString(value)?.trim();
|
|
if (result == null || result.isEmpty) {
|
|
throw const FormatException('必填标识不能为空');
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// 后端为避免 19 位 Long 在部分客户端丢失精度,可能把 ID 序列化为字符串。
|
|
/// 这里同时兼容字符串和数字,忽略无法安全转换的异常项。
|
|
static List<int> anyListToIntList(Object? value) {
|
|
if (value is! List) return const [];
|
|
return value
|
|
.map((item) {
|
|
if (item is int) return item;
|
|
if (item is num) return item.toInt();
|
|
return int.tryParse(item.toString());
|
|
})
|
|
.whereType<int>()
|
|
.toList(growable: false);
|
|
}
|
|
|
|
static Map<String, dynamic>? anyToMap(dynamic value) {
|
|
if (value == null) return null;
|
|
return dynamic as Map<String, dynamic>?;
|
|
}
|
|
|
|
static OrderType? stringToOrderType(String? value) {
|
|
final byCode = OrderType.values.fromCode(value);
|
|
if (byCode != null) return byCode;
|
|
if (value == null) return null;
|
|
final name = value.contains('.') ? value.split('.').last : value;
|
|
return OrderType.values.byName(name);
|
|
}
|
|
|
|
static String? orderTypeToString(OrderType? value) {
|
|
if (value == null) return null;
|
|
return value.code;
|
|
}
|
|
|
|
static OrderPayType? stringToOrderPayType(String? value) {
|
|
final byCode = OrderPayType.values.fromCode(value);
|
|
if (byCode != null) return byCode;
|
|
if (value == null) return null;
|
|
final name = value.contains('.') ? value.split('.').last : value;
|
|
return OrderPayType.values.byName(name);
|
|
}
|
|
|
|
static String? orderPayTypeToString(OrderPayType? value) {
|
|
if (value == null) return null;
|
|
return value.code;
|
|
}
|
|
|
|
static WalletActType? stringToWalletActType(String? value) {
|
|
final byCode = WalletActType.values.fromCode(value);
|
|
if (byCode != null) return byCode;
|
|
if (value == null) return null;
|
|
final name = value.contains('.') ? value.split('.').last : value;
|
|
return WalletActType.values.byName(name);
|
|
}
|
|
|
|
static String? walletActTypeToString(WalletActType? value) {
|
|
if (value == null) return null;
|
|
return value.code;
|
|
}
|
|
|
|
static DiscountsType? stringToDiscountsType(String? value) {
|
|
final byCode = DiscountsType.values.fromCode(value);
|
|
if (byCode != null) return byCode;
|
|
if (value == null) return null;
|
|
final name = value.contains('.') ? value.split('.').last : value;
|
|
return DiscountsType.values.byName(name);
|
|
}
|
|
|
|
static String? discountsTypeToString(DiscountsType? value) {
|
|
if (value == null) return null;
|
|
return value.code;
|
|
}
|
|
|
|
static ProductHourType? stringToProductHourType(String? value) {
|
|
final byCode = ProductHourType.values.fromCode(value);
|
|
if (byCode != null) return byCode;
|
|
if (value == null) return null;
|
|
final name = value.contains('.') ? value.split('.').last : value;
|
|
return ProductHourType.values.byName(name);
|
|
}
|
|
|
|
static String? productHourTypeToString(ProductHourType? value) {
|
|
if (value == null) return null;
|
|
return value.code;
|
|
}
|
|
}
|