Blame view

lib/network/network_manager.dart 1.42 KB
2a29701f   liangchengyou   feat:提交代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  import 'dart:io';
  
  import 'package:dio/dio.dart';
  
  enum HttpMethod {
    get,
    put,
    post,
    head,
    patch,
    delete,
  }
  
  class DioUtil {
    static final Dio _dio = getDefDio();
  
    static Dio getDefDio() {
      Dio dio = Dio();
      dio.options = getDefOptions();
      return dio;
    }
  
    static BaseOptions getDefOptions() {
      final BaseOptions options = BaseOptions();
      options.baseUrl = '';
      options.connectTimeout = const Duration(milliseconds: 1000);
      options.receiveTimeout = const Duration(milliseconds: 1000);
      return options;
    }
  
    Future<void> requestData<T>(
        String path, {
          data,
          HttpMethod method = HttpMethod.get,
          Map<String, dynamic>? queryParameters,
          ProgressCallback? onSendProgress,
          ProgressCallback? onReceiveProgress,
          required Function successCallBack,
          required Function errorCallBack,
        }) async{
      try {
        Response<dynamic> response;
        response = await _dio.request(
            path,
            data: data??{},
            queryParameters: queryParameters,
            options: Options(method: method.name),
            onSendProgress: onSendProgress,
            onReceiveProgress: onReceiveProgress);
        if (response.statusCode == HttpStatus.ok ||
            response.statusCode == HttpStatus.created) {
          successCallBack(response.data);
        } else {
          errorCallBack('请求失败');
        }
      } on Error {
        rethrow;
      }
    }
  }