import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:wow_english/models/response_model.dart'; import 'package:wow_english/network/basic_configuration.dart.txt'; enum HttpMethod { get, put, post, head, patch, delete, } class DioUtil { static final Dio _dio = getDefDio(); static Dio getDefDio() { Dio dio = Dio(); dio.options = getDefOptions(); return dio; } static BaseOptions getDefOptions() { final BaseOptions options = BaseOptions(); options.baseUrl = BasicConfigurationManager().baseUrl ?? ''; options.connectTimeout = const Duration(milliseconds: 1000); options.receiveTimeout = const Duration(milliseconds: 1000); return options; } Future requestData( HttpMethod method, String path, { data, Function? successCallBack, Function? errorCallBack, Map? queryParameters, ProgressCallback? onSendProgress, ProgressCallback? onReceiveProgress, }) async { try { Map headers = {}; if (method == HttpMethod.post) { headers['content-type'] = 'application/json'; } Response response; response = await _dio.request(path, data: data ?? {}, queryParameters: queryParameters, options: Options(method: method.name, headers: headers), onSendProgress: onSendProgress, onReceiveProgress: onReceiveProgress); int statusCode = response.statusCode ?? 0; if (statusCode >= 200 && statusCode < 300) { if (kDebugMode) { print(response.data); } final ResponseModel model = ResponseModel.fromJson(response.data); if (model.code == 200) { successCallBack?.call(response.data); } else { errorCallBack?.call(model.msg); } } else { errorCallBack?.call('请求失败'); } } on DioException catch (error) { if (kDebugMode) { print(error); } errorCallBack?.call("网络错误: ${error.message}"); } } }