feat: logout (#497)

* feat: logout

* update api type
This commit is contained in:
My-Responsitories
2025-03-23 13:46:26 +08:00
committed by GitHub
parent 7c3e3cb1f8
commit d6587cf3b6
11 changed files with 139 additions and 129 deletions

View File

@@ -16,7 +16,7 @@ abstract class Account {
bool activited = false;
Future<AnonymousAccount> logout();
Future<void> delete();
Future<void> onChange();
Map<String, dynamic>? toJson();
@@ -55,10 +55,7 @@ class LoginAccount implements Account {
bool activited = false;
@override
Future<AnonymousAccount> logout() async {
await Future.wait([cookieJar.deleteAll(), _box.delete(_midStr)]);
return AnonymousAccount();
}
Future<void> delete() => _box.delete(_midStr);
@override
Future<void> onChange() => _box.put(_midStr, this);
@@ -121,10 +118,9 @@ class AnonymousAccount implements Account {
bool activited = false;
@override
Future<AnonymousAccount> logout() async {
Future<void> delete() async {
await cookieJar.deleteAll();
activited = false;
return this;
}
@override

View File

@@ -19,23 +19,22 @@ final _setCookieReg = RegExp('(?<=)(,)(?=[^;]+?=)');
class AccountManager extends Interceptor {
static final Map<AccountType, Set<String>> apiTypeSet = {
AccountType.heartbeat: {
Api.videoUrl,
Api.videoIntro,
Api.relatedList,
Api.replyList,
Api.replyReplyList,
Api.searchSuggest,
Api.searchByType,
Api.heartBeat,
Api.ab2c,
Api.bangumiInfo,
Api.liveRoomInfo,
Api.liveRoomInfoH5,
Api.onlineTotal,
Api.dynamicDetail,
Api.aiConclusion,
Api.getSeasonDetailApi,
Api.liveRoomDmToken,
Api.liveRoomDmPrefetch,
Api.searchByType,
Api.memberDynamicSearch
},
AccountType.recommend: {
Api.recommendListWeb,
@@ -43,10 +42,11 @@ class AccountManager extends Interceptor {
Api.feedDislike,
Api.feedDislikeCancel,
Api.hotList,
Api.relatedList,
Api.hotSearchList, // 不同账号搜索结果可能不一样
Api.searchDefault,
Api.searchSuggest,
Api.searchByType
Api.liveList,
},
AccountType.video: {Api.videoUrl, Api.bangumiVideoUrl}
};

View File

@@ -21,9 +21,11 @@ extension ScrollControllerExt on ScrollController {
}
}
extension ListExt<T> on List<T>? {
extension IterableExt<T> on Iterable<T>? {
bool get isNullOrEmpty => this == null || this!.isEmpty;
}
extension ListExt<T> on List<T>? {
T? getOrNull(int index) {
if (isNullOrEmpty) {
return null;

View File

@@ -105,7 +105,7 @@ class LoginUtils {
} catch (_) {}
} else {
// 获取用户信息失败
await Accounts.set(AccountType.main, await account.logout());
await Accounts.deleteAll({account});
SmartDialog.showNotify(
msg: '登录失败请检查cookie是否正确${result['message']}',
notifyType: NotifyType.warning);

View File

@@ -862,9 +862,8 @@ class Accounts {
for (var i in AccountType.values) {
accountMode[i] = AnonymousAccount();
}
if (!AnonymousAccount().activited) {
Request.buvidActive(AnonymousAccount());
}
await AnonymousAccount().delete();
Request.buvidActive(AnonymousAccount());
}
static Future<void> close() async {
@@ -872,6 +871,16 @@ class Accounts {
account.close();
}
static Future<void> deleteAll(Set<Account> accounts) async {
var isloginMain = Accounts.main.isLogin;
Accounts.accountMode
.updateAll((_, a) => accounts.contains(a) ? AnonymousAccount() : a);
await Future.wait(accounts.map((i) => i.delete()));
if (isloginMain && !Accounts.main.isLogin) {
await LoginUtils.onLogoutMain();
}
}
static Future<void> set(AccountType key, Account account) async {
await (accountMode[key]?..type.remove(key))?.onChange();
accountMode[key] = account..type.add(key);
@@ -879,11 +888,9 @@ class Accounts {
if (!account.activited) await Request.buvidActive(account);
switch (key) {
case AccountType.main:
if (account.isLogin) {
await LoginUtils.onLoginMain();
} else {
await LoginUtils.onLogoutMain();
}
await (account.isLogin
? LoginUtils.onLoginMain()
: LoginUtils.onLogoutMain());
break;
case AccountType.heartbeat:
MineController.anonymity.value = !account.isLogin;