opt: account

This commit is contained in:
My-Responsitories
2025-09-23 02:26:14 +08:00
parent f48f90b253
commit 8d818f9bf8
4 changed files with 31 additions and 24 deletions

View File

@@ -17,7 +17,7 @@ import 'package:encrypt/encrypt.dart';
class LoginHttp {
static final String deviceId = LoginUtils.genDeviceId();
static final String buvid = LoginUtils.buvid;
static String get buvid => LoginUtils.buvid;
static final Map<String, String> headers = {
'buvid': buvid,
'env': 'prod',

View File

@@ -690,7 +690,11 @@ class LoginPageController extends GetxController
tokenInfo['refresh_token'],
);
await Future.wait([account.onChange(), AnonymousAccount().delete()]);
Accounts.accountMode.updateAll((_, a) => a == account ? account : a);
for (int i = 0; i < AccountType.values.length; i++) {
if (Accounts.accountMode[i].mid == account.mid) {
Accounts.accountMode[i] = account;
}
}
if (Accounts.main.isLogin) {
SmartDialog.showToast('登录成功');
} else {
@@ -704,7 +708,7 @@ class LoginPageController extends GetxController
SmartDialog.showToast('请先登录');
return Get.toNamed('/loginPage');
}
final selectAccount = Map.of(Accounts.accountMode);
final selectAccount = List.of(Accounts.accountMode);
final options = {
AnonymousAccount(): '0',
...Accounts.account.toMap().map(
@@ -729,9 +733,9 @@ class LoginPageController extends GetxController
.map(
(e) => Builder(
builder: (context) => RadioGroup(
groupValue: selectAccount[e],
groupValue: selectAccount[e.index],
onChanged: (v) {
selectAccount[e] = v!;
selectAccount[e.index] = v!;
(context as Element).markNeedsBuild();
},
child: WrapRadioOptionsGroup<Account>(
@@ -756,9 +760,9 @@ class LoginPageController extends GetxController
),
TextButton(
onPressed: () {
for (var i in selectAccount.entries) {
if (i.value != Accounts.get(i.key)) {
Accounts.set(i.key, i.value);
for (var (i, v) in selectAccount.indexed) {
if (v != Accounts.accountMode[i]) {
Accounts.set(AccountType.values[i], v);
}
}
Get.back();

View File

@@ -229,7 +229,8 @@ class MineController
}
res == true
? Accounts.set(AccountType.heartbeat, AnonymousAccount())
: Accounts.accountMode[AccountType.heartbeat] = AnonymousAccount();
: Accounts.accountMode[AccountType.heartbeat.index] =
AnonymousAccount();
});
} else {
Accounts.set(AccountType.heartbeat, Accounts.main);

View File

@@ -7,9 +7,12 @@ import 'package:hive/hive.dart';
abstract class Accounts {
static late final Box<LoginAccount> account;
static final Map<AccountType, Account> accountMode = {};
static Account get main => accountMode[AccountType.main]!;
static Account get heartbeat => accountMode[AccountType.heartbeat]!;
static final List<Account> accountMode = List.filled(
AccountType.values.length,
AnonymousAccount(),
);
static Account get main => accountMode[AccountType.main.index];
static Account get heartbeat => accountMode[AccountType.heartbeat.index];
static Account get history {
final heartbeat = Accounts.heartbeat;
if (heartbeat is AnonymousAccount) {
@@ -70,14 +73,11 @@ abstract class Accounts {
static Future<void> refresh() async {
for (var a in account.values) {
for (var t in a.type) {
accountMode[t] = a;
accountMode[t.index] = a;
}
}
for (var type in AccountType.values) {
accountMode[type] ??= AnonymousAccount();
}
await Future.wait(
(accountMode.values.toSet()..retainWhere((i) => !i.activited)).map(
(accountMode.toSet()..removeWhere((i) => i.activited)).map(
Request.buvidActive,
),
);
@@ -85,7 +85,7 @@ abstract class Accounts {
static Future<void> clear() async {
await account.clear();
for (var i in AccountType.values) {
for (int i = 0; i < AccountType.values.length; i++) {
accountMode[i] = AnonymousAccount();
}
await AnonymousAccount().delete();
@@ -100,9 +100,11 @@ abstract class Accounts {
static Future<void> deleteAll(Set<Account> accounts) async {
var isloginMain = Accounts.main.isLogin;
Accounts.accountMode.updateAll(
(_, a) => accounts.contains(a) ? AnonymousAccount() : a,
);
for (int i = 0; i < AccountType.values.length; i++) {
if (accounts.contains(accountMode[i])) {
accountMode[i] = AnonymousAccount();
}
}
await Future.wait(accounts.map((i) => i.delete()));
if (isloginMain && !Accounts.main.isLogin) {
await LoginUtils.onLogoutMain();
@@ -110,8 +112,8 @@ abstract class Accounts {
}
static Future<void> set(AccountType key, Account account) async {
await (accountMode[key]?..type.remove(key))?.onChange();
accountMode[key] = account..type.add(key);
await (accountMode[key.index]..type.remove(key)).onChange();
accountMode[key.index] = account..type.add(key);
await account.onChange();
if (!account.activited) await Request.buvidActive(account);
switch (key) {
@@ -129,6 +131,6 @@ abstract class Accounts {
}
static Account get(AccountType key) {
return accountMode[key]!;
return accountMode[key.index];
}
}