DioUtil.dart 3.4 KB

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