fix: 第三方登录302重定向失效、ua获取
This commit is contained in:
@ -4,6 +4,7 @@ import 'dart:io';
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:cookie_jar/cookie_jar.dart';
|
import 'package:cookie_jar/cookie_jar.dart';
|
||||||
|
import 'package:dio_http2_adapter/dio_http2_adapter.dart';
|
||||||
import 'package:hive/hive.dart';
|
import 'package:hive/hive.dart';
|
||||||
import 'package:pilipala/utils/storage.dart';
|
import 'package:pilipala/utils/storage.dart';
|
||||||
import 'package:pilipala/utils/utils.dart';
|
import 'package:pilipala/utils/utils.dart';
|
||||||
@ -59,9 +60,6 @@ class Request {
|
|||||||
static Future<String> getCsrf() async {
|
static Future<String> getCsrf() async {
|
||||||
var cookies = await cookieManager.cookieJar
|
var cookies = await cookieManager.cookieJar
|
||||||
.loadForRequest(Uri.parse(HttpString.baseApiUrl));
|
.loadForRequest(Uri.parse(HttpString.baseApiUrl));
|
||||||
// for (var i in cookies) {
|
|
||||||
// print(i);
|
|
||||||
// }
|
|
||||||
String token = '';
|
String token = '';
|
||||||
if (cookies.where((e) => e.name == 'bili_jct').isNotEmpty) {
|
if (cookies.where((e) => e.name == 'bili_jct').isNotEmpty) {
|
||||||
token = cookies.firstWhere((e) => e.name == 'bili_jct').value;
|
token = cookies.firstWhere((e) => e.name == 'bili_jct').value;
|
||||||
@ -92,14 +90,21 @@ class Request {
|
|||||||
receiveTimeout: const Duration(milliseconds: 12000),
|
receiveTimeout: const Duration(milliseconds: 12000),
|
||||||
//Http请求头.
|
//Http请求头.
|
||||||
headers: {
|
headers: {
|
||||||
'keep-alive': true,
|
'keep-alive': 'true',
|
||||||
'user-agent': headerUa('pc'),
|
'user-agent': headerUa(),
|
||||||
'Accept-Encoding': 'gzip'
|
|
||||||
},
|
},
|
||||||
persistentConnection: true,
|
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());
|
dio.interceptors.add(ApiInterceptor());
|
||||||
@ -120,23 +125,15 @@ class Request {
|
|||||||
/*
|
/*
|
||||||
* get请求
|
* get请求
|
||||||
*/
|
*/
|
||||||
get(url, {data, cacheOptions, options, cancelToken, extra}) async {
|
get(url, {data, options, cancelToken, extra}) async {
|
||||||
Response response;
|
Response response;
|
||||||
Options options;
|
Options options;
|
||||||
String ua = 'pc';
|
|
||||||
ResponseType resType = ResponseType.json;
|
ResponseType resType = ResponseType.json;
|
||||||
if (extra != null) {
|
if (extra != null) {
|
||||||
ua = extra!['ua'] ?? 'pc';
|
|
||||||
resType = extra!['resType'] ?? ResponseType.json;
|
resType = extra!['resType'] ?? ResponseType.json;
|
||||||
}
|
}
|
||||||
if (cacheOptions != null) {
|
options = Options();
|
||||||
cacheOptions.headers = {'user-agent': headerUa(ua)};
|
options.responseType = resType;
|
||||||
options = cacheOptions;
|
|
||||||
} else {
|
|
||||||
options = Options();
|
|
||||||
options.headers = {'user-agent': headerUa(ua)};
|
|
||||||
options.responseType = resType;
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
response = await dio.get(
|
response = await dio.get(
|
||||||
url,
|
url,
|
||||||
@ -203,15 +200,14 @@ class Request {
|
|||||||
token.cancel("cancelled");
|
token.cancel("cancelled");
|
||||||
}
|
}
|
||||||
|
|
||||||
String headerUa(ua) {
|
String headerUa() {
|
||||||
String headerUa = '';
|
String headerUa = '';
|
||||||
if (ua == 'mob') {
|
if (Platform.isIOS) {
|
||||||
headerUa = Platform.isIOS
|
headerUa =
|
||||||
? '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 (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';
|
||||||
: 'Mozilla/5.0 (Linux; Android 11; Pixel 5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.91 Mobile Safari/537.36';
|
|
||||||
} else {
|
} else {
|
||||||
headerUa =
|
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;
|
return headerUa;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -41,7 +41,7 @@ class WebviewController extends GetxController {
|
|||||||
|
|
||||||
webviewInit() {
|
webviewInit() {
|
||||||
controller
|
controller
|
||||||
..setUserAgent(Request().headerUa('mob'))
|
..setUserAgent(Request().headerUa())
|
||||||
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
..setJavaScriptMode(JavaScriptMode.unrestricted)
|
||||||
..setNavigationDelegate(
|
..setNavigationDelegate(
|
||||||
NavigationDelegate(
|
NavigationDelegate(
|
||||||
|
|||||||
Reference in New Issue
Block a user