From 5c6e772effea0f1a87604573953c11f0561f6f51 Mon Sep 17 00:00:00 2001 From: Cxx0822 <1556464090@qq.com> Date: Tue, 25 Nov 2025 15:42:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=E6=9B=B4=E6=96=B0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/log_utils.dart | 40 +++++++++++++++++++++++++++++----------- pubspec.lock | 24 ++++++++++++++++++++++++ pubspec.yaml | 37 +------------------------------------ 3 files changed, 54 insertions(+), 47 deletions(-) diff --git a/lib/utils/log_utils.dart b/lib/utils/log_utils.dart index f571317..dd745a5 100644 --- a/lib/utils/log_utils.dart +++ b/lib/utils/log_utils.dart @@ -1,12 +1,30 @@ -import 'package:logger/logger.dart'; +import 'dart:convert'; +import 'dart:io'; -// 全局日志实例,在整个项目中共享 -final Logger logger = Logger( - printer: PrettyPrinter( - methodCount: 1, - colors: true, - dateTimeFormat: DateTimeFormat.dateAndTime, - ), - // 可选:配置输出到文件(需配合文件操作库) - // output: FileOutput(file: File('logs/app.log')), -); \ No newline at end of file +import 'package:logger/logger.dart'; +import 'package:path_provider/path_provider.dart'; + +// 全局日志实例 +late Logger logger; + +// 初始化日志 +Future initLogger() async { + final directory = await getExternalStorageDirectory(); + final logFile = File('${directory?.path}/logs/app.log'); + + // 确保文件存在(会自动创建) + await logFile.create(recursive: true); + + logger = Logger( + filter: ProductionFilter(), + printer: SimplePrinter(printTime: true), + output: MultiOutput([ + ConsoleOutput(), + FileOutput( + file: logFile, + overrideExisting: true, + encoding: utf8 + ), + ]), + ); +} diff --git a/pubspec.lock b/pubspec.lock index a6f9b08..8c3fd00 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -552,6 +552,30 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "1.9.1" + path_provider: + dependency: "direct main" + description: + name: path_provider + sha256: "50c5dd5b6e1aaf6fb3a78b33f6aa3afca52bf903a8a5298f53101fdaee55bbcd" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.1.5" + path_provider_android: + dependency: transitive + description: + name: path_provider_android + sha256: "3b4c1fc3aa55ddc9cd4aa6759984330d5c8e66aa7702a6223c61540dc6380c37" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.2.19" + path_provider_foundation: + dependency: transitive + description: + name: path_provider_foundation + sha256: "16eef174aacb07e09c351502740fa6254c165757638eba1e9116b0a781201bbd" + url: "https://pub.flutter-io.cn" + source: hosted + version: "2.4.2" path_provider_linux: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 457bac4..71b06af 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,36 +1 @@ -name: flutter_common -description: "Flutter公共包" - -version: 1.0.0+1 - -environment: - sdk: '>=3.0.0 <4.0.0' - flutter: '>=3.0.0' - -dependencies: - flutter: - sdk: flutter - cupertino_icons: ^1.0.8 - shared_preferences: ^2.3.0 - intl: ^0.19.0 - provider: ^6.1.1 - syncfusion_flutter_charts: ^30.1.41 - dio: ^5.7.0 - logger: ^2.6.0 - json_annotation: ^4.9.0 - minio: ^3.5.8 - crypto: ^3.0.7 - file_picker: ^10.3.3 - fluttertoast: ^8.2.2 - flutter_image_compress: ^2.4.0 - awesome_dialog: ^3.3.0 - -dev_dependencies: - flutter_test: - sdk: flutter - flutter_lints: ^5.0.0 - build_runner: ^2.4.5 - json_serializable: ^6.7.1 - -flutter: - uses-material-design: true +name: flutter_common description: "Flutter公共包" version: 1.0.0+1 environment: sdk: '>=3.0.0 <4.0.0' flutter: '>=3.0.0' dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.8 shared_preferences: ^2.3.0 intl: ^0.19.0 provider: ^6.1.1 syncfusion_flutter_charts: ^30.1.41 dio: ^5.7.0 logger: ^2.6.0 json_annotation: ^4.9.0 minio: ^3.5.8 crypto: ^3.0.7 file_picker: ^10.3.3 fluttertoast: ^8.2.2 flutter_image_compress: ^2.4.0 awesome_dialog: ^3.3.0 path_provider: ^2.1.1 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^5.0.0 build_runner: ^2.4.5 json_serializable: ^6.7.1 flutter: uses-material-design: true \ No newline at end of file From c0f12dab7d38d2eea5493e0b3e3cb7422c76c73a Mon Sep 17 00:00:00 2001 From: Cxx0822 <1556464090@qq.com> Date: Tue, 25 Nov 2025 19:33:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=E6=9B=B4=E6=96=B0=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=B1=BB=E8=BF=94=E5=9B=9E=E7=9A=84=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/utils/http_utils.dart | 6 +++--- lib/utils/log_utils.dart | 36 ++++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/utils/http_utils.dart b/lib/utils/http_utils.dart index 0787503..6420a2b 100644 --- a/lib/utils/http_utils.dart +++ b/lib/utils/http_utils.dart @@ -108,7 +108,7 @@ class HttpUtil { // 没有转换器时尝试直接返回(可能不安全,建议提供转换器) return response.data; } catch (e) { - rethrow; + throw Exception(_handleError(e)); } } @@ -171,7 +171,7 @@ class HttpUtil { ); // 错误处理 - void _handleError(dynamic error) { + String _handleError(dynamic error) { String errorMessage = '未知错误'; if (error is DioException) { switch (error.type) { @@ -218,7 +218,7 @@ class HttpUtil { } logger.e(errorMessage); - // showErrorToast(errorMessage); + return errorMessage; } String _getHttpErrorMessage(int statusCode) { diff --git a/lib/utils/log_utils.dart b/lib/utils/log_utils.dart index dd745a5..4ec27aa 100644 --- a/lib/utils/log_utils.dart +++ b/lib/utils/log_utils.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'dart:io'; +import 'package:flutter/foundation.dart'; import 'package:logger/logger.dart'; import 'package:path_provider/path_provider.dart'; @@ -9,22 +10,25 @@ late Logger logger; // 初始化日志 Future initLogger() async { - final directory = await getExternalStorageDirectory(); - final logFile = File('${directory?.path}/logs/app.log'); + if (kIsWeb) { + logger = Logger( + printer: SimplePrinter(printTime: true), + output: ConsoleOutput(), + ); + } else { + final directory = await getExternalStorageDirectory(); + final logFile = File('${directory?.path}/logs/app.log'); - // 确保文件存在(会自动创建) - await logFile.create(recursive: true); + // 确保文件存在(会自动创建) + await logFile.create(recursive: true); - logger = Logger( - filter: ProductionFilter(), - printer: SimplePrinter(printTime: true), - output: MultiOutput([ - ConsoleOutput(), - FileOutput( - file: logFile, - overrideExisting: true, - encoding: utf8 - ), - ]), - ); + logger = Logger( + filter: ProductionFilter(), + printer: SimplePrinter(printTime: true), + output: MultiOutput([ + ConsoleOutput(), + FileOutput(file: logFile, overrideExisting: true, encoding: utf8), + ]), + ); + } }