You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
140 lines
4.7 KiB
140 lines
4.7 KiB
class User {
|
|
// 单例实例
|
|
static User? _instance;
|
|
|
|
// 单例访问器(增加空安全保护)
|
|
static User get instance {
|
|
if (_instance == null) {
|
|
throw Exception("User must be initialized first");
|
|
}
|
|
return _instance!;
|
|
}
|
|
|
|
// 最终属性(不可变)
|
|
String name;
|
|
String uid;
|
|
String avatar;
|
|
String language;
|
|
String token;
|
|
String mail;
|
|
String phone;
|
|
String phonemac;
|
|
String password;
|
|
int viplv = 0; //添加会员等级字段,0表示普通用户
|
|
String earphonename;
|
|
String earphonemac;
|
|
String earphoneauthcode;
|
|
// final Int64 lastbettime;
|
|
// final Int64 createtime;
|
|
// final String wxopenid;
|
|
// final String googleopenid;
|
|
// final String facebookopenid;
|
|
|
|
// 私有构造函数
|
|
User._({
|
|
required this.name,
|
|
required this.uid,
|
|
required this.avatar,
|
|
required this.language,
|
|
required this.token,
|
|
required this.mail,
|
|
required this.phone,
|
|
required this.phonemac,
|
|
required this.password,
|
|
required this.viplv, // 添加默认值
|
|
required this.earphonename,
|
|
required this.earphonemac,
|
|
required this.earphoneauthcode,
|
|
// required this.lastbettime,
|
|
// required this.createtime,
|
|
// required this.wxopenid,
|
|
// required this.googleopenid,
|
|
// required this.facebookopenid,
|
|
});
|
|
|
|
factory User.fromJson(Map<String, dynamic> json) {
|
|
// 修复1:安全获取嵌套的 user 字段
|
|
final userData = json['user'] as Map<String, dynamic>?; // 允许空值
|
|
//final token = json['data']['token'] as String? ?? '';
|
|
//final token = '';
|
|
// 更新单例实例
|
|
return _instance = User._(
|
|
token: '',
|
|
//token: token as String? ?? '',
|
|
name: (userData?['name'] as String?) ?? '默认名称', // 提供默认值
|
|
uid: (userData?['uid'] as String?) ?? '',
|
|
avatar: (userData?['avatar'] as String?) ?? '',
|
|
|
|
language: (userData?['language'] as String?) ?? 'en',
|
|
|
|
mail: (userData?['mail'] as String?) ?? '',
|
|
phone: (userData?['phone'] as String?) ?? '',
|
|
phonemac: (userData?['phonemac'] as String?) ?? '',
|
|
password: (userData?['password'] as String?) ?? '',
|
|
viplv: (userData?['viplv'] as int?) ?? 0, // 提供默认值
|
|
earphonename: (userData?['earphonename'] as String?) ?? '',
|
|
earphonemac: (userData?['earphonemac'] as String?) ?? '',
|
|
earphoneauthcode: (userData?['earphoneauthcode'] as String?) ?? '',
|
|
// lastbettime: _parseTimestamp(userData['lastbettime']),
|
|
// createtime: _parseTimestamp(userData['createtime']),
|
|
// wxopenid: (userData?['wxopenid'] as String?) ?? '',
|
|
// googleopenid: (userData?['googleopenid'] as String?) ?? '',
|
|
// facebookopenid: (userData?['facebookopenid'] as String?) ?? '',
|
|
);
|
|
}
|
|
factory User.fronTokenJson(Map<String, dynamic> json) {
|
|
// 修复1:安全获取嵌套的 user 字段
|
|
final userData = json['user'] as Map<String, dynamic>?; // 允许空值
|
|
print("数据获取fronTokenJson${userData}");
|
|
// 更新单例实例
|
|
return _instance = User._(
|
|
token: '',
|
|
name: (userData?['name'] as String?) ?? '默认名称', // 提供默认值
|
|
uid: (userData?['uid'] as String?) ?? '',
|
|
avatar: (userData?['avatar'] as String?) ?? '',
|
|
language: (userData?['language'] as String?) ?? 'en',
|
|
|
|
mail: (userData?['mail'] as String?) ?? '',
|
|
phone: (userData?['phone'] as String?) ?? '',
|
|
phonemac: (userData?['phonemac'] as String?) ?? '',
|
|
password: (userData?['password'] as String?) ?? '',
|
|
|
|
viplv: (userData?['viplv'] as int?) ?? 0, // 提供默认值
|
|
earphonename: (userData?['earphonename'] as String?) ?? '',
|
|
earphonemac: (userData?['earphonemac'] as String?) ?? '',
|
|
earphoneauthcode: (userData?['earphoneauthcode'] as String?) ?? '',
|
|
// lastbettime: _parseTimestamp(userData['lastbettime']),
|
|
// createtime: _parseTimestamp(userData['createtime']),
|
|
// wxopenid: (userData?['wxopenid'] as String?) ?? '',
|
|
// googleopenid: (userData?['googleopenid'] as String?) ?? '',
|
|
// facebookopenid: (userData?['facebookopenid'] as String?) ?? '',
|
|
);
|
|
}
|
|
|
|
// 完整的JSON序列化
|
|
Map<String, dynamic> toJson() => {
|
|
'user': {
|
|
'name': name,
|
|
'uid': uid,
|
|
'avatar': avatar,
|
|
|
|
'language': language,
|
|
'token': token,
|
|
'mail': mail,
|
|
'phone': phone,
|
|
'phonemac': phonemac,
|
|
'password': password,
|
|
'viplv': viplv,
|
|
|
|
'earphonename': earphonename,
|
|
'earphonemac': earphonemac,
|
|
'earphoneauthcode': earphoneauthcode,
|
|
|
|
// 'lastbettime': lastbettime,
|
|
// 'createtime': createtime,
|
|
// 'wxopenid': wxopenid,
|
|
// 'googleopenid': googleopenid,
|
|
// 'facebookopenid': facebookopenid,
|
|
}
|
|
};
|
|
}
|
|
|