base_service.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'dart:io';
  2. import 'package:dio/dio.dart';
  3. import 'package:sprintf/sprintf.dart';
  4. const String BASE_URL = "https://www.test.com/api";
  5. /// 请求配置类
  6. class HTTPConfig {
  7. static const baseURL = BASE_URL;
  8. static const connectTimeout = 5000;
  9. static const receiveTimeout = 10000;
  10. static int _channelType = 0;
  11. /// 获取平台信息
  12. static int getChannelType() {
  13. if (_channelType != 0) {
  14. return _channelType;
  15. }
  16. if (Platform.isAndroid) {
  17. _channelType = 1;
  18. } else {
  19. _channelType = 2;
  20. }
  21. return _channelType;
  22. }
  23. /// HTTP标准文件请求
  24. static Dio fileDio() {
  25. BaseOptions options =
  26. BaseOptions(baseUrl: baseURL, connectTimeout: connectTimeout);
  27. Dio dio = Dio(options);
  28. Interceptor dInter = InterceptorsWrapper(
  29. onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
  30. print(sprintf("Dio request file to [%s].", [options.uri.toString()]));
  31. return handler.next(options);
  32. }, onResponse: (Response response, ResponseInterceptorHandler handler) {
  33. print(sprintf("Dio request file to [%s] replied with http status %d.",
  34. [response.requestOptions.uri.toString(), response.statusCode]));
  35. return handler.next(response);
  36. }, onError: (DioError error, ErrorInterceptorHandler handler) {
  37. print(sprintf("Dio request file to [%s] failed. %s",
  38. [error.requestOptions.uri.toString(), error.toString()]));
  39. return handler.next(error);
  40. });
  41. dio.interceptors.addAll([dInter]);
  42. return dio;
  43. }
  44. /// HTTP标准请求
  45. static Dio defaultDio() {
  46. BaseOptions options = BaseOptions(
  47. baseUrl: baseURL,
  48. connectTimeout: connectTimeout,
  49. receiveTimeout: receiveTimeout);
  50. Dio dio = Dio(options);
  51. Interceptor dInter = InterceptorsWrapper(
  52. onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
  53. print(sprintf("Dio request to [%s].", [options.uri.toString()]));
  54. return handler.next(options);
  55. }, onResponse: (Response response, ResponseInterceptorHandler handler) {
  56. print(sprintf("Dio request to [%s] replied with http status %d. %s", [
  57. response.requestOptions.uri.toString(),
  58. response.statusCode,
  59. response.toString()
  60. ]));
  61. return handler.next(response);
  62. }, onError: (DioError error, ErrorInterceptorHandler handler) {
  63. print(sprintf("Dio request to [%s] failed. %s",
  64. [error.requestOptions.uri.toString(), error.toString()]));
  65. return handler.next(error);
  66. });
  67. dio.interceptors.addAll([dInter]);
  68. return dio;
  69. }
  70. }
  71. /// 数据请求基类
  72. class BaseService {
  73. static final Dio dio = HTTPConfig.defaultDio();
  74. static final Dio fileDio = HTTPConfig.fileDio();
  75. /// HTTP请求数据
  76. Future<T?> request<T>(String url,
  77. {String method = 'get',
  78. dynamic data,
  79. Map<String, String>? headers}) async {
  80. final options = Options(method: method);
  81. try {
  82. if (null != headers && headers.length > 0) {
  83. if (null == options.headers) {
  84. options.headers = {};
  85. }
  86. headers.forEach((key, value) {
  87. options.headers![key] = value;
  88. });
  89. }
  90. Response response =
  91. await dio.request<T>(url, data: data, options: options);
  92. return response.data;
  93. } on DioError {
  94. return null;
  95. }
  96. }
  97. /// HTTP请求文件
  98. Future<Response?> requestFile(String url,
  99. {String method = 'get',
  100. dynamic data,
  101. ProgressCallback? onReceiveProgress,
  102. Map<String, String>? headers}) async {
  103. final options = Options(
  104. method: method,
  105. responseType: ResponseType.bytes,
  106. followRedirects: false,
  107. );
  108. try {
  109. if (null != headers && headers.length > 0) {
  110. if (null == options.headers) {
  111. options.headers = {};
  112. }
  113. headers.forEach((key, value) {
  114. options.headers![key] = value;
  115. });
  116. }
  117. Response response = await fileDio.request(url,
  118. data: data, onReceiveProgress: onReceiveProgress, options: options);
  119. return response;
  120. } on DioError {
  121. return null;
  122. }
  123. }
  124. }