Files
zhinian_manage/lib/models/user.dart
2026-05-15 14:41:37 +08:00

70 lines
1.6 KiB
Dart

enum UserRole { boss, employee }
class User {
final String id;
final String phone;
final String name;
final String avatar;
final UserRole role;
final String token;
final String? hotelName;
final String? scenicName;
const User({
required this.id,
required this.phone,
required this.name,
required this.avatar,
required this.role,
required this.token,
this.hotelName,
this.scenicName,
});
bool get isBoss => role == UserRole.boss;
User copyWith({
String? id,
String? phone,
String? name,
String? avatar,
UserRole? role,
String? token,
String? hotelName,
String? scenicName,
}) {
return User(
id: id ?? this.id,
phone: phone ?? this.phone,
name: name ?? this.name,
avatar: avatar ?? this.avatar,
role: role ?? this.role,
token: token ?? this.token,
hotelName: hotelName ?? this.hotelName,
scenicName: scenicName ?? this.scenicName,
);
}
Map<String, dynamic> toJson() => {
'id': id,
'phone': phone,
'name': name,
'avatar': avatar,
'role': role.name,
'token': token,
'hotelName': hotelName,
'scenicName': scenicName,
};
factory User.fromJson(Map<String, dynamic> json) => User(
id: json['id'] ?? '',
phone: json['phone'] ?? '',
name: json['name'] ?? '',
avatar: json['avatar'] ?? '',
role: UserRole.values.byName(json['role'] ?? 'employee'),
token: json['token'] ?? '',
hotelName: json['hotelName'],
scenicName: json['scenicName'],
);
}