Files
FlutterUnit/lib/app/utils/http/interceptors/log_interceptor.dart
2020-06-19 09:59:57 +08:00

113 lines
3.0 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:convert';
import 'package:dio/dio.dart';
/// create by 张风捷特烈 on 2020/4/28
/// contact me by email 1981462002@qq.com
/// 说明:
///
class LogsInterceptors extends InterceptorsWrapper {
static bool debug = true;
static List<Map> sHttpResponses = new List<Map>();
static List<String> sResponsesHttpUrl = new List<String>();
static List<Map<String, dynamic>> sHttpRequest =
new List<Map<String, dynamic>>();
static List<String> sRequestHttpUrl = new List<String>();
static List<Map<String, dynamic>> sHttpError =
new List<Map<String, dynamic>>();
static List<String> sHttpErrorUrl = new List<String>();
@override
onRequest(RequestOptions options) async {
if (debug) {
print("请求url${options.path}");
print('请求头: ' + options.headers.toString());
if (options.data != null) {
print('请求参数: ' + options.data.toString());
}
}
try {
addLogic(sRequestHttpUrl, options.path ?? "");
var data;
if (options.data is Map) {
data = options.data;
} else {
data = Map<String, dynamic>();
}
var map = {
"header:": {...options.headers},
};
if (options.method == "POST") {
map["data"] = data;
}
addLogic(sHttpRequest, map);
} catch (e) {
print(e);
}
return options;
}
@override
onResponse(Response response) async {
if (debug) {
if (response != null) {
print('返回参数: ' + response.toString());
}
}
if (response.data is Map || response.data is List) {
try {
var data = Map<String, dynamic>();
data["data"] = response.data;
addLogic(sResponsesHttpUrl, response?.request?.uri?.toString() ?? "");
addLogic(sHttpResponses, data);
} catch (e) {
print(e);
}
} else if (response.data is String) {
try {
var data = Map<String, dynamic>();
data["data"] = response.data;
addLogic(sResponsesHttpUrl, response?.request?.uri.toString() ?? "");
addLogic(sHttpResponses, data);
} catch (e) {
print(e);
}
} else if (response.data != null) {
try {
String data = response.data.toJson();
addLogic(sResponsesHttpUrl, response?.request?.uri.toString() ?? "");
addLogic(sHttpResponses, json.decode(data));
} catch (e) {
print(e);
}
}
return response; // continue
}
@override
onError(DioError err) async {
if (debug) {
print('请求异常: ' + err.toString());
print('请求异常信息: ' + (err.response?.toString() ?? ""));
}
try {
addLogic(sHttpErrorUrl, err.request.path ?? "null");
var errors = Map<String, dynamic>();
errors["error"] = err.message;
addLogic(sHttpError, errors);
} catch (e) {
print(e);
}
return err; // continue;
}
static addLogic(List list, data) {
if (list.length > 20) {
list.removeAt(0);
}
list.add(data);
}
}