|
...
|
...
|
@@ -2,6 +2,8 @@ import 'dart:convert'; |
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:one_key/constant/error.dart';
|
|
|
|
import 'package:one_key/utils/utils.dart';
|
|
...
|
...
|
@@ -53,8 +55,10 @@ class HttpConfig { |
|
|
|
/// BaseResp [T data]字段 key, 默认:data.
|
|
|
|
String data;
|
|
|
|
|
|
|
|
// /// Options.
|
|
|
|
// Options options;
|
|
|
|
/// Options.
|
|
|
|
Options options;
|
|
|
|
BaseOptions options;
|
|
|
|
|
|
|
|
/// 详细使用请查看dio官网 https://github.com/flutterchina/dio/blob/flutter/README-ZH.md#Https证书校验.
|
|
|
|
/// PEM证书内容.
|
|
...
|
...
|
@@ -89,7 +93,8 @@ class DioUtil { |
|
|
|
String _dataKey = "data";
|
|
|
|
|
|
|
|
/// Options.
|
|
|
|
Options _options = getDefOptions();
|
|
|
|
// Options _options = getDefOptions();
|
|
|
|
BaseOptions _options = getDefOptions();
|
|
|
|
|
|
|
|
/// PEM证书内容.
|
|
|
|
String _pem;
|
|
...
|
...
|
@@ -126,7 +131,7 @@ class DioUtil { |
|
|
|
_dio.options.headers.addAll(_headers);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// set Config.
|
|
|
|
// / set Config.
|
|
|
|
void setConfig(HttpConfig config) {
|
|
|
|
_statusKey = config.status ?? _statusKey;
|
|
|
|
_codeKey = config.code ?? _codeKey;
|
|
...
|
...
|
@@ -136,27 +141,27 @@ class DioUtil { |
|
|
|
_pem = config.pem ?? _pem;
|
|
|
|
if (_dio != null) {
|
|
|
|
_dio.options = _options;
|
|
|
|
if (_pem != null) {
|
|
|
|
_dio.onHttpClientCreate = (HttpClient client) {
|
|
|
|
client.badCertificateCallback =
|
|
|
|
(X509Certificate cert, String host, int port) {
|
|
|
|
if (cert.pem == _pem) {
|
|
|
|
// 证书一致,则放行
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (_pKCSPath != null) {
|
|
|
|
_dio.onHttpClientCreate = (HttpClient client) {
|
|
|
|
SecurityContext sc = new SecurityContext();
|
|
|
|
//file为证书路径
|
|
|
|
sc.setTrustedCertificates(_pKCSPath, password: _pKCSPwd);
|
|
|
|
HttpClient httpClient = new HttpClient(context: sc);
|
|
|
|
return httpClient;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// if (_pem != null) {
|
|
|
|
// _dio.onHttpClientCreate = (HttpClient client) {
|
|
|
|
// client.badCertificateCallback =
|
|
|
|
// (X509Certificate cert, String host, int port) {
|
|
|
|
// if (cert.pem == _pem) {
|
|
|
|
// // 证书一致,则放行
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// };
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
// if (_pKCSPath != null) {
|
|
|
|
// _dio.onHttpClientCreate = (HttpClient client) {
|
|
|
|
// SecurityContext sc = new SecurityContext();
|
|
|
|
// //file为证书路径
|
|
|
|
// sc.setTrustedCertificates(_pKCSPath, password: _pKCSPwd);
|
|
|
|
// HttpClient httpClient = new HttpClient(context: sc);
|
|
|
|
// return httpClient;
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -222,7 +227,7 @@ class DioUtil { |
|
|
|
Loading.hideLoading(context);
|
|
|
|
return new Future.error(new DioError(
|
|
|
|
response: response,
|
|
|
|
message: "data parsing exception...",
|
|
|
|
error: "data parsing exception...",
|
|
|
|
type: DioErrorType.RESPONSE,
|
|
|
|
));
|
|
|
|
}
|
|
...
|
...
|
@@ -230,7 +235,7 @@ class DioUtil { |
|
|
|
Loading.hideLoading(context);
|
|
|
|
return new Future.error(new DioError(
|
|
|
|
response: response,
|
|
|
|
message: "statusCode: $response.statusCode, service error",
|
|
|
|
error: "statusCode: $response.statusCode, service error",
|
|
|
|
type: DioErrorType.RESPONSE,
|
|
|
|
));
|
|
|
|
} on DioError catch (error) {
|
|
...
|
...
|
@@ -290,14 +295,14 @@ class DioUtil { |
|
|
|
} catch (e) {
|
|
|
|
return new Future.error(new DioError(
|
|
|
|
response: response,
|
|
|
|
message: "data parsing exception...",
|
|
|
|
error: "data parsing exception...",
|
|
|
|
type: DioErrorType.RESPONSE,
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Future.error(new DioError(
|
|
|
|
response: response,
|
|
|
|
message: "statusCode: $response.statusCode, service error",
|
|
|
|
error: "statusCode: $response.statusCode, service error",
|
|
|
|
type: DioErrorType.RESPONSE,
|
|
|
|
));
|
|
|
|
}
|
|
...
|
...
|
@@ -309,13 +314,13 @@ class DioUtil { |
|
|
|
Future<Response> download(
|
|
|
|
String urlPath,
|
|
|
|
savePath, {
|
|
|
|
OnDownloadProgress onProgress,
|
|
|
|
ProgressCallback onProgress,
|
|
|
|
CancelToken cancelToken,
|
|
|
|
data,
|
|
|
|
Options options,
|
|
|
|
}) {
|
|
|
|
return _dio.download(urlPath, savePath,
|
|
|
|
onProgress: onProgress,
|
|
|
|
onReceiveProgress: onProgress,
|
|
|
|
cancelToken: cancelToken,
|
|
|
|
data: data,
|
|
|
|
options: options);
|
|
...
|
...
|
@@ -341,14 +346,14 @@ class DioUtil { |
|
|
|
}
|
|
|
|
|
|
|
|
/// merge Option.
|
|
|
|
void _mergeOption(Options opt) {
|
|
|
|
void _mergeOption(BaseOptions opt) {
|
|
|
|
_options.method = opt.method ?? _options.method;
|
|
|
|
_options.headers = (new Map.from(_options.headers))..addAll(opt.headers);
|
|
|
|
_options.baseUrl = opt.baseUrl ?? _options.baseUrl;
|
|
|
|
_options.connectTimeout = opt.connectTimeout ?? _options.connectTimeout;
|
|
|
|
_options.receiveTimeout = opt.receiveTimeout ?? _options.receiveTimeout;
|
|
|
|
_options.responseType = opt.responseType ?? _options.responseType;
|
|
|
|
_options.data = opt.data ?? _options.data;
|
|
|
|
// _options.data = opt.data ?? _options.data;
|
|
|
|
_options.extra = (new Map.from(_options.extra))..addAll(opt.extra);
|
|
|
|
_options.contentType = opt.contentType ?? _options.contentType;
|
|
|
|
_options.validateStatus = opt.validateStatus ?? _options.validateStatus;
|
|
...
|
...
|
@@ -374,13 +379,13 @@ class DioUtil { |
|
|
|
}
|
|
|
|
|
|
|
|
/// get Options Str.
|
|
|
|
String _getOptionsStr(Options request) {
|
|
|
|
String _getOptionsStr(path) {
|
|
|
|
return "method: " +
|
|
|
|
request.method +
|
|
|
|
_dio.options.method +
|
|
|
|
" baseUrl: " +
|
|
|
|
request.baseUrl +
|
|
|
|
_dio.options.baseUrl +
|
|
|
|
" path: " +
|
|
|
|
request.path;
|
|
|
|
path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// print Data Str.
|
|
...
|
...
|
@@ -403,17 +408,16 @@ class DioUtil { |
|
|
|
}
|
|
|
|
|
|
|
|
/// create new dio.
|
|
|
|
static Dio createNewDio([Options options]) {
|
|
|
|
static Dio createNewDio([BaseOptions options]) {
|
|
|
|
options = options ?? getDefOptions();
|
|
|
|
Dio dio = new Dio(options);
|
|
|
|
return dio;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// get Def Options.
|
|
|
|
static Options getDefOptions() {
|
|
|
|
Options options = new Options();
|
|
|
|
options.contentType =
|
|
|
|
ContentType.parse("application/x-www-form-urlencoded");
|
|
|
|
static BaseOptions getDefOptions() {
|
|
|
|
BaseOptions options = new BaseOptions();
|
|
|
|
options.contentType = 'application/json; charset=utf-8';
|
|
|
|
options.connectTimeout = 1000 * 10; //默认10s
|
|
|
|
options.receiveTimeout = 1000 * 10; //默认10s
|
|
|
|
return options;
|
...
|
...
|
|