123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- 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<T?> request<T>(String url,
- {String method = 'get',
- dynamic data,
- Map<String, String>? 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<T>(url, data: data, options: options);
- return response.data;
- } on DioError {
- return null;
- }
- }
- /// HTTP请求文件
- Future<Response?> requestFile(String url,
- {String method = 'get',
- dynamic data,
- ProgressCallback? onReceiveProgress,
- Map<String, String>? 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;
- }
- }
- }
|