Blame view

lib/common/request/request_client.dart 4.31 KB
056970d8   Key   feat: api
1
2
3
4
5
  import 'dart:convert';
  
  import 'package:dio/dio.dart';
  import 'package:pretty_dio_logger/pretty_dio_logger.dart';
  
1892df31   Key   优化接口调用
6
  import '../core/user_util.dart';
056970d8   Key   feat: api
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  import 'api_response/api_response_entity.dart';
  import 'api_response/raw_data.dart';
  import 'config.dart';
  import 'exception.dart';
  import 'token_interceptor.dart';
  
  RequestClient requestClient = RequestClient();
  
  class RequestClient {
    late Dio _dio;
  
    RequestClient() {
      _dio = Dio(BaseOptions(baseUrl: RequestConfig.baseUrl, connectTimeout: RequestConfig.connectTimeout));
      _dio.interceptors.add(TokenInterceptor());
e12dbc82   Key   user module
21
      _dio.interceptors.add(PrettyDioLogger(requestHeader: true, requestBody: true));
056970d8   Key   feat: api
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
    }
  
    Future<T?> request<T>(
      String url, {
      required String method,
      Map<String, dynamic>? queryParameters,
      data,
      Map<String, dynamic>? headers,
      Function(ApiResponse<T>)? onResponse,
      bool Function(ApiException)? onError,
    }) async {
      try {
        Options options = Options()
          ..method = method
          ..headers = headers;
  
        data = _convertRequestData(data);
  
        Response response = await _dio.request(url, queryParameters: queryParameters, data: data, options: options);
  
        return _handleResponse<T>(response, onResponse);
      } catch (e) {
1892df31   Key   优化接口调用
44
45
46
47
        if ((e as ApiException?)?.code == 405) {
          UserUtil.logout();
          return null;
        }
056970d8   Key   feat: api
48
49
50
51
52
        var exception = ApiException.from(e);
        if (onError?.call(exception) != true) {
          throw exception;
        }
      }
056970d8   Key   feat: api
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
      return null;
    }
  
    _convertRequestData(data) {
      if (data != null) {
        data = jsonDecode(jsonEncode(data));
      }
      return data;
    }
  
    /// get
    Future<T?> get<T>(
      String url, {
      Map<String, dynamic>? queryParameters,
      Map<String, dynamic>? headers,
      bool showLoading = true,
      Function(ApiResponse<T>)? onResponse,
      bool Function(ApiException)? onError,
    }) {
      return request(url,
          method: 'GET', queryParameters: queryParameters, headers: headers, onResponse: onResponse, onError: onError);
    }
  
    /// post
    Future<T?> post<T>(
      String url, {
      Map<String, dynamic>? queryParameters,
      data,
      Map<String, dynamic>? headers,
      bool showLoading = true,
      Function(ApiResponse<T>)? onResponse,
      bool Function(ApiException)? onError,
    }) {
      return request(url,
          method: "POST",
          queryParameters: queryParameters,
          data: data,
          headers: headers,
          onResponse: onResponse,
          onError: onError);
    }
  
    /// delete
    Future<T?> delete<T>(
      String url, {
      Map<String, dynamic>? queryParameters,
      data,
      Map<String, dynamic>? headers,
      bool showLoading = true,
      Function(ApiResponse<T>)? onResponse,
      bool Function(ApiException)? onError,
    }) {
      return request(url,
          method: "DELETE",
          queryParameters: queryParameters,
          data: data,
          headers: headers,
          onResponse: onResponse,
          onError: onError);
    }
  
    /// put
    Future<T?> put<T>(
      String url, {
      Map<String, dynamic>? queryParameters,
      data,
      Map<String, dynamic>? headers,
      bool showLoading = true,
      Function(ApiResponse<T>)? onResponse,
      bool Function(ApiException)? onError,
    }) {
      return request(url,
          method: "PUT",
          queryParameters: queryParameters,
          data: data,
          headers: headers,
          onResponse: onResponse,
          onError: onError);
    }
  
    /// 请求响应内容处理
    T? _handleResponse<T>(
      Response response,
      Function(ApiResponse<T>)? onResponse,
    ) {
      int statusCode = response.statusCode ?? -1;
056970d8   Key   feat: api
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
      // 200..299 成功响应
      if (statusCode >= 200 && statusCode <= 299) {
        if (T.toString() == (RawData).toString()) {
          RawData raw = RawData();
          raw.value = response.data;
          return raw as T;
        } else {
          ApiResponse<T> apiResponse = ApiResponse<T>.fromJson(response.data);
          onResponse?.call(apiResponse);
          return _handleBusinessResponse<T>(apiResponse);
        }
      } else {
        var exception = ApiException(response.statusCode, ApiException.unknownException);
        throw exception;
      }
    }
  
    /// 业务内容处理
    T? _handleBusinessResponse<T>(ApiResponse<T> response) {
      if (response.code == RequestConfig.successCode) {
        return response.data;
      } else {
        var exception = ApiException(response.code, response.msg);
        throw exception;
      }
    }
  }