Flutter 基于 json_serializable 的通用接口泛型封装

67 min read

参考

Flutter json_serializable 使用说明

请求响应基类

class ResponseEntity<T> {
  T? data;
  int? errorCode;
  String? errorMsg;

  ResponseEntity(
      {required this.data, required this.errorCode, required this.errorMsg});

  ResponseEntity.fromJson(Map<String, dynamic> json) {
    data = JsonConvert.fromJsonAsT<T>(json['data']);
    errorCode = json['code'];
    errorMsg = json['msg'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    if (this.data != null) {
      data['data'] = this.data;
    }
    data['code'] = errorCode;
    data['msg'] = errorMsg;
    return data;
  }
}

请求配置类

class RequestConfig {
  static const String baseUrl = 'http://192.168.2.230:9203';

  // 请求超时
  static const int timeout = 5000;

  // 响应超时
  static const int receiveTimeout = 5000;

  // 是否开启调试模式
  static const bool isDebug = kDebugMode;

  // static const bool isDebug = false;

  // 代理地址
  static const String proxyUrl = '192.168.2.199:8888';
}

class ResponseCode {
  // 401:token过期
  static const int unauthorized = 401;
}

请求封装类

class Http {

  final String baseUrl = RequestConfig.baseUrl;

  // 连接超时时间
  final int timeout = RequestConfig.timeout;

  // 响应超时时间
  final int receiveTimeout = RequestConfig.receiveTimeout;

  static Http? _http;

  static Http getInstance() {
    return _http ??= Http._internal();
  }

  Dio dio = Dio();

  Http._internal() {
    dio.options.baseUrl = baseUrl;
    dio.options.connectTimeout = 5000;
    dio.options.receiveTimeout = 5000;
    // 拦截器
    dio.interceptors.add(
      InterceptorsWrapper(
        onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
          handler.next(options);
          print(
              "\n================================= 请求数据 =================================");
          print("method = ${options.method.toString()}");
          print("url = ${options.uri.toString()}");
          print("headers = ${options.headers}");
          print("params = ${options.queryParameters}");
        },
        onResponse: (Response response, ResponseInterceptorHandler handler) {
          print(
              "\n================================= 响应数据开始 =================================");
          print("code = ${response.statusCode}");
          print("data = ${response.data}");
          print(
              "================================= 响应数据结束 =================================\n");
          handler.next(response);
        },
        onError: (DioError error, ErrorInterceptorHandler handler) {
          print(
              "\n=================================错误响应数据 =================================");
          print("type = ${error.type}");
          print("message = ${error.message}");
          print("stackTrace = ${error.stackTrace}");
          print("\n");
          handler.next(error);
        },
      ),
    );
  }
  post<T>(
    api, {
    Map<String, dynamic>? queryParameters,
    required Map<String, dynamic> data,
  }) async {
    Response response =
        await dio.post(api, data: data, queryParameters: queryParameters);
    var a = ResponseEntity<T>.fromJson(response.data as Map<String, dynamic>);
    return a;
  }

  postForm(
    api, {
    Map<String, dynamic>? queryParameters,
    required Map<String, dynamic> data,
  }) async {
    Response res = await dio.post(api,
        data: FormData.fromMap(data), queryParameters: queryParameters);
    return res.data;
  }

  get<T>(
    api, {
    Map<String, dynamic>? queryParameters,
    required Map<String, dynamic> data,
  }) async {
    Response response = await dio.get(api, queryParameters: queryParameters);
    var a = ResponseEntity<T>.fromJson(response.data as Map<String, dynamic>);
    return a;
  }
}

Serive类

class MaterialService {
  static getCallbackIP() async {
    var res = await Http.getInstance()
        .get<CallbackIPEntity>('/official/base/callbackIP', queryParameters: {}, data: {});
    return res.data;
  }
}