|
@@ -1,10 +1,25 @@
|
|
|
package com.idiot.operationbackend.controller;
|
|
|
|
|
|
-import com.idiot.operationbackend.support.JsonResult;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.idiot.operationbackend.entity.Material;
|
|
|
+import com.idiot.operationbackend.service.facade.MaterialService;
|
|
|
+import com.idiot.operationbackend.service.facade.WeChatService;
|
|
|
+import com.idiot.operationbackend.support.*;
|
|
|
+import com.idiot.operationbackend.util.JwtTokenUtil;
|
|
|
import io.swagger.annotations.Api;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
+import io.swagger.annotations.ApiParam;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Arrays;
|
|
|
|
|
|
|
|
|
/**
|
|
@@ -18,27 +33,116 @@ import org.springframework.web.bind.annotation.*;
|
|
|
public class MaterialController {
|
|
|
|
|
|
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(MaterialController.class);
|
|
|
+
|
|
|
+ private final String splitStr = ",";
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WeChatService weChatService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MaterialService materialService;
|
|
|
|
|
|
@GetMapping("/{accountId}")
|
|
|
@ApiOperation(value = "查询公众号素材")
|
|
|
- public ResponseEntity<JsonResult<Object>> materialList (@RequestHeader String token,
|
|
|
- @PathVariable String accountId,
|
|
|
- @RequestParam String type){
|
|
|
- return null;
|
|
|
+ public ResponseEntity<JsonResult<Page<Material>>> materialList (@RequestHeader String token,
|
|
|
+ @PathVariable String accountId,
|
|
|
+ @RequestParam String type,
|
|
|
+ @RequestParam int page){
|
|
|
+ String userId = JwtTokenUtil.getUserId(token);
|
|
|
+ logger.info("用户:{}查询微信公众号{}素材,素材类型:{}-------------start",userId,accountId,type);
|
|
|
+ Page<Material> materialPage = materialService.pageMaterial(accountId,type,page);
|
|
|
+ logger.info("用户:{}查询微信公众号{}素材,素材类型:{}-------------end",userId,accountId,type);
|
|
|
+ return ResponseEntity.ok(JsonResult.success(materialPage));
|
|
|
}
|
|
|
|
|
|
|
|
|
@PostMapping("/{accountId}")
|
|
|
@ApiOperation(value = "公众号上传素材(其他素材,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb))")
|
|
|
- public ResponseEntity<JsonResult<Object>> addMaterial (@RequestHeader String token,
|
|
|
+ @ApiParam(value = "type",name = "type",allowableValues = "image,voice,video,thumb")
|
|
|
+ public ResponseEntity<JsonResult<Boolean>> addMaterial ( @RequestHeader String token,
|
|
|
@PathVariable String accountId,
|
|
|
- @RequestParam String type){
|
|
|
+ @RequestParam String type,
|
|
|
+ @RequestParam(required = false) String title,
|
|
|
+ @RequestParam(required = false) String description,
|
|
|
+ MultipartFile file){
|
|
|
|
|
|
- return null;
|
|
|
+ String userId = JwtTokenUtil.getUserId(token);
|
|
|
+ long size = file.getSize();
|
|
|
+ String fileName = file.getName();
|
|
|
+ String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
|
|
|
+ switch (type){
|
|
|
+ case Constants.IMAGE:
|
|
|
+ validImg(size,suffix);
|
|
|
+ break;
|
|
|
+ case Constants.VIDEO:
|
|
|
+ validVideo(size,suffix);
|
|
|
+ break;
|
|
|
+ case Constants.VOICE:
|
|
|
+ validVoice(size,suffix);
|
|
|
+ break;
|
|
|
+ case Constants.THUMB:
|
|
|
+ validThumb(size,suffix);
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new CustomException(500,"其他类型素材暂不支持上传");
|
|
|
+ }
|
|
|
+ logger.info("用户:{}上传素材到微信公众号{}服务器,素材名称:{},素材类型:{}-------------start",userId,accountId,fileName,type);
|
|
|
+ Material material = new Material();
|
|
|
+ material.setAccountId(accountId);
|
|
|
+ material.setCreateTime(LocalDateTime.now().format(Constants.DATE_TIME_FORMATTER));
|
|
|
+ material.setType(MediaType.nameOf(type));
|
|
|
+ material.setName(title);
|
|
|
+ material.setDescription(description);
|
|
|
+ String wxResult = null;
|
|
|
+ try {
|
|
|
+ WxInputStreamResource inputStreamResource = new WxInputStreamResource(file.getInputStream(),fileName,size);
|
|
|
+ wxResult = weChatService.addMaterial(accountId,type,inputStreamResource,title,description);
|
|
|
+ }catch (IOException e) {
|
|
|
+ logger.error("上传文件发生IO异常:{}",e.getMessage());
|
|
|
+ }
|
|
|
+ boolean addResult = materialService.addMaterial(material,wxResult);
|
|
|
+ logger.info("用户:{}上传素材到微信公众号{}服务器,素材名称:{},素材类型:{}-------------end,结果:{}",userId,accountId,fileName,type,addResult);
|
|
|
+ return ResponseEntity.ok(JsonResult.success(addResult));
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ private void validImg(long size, String suffixName) {
|
|
|
+ if (size > Constants.IMAGE_MAX_SIZE) {
|
|
|
+ throw new CustomException(500,"文件太大,图片文件的大小最大为10M,请重新上传!");
|
|
|
+ }
|
|
|
+ if (!Arrays.asList(Constants.IMAGE_SUPPORTED.split(splitStr)).contains(suffixName)) {
|
|
|
+ throw new CustomException(500,"图片格式不支持,请选择bmp/png/jpeg/jpg/gif的任意一种!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validVoice(long size, String suffixName) {
|
|
|
+ if (size > Constants.VOICE_MAX_SIZE) {
|
|
|
+ throw new CustomException(500,"文件太大,音频文件的大小最大为2M,请重新上传!");
|
|
|
+ }
|
|
|
+ if (!Arrays.asList(Constants.VOICE_SUPPORTED.split(splitStr)).contains(suffixName)) {
|
|
|
+ throw new CustomException(500,"图片格式不支持,请选择mp3/wma/wav/amr的任意一种!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validVideo(long size, String suffixName) {
|
|
|
+ if (size > Constants.VIDEO_MAX_SIZE) {
|
|
|
+ throw new CustomException(500,"文件太大,视频文件的大小最大为10M,请重新上传!");
|
|
|
+ }
|
|
|
+ if (!suffixName.endsWith(Constants.VIDEO_SUPPORTED)) {
|
|
|
+ throw new CustomException(500,"视频格式不支持,请选择mp4格式!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validThumb(long size, String suffixName) {
|
|
|
+ if (size > Constants.THUMB_MAX_SIZE) {
|
|
|
+ throw new CustomException(500,"文件太大,缩略图文件的大小最大为64K,请重新上传!");
|
|
|
+ }
|
|
|
+ if (!suffixName.endsWith(Constants.THUMB_SUPPORTED)) {
|
|
|
+ throw new CustomException(500,"图片格式不支持,请选择JPG格式!");
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|