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] =?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), + ]), + ); + } }