MaterialController.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package com.idiot.operationbackend.controller;
  2. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  3. import com.idiot.operationbackend.entity.Material;
  4. import com.idiot.operationbackend.service.facade.MaterialService;
  5. import com.idiot.operationbackend.service.facade.WeChatService;
  6. import com.idiot.operationbackend.support.*;
  7. import com.idiot.operationbackend.util.JwtTokenUtil;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import io.swagger.annotations.ApiParam;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.http.ResponseEntity;
  15. import org.springframework.web.bind.annotation.*;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.time.LocalDateTime;
  20. import java.util.Arrays;
  21. /**
  22. * 素材
  23. * @author wang xiao
  24. * @date Created in 17:57 2020/9/18
  25. */
  26. @RestController
  27. @RequestMapping("/material")
  28. @Api(value = "MaterialController", tags ="素材")
  29. public class MaterialController {
  30. private final Logger logger = LoggerFactory.getLogger(MaterialController.class);
  31. private final String splitStr = ",";
  32. @Autowired
  33. private WeChatService weChatService;
  34. @Autowired
  35. private MaterialService materialService;
  36. @GetMapping("/{accountId}")
  37. @ApiOperation(value = "查询公众号素材")
  38. public ResponseEntity<JsonResult<Page<Material>>> materialList (@RequestHeader String token,
  39. @PathVariable String accountId,
  40. @RequestParam String type,
  41. @RequestParam int page){
  42. String userId = JwtTokenUtil.getUserId(token);
  43. logger.info("用户:{}查询微信公众号{}素材,素材类型:{}-------------start",userId,accountId,type);
  44. Page<Material> materialPage = materialService.pageMaterial(accountId,type,page);
  45. logger.info("用户:{}查询微信公众号{}素材,素材类型:{}-------------end",userId,accountId,type);
  46. return ResponseEntity.ok(JsonResult.success(materialPage));
  47. }
  48. @PostMapping("/{accountId}")
  49. @ApiOperation(value = "公众号上传素材(其他素材,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb))")
  50. @ApiParam(value = "type",name = "type",allowableValues = "image,voice,video,thumb")
  51. public ResponseEntity<JsonResult<Boolean>> addMaterial ( @RequestHeader String token,
  52. @PathVariable String accountId,
  53. @RequestParam String type,
  54. @RequestParam(required = false) String title,
  55. @RequestParam(required = false) String description,
  56. MultipartFile file){
  57. String userId = JwtTokenUtil.getUserId(token);
  58. long size = file.getSize();
  59. String fileName = file.getName();
  60. String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
  61. switch (type){
  62. case Constants.IMAGE:
  63. validImg(size,suffix);
  64. break;
  65. case Constants.VIDEO:
  66. validVideo(size,suffix);
  67. break;
  68. case Constants.VOICE:
  69. validVoice(size,suffix);
  70. break;
  71. case Constants.THUMB:
  72. validThumb(size,suffix);
  73. break;
  74. default:
  75. throw new CustomException(500,"其他类型素材暂不支持上传");
  76. }
  77. logger.info("用户:{}上传素材到微信公众号{}服务器,素材名称:{},素材类型:{}-------------start",userId,accountId,fileName,type);
  78. Material material = new Material();
  79. material.setAccountId(accountId);
  80. material.setCreateTime(LocalDateTime.now().format(Constants.DATE_TIME_FORMATTER));
  81. material.setType(MediaType.nameOf(type));
  82. material.setName(title);
  83. material.setDescription(description);
  84. String wxResult = null;
  85. try {
  86. WxInputStreamResource inputStreamResource = new WxInputStreamResource(file.getInputStream(),fileName,size);
  87. wxResult = weChatService.addMaterial(accountId,type,inputStreamResource,title,description);
  88. }catch (IOException e) {
  89. logger.error("上传文件发生IO异常:{}",e.getMessage());
  90. }
  91. boolean addResult = materialService.addMaterial(material,wxResult);
  92. logger.info("用户:{}上传素材到微信公众号{}服务器,素材名称:{},素材类型:{}-------------end,结果:{}",userId,accountId,fileName,type,addResult);
  93. return ResponseEntity.ok(JsonResult.success(addResult));
  94. }
  95. private void validImg(long size, String suffixName) {
  96. if (size > Constants.IMAGE_MAX_SIZE) {
  97. throw new CustomException(500,"文件太大,图片文件的大小最大为10M,请重新上传!");
  98. }
  99. if (!Arrays.asList(Constants.IMAGE_SUPPORTED.split(splitStr)).contains(suffixName)) {
  100. throw new CustomException(500,"图片格式不支持,请选择bmp/png/jpeg/jpg/gif的任意一种!");
  101. }
  102. }
  103. private void validVoice(long size, String suffixName) {
  104. if (size > Constants.VOICE_MAX_SIZE) {
  105. throw new CustomException(500,"文件太大,音频文件的大小最大为2M,请重新上传!");
  106. }
  107. if (!Arrays.asList(Constants.VOICE_SUPPORTED.split(splitStr)).contains(suffixName)) {
  108. throw new CustomException(500,"图片格式不支持,请选择mp3/wma/wav/amr的任意一种!");
  109. }
  110. }
  111. private void validVideo(long size, String suffixName) {
  112. if (size > Constants.VIDEO_MAX_SIZE) {
  113. throw new CustomException(500,"文件太大,视频文件的大小最大为10M,请重新上传!");
  114. }
  115. if (!suffixName.endsWith(Constants.VIDEO_SUPPORTED)) {
  116. throw new CustomException(500,"视频格式不支持,请选择mp4格式!");
  117. }
  118. }
  119. private void validThumb(long size, String suffixName) {
  120. if (size > Constants.THUMB_MAX_SIZE) {
  121. throw new CustomException(500,"文件太大,缩略图文件的大小最大为64K,请重新上传!");
  122. }
  123. if (!suffixName.endsWith(Constants.THUMB_SUPPORTED)) {
  124. throw new CustomException(500,"图片格式不支持,请选择JPG格式!");
  125. }
  126. }
  127. }