SikuController.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package com.malk.siku.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.malk.server.common.McR;
  4. import com.malk.siku.service.SikuService;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.core.io.Resource;
  9. import org.springframework.core.io.UrlResource;
  10. import org.springframework.http.HttpHeaders;
  11. import org.springframework.http.MediaType;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.web.bind.annotation.*;
  14. import java.io.IOException;
  15. import java.nio.file.Files;
  16. import java.nio.file.Path;
  17. import java.nio.file.Paths;
  18. import java.util.Map;
  19. @Slf4j
  20. @RestController
  21. @RequestMapping()
  22. public class SikuController {
  23. @Autowired
  24. private SikuService sikuService;
  25. @Value(value = "${mk.downloadFilePath}")
  26. private String fileStoragePath;
  27. //保存每刻往来单位
  28. @PostMapping("/saveTradingPartner")
  29. public McR saveTradingPartner(@RequestBody Map map) {
  30. return sikuService.saveTradingPartner(map);
  31. }
  32. /**
  33. * 保存云票客户
  34. * @param map
  35. * @return
  36. */
  37. /*@PostMapping("/saveYpClient")
  38. public McR saveYpClient(@RequestBody Map map){
  39. return sikuService.saveYpClient(map);
  40. }*/
  41. /**
  42. * 开票申请单导入和更新
  43. * @param map
  44. * @return
  45. */
  46. @PostMapping("/saveYpApplication")
  47. public McR saveYpApplication(@RequestBody Map map){
  48. return sikuService.saveYpApplication(map);
  49. }
  50. //每刻云票回款回调
  51. @PostMapping("/receive/callback")
  52. public McR callback(@RequestBody Map map){
  53. log.info("每刻云票回款回调:{}", JSONObject.toJSONString(map));
  54. return McR.success();
  55. }
  56. //每刻云票开票回调
  57. @PostMapping("/application/callback")
  58. public McR callback2(@RequestBody Map map){
  59. log.info("每刻云票开票回调:{}",JSONObject.toJSONString(map));
  60. sikuService.invoiceWriteBack(map);
  61. return McR.success();
  62. }
  63. @GetMapping("/files/{fileId}")
  64. public ResponseEntity<Resource> getFileResource(
  65. @PathVariable String fileId,
  66. @RequestParam(defaultValue = "download") String option) throws IOException {
  67. // 根据fileId获取实际文件路径,这里简化处理,实际可能需要从数据库查询
  68. Path filePath = Paths.get(fileStoragePath).resolve(fileId).normalize();
  69. // 检查文件是否存在
  70. if (!Files.exists(filePath)) {
  71. return ResponseEntity.notFound().build();
  72. }
  73. // 创建Resource对象
  74. Resource resource = new UrlResource(filePath.toUri());
  75. // 根据选项设置响应头
  76. HttpHeaders headers = new HttpHeaders();
  77. if ("preview".equalsIgnoreCase(option)) {
  78. // 预览模式 - 尝试确定内容类型
  79. String contentType = Files.probeContentType(filePath);
  80. // 强制修正 PDF 的 Content-Type
  81. if (filePath.toString().toLowerCase().endsWith(".pdf")) {
  82. contentType = "application/pdf";
  83. } else if (contentType == null) {
  84. contentType = "application/octet-stream";
  85. }
  86. headers.setContentType(MediaType.parseMediaType(contentType));
  87. headers.add("Content-Disposition", "inline; filename=\"" + resource.getFilename() + "\"");
  88. headers.add("X-Content-Type-Options", "nosniff"); // 防止浏览器忽略 Content-Type
  89. } else {
  90. // 下载模式 - 默认处理
  91. headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
  92. headers.add("Content-Disposition", "attachment; filename=\"" + resource.getFilename() + "\"");
  93. }
  94. return ResponseEntity.ok()
  95. .headers(headers)
  96. .contentLength(resource.contentLength())
  97. .body(resource);
  98. }
  99. }