import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:platform/platform.dart'; import '../../constants/api.dart'; import '../../utils/toast/cloud_toast.dart'; import 'base_list_model.dart'; import 'base_model.dart'; import 'net_interceptor.dart'; final ApiClient apiClient = ApiClient(); class ApiClient { late Dio _dio; Dio get dio => _dio; ApiClient() { var headers = {}; if (kIsWeb) { headers = {'device-type': 'web'}; } else { headers = { 'device-type': const LocalPlatform().operatingSystem.toLowerCase() }; } _dio = Dio() ..options.baseUrl = API.baseURL ..options.headers = headers ..interceptors.add(NetworkInterceptor()); } ///设置token Dio setToken(String token) { return _dio..options.headers[API.tokenKey] = token; } ///清除token Dio clearToken() { return _dio ..options.headers.removeWhere((key, value) => key == API.tokenKey); } ///发送请求 Future request( String path, { dynamic data, bool showMessage = false, }) async { try { var response = await _dio.post(path, data: data); var baseModel = BaseModel.fromJson(response.data); if (showMessage) CloudToast.show(baseModel.msg); return baseModel; } catch (e) { if (e is DioError) { return BaseModel.dioErr(e); } } return BaseModel.unknown(); } ///请求列表 Future requestList( String path, { dynamic data, bool showMessage = false, }) async { var response = await _dio.post(path, data: data); var baseModel = BaseListModel.fromJson(response.data); if (showMessage) CloudToast.show(baseModel.msg); return baseModel; } // ///上传图片 // Future uploadImage(File file) async { // var response = await _dio.post( // API.file.uploadImage, // data: FormData.fromMap( // {'image': await MultipartFile.fromFile(file.path)}, // ), // ); // var model = BaseModel.fromJson(response.data); // return model.data['path']; // } // Future> uploadFiles( // List files, // ) async { // List urls = []; // if (files.isEmpty) { // return []; // } else { // for (var item in files) { // String path = await uploadImage(item); // if (path.isNotEmpty) { // urls.add(path); // } // } // } // return urls; // } }