Files
ttstd_family_care/lib/features/contacts/domain/contact_models.dart
T

109 lines
3.4 KiB
Dart

/// 联系人相关领域实体(家属端对接 client 接口)。
///
/// 以不可变类表达,避免 UI / 仓库直接依赖后台 DTO 结构。
/// 因未运行 build_runner(避免改动依赖与生成产物),此处手写等价实现:
/// 所有字段 final、提供 const 构造,并手动实现 JSON 转换。
/// 联系人视图对象(与后台 ContactVO 对齐)。
class Contact {
const Contact({
required this.id,
required this.name,
this.nickName,
required this.phoneNumber,
this.avatar,
this.position = 0,
this.emergency = false,
this.showDesktop = true,
this.bindPhone,
this.bindSn,
this.createTime,
this.updateTime,
});
final int id;
final String name;
final String? nickName;
final String phoneNumber;
final String? avatar;
final int position;
/// 是否紧急联系人(对应 UI 中的「常用」分组)。
final bool emergency;
/// 是否显示。
final bool showDesktop;
final String? bindPhone;
final String? bindSn;
final int? createTime;
final int? updateTime;
/// 列表展示用标题:昵称优先,缺省回退姓名。
String get displayName => (nickName != null && nickName!.isNotEmpty) ? nickName! : name;
/// 复制并覆盖 id(用于编辑场景,保留其余字段)。
Contact copyWithId(int id) => Contact(
id: id,
name: name,
nickName: nickName,
phoneNumber: phoneNumber,
avatar: avatar,
position: position,
emergency: emergency,
showDesktop: showDesktop,
bindPhone: bindPhone,
bindSn: bindSn,
createTime: createTime,
updateTime: updateTime,
);
factory Contact.fromJson(Map<String, dynamic> json) => Contact(
id: _toInt(json['id']) ?? 0,
name: json['name'] as String? ?? '',
nickName: json['nickName'] as String?,
phoneNumber: json['phoneNumber'] as String? ?? '',
avatar: json['avatar'] as String?,
position: _toInt(json['position']) ?? 0,
emergency: json['emergency'] as bool? ?? false,
showDesktop: json['showDesktop'] as bool? ?? true,
bindPhone: json['bindPhone'] as String?,
bindSn: json['bindSn'] as String?,
createTime: _toInt(json['createTime']),
updateTime: _toInt(json['updateTime']),
);
/// 转回后台表单结构(新增/修改复用)。
Map<String, dynamic> toFormJson() => {
'name': name,
'nickName': nickName,
'phoneNumber': phoneNumber,
'avatar': avatar,
'position': position,
'emergency': emergency,
'showDesktop': showDesktop,
'bindPhone': bindPhone,
'bindSn': bindSn,
};
}
/// 编辑页删除成功返回的哨兵 Contact。
///
/// 编辑页点击删除后以 [context.pop] 返回该常量,详情对话框据此识别「已删除」,
/// 从而关闭自身并刷新列表,避免把空对象当正常更新处理。
const Contact deletedContactSentinel = Contact(
id: -1,
name: '__deleted__',
phoneNumber: '',
);
/// 容错整型解析:后台可能以字符串(如 "123")或数字返回整型字段,
/// 直接用 `as int?` 在字符串场景下会抛出类型错误,故统一在此转换。
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) return int.tryParse(value);
return null;
}