Blame view

lib/network/network_manager.dart 1.97 KB
2a29701f   liangchengyou   feat:提交代码
1
2
3
  import 'dart:io';
  
  import 'package:dio/dio.dart';
478f2c8c   liangchengyou   feat:更新插件
4
5
  import 'package:flutter/foundation.dart';
  import 'package:flutter_easyloading/flutter_easyloading.dart';
a117a5a3   liangchengyou   feat:更新代码
6
  import 'package:wow_english/network/basic_configuration.dart';
2a29701f   liangchengyou   feat:提交代码
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  
  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();
a117a5a3   liangchengyou   feat:更新代码
28
      options.baseUrl = BasicConfigurationManager().baseUrl??'';
2a29701f   liangchengyou   feat:提交代码
29
30
      options.connectTimeout = const Duration(milliseconds: 1000);
      options.receiveTimeout = const Duration(milliseconds: 1000);
a117a5a3   liangchengyou   feat:更新代码
31
      options.headers['content-type'] = 'application/x-www-form-urlencoded';
2a29701f   liangchengyou   feat:提交代码
32
33
34
35
36
37
38
39
40
41
42
43
44
45
      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 {
a117a5a3   liangchengyou   feat:更新代码
46
47
48
49
50
        Map<String, dynamic> headers = <String, dynamic>{};
  
        if (method == HttpMethod.post) {
          headers['content-type'] = 'application/x-www-form-urlencoded';
        }
2a29701f   liangchengyou   feat:提交代码
51
52
53
54
55
        Response<dynamic> response;
        response = await _dio.request(
            path,
            data: data??{},
            queryParameters: queryParameters,
a117a5a3   liangchengyou   feat:更新代码
56
            options: Options(method: method.name,headers: headers),
2a29701f   liangchengyou   feat:提交代码
57
58
59
60
61
62
63
64
            onSendProgress: onSendProgress,
            onReceiveProgress: onReceiveProgress);
        if (response.statusCode == HttpStatus.ok ||
            response.statusCode == HttpStatus.created) {
          successCallBack(response.data);
        } else {
          errorCallBack('请求失败');
        }
478f2c8c   liangchengyou   feat:更新插件
65
66
67
68
69
      } on DioError catch(error) {
        EasyLoading.dismiss();
        if (kDebugMode) {
          print(error);
        }
2a29701f   liangchengyou   feat:提交代码
70
71
72
73
        rethrow;
      }
    }
  }