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.
132 lines
4.5 KiB
132 lines
4.5 KiB
import 'dart:convert';
|
|
import 'dart:ffi';
|
|
|
|
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 deviceid;
|
|
String language;
|
|
String token;
|
|
String mail;
|
|
String phone;
|
|
String phonemac;
|
|
String password;
|
|
int membershipLevel=0;//添加会员等级字段,0表示普通用户
|
|
// 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.deviceid,
|
|
required this.language,
|
|
required this.token,
|
|
required this.mail,
|
|
required this.phone,
|
|
required this.phonemac,
|
|
required this.password,
|
|
this.membershipLevel = 0, // 添加默认值
|
|
// 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['data']['user'] as Map<String, dynamic>?; // 允许空值
|
|
final token = json['data']['token'] as String? ?? '';
|
|
// 更新单例实例
|
|
return _instance = User._(
|
|
token: token as String? ?? '',
|
|
name: (userData?['name'] as String?) ?? '默认名称', // 提供默认值
|
|
uid: (userData?['uid'] as String?) ?? '',
|
|
avatar: (userData?['avatar'] as String?) ?? '',
|
|
deviceid: (userData?['deviceid'] 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?) ?? '',
|
|
membershipLevel: (userData?['membershipLevel'] as int?) ?? 0, // 提供默认值
|
|
// 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>?; // 允许空值
|
|
final token = '';
|
|
// 更新单例实例
|
|
return _instance = User._(
|
|
token: token as String? ?? '',
|
|
name: (userData?['name'] as String?) ?? '默认名称', // 提供默认值
|
|
uid: (userData?['uid'] as String?) ?? '',
|
|
avatar: (userData?['avatar'] as String?) ?? '',
|
|
deviceid: (userData?['deviceid'] 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?) ?? '',
|
|
// 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,
|
|
'deviceid': deviceid,
|
|
'language': language,
|
|
'token': token,
|
|
'mail': mail,
|
|
'phone': phone,
|
|
'phonemac': phonemac,
|
|
'password': password,
|
|
'membershipLevel': membershipLevel,
|
|
// 'lastbettime': lastbettime,
|
|
// 'createtime': createtime,
|
|
// 'wxopenid': wxopenid,
|
|
// 'googleopenid': googleopenid,
|
|
// 'facebookopenid': facebookopenid,
|
|
}
|
|
};
|
|
|
|
// 获取用户名的快捷方法
|
|
static String getname() => instance.name;
|
|
static String getdeviceInfo() => instance.deviceid;
|
|
static String getuserId() => instance.uid;
|
|
static String getstanding() => "";
|
|
static String getnavatar() => instance.avatar;
|
|
}
|
|
|