import 'package:dio/dio.dart';
import 'package:wallpaperx/common/utils/log_print.dart';

class Http {
  Dio dio = Dio(BaseOptions(
    connectTimeout: const Duration(milliseconds: 40000),
    receiveTimeout: const Duration(milliseconds: 40000),
    sendTimeout: const Duration(milliseconds: 40000),
  ));

  Http() {
    dio.interceptors.add(
      InterceptorsWrapper(
        onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
          return handler.next(options);
        },
        onResponse:
            (Response response, ResponseInterceptorHandler handler) async {
          int code = response.data['code'] ?? 200;
          switch (code) {
            case 401: // 重新登陆后请求
              LogPrint.d("e: ${response.data}");
              break;
          }

          return handler.next(response);
        },
        onError: (DioException e, ErrorInterceptorHandler handler) async {
          String url = e.response?.requestOptions.path ?? "";
          if (e.response?.data is String) {
            return handler.reject(e);
          } else if (e.response?.data is Map) {
            int code = e.response?.data["code"] ?? 200;
            switch (code) {
              case 401: // 重新登陆后请求
                LogPrint.d("e: ${e.response?.data}");
                break;
            }
            if (url.isNotEmpty) {
            }
          }
          return handler.next(e);
        },
      ),
    );
  }
}
