http 初始化

This commit is contained in:
guozhigq
2023-04-18 11:28:59 +08:00
parent aaccfe4542
commit 13aab2e20f
11 changed files with 385 additions and 1 deletions

7
lib/http/api.dart Normal file
View File

@ -0,0 +1,7 @@
class Api {
// 推荐视频
static const String recommendList = '/x/web-interface/index/top/feed/rcmd';
// 视频详情
// 竖屏 https://api.bilibili.com/x/web-interface/view?aid=527403921
static const String videoDetail = '/x/web-interface/view';
}

4
lib/http/constants.dart Normal file
View File

@ -0,0 +1,4 @@
class HttpString {
static const String baseUrl = 'https://www.bilibili.com';
static const String baseApiUrl = 'https://api.bilibili.com';
}

181
lib/http/init.dart Normal file
View File

@ -0,0 +1,181 @@
// ignore_for_file: avoid_print
import 'dart:developer';
import 'dart:io';
import 'dart:async';
import 'package:dio/dio.dart';
import 'package:cookie_jar/cookie_jar.dart';
import 'package:pilipala/utils/utils.dart';
import 'package:pilipala/http/constants.dart';
import 'package:pilipala/http/interceptor.dart';
import 'package:dio_http2_adapter/dio_http2_adapter.dart';
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
class Request {
static final Request _instance = Request._internal();
factory Request() => _instance;
static Dio dio = Dio()
..httpClientAdapter = Http2Adapter(
ConnectionManager(
idleTimeout: const Duration(milliseconds: 10000),
// Ignore bad certificate
onClientCreate: (_, config) => config.onBadCertificate = (_) => true,
),
);
/// 设置cookie
static setCookie() async {
var cookiePath = await Utils.getCookiePath();
var cookieJar = PersistCookieJar(
ignoreExpires: true,
storage: FileStorage(cookiePath),
);
dio.interceptors.add(CookieManager(cookieJar));
var cookie = await CookieManager(cookieJar)
.cookieJar
.loadForRequest(Uri.parse(HttpString.baseUrl));
if (cookie.isEmpty) {
try {
await Request().get(HttpString.baseUrl);
} catch (e) {
log("setCookie, ${e.toString()}");
}
}
}
/*
* config it and create
*/
Request._internal() {
//BaseOptions、Options、RequestOptions 都可以配置参数,优先级别依次递增,且可以根据优先级别覆盖参数
BaseOptions options = BaseOptions(
//请求基地址,可以包含子路径
baseUrl: HttpString.baseApiUrl,
//连接服务器超时时间,单位是毫秒.
connectTimeout: const Duration(milliseconds: 12000),
//响应流上前后两次接受到数据的间隔,单位为毫秒。
receiveTimeout: const Duration(milliseconds: 12000),
//Http请求头.
// headers: {
// 'cookie': '',
// },
);
dio.options = options;
//添加拦截器
dio.interceptors
..add(ApiInterceptor())
// 日志拦截器 输出请求、响应内容
..add(LogInterceptor(
request: false,
requestHeader: false,
responseHeader: false,
));
dio.transformer = BackgroundTransformer();
dio.options.validateStatus = (status) {
return status! >= 200 && status < 300 || status == 304 || status == 302;
};
}
/*
* get请求
*/
get(url, {data, cacheOptions, 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;
}
try {
response = await dio.get(
url,
queryParameters: data,
options: options,
cancelToken: cancelToken,
);
return response;
} on DioError catch (e) {
print('get error: $e');
return Future.error(ApiInterceptor.dioError(e));
}
}
/*
* post请求
*/
post(url, {data, options, cancelToken, extra}) async {
print('post-data: $data');
Response response;
try {
response = await dio.post(
url,
data: data,
options: options,
cancelToken: cancelToken,
);
print('post success: ${response.data}');
return response;
} on DioError catch (e) {
print('post error: $e');
return Future.error(ApiInterceptor.dioError(e));
}
}
/*
* 下载文件
*/
downloadFile(urlPath, savePath) async {
Response response;
try {
response = await dio.download(urlPath, savePath,
onReceiveProgress: (int count, int total) {
//进度
// print("$count $total");
});
print('downloadFile success: ${response.data}');
return response.data;
} on DioError catch (e) {
print('downloadFile error: $e');
return Future.error(ApiInterceptor.dioError(e));
}
}
/*
* 取消请求
*
* 同一个cancel token 可以用于多个请求当一个cancel token取消时所有使用该cancel token的请求都会被取消。
* 所以参数可选
*/
void cancelRequests(CancelToken token) {
token.cancel("cancelled");
}
String headerUa(ua) {
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';
} else {
headerUa =
'Mozilla/5.0 (MaciMozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36';
}
return headerUa;
}
}

71
lib/http/interceptor.dart Normal file
View File

@ -0,0 +1,71 @@
import 'package:dio/dio.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
class ApiInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
// print("请求之前");
// 在请求之前添加头部或认证信息
// options.headers['Authorization'] = 'Bearer token';
// options.headers['Content-Type'] = 'application/json';
handler.next(options);
}
@override
void onResponse(Response response, ResponseInterceptorHandler handler) {
// print("响应之前");
handler.next(response);
}
@override
void onError(DioError err, ErrorInterceptorHandler handler) {
// 处理网络请求错误
handler.next(err);
super.onError(err, handler);
}
static Future dioError(DioError error) async {
switch (error.type) {
case DioErrorType.badCertificate:
return '证书有误!';
case DioErrorType.badResponse:
return '服务器异常,请稍后重试!';
case DioErrorType.cancel:
return "请求已被取消,请重新请求";
case DioErrorType.connectionError:
return '连接错误,请检查网络设置';
case DioErrorType.connectionTimeout:
return "网络连接超时,请检查网络设置";
case DioErrorType.receiveTimeout:
return "响应超时,请稍后重试!";
case DioErrorType.sendTimeout:
return "发送请求超时,请检查网络设置";
case DioErrorType.unknown:
var res = await checkConect();
return "$res 网络异常,请稍后重试!";
default:
return "Dio异常";
}
}
static Future<dynamic> checkConect() async {
final connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
return 'connected with mobile network';
} else if (connectivityResult == ConnectivityResult.wifi) {
return 'connected with wifi network';
} else if (connectivityResult == ConnectivityResult.ethernet) {
// I am connected to a ethernet network.
} else if (connectivityResult == ConnectivityResult.vpn) {
// I am connected to a vpn network.
// Note for iOS and macOS:
// There is no separate network interface type for [vpn].
// It returns [other] on any device (also simulator)
} else if (connectivityResult == ConnectivityResult.other) {
// I am connected to a network which is not in the above mentioned networks.
} else if (connectivityResult == ConnectivityResult.none) {
return 'not connected to any network';
}
}
}

View File

@ -1,11 +1,13 @@
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:dynamic_color/dynamic_color.dart';
import 'package:pilipala/http/init.dart';
import 'package:pilipala/router/app_pages.dart';
import 'package:pilipala/pages/main/view.dart';
void main() {
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Request.setCookie();
runApp(const MyApp());
}

16
lib/utils/utils.dart Normal file
View File

@ -0,0 +1,16 @@
// 工具函数
import 'dart:io';
import 'package:path_provider/path_provider.dart';
class Utils {
static Future<String> getCookiePath() async {
Directory tempDir = await getApplicationSupportDirectory();
String tempPath = "${tempDir.path}/.plpl/";
Directory dir = Directory(tempPath);
bool b = await dir.exists();
if (!b) {
dir.createSync(recursive: true);
}
return tempPath;
}
}