mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
feat: account manager (#468)
* feat: account manager * remove dep * some fixes * migrate accounts * reimplement clearCookie
This commit is contained in:
committed by
GitHub
parent
94fa0652ac
commit
b15fdfa2ff
193
lib/utils/accounts/account.dart
Normal file
193
lib/utils/accounts/account.dart
Normal file
@@ -0,0 +1,193 @@
|
||||
import 'package:PiliPlus/utils/storage.dart';
|
||||
import 'package:PiliPlus/utils/utils.dart';
|
||||
import 'package:cookie_jar/cookie_jar.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
abstract class Account {
|
||||
final bool isLogin = false;
|
||||
late final DefaultCookieJar cookieJar;
|
||||
String? accessKey;
|
||||
String? refresh;
|
||||
late final Set<AccountType> type;
|
||||
|
||||
final int mid = 0;
|
||||
late String csrf;
|
||||
final Map<String, String> headers = const {};
|
||||
|
||||
bool activited = false;
|
||||
|
||||
Future<AnonymousAccount> logout();
|
||||
Future<void> onChange();
|
||||
|
||||
Map<String, dynamic>? toJson();
|
||||
}
|
||||
|
||||
@HiveType(typeId: 9)
|
||||
class LoginAccount implements Account {
|
||||
@override
|
||||
final bool isLogin = true;
|
||||
@override
|
||||
@HiveField(0)
|
||||
late final DefaultCookieJar cookieJar;
|
||||
@override
|
||||
@HiveField(1)
|
||||
String? accessKey;
|
||||
@override
|
||||
@HiveField(2)
|
||||
String? refresh;
|
||||
@override
|
||||
@HiveField(3)
|
||||
late final Set<AccountType> type;
|
||||
|
||||
@override
|
||||
late final int mid = int.parse(_midStr);
|
||||
|
||||
@override
|
||||
late final Map<String, String> headers = {
|
||||
'x-bili-mid': _midStr,
|
||||
'x-bili-aurora-eid': Utils.genAuroraEid(mid),
|
||||
};
|
||||
@override
|
||||
late String csrf =
|
||||
cookieJar.domainCookies['bilibili.com']!['/']!['bili_jct']!.cookie.value;
|
||||
|
||||
@override
|
||||
bool activited = false;
|
||||
|
||||
@override
|
||||
Future<AnonymousAccount> logout() async {
|
||||
await Future.wait([cookieJar.deleteAll(), _box.delete(_midStr)]);
|
||||
return AnonymousAccount();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onChange() => _box.put(_midStr, this);
|
||||
|
||||
@override
|
||||
Map<String, dynamic>? toJson() => {
|
||||
'cookies': cookieJar.toJson(),
|
||||
'accessKey': accessKey,
|
||||
'refresh': refresh,
|
||||
'type': type.map((i) => i.index).toList()
|
||||
};
|
||||
|
||||
late final String _midStr = cookieJar
|
||||
.domainCookies['bilibili.com']!['/']!['DedeUserID']!.cookie.value;
|
||||
|
||||
late final Box<LoginAccount> _box = Accounts.account;
|
||||
|
||||
LoginAccount(this.cookieJar, this.accessKey, this.refresh,
|
||||
[Set<AccountType>? type]) {
|
||||
this.type = type ?? {};
|
||||
}
|
||||
|
||||
LoginAccount.fromJson(Map json) {
|
||||
cookieJar = BiliCookieJar.fromJson(json['cookies']);
|
||||
accessKey = json['accessKey'];
|
||||
refresh = json['refresh'];
|
||||
type = (json['type'] as Iterable?)
|
||||
?.map((i) => AccountType.values[i])
|
||||
.toSet() ??
|
||||
{};
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => mid.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) || (other is Account && mid == other.mid);
|
||||
}
|
||||
|
||||
class AnonymousAccount implements Account {
|
||||
@override
|
||||
final bool isLogin = false;
|
||||
@override
|
||||
late final DefaultCookieJar cookieJar;
|
||||
@override
|
||||
String? accessKey;
|
||||
@override
|
||||
String? refresh;
|
||||
@override
|
||||
Set<AccountType> type = {};
|
||||
@override
|
||||
final int mid = 0;
|
||||
@override
|
||||
String csrf = '';
|
||||
@override
|
||||
final Map<String, String> headers = const {};
|
||||
|
||||
@override
|
||||
bool activited = false;
|
||||
|
||||
@override
|
||||
Future<AnonymousAccount> logout() async {
|
||||
await cookieJar.deleteAll();
|
||||
activited = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> onChange() async {}
|
||||
|
||||
@override
|
||||
Map<String, dynamic>? toJson() => null;
|
||||
|
||||
static final _instance = AnonymousAccount._();
|
||||
|
||||
AnonymousAccount._() {
|
||||
cookieJar = DefaultCookieJar(ignoreExpires: true);
|
||||
}
|
||||
|
||||
factory AnonymousAccount() => _instance;
|
||||
|
||||
@override
|
||||
int get hashCode => cookieJar.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
(other is Account && cookieJar == other.cookieJar);
|
||||
}
|
||||
|
||||
extension BiliCookie on Cookie {
|
||||
void setBiliDomain([String domain = '.bilibili.com']) {
|
||||
this
|
||||
..domain = domain
|
||||
..httpOnly = false
|
||||
..path = '/';
|
||||
}
|
||||
}
|
||||
|
||||
extension BiliCookieJar on DefaultCookieJar {
|
||||
Map<String, String> toJson() {
|
||||
final cookies = domainCookies['bilibili.com']?['/'] ?? {};
|
||||
return {for (var i in cookies.values) i.cookie.name: i.cookie.value};
|
||||
}
|
||||
|
||||
List<Cookie> toList() =>
|
||||
domainCookies['bilibili.com']?['/']
|
||||
?.entries
|
||||
.map((i) => i.value.cookie)
|
||||
.toList() ??
|
||||
[];
|
||||
|
||||
static DefaultCookieJar fromJson(Map json) =>
|
||||
DefaultCookieJar(ignoreExpires: true)
|
||||
..domainCookies['bilibili.com'] = {
|
||||
'/': {
|
||||
for (var i in json.entries)
|
||||
i.key: SerializableCookie(Cookie(i.key, i.value)..setBiliDomain())
|
||||
},
|
||||
};
|
||||
|
||||
static DefaultCookieJar fromList(List cookies) =>
|
||||
DefaultCookieJar(ignoreExpires: true)
|
||||
..domainCookies['bilibili.com'] = {
|
||||
'/': {
|
||||
for (var i in cookies)
|
||||
i['name']!: SerializableCookie(
|
||||
Cookie(i['name']!, i['value']!)..setBiliDomain()),
|
||||
},
|
||||
};
|
||||
}
|
||||
48
lib/utils/accounts/account_adapter.dart
Normal file
48
lib/utils/accounts/account_adapter.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'package:cookie_jar/cookie_jar.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
import '../storage.dart';
|
||||
import 'account.dart';
|
||||
|
||||
class LoginAccountAdapter extends TypeAdapter<LoginAccount> {
|
||||
@override
|
||||
final int typeId = 9;
|
||||
|
||||
@override
|
||||
LoginAccount read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return LoginAccount(
|
||||
fields[0] as DefaultCookieJar,
|
||||
fields[1] as String?,
|
||||
fields[2] as String?,
|
||||
(fields[3] as List?)?.cast<AccountType>().toSet(),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, LoginAccount obj) {
|
||||
writer
|
||||
..writeByte(4)
|
||||
..writeByte(0)
|
||||
..write(obj.cookieJar)
|
||||
..writeByte(1)
|
||||
..write(obj.accessKey)
|
||||
..writeByte(2)
|
||||
..write(obj.refresh)
|
||||
..writeByte(3)
|
||||
..write(obj.type.toList());
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is LoginAccountAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
22
lib/utils/accounts/account_manager/LICENSE
Normal file
22
lib/utils/accounts/account_manager/LICENSE
Normal file
@@ -0,0 +1,22 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Wen Du (wendux)
|
||||
Copyright (c) 2022 The CFUG Team
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
90
lib/utils/accounts/account_manager/README.md
Normal file
90
lib/utils/accounts/account_manager/README.md
Normal file
@@ -0,0 +1,90 @@
|
||||
# dio_cookie_manager
|
||||
|
||||
[](https://pub.dev/packages/dio_cookie_manager)
|
||||
|
||||
A cookie manager combines cookie_jar and dio, based on the interceptor algorithm.
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Install
|
||||
|
||||
Add the `dio_cookie_manager` package to your
|
||||
[pubspec dependencies](https://pub.dev/packages/dio_cookie_manager/install).
|
||||
|
||||
### Usage
|
||||
|
||||
```dart
|
||||
import 'package:cookie_jar/cookie_jar.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
|
||||
|
||||
void main() async {
|
||||
final dio = Dio();
|
||||
final cookieJar = CookieJar();
|
||||
dio.interceptors.add(CookieManager(cookieJar));
|
||||
// First request, and save cookies (CookieManager do it).
|
||||
await dio.get("https://dart.dev");
|
||||
// Print cookies
|
||||
print(await cookieJar.loadForRequest(Uri.parse("https://dart.dev")));
|
||||
// Second request with the cookies
|
||||
await dio.get('https://dart.dev');
|
||||
}
|
||||
```
|
||||
|
||||
## Cookie Manager
|
||||
|
||||
`CookieManager` Interceptor can help us manage the request/response cookies automatically.
|
||||
`CookieManager` depends on the `cookie_jar` package:
|
||||
|
||||
> The dio_cookie_manager manage API is based on the withdrawn
|
||||
> [cookie_jar](https://github.com/flutterchina/cookie_jar).
|
||||
|
||||
You can create a `CookieJar` or `PersistCookieJar` to manage cookies automatically,
|
||||
and dio use the `CookieJar` by default, which saves the cookies **in RAM**.
|
||||
If you want to persists cookies, you can use the `PersistCookieJar` class, for example:
|
||||
|
||||
```dart
|
||||
dio.interceptors.add(CookieManager(PersistCookieJar()))
|
||||
```
|
||||
|
||||
`PersistCookieJar` persists the cookies in files,
|
||||
so if the application exit, the cookies always exist unless call `delete` explicitly.
|
||||
|
||||
> Note: In flutter, the path passed to `PersistCookieJar` must be valid (exists in phones and with write access).
|
||||
> Use [path_provider](https://pub.dev/packages/path_provider) package to get the right path.
|
||||
|
||||
In flutter:
|
||||
|
||||
```dart
|
||||
Future<void> prepareJar() async {
|
||||
final Directory appDocDir = await getApplicationDocumentsDirectory();
|
||||
final String appDocPath = appDocDir.path;
|
||||
final jar = PersistCookieJar(
|
||||
ignoreExpires: true,
|
||||
storage: FileStorage(appDocPath + "/.cookies/"),
|
||||
);
|
||||
dio.interceptors.add(CookieManager(jar));
|
||||
}
|
||||
```
|
||||
|
||||
## Handling Cookies with redirect requests
|
||||
|
||||
Redirect requests require extra configuration to parse cookies correctly.
|
||||
In shortly:
|
||||
- Set `followRedirects` to `false`.
|
||||
- Allow `statusCode` from `300` to `399` responses predicated as succeed.
|
||||
- Make further requests using the `HttpHeaders.locationHeader`.
|
||||
|
||||
For example:
|
||||
```dart
|
||||
final cookieJar = CookieJar();
|
||||
final dio = Dio()
|
||||
..interceptors.add(CookieManager(cookieJar))
|
||||
..options.followRedirects = false
|
||||
..options.validateStatus =
|
||||
(status) => status != null && status >= 200 && status < 400;
|
||||
final redirected = await dio.get('/redirection');
|
||||
final response = await dio.get(
|
||||
redirected.headers.value(HttpHeaders.locationHeader)!,
|
||||
);
|
||||
```
|
||||
258
lib/utils/accounts/account_manager/account_mgr.dart
Normal file
258
lib/utils/accounts/account_manager/account_mgr.dart
Normal file
@@ -0,0 +1,258 @@
|
||||
// edit from package:dio_cookie_manager
|
||||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:PiliPlus/http/api.dart';
|
||||
import 'package:PiliPlus/http/constants.dart';
|
||||
import 'package:PiliPlus/utils/extension.dart';
|
||||
import 'package:PiliPlus/utils/storage.dart';
|
||||
import 'package:PiliPlus/utils/utils.dart';
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
|
||||
|
||||
import '../account.dart';
|
||||
|
||||
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.onlineTotal,
|
||||
Api.dynamicDetail,
|
||||
Api.aiConclusion,
|
||||
Api.getSeasonDetailApi,
|
||||
Api.liveRoomDmToken,
|
||||
Api.liveRoomDmPrefetch,
|
||||
},
|
||||
AccountType.recommend: {
|
||||
Api.recommendListWeb,
|
||||
Api.recommendListApp,
|
||||
Api.feedDislike,
|
||||
Api.feedDislikeCancel,
|
||||
Api.hotList,
|
||||
Api.hotSearchList, // 不同账号搜索结果可能不一样
|
||||
Api.searchDefault,
|
||||
Api.searchSuggest,
|
||||
Api.searchByType
|
||||
},
|
||||
AccountType.video: {Api.videoUrl, Api.bangumiVideoUrl}
|
||||
};
|
||||
|
||||
static final loginApi = {
|
||||
Api.getTVCode,
|
||||
Api.qrcodePoll,
|
||||
Api.getCaptcha,
|
||||
Api.getWebKey,
|
||||
Api.appSmsCode,
|
||||
Api.loginByPwdApi,
|
||||
Api.logInByAppSms,
|
||||
Api.safeCenterGetInfo,
|
||||
Api.preCapture,
|
||||
Api.safeCenterSmsCode,
|
||||
Api.safeCenterSmsVerify,
|
||||
Api.oauth2AccessToken,
|
||||
};
|
||||
|
||||
const AccountManager();
|
||||
|
||||
static String getCookies(List<Cookie> cookies) {
|
||||
// Sort cookies by path (longer path first).
|
||||
cookies.sort((a, b) {
|
||||
if (a.path == null && b.path == null) {
|
||||
return 0;
|
||||
} else if (a.path == null) {
|
||||
return -1;
|
||||
} else if (b.path == null) {
|
||||
return 1;
|
||||
} else {
|
||||
return b.path!.length.compareTo(a.path!.length);
|
||||
}
|
||||
});
|
||||
return cookies.map((cookie) => '${cookie.name}=${cookie.value}').join('; ');
|
||||
}
|
||||
|
||||
@override
|
||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
||||
final path = options.path;
|
||||
|
||||
if (path.startsWith(GStorage.blockServer)) return handler.next(options);
|
||||
|
||||
final Account account = options.extra['account'] ?? _findAccount(path);
|
||||
|
||||
if (account.isLogin) options.headers.addAll(account.headers);
|
||||
|
||||
// app端不需要管理cookie
|
||||
if (path.startsWith(HttpString.appBaseUrl)) {
|
||||
// debugPrint('is app: ${options.path}');
|
||||
// bytes是grpc响应
|
||||
if (options.responseType != ResponseType.bytes) {
|
||||
final dataPtr = (options.method == 'POST' && options.data is Map
|
||||
? options.data as Map
|
||||
: options.queryParameters)
|
||||
.cast<String, dynamic>();
|
||||
if (dataPtr.isNotEmpty) {
|
||||
if (!account.accessKey.isNullOrEmpty) {
|
||||
dataPtr['access_key'] = account.accessKey!;
|
||||
}
|
||||
dataPtr['ts'] ??=
|
||||
(DateTime.now().millisecondsSinceEpoch ~/ 1000).toString();
|
||||
Utils.appSign(dataPtr);
|
||||
// debugPrint(dataPtr.toString());
|
||||
}
|
||||
}
|
||||
return handler.next(options);
|
||||
} else {
|
||||
account.cookieJar.loadForRequest(options.uri).then((cookies) {
|
||||
final previousCookies =
|
||||
options.headers[HttpHeaders.cookieHeader] as String?;
|
||||
final newCookies = getCookies([
|
||||
...?previousCookies
|
||||
?.split(';')
|
||||
.where((e) => e.isNotEmpty)
|
||||
.map((c) => Cookie.fromSetCookieValue(c)),
|
||||
...cookies,
|
||||
]);
|
||||
options.headers[HttpHeaders.cookieHeader] =
|
||||
newCookies.isNotEmpty ? newCookies : null;
|
||||
handler.next(options);
|
||||
}).catchError((dynamic e, StackTrace s) {
|
||||
final err = DioException(
|
||||
requestOptions: options,
|
||||
error: e,
|
||||
stackTrace: s,
|
||||
);
|
||||
handler.reject(err, true);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onResponse(Response response, ResponseInterceptorHandler handler) {
|
||||
final path = response.requestOptions.path;
|
||||
if (path.startsWith(HttpString.appBaseUrl) ||
|
||||
path.startsWith(GStorage.blockServer)) {
|
||||
return handler.next(response);
|
||||
} else {
|
||||
_saveCookies(response).then((_) => handler.next(response)).catchError(
|
||||
(dynamic e, StackTrace s) {
|
||||
final error = DioException(
|
||||
requestOptions: response.requestOptions,
|
||||
error: e,
|
||||
stackTrace: s,
|
||||
);
|
||||
handler.reject(error, true);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) {
|
||||
String url = err.requestOptions.uri.toString();
|
||||
debugPrint('🌹🌹ApiInterceptor: $url');
|
||||
if (url.contains('heartbeat') ||
|
||||
url.contains('seg.so') ||
|
||||
url.contains('online/total') ||
|
||||
url.contains('github') ||
|
||||
(url.contains('skipSegments') && err.requestOptions.method == 'GET')) {
|
||||
// skip
|
||||
} else {
|
||||
dioError(err).then((res) => SmartDialog.showToast(res + url));
|
||||
}
|
||||
if (err.response != null &&
|
||||
!err.response!.requestOptions.path.startsWith(HttpString.appBaseUrl)) {
|
||||
_saveCookies(err.response!).then((_) => handler.next(err)).catchError(
|
||||
(dynamic e, StackTrace s) {
|
||||
final error = DioException(
|
||||
requestOptions: err.response!.requestOptions,
|
||||
error: e,
|
||||
stackTrace: s,
|
||||
);
|
||||
handler.next(error);
|
||||
},
|
||||
);
|
||||
} else {
|
||||
handler.next(err);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _saveCookies(Response response) async {
|
||||
final account = (response.requestOptions.extra['account'] as Account? ??
|
||||
_findAccount(response.requestOptions.path));
|
||||
final setCookies = response.headers[HttpHeaders.setCookieHeader];
|
||||
if (setCookies == null || setCookies.isEmpty) {
|
||||
return;
|
||||
}
|
||||
final List<Cookie> cookies = setCookies
|
||||
.map((str) => str.split(_setCookieReg))
|
||||
.expand((cookie) => cookie)
|
||||
.where((cookie) => cookie.isNotEmpty)
|
||||
.map((str) => Cookie.fromSetCookieValue(str))
|
||||
.toList();
|
||||
final statusCode = response.statusCode ?? 0;
|
||||
final locations = response.headers[HttpHeaders.locationHeader] ?? [];
|
||||
final isRedirectRequest = statusCode >= 300 && statusCode < 400;
|
||||
final originalUri = response.requestOptions.uri;
|
||||
final realUri = originalUri.resolveUri(response.realUri);
|
||||
await account.cookieJar.saveFromResponse(realUri, cookies);
|
||||
if (isRedirectRequest && locations.isNotEmpty) {
|
||||
final originalUri = response.realUri;
|
||||
await Future.wait(
|
||||
locations.map(
|
||||
(location) => account.cookieJar.saveFromResponse(
|
||||
// Resolves the location based on the current Uri.
|
||||
originalUri.resolve(location),
|
||||
cookies,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
await account.onChange();
|
||||
}
|
||||
|
||||
Account _findAccount(String path) => loginApi.contains(path)
|
||||
? AnonymousAccount()
|
||||
: Accounts.get(AccountType.values.firstWhere(
|
||||
(i) => apiTypeSet[i]?.contains(path) == true,
|
||||
orElse: () => AccountType.main));
|
||||
|
||||
static Future<String> dioError(DioException error) async {
|
||||
switch (error.type) {
|
||||
case DioExceptionType.badCertificate:
|
||||
return '证书有误!';
|
||||
case DioExceptionType.badResponse:
|
||||
return '服务器异常,请稍后重试!';
|
||||
case DioExceptionType.cancel:
|
||||
return '请求已被取消,请重新请求';
|
||||
case DioExceptionType.connectionError:
|
||||
return '连接错误,请检查网络设置';
|
||||
case DioExceptionType.connectionTimeout:
|
||||
return '网络连接超时,请检查网络设置';
|
||||
case DioExceptionType.receiveTimeout:
|
||||
return '响应超时,请稍后重试!';
|
||||
case DioExceptionType.sendTimeout:
|
||||
return '发送请求超时,请检查网络设置';
|
||||
case DioExceptionType.unknown:
|
||||
final String res =
|
||||
(await Connectivity().checkConnectivity()).first.title;
|
||||
return '$res网络异常 ${error.error}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension _ConnectivityResultExt on ConnectivityResult {
|
||||
String get title => const ['蓝牙', 'Wi-Fi', '局域', '流量', '无', '代理', '其他'][index];
|
||||
}
|
||||
28
lib/utils/accounts/account_type_adapter.dart
Normal file
28
lib/utils/accounts/account_type_adapter.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
import 'package:PiliPlus/utils/extension.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
import '../storage.dart' show AccountType;
|
||||
|
||||
class AccountTypeAdapter extends TypeAdapter<AccountType> {
|
||||
@override
|
||||
final int typeId = 10;
|
||||
|
||||
@override
|
||||
AccountType read(BinaryReader reader) =>
|
||||
AccountType.values.getOrNull(reader.readByte()) ?? AccountType.main;
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, AccountType obj) {
|
||||
writer.writeByte(obj.index);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is AccountTypeAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
28
lib/utils/accounts/cookie_jar_adapter.dart
Normal file
28
lib/utils/accounts/cookie_jar_adapter.dart
Normal file
@@ -0,0 +1,28 @@
|
||||
import 'package:cookie_jar/cookie_jar.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
import 'account.dart';
|
||||
|
||||
class BiliCookieJarAdapter extends TypeAdapter<DefaultCookieJar> {
|
||||
@override
|
||||
final int typeId = 8;
|
||||
|
||||
@override
|
||||
DefaultCookieJar read(BinaryReader reader) =>
|
||||
BiliCookieJar.fromJson(reader.readMap().cast<String, String>());
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, DefaultCookieJar obj) {
|
||||
writer.writeMap(obj.toJson());
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is BiliCookieJarAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
Reference in New Issue
Block a user