import 'dart:io'; import 'package:dio/dio.dart'; import 'package:sprintf/sprintf.dart'; const String BASE_URL = "https://www.test.com/api"; /// 请求配置类 class HTTPConfig { static const baseURL = BASE_URL; static const connectTimeout = 5000; static const receiveTimeout = 10000; static int _channelType = 0; /// 获取平台信息 static int getChannelType() { if (_channelType != 0) { return _channelType; } if (Platform.isAndroid) { _channelType = 1; } else { _channelType = 2; } return _channelType; } /// HTTP标准文件请求 static Dio fileDio() { BaseOptions options = BaseOptions(baseUrl: baseURL, connectTimeout: connectTimeout); Dio dio = Dio(options); Interceptor dInter = InterceptorsWrapper( onRequest: (RequestOptions options, RequestInterceptorHandler handler) { print(sprintf("Dio request file to [%s].", [options.uri.toString()])); return handler.next(options); }, onResponse: (Response response, ResponseInterceptorHandler handler) { print(sprintf("Dio request file to [%s] replied with http status %d.", [response.requestOptions.uri.toString(), response.statusCode])); return handler.next(response); }, onError: (DioError error, ErrorInterceptorHandler handler) { print(sprintf("Dio request file to [%s] failed. %s", [error.requestOptions.uri.toString(), error.toString()])); return handler.next(error); }); dio.interceptors.addAll([dInter]); return dio; } /// HTTP标准请求 static Dio defaultDio() { BaseOptions options = BaseOptions( baseUrl: baseURL, connectTimeout: connectTimeout, receiveTimeout: receiveTimeout); Dio dio = Dio(options); Interceptor dInter = InterceptorsWrapper( onRequest: (RequestOptions options, RequestInterceptorHandler handler) { print(sprintf("Dio request to [%s].", [options.uri.toString()])); return handler.next(options); }, onResponse: (Response response, ResponseInterceptorHandler handler) { print(sprintf("Dio request to [%s] replied with http status %d. %s", [ response.requestOptions.uri.toString(), response.statusCode, response.toString() ])); return handler.next(response); }, onError: (DioError error, ErrorInterceptorHandler handler) { print(sprintf("Dio request to [%s] failed. %s", [error.requestOptions.uri.toString(), error.toString()])); return handler.next(error); }); dio.interceptors.addAll([dInter]); return dio; } } /// 数据请求基类 class BaseService { static final Dio dio = HTTPConfig.defaultDio(); static final Dio fileDio = HTTPConfig.fileDio(); /// HTTP请求数据 Future request(String url, {String method = 'get', dynamic data, Map? headers}) async { final options = Options(method: method); try { if (null != headers && headers.length > 0) { if (null == options.headers) { options.headers = {}; } headers.forEach((key, value) { options.headers![key] = value; }); } Response response = await dio.request(url, data: data, options: options); return response.data; } on DioError { return null; } } /// HTTP请求文件 Future requestFile(String url, {String method = 'get', dynamic data, ProgressCallback? onReceiveProgress, Map? headers}) async { final options = Options( method: method, responseType: ResponseType.bytes, followRedirects: false, ); try { if (null != headers && headers.length > 0) { if (null == options.headers) { options.headers = {}; } headers.forEach((key, value) { options.headers![key] = value; }); } Response response = await fileDio.request(url, data: data, onReceiveProgress: onReceiveProgress, options: options); return response; } on DioError { return null; } } }