import 'dart:io'; import 'package:dio/dio.dart'; import 'package:qhd_prevention/constants/app_enums.dart'; import 'package:qhd_prevention/http/HttpManager.dart'; import 'package:qhd_prevention/http/ApiService.dart'; class FileApi { /// 单文件上传 static Future> uploadFile( String imagePath, UploadFileType fileEnum, String foreignKey, ) async { final file = File(imagePath); if (!await file.exists()) { throw ApiException('file_not_found', '图片不存在:$imagePath'); } final fileName = file.path.split(Platform.pathSeparator).last; return HttpManager().uploadImages( baseUrl: ApiService.basePath, path: '/basicInfo/imgFiles/save', fromData: { 'foreignKey': foreignKey, 'type': fileEnum.type, 'path': fileEnum.path, 'files': await MultipartFile.fromFile(file.path, filename: fileName), }, ); } /// 多文件上传 static Future> uploadFiles( List imagePaths, UploadFileType fileEnum, String hiddenId, ) async { List files = []; for (int i = 0; i < imagePaths.length; i++) { final file = File(imagePaths[i]); if (!await file.exists()) { throw ApiException('file_not_found', '图片不存在:${imagePaths[i]}'); } final fileName = file.path.split(Platform.pathSeparator).last; files.add(await MultipartFile.fromFile(file.path, filename: fileName)); } return HttpManager().uploadImages( baseUrl: ApiService.basePath, path: '/basicInfo/imgFiles/batchSave', fromData: { 'foreignKey': hiddenId, 'type': fileEnum.type, 'path': fileEnum.path, 'files': files, }, ); } /// 删除已上传图片 static Future> deleteImage(String path) { return HttpManager().request( ApiService.basePath, '/basicInfo/imgFiles/delete?filePath=$path', method: Method.delete, data: { }, ); } /// 删除多图 static Future> deleteImages(List ids) { return HttpManager().request( ApiService.basePath, '/basicInfo/imgFiles/ids?ids=${ids.join(",")}', method: Method.delete, data: { }, ); } /// 获取图片路径 static Future> getImagePath(String id, UploadFileType fileEnum) { return HttpManager().request( ApiService.basePath, '/basicInfo/imgFiles/listAll', method: Method.get, data: { "eqForeignKey": id, "eqType": fileEnum.type, }, ); } /// 获取图片路径 static Future> getImagePathWithType(String eqForeignKey,String inForeignKey, UploadFileType typeEnum) { return HttpManager().request( ApiService.basePath, '/basicInfo/imgFiles/listAll', method: Method.get, data: { /// 外键id "eqForeignKey": eqForeignKey, /// 外键ids,多个逗号分割 "inForeignKey": inForeignKey, "eqType": typeEnum.type, }, ); } }