http.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import 'package:auto_route/auto_route.dart';
  2. import 'package:dio/dio.dart';
  3. import 'package:eitc_erm_dental_flutter/app_router.gr.dart';
  4. import 'package:eitc_erm_dental_flutter/funcs.dart';
  5. import 'package:eitc_erm_dental_flutter/http/api_exception.dart';
  6. import 'package:eitc_erm_dental_flutter/http/api_response.dart';
  7. import 'package:eitc_erm_dental_flutter/http/gzkj_response.dart';
  8. import 'package:eitc_erm_dental_flutter/http/token_interceptor.dart';
  9. import 'package:eitc_erm_dental_flutter/main.dart';
  10. import 'package:pretty_dio_logger/pretty_dio_logger.dart';
  11. class Http {
  12. static Http? _instance;
  13. static Http get instance => _instance ??= Http._();
  14. late Dio _dio;
  15. Http._() {
  16. _dio = Dio(BaseOptions(
  17. //本地开发
  18. //baseUrl: "http://192.168.3.142:8081",
  19. //测试服
  20. //baseUrl: "http://39.105.121.97:8081",
  21. //生产环境
  22. baseUrl: "http://106.74.15.131:8081",
  23. connectTimeout: const Duration(seconds: 10),
  24. sendTimeout: Duration.zero,
  25. receiveTimeout: Duration.zero,
  26. ));
  27. _dio.interceptors.addAll([
  28. TokenInterceptor(),
  29. PrettyDioLogger(
  30. requestHeader: true, requestBody: true, responseHeader: true),
  31. ]);
  32. }
  33. ///返回dio对象
  34. Dio get dio => _dio;
  35. Future<T?> request<T>(Future<ApiResponse<T>> func) async {
  36. try {
  37. ApiResponse<T> response = await func;
  38. if (response.isSuccess) {
  39. return response.data;
  40. }
  41. //401:Token失效,403:无效的Token
  42. if (response.code == 401 || response.code == 403) {
  43. //清除数据
  44. logoutClearData();
  45. if (navigatorKey.currentState != null &&
  46. navigatorKey.currentState!.mounted) {
  47. logd("token失效,跳转到登录页面");
  48. popAllRoutes(navigatorKey.currentState!.context);
  49. navigatorKey.currentState!.context
  50. .pushRoute(LoginRoute(cancelable: false));
  51. showToast(text: getS().tokenInvalidHint);
  52. return response.data;
  53. }
  54. }
  55. throw ApiException(response.code, response.msg);
  56. } catch (e) {
  57. if (e is! ApiException) {
  58. loge("请求异常", error: e);
  59. }
  60. throw ApiException.from(e);
  61. }
  62. }
  63. ///上传
  64. Future<ApiResponse<T>> upload<T>(String path,
  65. {Object? data,
  66. Map<String, dynamic>? queryParameters,
  67. Options? options,
  68. CancelToken? cancelToken,
  69. ProgressCallback? onSendProgress,
  70. ProgressCallback? onReceiveProgress,
  71. required T Function(Object? json) fromJsonT}) async {
  72. Response response = await _dio.post(path,
  73. data: data,
  74. queryParameters: queryParameters,
  75. options: options,
  76. cancelToken: cancelToken,
  77. onSendProgress: onSendProgress,
  78. onReceiveProgress: onReceiveProgress);
  79. return ApiResponse.fromJson(response.data, fromJsonT);
  80. }
  81. ///上传到国中康健接口
  82. Future<GzkjResponse<T>> uploadGzkj<T>(String path,
  83. {Object? data,
  84. Map<String, dynamic>? queryParameters,
  85. Options? options,
  86. CancelToken? cancelToken,
  87. ProgressCallback? onSendProgress,
  88. ProgressCallback? onReceiveProgress,
  89. required T Function(Object? json) fromJsonT}) async {
  90. Response response = await _dio.post(path,
  91. data: data,
  92. queryParameters: queryParameters,
  93. options: options,
  94. cancelToken: cancelToken,
  95. onSendProgress: onSendProgress,
  96. onReceiveProgress: onReceiveProgress);
  97. return GzkjResponse.fromJson(response.data, fromJsonT);
  98. }
  99. ///下载
  100. Future<Response<dynamic>> download<T>(
  101. String urlPath,
  102. dynamic savePath, {
  103. ProgressCallback? onReceiveProgress,
  104. Map<String, dynamic>? queryParameters,
  105. CancelToken? cancelToken,
  106. bool deleteOnError = true,
  107. String lengthHeader = Headers.contentLengthHeader,
  108. Object? data,
  109. Options? options,
  110. }) async {
  111. return _dio.download(urlPath, savePath,
  112. onReceiveProgress: onReceiveProgress,
  113. queryParameters: queryParameters,
  114. cancelToken: cancelToken,
  115. deleteOnError: deleteOnError,
  116. lengthHeader: lengthHeader,
  117. data: data,
  118. options: options);
  119. }
  120. }