package cn.huge.module.file.controller.wechat; import cn.huge.base.common.exception.ClientException; import cn.huge.base.common.utils.ContentTypeUtils; import cn.huge.base.common.utils.ReturnFailUtils; import cn.huge.base.common.utils.ReturnSucUtils; import cn.huge.base.config.CurrentUser; import cn.huge.module.client.api.impl.CustClientImpl; import cn.huge.module.constant.BaseConsts; import cn.huge.module.cust.dto.CtUserDTO; import cn.huge.module.cust.dto.PaUserDTO; import cn.huge.module.file.domain.dto.UploaderDTO; import cn.huge.module.file.domain.po.FileInfo; import cn.huge.module.file.service.FileInfoService; import cn.huge.module.file.service.FileRelateService; import cn.huge.module.file.utils.FileUtils; import cn.huge.module.file.utils.FtpUtils; import cn.huge.module.sys.constant.FileBaseConsts; import cn.huge.module.sys.constant.FileOwnerTypeBaseEnum; import cn.huge.module.sys.dto.FileInfoBaseDTO; import cn.huge.module.sys.dto.IdcardOcrResultDTO; import cn.huge.module.utils.BaiduOcrUtils; import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; /** * @title: 附件信息表接口api-wechat端 * @description: 附件信息表接口api-wechat端 * @company: hugeinfo * @author: liyj * @time: 2024-08-28 20:06:18 * @version: 1.0.0 */ @Slf4j @RestController @RequestMapping("/api/wechat/fileInfo") public class FileInfoWechatController { @Autowired(required = false) private HttpServletRequest request; @Autowired private FileInfoService service; @Autowired private FileRelateService fileRelateService; @Autowired private CustClientImpl custClient; /** * 页面附件上传-文件流方式 * @url {ctx}/api/wechat/fileInfo/upload?mainId=&ownerId=&ownerType= * @param mainId 所属业务主体编号 * @param ownerId 所属业务编号 * @param ownerType 所属业务类型 * @param request 请求头 * @return Object */ @PostMapping("/upload") public Object upload(@RequestParam(value = "mainId") String mainId, @RequestParam(value = "ownerId") String ownerId, @RequestParam(value = "ownerType") String ownerType, @CurrentUser String userId, MultipartHttpServletRequest request) { try{ PaUserDTO loginUser = custClient.paClientGetUserAll(userId); UploaderDTO uploaderDTO = new UploaderDTO(); uploaderDTO.setUploaderId(loginUser.getId()); uploaderDTO.setUploaderName(loginUser.getTrueName()); uploaderDTO.setUploaderType(FileBaseConsts.UPLOADER_TYPE_1); uploaderDTO.setCustId(loginUser.getCustId()); List files = new ArrayList<>(); Iterator itr = request.getFileNames(); int fileCount = fileRelateService.countByOwnerIdAndType(ownerId, ownerType)+1; while (itr.hasNext()) { MultipartFile file = request.getFile(itr.next()); if(file.getSize() > 30000000){ return ReturnFailUtils.getRepInfo("您上传的文件已超过30M大小限制!"); }else { String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); if (FileUtils.checkFileSuffix(fileSuffix)) { if (StringUtils.isEmpty(ownerType)) { ownerType = FileOwnerTypeBaseEnum.OWNER_TYPE_000.getIndex(); } FileInfo fileInfo = service.wechatUploadFile(file, mainId, ownerId, ownerType, fileCount, uploaderDTO); FileInfoBaseDTO fileInfoBaseDTO = new FileInfoBaseDTO(); BeanUtils.copyProperties(fileInfo, fileInfoBaseDTO); // 如果是身份证,就调用ocr识别 if (FileOwnerTypeBaseEnum.OWNER_TYPE_202.getIndex().equals(ownerType)){ fileInfoBaseDTO.setIdcardOcrResult(BaiduOcrUtils.ocrIdcard(file.getBytes())); } files.add(fileInfoBaseDTO); fileCount++; } else { return ReturnFailUtils.getRepInfo("您上传的文件格式不正确!"); } } } return ReturnSucUtils.getRepInfo( "上传成功!", files); } catch (Exception e) { return ReturnFailUtils.getRepInfo(); } } /** * 在线查附件 * @url {ctx}/api/wechat/fileInfo/show/{id} * @param id 附件编号 * @param response 响应头 * @throws Exception */ @GetMapping("/show/{id}") public void show(@PathVariable(value = "id") String id, HttpServletResponse response){ try { FileInfo fileInfo = service.getById(id); response.reset(); String contentType = ContentTypeUtils.getContentType(fileInfo.getSuffix()); response.setContentType(contentType); OutputStream outputStream = response.getOutputStream(); FtpUtils ftpUtil = new FtpUtils(); InputStream inputStream = ftpUtil.retrieveFileStream(fileInfo.getFullPath()); // double fileSize = fileInfo.getSize(); // 文件大小 int chunkSize = 512 * 1024; // 分块大小( KB) byte[] buffer = new byte[chunkSize]; int bytesRead; // 循环读取和发送文件的各个部分 while ((bytesRead = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); outputStream.flush(); } // IOUtils.copy(inputStream, outputStream, chunkSize); //关闭流 IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } catch (Exception e) { log.error("controller接口[FileInfoWechatController.show]请求异常:"+e, e); throw new ClientException("FileInfoWechatController.show", e); } } /** * 在线下载附件-文件流方式 * @url {ctx}/api/wechat/fileInfo/down/{id} * @param id 附件编号 * @param response 响应头 */ @GetMapping("/down/{id}") public void down(@PathVariable(value = "id") String id, HttpServletResponse response){ try { FileInfo fileInfo = service.getById(id); String contentType = ContentTypeUtils.getContentType(fileInfo.getSuffix()); response.setContentType(contentType); String fileName = URLEncoder.encode(fileInfo.getName() + BaseConsts.DOT + fileInfo.getSuffix(), "UTF-8"); response.addHeader("Content-Disposition", "attachment;filename=" + fileName); OutputStream outputStream = response.getOutputStream(); FtpUtils ftpUtil = new FtpUtils(); InputStream inputStream = ftpUtil.retrieveFileStream(fileInfo.getFullPath()); IOUtils.copy(inputStream, outputStream); //关闭流 IOUtils.closeQuietly(inputStream); IOUtils.closeQuietly(outputStream); } catch (Exception e) { log.error("controller接口[FileInfoWechatController.down]请求异常:"+e, e); throw new ClientException("FileInfoWechatController.down", e); } } /** * 删除附件 * @url {ctx}/api/wechat/fileInfo/deleteFileById?id= * @param id 附件编号 * @return Object */ @GetMapping("/deleteFileById") public Object deleteFileById(@RequestParam(value = "id") String id) { try { service.deleteFileById(id); return ReturnSucUtils.getRepInfo(); } catch (Exception e) { log.error("controller接口[FileInfoWechatController.deleteFileById]请求异常:"+e, e); return ReturnFailUtils.getRepInfo(); } } /** * 根据多个所属编号查询附件 * @url {ctx}/api/wechat/fileInfo/listByMainId * @return Object */ @GetMapping("/listByMainId") public Object listByMainId(@RequestParam(value = "mainId") String mainId) { try { return ReturnSucUtils.getRepInfo(service.listByMainId(mainId)); } catch (Exception e) { log.error("controller接口[FileInfoWechatController.listByMainId]请求异常:"+e, e); return ReturnFailUtils.getRepInfo(); } } /** * 根据多个所属编号查询附件 * @url {ctx}/api/wechat/fileInfo/listByMainIdAndType?mainId=&ownerType= * @return Object */ @GetMapping("/listByMainIdAndType") public Object listByMainIdAndType(@RequestParam(value = "mainId") String mainId,@RequestParam(value = "ownerType") String ownerType) { try { return ReturnSucUtils.getRepInfo(service.listByMainIdAndType(mainId,ownerType)); } catch (Exception e) { log.error("controller接口[FileInfoWechatController.listByMainIdAndType]请求异常:"+e, e); return ReturnFailUtils.getRepInfo(); } } /** * ocr识别身份证 * @url {ctx}/api/wechat/fileInfo/wecatRecognition?ownerId=&ownerType= * @param request * @return Object */ @PostMapping(value = "/wecatRecognition") public Object wecatRecognition(MultipartHttpServletRequest request, @CurrentUser String userId){ try{ // todo 判断是小程序还是PC // UploaderDTO uploaderDTO = new UploaderDTO(); // PaUserDTO paUserDTO = custClient.paClientGetUserAll(userId); // BeanUtils.copyProperties(paUserDTO, uploaderDTO); Map result = Maps.newHashMap(); Iterator itr = request.getFileNames(); while (itr.hasNext()) { MultipartFile file = request.getFile(itr.next()); // try{ // String ownerId = request.getParameter("ownerId"); // if (StringUtils.isNotEmpty(ownerId)) { // String ownerType = request.getParameter("ownerType"); // int fileCount = fileRelateService.countByOwnerIdAndType(ownerId, ownerType)+1; // if(StringUtils.isEmpty(ownerType)){ // ownerType = FileOwnerTypeBaseEnum.OWNER_TYPE_000.getIndex(); // } // FileInfo fileInfo = service.wechatUploadFile(file, ownerId,ownerId, ownerType, fileCount, uploaderDTO); // FileInfoBaseDTO fileInfoBaseDTO = new FileInfoBaseDTO(); // BeanUtils.copyProperties(fileInfo, fileInfoBaseDTO); // result.put("attachment", fileInfoBaseDTO); // } // }catch (Exception e){ // log.error("Controller接口[FileInfoWechatController.wecatRecognition]请求异常:"+e, e); // } try{ IdcardOcrResultDTO map = BaiduOcrUtils.ocrIdcard(file.getBytes()); result.put("ocrResult", map); }catch (Exception e){ log.error("Controller接口[FileInfoWechatController.wecatRecognition]请求异常:"+e, e); return ReturnFailUtils.getRepInfo("OCR失败失败!"); } } return ReturnSucUtils.getRepInfo(result); }catch (Exception e){ log.error("Controller接口[FileInfoWechatController.wecatRecognition]请求异常:"+e, e); return ReturnFailUtils.getRepInfo(e.getMessage()); } } /** * ocr识别文字 * @url {ctx}/api/wechat/fileInfo/recognitionText * @param request * @return Object */ @PostMapping(value = "/recognitionText") public Object recognitionText(MultipartHttpServletRequest request){ try{ Map result = Maps.newHashMap(); Iterator itr = request.getFileNames(); while (itr.hasNext()) { MultipartFile file = request.getFile(itr.next()); try{ Map map = BaiduOcrUtils.ocrText(file.getBytes()); result.put("ocrResult", map); }catch (Exception e){ log.error("Controller接口[FileInfoWechatController.recognitionText]请求异常:"+e, e); return ReturnFailUtils.getRepInfo("OCR失败!"); } } return ReturnSucUtils.getRepInfo("识别成功", result); }catch (Exception e){ log.error("Controller接口[FileInfoWechatController.recognitionText]请求异常:"+e, e); return ReturnFailUtils.getRepInfo(e.getMessage()); } } }