Ver código fonte

[New]新增网络请求日志

zhipeng 1 ano atrás
pai
commit
815b73da35
4 arquivos alterados com 117 adições e 109 exclusões
  1. 1 1
      lib/data/api/atmob_api.dart
  2. 107 99
      lib/data/api/network_module.dart
  3. 8 8
      pubspec.lock
  4. 1 1
      pubspec.yaml

+ 1 - 1
lib/data/api/atmob_api.dart

@@ -20,4 +20,4 @@ abstract class AtmobApi {
 }
 
 // final atmobApi = AtmobApi(defaultDio, baseUrl: 'https://api.atmob.com');
-final atmobApi = AtmobApi(defaultDio, baseUrl: 'http://192.168.10.171:8880');
+final atmobApi = AtmobApi(defaultDio, baseUrl: 'http://192.168.10.230:8880');

+ 107 - 99
lib/data/api/network_module.dart

@@ -1,101 +1,109 @@
 import 'package:dio/dio.dart';
 
-final defaultDio = Dio(BaseOptions(
-
-    /// 发送数据的超时设置。
-    ///
-    /// 超时时会抛出类型为 [DioExceptionType.sendTimeout] 的
-    /// [DioException]。
-    ///
-    /// `null` 或 `Duration.zero` 即不设置超时。
-    //Duration ? sendTimeout;
-
-    /// 接收数据的超时设置。
-    ///
-    /// 这里的超时对应的时间是:
-    ///  - 在建立连接和第一次收到响应数据事件之前的超时。
-    ///  - 每个数据事件传输的间隔时间,而不是接收的总持续时间。
-    ///
-    /// 超时时会抛出类型为 [DioExceptionType.receiveTimeout] 的
-    /// [DioException]。
-    ///
-    /// `null` 或 `Duration.zero` 即不设置超时。
-    //Duration ? receiveTimeout;
-
-    /// 可以在 [Interceptor]、[Transformer] 和
-    /// [Response.requestOptions] 中获取到的自定义对象。
-    //Map<String, dynamic> ? extra;
-
-    /// HTTP 请求头。
-    ///
-    /// 请求头的键是否相等的判断大小写不敏感的。
-    /// 例如:`content-type` 和 `Content-Type` 会视为同样的请求头键。
-    //Map<String, dynamic> ? headers;
-
-    /// 是否保留请求头的大小写。
-    ///
-    /// 默认值为 false。
-    ///
-    /// 该选项在以下场景无效:
-    ///  - XHR 不支持直接处理。
-    ///  - 按照 HTTP/2 的标准,只支持小写请求头键。
-    //bool ? preserveHeaderCase;
-
-    /// 表示 [Dio] 处理请求响应数据的类型。
-    ///
-    /// 默认值为 [ResponseType.json]。
-    /// [Dio] 会在请求响应的 content-type
-    /// 为 [Headers.jsonContentType] 时自动将响应字符串处理为 JSON 对象。
-    ///
-    /// 在以下情况时,分别使用:
-    ///  - `plain` 将数据处理为 `String`;
-    ///  - `bytes` 将数据处理为完整的 bytes。
-    ///  - `stream` 将数据处理为流式返回的二进制数据;
-    //ResponseType? responseType;
-
-    /// 请求的 content-type。
-    ///
-    /// 请求默认的 `content-type` 会由 [ImplyContentTypeInterceptor]
-    /// 根据发送数据的类型推断。它可以通过
-    /// [Interceptors.removeImplyContentTypeInterceptor] 移除。
-    //String? contentType;
-
-    /// 判断当前返回的状态码是否可以视为请求成功。
-    //ValidateStatus? validateStatus;
-
-    /// 是否在请求失败时仍然获取返回数据内容。
-    ///
-    /// 默认为 true。
-    //bool? receiveDataWhenStatusError;
-
-    /// 参考 [HttpClientRequest.followRedirects]。
-    ///
-    /// 默认为 true。
-    //bool? followRedirects;
-
-    /// 当 [followRedirects] 为 true 时,指定的最大重定向次数。
-    /// 如果请求超出了重定向次数上线,会抛出 [RedirectException]。
-    ///
-    /// 默认为 5。
-    //int? maxRedirects;
-
-    /// 参考 [HttpClientRequest.persistentConnection]。
-    ///
-    /// 默认为 true。
-    //bool? persistentConnection;
-
-    /// 对请求内容进行自定义编码转换。
-    ///
-    /// 默认为 [Utf8Encoder]。
-    //RequestEncoder? requestEncoder;
-
-    /// 对请求响应内容进行自定义解码转换。
-    ///
-    /// 默认为 [Utf8Decoder]。
-    //ResponseDecoder? responseDecoder;
-
-    /// 当请求参数以 `x-www-url-encoded` 方式发送时,如何处理集合参数。
-    ///
-    /// 默认为 [ListFormat.multi]。
-    //ListFormat? listFormat;
-    ));
+class _NetworkModule {
+  static Dio _createDefaultDio() {
+    Dio dio = Dio(BaseOptions(
+
+        /// 发送数据的超时设置。
+        ///
+        /// 超时时会抛出类型为 [DioExceptionType.sendTimeout] 的
+        /// [DioException]。
+        ///
+        /// `null` 或 `Duration.zero` 即不设置超时。
+        //Duration ? sendTimeout;
+
+        /// 接收数据的超时设置。
+        ///
+        /// 这里的超时对应的时间是:
+        ///  - 在建立连接和第一次收到响应数据事件之前的超时。
+        ///  - 每个数据事件传输的间隔时间,而不是接收的总持续时间。
+        ///
+        /// 超时时会抛出类型为 [DioExceptionType.receiveTimeout] 的
+        /// [DioException]。
+        ///
+        /// `null` 或 `Duration.zero` 即不设置超时。
+        //Duration ? receiveTimeout;
+
+        /// 可以在 [Interceptor]、[Transformer] 和
+        /// [Response.requestOptions] 中获取到的自定义对象。
+        //Map<String, dynamic> ? extra;
+
+        /// HTTP 请求头。
+        ///
+        /// 请求头的键是否相等的判断大小写不敏感的。
+        /// 例如:`content-type` 和 `Content-Type` 会视为同样的请求头键。
+        //Map<String, dynamic> ? headers;
+
+        /// 是否保留请求头的大小写。
+        ///
+        /// 默认值为 false。
+        ///
+        /// 该选项在以下场景无效:
+        ///  - XHR 不支持直接处理。
+        ///  - 按照 HTTP/2 的标准,只支持小写请求头键。
+        //bool ? preserveHeaderCase;
+
+        /// 表示 [Dio] 处理请求响应数据的类型。
+        ///
+        /// 默认值为 [ResponseType.json]。
+        /// [Dio] 会在请求响应的 content-type
+        /// 为 [Headers.jsonContentType] 时自动将响应字符串处理为 JSON 对象。
+        ///
+        /// 在以下情况时,分别使用:
+        ///  - `plain` 将数据处理为 `String`;
+        ///  - `bytes` 将数据处理为完整的 bytes。
+        ///  - `stream` 将数据处理为流式返回的二进制数据;
+        //ResponseType? responseType;
+
+        /// 请求的 content-type。
+        ///
+        /// 请求默认的 `content-type` 会由 [ImplyContentTypeInterceptor]
+        /// 根据发送数据的类型推断。它可以通过
+        /// [Interceptors.removeImplyContentTypeInterceptor] 移除。
+        //String? contentType;
+
+        /// 判断当前返回的状态码是否可以视为请求成功。
+        //ValidateStatus? validateStatus;
+
+        /// 是否在请求失败时仍然获取返回数据内容。
+        ///
+        /// 默认为 true。
+        //bool? receiveDataWhenStatusError;
+
+        /// 参考 [HttpClientRequest.followRedirects]。
+        ///
+        /// 默认为 true。
+        //bool? followRedirects;
+
+        /// 当 [followRedirects] 为 true 时,指定的最大重定向次数。
+        /// 如果请求超出了重定向次数上线,会抛出 [RedirectException]。
+        ///
+        /// 默认为 5。
+        //int? maxRedirects;
+
+        /// 参考 [HttpClientRequest.persistentConnection]。
+        ///
+        /// 默认为 true。
+        //bool? persistentConnection;
+
+        /// 对请求内容进行自定义编码转换。
+        ///
+        /// 默认为 [Utf8Encoder]。
+        //RequestEncoder? requestEncoder;
+
+        /// 对请求响应内容进行自定义解码转换。
+        ///
+        /// 默认为 [Utf8Decoder]。
+        //ResponseDecoder? responseDecoder;
+
+        /// 当请求参数以 `x-www-url-encoded` 方式发送时,如何处理集合参数。
+        ///
+        /// 默认为 [ListFormat.multi]。
+        //ListFormat? listFormat;
+        ));
+    dio.interceptors.add(LogInterceptor(requestBody: true, responseBody: true));
+    return dio;
+  }
+}
+
+final defaultDio = _NetworkModule._createDefaultDio();

+ 8 - 8
pubspec.lock

@@ -347,14 +347,6 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "3.0.0"
-  logger:
-    dependency: "direct main"
-    description:
-      name: logger
-      sha256: "697d067c60c20999686a0add96cf6aba723b3aa1f83ecf806a8097231529ec32"
-      url: "https://pub.dev"
-    source: hosted
-    version: "2.4.0"
   logging:
     dependency: transitive
     description:
@@ -427,6 +419,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "1.5.1"
+  pretty_dio_logger:
+    dependency: "direct main"
+    description:
+      name: pretty_dio_logger
+      sha256: "36f2101299786d567869493e2f5731de61ce130faa14679473b26905a92b6407"
+      url: "https://pub.dev"
+    source: hosted
+    version: "1.4.0"
   pub_semver:
     dependency: transitive
     description:

+ 1 - 1
pubspec.yaml

@@ -39,7 +39,7 @@ dependencies:
   # 网络请求
   dio: ^5.6.0
   retrofit: '>=4.0.0 <5.0.0'
-  logger: any
+  pretty_dio_logger: ^1.4.0
   json_annotation: ^4.8.1
 
   # 包信息