DioUtil.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import 'package:deus_app/common/utils/ToastUtils.dart';
  2. import 'package:dio/dio.dart';
  3. /// 请求方法
  4. enum DioMethod {
  5. get,
  6. post,
  7. put,
  8. delete,
  9. patch,
  10. head,
  11. }
  12. class DioUtil {
  13. /// 单例模式
  14. static DioUtil? _instance;
  15. factory DioUtil() => _instance ?? DioUtil._internal();
  16. static DioUtil? get instance => _instance ?? DioUtil._internal();
  17. /// 连接超时时间
  18. static const int connectTimeout = 60 * 1000;
  19. /// 响应超时时间
  20. static const int receiveTimeout = 60 * 1000;
  21. /// Dio实例
  22. static Dio _dio = Dio();
  23. /// 初始化
  24. DioUtil._internal() {
  25. // 初始化基本选项
  26. Map<String, String> map={
  27. // 'Bearer Token':''
  28. };
  29. BaseOptions options = BaseOptions(
  30. baseUrl: 'https://fqgz.flowbb.cn:7070/system-api/',
  31. headers: map,
  32. connectTimeout: connectTimeout,
  33. receiveTimeout: receiveTimeout);
  34. _instance = this;
  35. // 初始化dio
  36. _dio = Dio(options);
  37. // 添加拦截器
  38. _dio.interceptors.add(InterceptorsWrapper(
  39. onRequest: _onRequest, onResponse: _onResponse, onError: _onError));
  40. }
  41. /// 请求拦截器
  42. void _onRequest(RequestOptions options, RequestInterceptorHandler handler) {
  43. // 对非open的接口的请求参数全部增加userId
  44. if (!options.path.contains("open")) {
  45. options.queryParameters["userId"] = "xxx";
  46. }
  47. // 头部添加token
  48. options.headers["Token"] = "xxx";
  49. // 更多业务需求
  50. handler.next(options);
  51. // super.onRequest(options, handler);
  52. }
  53. /// 相应拦截器
  54. void _onResponse(
  55. Response response, ResponseInterceptorHandler handler) async {
  56. // 请求成功是对数据做基本处理
  57. if (response.statusCode == 200) {
  58. // ....
  59. handler.next(response);
  60. } else {
  61. // ....
  62. // showToast(response.statusMessage);
  63. }
  64. if (response.requestOptions.baseUrl.contains("???????")) {
  65. // 对某些单独的url返回数据做特殊处理
  66. }
  67. }
  68. /// 错误处理
  69. void _onError(DioError error, ErrorInterceptorHandler handler) {
  70. handler.next(error);
  71. showToast(error.message);
  72. }
  73. /// 请求类
  74. Future<T> request<T>(
  75. String path, {
  76. DioMethod method = DioMethod.get,
  77. Map<String, dynamic>? params,
  78. data,
  79. CancelToken? cancelToken,
  80. Options? options,
  81. ProgressCallback? onSendProgress,
  82. ProgressCallback? onReceiveProgress,
  83. }) async {
  84. const _methodValues = {
  85. DioMethod.get: 'get',
  86. DioMethod.post: 'post',
  87. DioMethod.put: 'put',
  88. DioMethod.delete: 'delete',
  89. DioMethod.patch: 'patch',
  90. DioMethod.head: 'head'
  91. };
  92. options ??= Options(method: _methodValues[method]);
  93. try {
  94. Response response;
  95. response = await _dio.request(path,
  96. data: data,
  97. queryParameters: params,
  98. cancelToken: cancelToken,
  99. options: options,
  100. onSendProgress: onSendProgress,
  101. onReceiveProgress: onReceiveProgress);
  102. return response.data;
  103. } on DioError catch (e) {
  104. showToast(e.message);
  105. rethrow;
  106. }
  107. }
  108. /// 开启日志打印
  109. /// 需要打印日志的接口在接口请求前 DioUtil.instance?.openLog();
  110. void openLog() {
  111. _dio.interceptors
  112. .add(LogInterceptor(responseHeader: false, responseBody: true));
  113. }
  114. }