fix: 第三方登录302重定向失效、ua获取
This commit is contained in:
@ -4,6 +4,7 @@ import 'dart:io';
|
||||
import 'dart:async';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:cookie_jar/cookie_jar.dart';
|
||||
import 'package:dio_http2_adapter/dio_http2_adapter.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:pilipala/utils/storage.dart';
|
||||
import 'package:pilipala/utils/utils.dart';
|
||||
@ -59,9 +60,6 @@ class Request {
|
||||
static Future<String> getCsrf() async {
|
||||
var cookies = await cookieManager.cookieJar
|
||||
.loadForRequest(Uri.parse(HttpString.baseApiUrl));
|
||||
// for (var i in cookies) {
|
||||
// print(i);
|
||||
// }
|
||||
String token = '';
|
||||
if (cookies.where((e) => e.name == 'bili_jct').isNotEmpty) {
|
||||
token = cookies.firstWhere((e) => e.name == 'bili_jct').value;
|
||||
@ -92,14 +90,21 @@ class Request {
|
||||
receiveTimeout: const Duration(milliseconds: 12000),
|
||||
//Http请求头.
|
||||
headers: {
|
||||
'keep-alive': true,
|
||||
'user-agent': headerUa('pc'),
|
||||
'Accept-Encoding': 'gzip'
|
||||
'keep-alive': 'true',
|
||||
'user-agent': headerUa(),
|
||||
},
|
||||
persistentConnection: true,
|
||||
);
|
||||
|
||||
dio = Dio(options);
|
||||
dio = Dio(options)
|
||||
|
||||
/// fix 第三方登录 302重定向 跟iOS代理问题冲突
|
||||
..httpClientAdapter = Http2Adapter(
|
||||
ConnectionManager(
|
||||
idleTimeout: const Duration(milliseconds: 10000),
|
||||
onClientCreate: (_, config) => config.onBadCertificate = (_) => true,
|
||||
),
|
||||
);
|
||||
|
||||
//添加拦截器
|
||||
dio.interceptors.add(ApiInterceptor());
|
||||
@ -120,23 +125,15 @@ class Request {
|
||||
/*
|
||||
* get请求
|
||||
*/
|
||||
get(url, {data, cacheOptions, options, cancelToken, extra}) async {
|
||||
get(url, {data, options, cancelToken, extra}) async {
|
||||
Response response;
|
||||
Options options;
|
||||
String ua = 'pc';
|
||||
ResponseType resType = ResponseType.json;
|
||||
if (extra != null) {
|
||||
ua = extra!['ua'] ?? 'pc';
|
||||
resType = extra!['resType'] ?? ResponseType.json;
|
||||
}
|
||||
if (cacheOptions != null) {
|
||||
cacheOptions.headers = {'user-agent': headerUa(ua)};
|
||||
options = cacheOptions;
|
||||
} else {
|
||||
options = Options();
|
||||
options.headers = {'user-agent': headerUa(ua)};
|
||||
options.responseType = resType;
|
||||
}
|
||||
options = Options();
|
||||
options.responseType = resType;
|
||||
try {
|
||||
response = await dio.get(
|
||||
url,
|
||||
@ -203,15 +200,14 @@ class Request {
|
||||
token.cancel("cancelled");
|
||||
}
|
||||
|
||||
String headerUa(ua) {
|
||||
String headerUa() {
|
||||
String headerUa = '';
|
||||
if (ua == 'mob') {
|
||||
headerUa = Platform.isIOS
|
||||
? 'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1'
|
||||
: 'Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.91 Mobile Safari/537.36';
|
||||
if (Platform.isIOS) {
|
||||
headerUa =
|
||||
'Mozilla/5.0 (iPhone; CPU iPhone OS 14_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Mobile/15E148 Safari/604.1';
|
||||
} else {
|
||||
headerUa =
|
||||
'Mozilla/5.0 (Macintosh; Intel Mac OS X 13_3_1) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.4 Safari/605.1.15';
|
||||
'Mozilla/5.0 (Linux; Android 10; SM-G975F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Mobile Safari/537.36';
|
||||
}
|
||||
return headerUa;
|
||||
}
|
||||
|
Reference in New Issue
Block a user