DioUtil.dart 3.3 KB

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