// Author: fengshengxiong
// Date: 2024/5/9
// Description: dio拦截器

import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:hello_wallpaper/models/base_resp_model.dart';

class DioInterceptor extends Interceptor {
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if (response.data is! ResponseBody) {
      if (response.statusCode == HttpStatus.ok) {
        BaseRespModel baseRespModel = BaseRespModel();
        if (response.data != null && response.data is Map) {
          baseRespModel = BaseRespModel.fromJson(response.data);
        } else {
          try {
            baseRespModel = BaseRespModel.fromJson(jsonDecode(response.data));
          } catch (e) {
            baseRespModel.data = response.data;
          }
        }
        response.data = baseRespModel;
        return handler.resolve(response);
      }
    }
    super.onResponse(response, handler);
  }
}
