import 'package:dio/dio.dart'; import 'package:get/get.dart'; import 'package:get_storage/get_storage.dart'; import 'package:vbvs_app/common/util/MyUtils.dart'; import 'package:vbvs_app/controller/login/login_controller.dart'; import 'package:vbvs_app/pages/common/selectDialog.dart'; class ApiService { static Dio dio = Dio(); static Dio request = Dio(); static Dio requestNoInfo = Dio(); static Dio requestNoError = Dio(); //不处理错误的请求,用于设备操作上报 static Dio reservation = Dio(); static void init() { // 添加全局配置,如基础URL、 // 拦截器等 // dio.httpClientAdapter = IOHttpClientAdapter( // createHttpClient: () { // final client = HttpClient(); // client.badCertificateCallback = // (X509Certificate cert, String host, int port) { // return true; // Verify the certificate. // }; // return client; // }, // ); // request.options.baseUrl = CommonVariables.apiUrl; // request.options.connectTimeout = const Duration(seconds: 15); // request.options.receiveTimeout = const Duration(seconds: 10); // request.options.sendTimeout = const Duration(seconds: 10); // dio.options.headers['Content-Type'] = "application/json"; // dio.options.headers['Token'] = "1"; // 添加其他配置... // 添加拦截器(可选) // request.interceptors.add(InterceptorsWrapper( // onRequest: (options, handler) { // print("---> ${options.method}, ${options.path}, ${options.data}"); // options.headers['token'] = Get.find().model.token; // print("${options.headers['token']}"); // return handler.next(options); // }, // onResponse: (response, ResponseInterceptorHandler handler) { // // 在响应处理前的操作,例如解析响应数据 // // if (response.realUri.toString().contains("/login")) { // // String? token = response.headers.value('token'); // // if (token != null) { // // print("token = ${token}"); // // Get.find().token.value = token; // // } // // } // print("<--- ${response.statusCode}, ${response.data}"); // if (response.data is Map) { // if (response.data['code'] == HttpStatusCodes.ok) { // return handler.next(response); // } else { // if (response.data['code'] == 10011) { // Get.offAndToNamed( // "/loginPage", // ); // GlobalController globalController = Get.find(); // globalController.resetParmAll(); // showCustomConfirmDialog(Get.context!, "当前用户已在别处登录,请确认账号密码安全!", // btnName: "确定", icon: ConfirmDialogIcon.warn) // .then((d) { // if (d == "confirm") { // Get.back(); // } // }); // } else if (response.data['code'] == 11004) { // showCustomConfirmAndCancelDialog(Get.context!, "当前用户已登录,是否重新登录", // confirmName: "确定", cancelName: "取消") // .then((d) async { // if (d == "confirm") { // LoginController loginController = Get.find(); // loginController.model.forceLogin = 1; // String msg = await loginController.login(Get.context!); // if (msg == null || msg.isEmpty) { // Get.offAndToNamed("/mianPageBottomChange"); // } // } else if (d == "cancel") { // // Get.offAndToNamed( // // "/loginPage", // // ); // Get.back(); // } // }); // } else if (response.data['code'] == 10009 || // response.data['code'] == 11007 || // response.data['code'] == 11008) { // // 有可能对象获取不到,所以try catch包住 // try { // GlobalController globalController = // Get.find(); // globalController.resetParmAll(); // UserInfoController userInfoController = // Get.find(); // userInfoController.model.token = null; // userInfoController.model.user = null; // userInfoController.model.login = 0; // userInfoController.model.message = 0; // LoginController loginController = Get.find(); // loginController.model.account = null; // loginController.model.password = null; // loginController.model.phone = null; // loginController.model.code = null; // } catch (e) { // print(e); // } // final box = GetStorage(); // box.remove('user'); // box.remove('token'); // showToast(response.data["msg"]); // } else if (response.data['code'] == 11005) { // if (response.data['code'] == 11005) { // var authUserData = response.data['data']; // AuthBindTelController authBindTelController = Get.find(); // authBindTelController.model.authUserInfo = authUserData; // Get.toNamed("/auth_bind_tel"); // } // } else { // showToast(response.data["msg"]); // } // return handler.reject( // DioException( // requestOptions: RequestOptions(data: response.data['msg']), // message: response.data['msg']), // ); // } // } // return handler.next(response); // }, // onError: (e, handler) { // // 错误处理,例如打印错误信息 // showToast("网络异常或服务连接异常,请稍候再试", color: color_error); // print("DioError: $e"); // return handler.reject(e); // }, // )); // requestNoInfo.options.baseUrl = CommonVariables.apiUrl; // requestNoInfo.options.connectTimeout = const Duration(seconds: 15); // requestNoInfo.options.receiveTimeout = const Duration(seconds: 10); // requestNoInfo.options.sendTimeout = const Duration(seconds: 10); // requestNoInfo.interceptors.add(InterceptorsWrapper( // onRequest: (options, handler) { // print("---> ${options.method}, ${options.path}, ${options.data}"); // options.headers['token'] = Get.find().model.token; // print("${options.headers['token']}"); // return handler.next(options); // }, // onResponse: (response, ResponseInterceptorHandler handler) { // // 在响应处理前的操作,例如解析响应数据 // // if (response.realUri.toString().contains("/login")) { // // String? token = response.headers.value('token'); // // if (token != null) { // // print("token = ${token}"); // // Get.find().token.value = token; // // } // // } // print("<--- ${response.statusCode}, ${response.data}"); // if (response.data is Map) { // if (response.data['code'] == HttpStatusCodes.ok) { // return handler.next(response); // } else { // // showToast(response.data["msg"]); // return handler.reject( // DioException( // requestOptions: RequestOptions(data: response.data['msg']), // message: response.data['msg'], // ), // ); // } // } // return handler.next(response); // }, // onError: (e, handler) { // // 错误处理,例如打印错误信息 // showToast("网络异常或服务连接异常,请稍候再试", color: color_error); // print("DioError: $e"); // return handler.reject(e); // }, // )); // requestNoError.options.baseUrl = CommonVariables.apiUrl; // requestNoError.options.connectTimeout = const Duration(seconds: 5); // requestNoError.options.receiveTimeout = const Duration(seconds: 3); // requestNoError.options.sendTimeout = const Duration(seconds: 3); // requestNoError.interceptors.add(InterceptorsWrapper( // onRequest: (options, handler) { // print( // "requestNoError---> ${options.method}, ${options.path}, ${options.data}"); // options.headers['token'] = Get.find().model.token; // print("${options.headers['token']}"); // return handler.next(options); // }, // onResponse: (response, ResponseInterceptorHandler handler) { // print("requestNoError<--- ${response.statusCode}, ${response.data}"); // return handler.next(response); // }, // onError: (e, handler) { // // 错误处理,例如打印错误信息 // print("requestNoError-->DioError: $e"); // return handler.reject(e); // }, // )); reservationInit(); } static void reservationInit() { reservation.options.baseUrl = "https://crm-api.swes.com.cn"; reservation.options.connectTimeout = const Duration(seconds: 15); reservation.options.receiveTimeout = const Duration(seconds: 10); reservation.options.sendTimeout = const Duration(seconds: 10); reservation.interceptors.add(InterceptorsWrapper( onRequest: (options, handler) { print("---> ${options.method}, ${options.path}, ${options.data}"); options.headers['token'] = "bMAQVR1x48t66u8EDYSftAJGo17r0rIB3z15JgyyoGz1rAEZHs1htHOCorYFJ2RT"; return handler.next(options); }, onResponse: (response, ResponseInterceptorHandler handler) { print("<--- ${response.statusCode} ${response.data}"); return handler.next(response); }, onError: (e, handler) { // 错误处理,例如打印错误信息 showToast("网络异常或服务连接异常,请稍候再试", color: color_error); print("DioError: $e"); return handler.reject(e); }, )); } }