Blame view

lib/network/network_manager.dart 2.28 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';
ea2c8205   liangchengyou   feat:更新代码
6
  import 'package:wow_english/models/response_model.dart';
a117a5a3   liangchengyou   feat:更新代码
7
  import 'package:wow_english/network/basic_configuration.dart';
2a29701f   liangchengyou   feat:提交代码
8
  
23384a42   liangchengyou   feat:登陆请求
9
  
2a29701f   liangchengyou   feat:提交代码
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
  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:更新代码
30
      options.baseUrl = BasicConfigurationManager().baseUrl??'';
2a29701f   liangchengyou   feat:提交代码
31
32
      options.connectTimeout = const Duration(milliseconds: 1000);
      options.receiveTimeout = const Duration(milliseconds: 1000);
a117a5a3   liangchengyou   feat:更新代码
33
      options.headers['content-type'] = 'application/x-www-form-urlencoded';
2a29701f   liangchengyou   feat:提交代码
34
35
36
37
38
39
      return options;
    }
  
    Future<void> requestData<T>(
        String path, {
          data,
ea2c8205   liangchengyou   feat:更新代码
40
          // HttpMethod method = HttpMethod.post,
2a29701f   liangchengyou   feat:提交代码
41
42
43
44
45
          Map<String, dynamic>? queryParameters,
          ProgressCallback? onSendProgress,
          ProgressCallback? onReceiveProgress,
          required Function successCallBack,
          required Function errorCallBack,
ea2c8205   liangchengyou   feat:更新代码
46
          required HttpMethod method,
2a29701f   liangchengyou   feat:提交代码
47
48
        }) async{
      try {
a117a5a3   liangchengyou   feat:更新代码
49
50
51
        Map<String, dynamic> headers = <String, dynamic>{};
  
        if (method == HttpMethod.post) {
23384a42   liangchengyou   feat:登陆请求
52
          headers['content-type'] = 'application/json';
a117a5a3   liangchengyou   feat:更新代码
53
        }
2a29701f   liangchengyou   feat:提交代码
54
55
56
57
58
        Response<dynamic> response;
        response = await _dio.request(
            path,
            data: data??{},
            queryParameters: queryParameters,
a117a5a3   liangchengyou   feat:更新代码
59
            options: Options(method: method.name,headers: headers),
2a29701f   liangchengyou   feat:提交代码
60
61
            onSendProgress: onSendProgress,
            onReceiveProgress: onReceiveProgress);
23384a42   liangchengyou   feat:登陆请求
62
63
64
65
66
67
68
69
70
71
        if (response.statusCode == HttpStatus.ok || response.statusCode == HttpStatus.created) {
          if (kDebugMode) {
            print(response.data);
          }
          final ResponseModel model = ResponseModel.fromJson(response.data);
          if (model.code != 200) {
            errorCallBack(model.msg);
          } else {
            successCallBack(response.data);
          }
2a29701f   liangchengyou   feat:提交代码
72
73
74
        } else {
          errorCallBack('请求失败');
        }
478f2c8c   liangchengyou   feat:更新插件
75
76
77
78
79
      } on DioError catch(error) {
        EasyLoading.dismiss();
        if (kDebugMode) {
          print(error);
        }
2a29701f   liangchengyou   feat:提交代码
80
81
82
83
        rethrow;
      }
    }
  }