mirror of
https://github.com/qinglong-app/qinglong_app.git
synced 2025-10-09 16:48:19 +08:00
支持多帐号登录
This commit is contained in:
@@ -1,18 +1,22 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:qinglong_app/utils/sp_utils.dart';
|
||||
|
||||
import 'sp_const.dart';
|
||||
import 'theme.dart';
|
||||
|
||||
class UserInfoViewModel {
|
||||
int _primaryColor = commonColor.value;
|
||||
String? _token;
|
||||
String? _host = "";
|
||||
String? _userName;
|
||||
String? _passWord;
|
||||
bool _useSecertLogined = false;
|
||||
int _primaryColor = commonColor.value;
|
||||
|
||||
List<UserInfoBean> historyAccounts = [];
|
||||
|
||||
UserInfoViewModel() {
|
||||
String userInfoJson = SpUtil.getString(spUserInfo);
|
||||
_token = SpUtil.getString(spUserInfo);
|
||||
_userName = SpUtil.getString(spUserName);
|
||||
_passWord = SpUtil.getString(spPassWord);
|
||||
_primaryColor = SpUtil.getInt(spCustomColor, defValue: commonColor.value);
|
||||
@@ -20,8 +24,13 @@ class UserInfoViewModel {
|
||||
_useSecertLogined = SpUtil.getBool(spSecretLogined, defValue: false);
|
||||
_host = SpUtil.getString(spHost, defValue: '');
|
||||
|
||||
if (userInfoJson.isNotEmpty) {
|
||||
_token = userInfoJson;
|
||||
List<dynamic>? tempList =
|
||||
jsonDecode(SpUtil.getString(spLoginHistory, defValue: '[]'));
|
||||
|
||||
if (tempList != null && tempList.isNotEmpty) {
|
||||
for (Map<String, dynamic> value in tempList) {
|
||||
historyAccounts.add(UserInfoBean.fromJson(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,14 +39,19 @@ class UserInfoViewModel {
|
||||
SpUtil.putString(spUserInfo, token);
|
||||
}
|
||||
|
||||
void updateUserName(String userName, String password) {
|
||||
void updateUserName(
|
||||
String host, String userName, String password, bool secretLogin) {
|
||||
updateHost(host);
|
||||
_useSecretLogin(secretLogin);
|
||||
_userName = userName;
|
||||
_passWord = password;
|
||||
SpUtil.putString(spUserName, userName);
|
||||
SpUtil.putString(spPassWord, password);
|
||||
|
||||
save2HistoryAccount();
|
||||
}
|
||||
|
||||
void useSecretLogin(bool use) {
|
||||
void _useSecretLogin(bool use) {
|
||||
_useSecertLogined = use;
|
||||
SpUtil.putBool(spSecretLogined, _useSecertLogined);
|
||||
}
|
||||
@@ -67,4 +81,60 @@ class UserInfoViewModel {
|
||||
bool isLogined() {
|
||||
return token != null && token!.isNotEmpty;
|
||||
}
|
||||
|
||||
void save2HistoryAccount() {
|
||||
if (_host == null || _host!.isEmpty) return;
|
||||
if (_userName == null || _userName!.isEmpty) return;
|
||||
if (_passWord == null || _passWord!.isEmpty) return;
|
||||
|
||||
//如果已经存在host,那就更新
|
||||
|
||||
print("login success $_host $userName $passWord");
|
||||
|
||||
historyAccounts.removeWhere((element) => element.host == _host);
|
||||
|
||||
historyAccounts.insert(
|
||||
0,
|
||||
UserInfoBean(
|
||||
userName: _userName,
|
||||
password: _passWord,
|
||||
useSecretLogined: _useSecertLogined,
|
||||
host: _host));
|
||||
|
||||
SpUtil.putString(spLoginHistory, jsonEncode(historyAccounts));
|
||||
}
|
||||
|
||||
void removeHistoryAccount(String? host) {
|
||||
if (host == null || host.isEmpty) return;
|
||||
|
||||
historyAccounts.removeWhere((element) => element.host == host);
|
||||
|
||||
SpUtil.putString(spLoginHistory, jsonEncode(historyAccounts));
|
||||
}
|
||||
}
|
||||
|
||||
class UserInfoBean {
|
||||
String? userName;
|
||||
String? password;
|
||||
bool useSecretLogined = false;
|
||||
String? host;
|
||||
|
||||
UserInfoBean(
|
||||
{this.userName, this.password, this.useSecretLogined = false, this.host});
|
||||
|
||||
UserInfoBean.fromJson(Map<String, dynamic> json) {
|
||||
userName = json['userName'];
|
||||
password = json['password'];
|
||||
useSecretLogined = json['useSecretLogined'] ?? false;
|
||||
host = json['host'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = new Map<String, dynamic>();
|
||||
data['userName'] = this.userName;
|
||||
data['password'] = this.password;
|
||||
data['useSecretLogined'] = this.useSecretLogined;
|
||||
data['host'] = this.host;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user