package com.malk.siku.controller; import com.alibaba.fastjson.JSONObject; import com.malk.server.common.McR; import com.malk.siku.service.SikuService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.Resource; import org.springframework.core.io.UrlResource; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Map; @Slf4j @RestController @RequestMapping() public class SikuController { @Autowired private SikuService sikuService; @Value(value = "${mk.downloadFilePath}") private String fileStoragePath; //保存每刻往来单位 @PostMapping("/saveTradingPartner") public McR saveTradingPartner(@RequestBody Map map) { return sikuService.saveTradingPartner(map); } /** * 保存云票客户 * @param map * @return */ /*@PostMapping("/saveYpClient") public McR saveYpClient(@RequestBody Map map){ return sikuService.saveYpClient(map); }*/ /** * 开票申请单导入和更新 * @param map * @return */ @PostMapping("/saveYpApplication") public McR saveYpApplication(@RequestBody Map map){ return sikuService.saveYpApplication(map); } //每刻云票回款回调 @PostMapping("/receive/callback") public McR callback(@RequestBody Map map){ log.info("每刻云票回款回调:{}", JSONObject.toJSONString(map)); return McR.success(); } //每刻云票开票回调 @PostMapping("/application/callback") public McR callback2(@RequestBody Map map){ log.info("每刻云票开票回调:{}",JSONObject.toJSONString(map)); sikuService.invoiceWriteBack(map); return McR.success(); } @GetMapping("/files/{fileId}") public ResponseEntity getFileResource( @PathVariable String fileId, @RequestParam(defaultValue = "download") String option) throws IOException { // 根据fileId获取实际文件路径,这里简化处理,实际可能需要从数据库查询 Path filePath = Paths.get(fileStoragePath).resolve(fileId).normalize(); // 检查文件是否存在 if (!Files.exists(filePath)) { return ResponseEntity.notFound().build(); } // 创建Resource对象 Resource resource = new UrlResource(filePath.toUri()); // 根据选项设置响应头 HttpHeaders headers = new HttpHeaders(); if ("preview".equalsIgnoreCase(option)) { // 预览模式 - 尝试确定内容类型 String contentType = Files.probeContentType(filePath); // 强制修正 PDF 的 Content-Type if (filePath.toString().toLowerCase().endsWith(".pdf")) { contentType = "application/pdf"; } else if (contentType == null) { contentType = "application/octet-stream"; } headers.setContentType(MediaType.parseMediaType(contentType)); headers.add("Content-Disposition", "inline; filename=\"" + resource.getFilename() + "\""); headers.add("X-Content-Type-Options", "nosniff"); // 防止浏览器忽略 Content-Type } else { // 下载模式 - 默认处理 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.add("Content-Disposition", "attachment; filename=\"" + resource.getFilename() + "\""); } return ResponseEntity.ok() .headers(headers) .contentLength(resource.contentLength()) .body(resource); } }