Browse Source

添加了新闻发表置顶和附件功能

bigcat 2 years ago
parent
commit
a914ac00a0

+ 2 - 1
src/main/java/sqgxy/xxydz/controller/NewsController.java

@@ -37,9 +37,10 @@ public class NewsController {
     @GeneralAdmin
     @ApiOperation(value = "添加新闻")
     @PostMapping(value = "/add")
-    public Result addNews(MultipartFile pictureFile, NewsAddVO vo) throws IOException {
+    public Result addNews(MultipartFile pictureFile, NewsAddVO vo, MultipartFile attachment) throws IOException {
         NewsAddDTO newsAddDTO = modelMapper.map(vo, NewsAddDTO.class);
         newsAddDTO.setPictureFile(pictureFile);
+        newsAddDTO.setAttachment(attachment);
         return new Result().ok().data(newsService.saveNews(newsAddDTO));
     }
 

+ 6 - 0
src/main/java/sqgxy/xxydz/dto/NewsAddDTO.java

@@ -28,5 +28,11 @@ public class NewsAddDTO {
 
     private String content;
 
+    private Integer sticky;
+
     private Integer newsCategoryId;
+
+    private MultipartFile attachment;
+
+    private String attachmentPath;
 }

+ 11 - 0
src/main/java/sqgxy/xxydz/entity/News.java

@@ -57,6 +57,17 @@ public class News {
      */
     private Integer newsCategoryId;
 
+    /**
+     * 是否置顶
+     */
+
+    private Integer sticky;
+
+    /**
+     * 附件路径
+     */
+    private String attachmentPath;
+
     private Date createTime;
 
     private Date updateTime;

+ 15 - 6
src/main/java/sqgxy/xxydz/module/HeaderImgUpload.java

@@ -1,5 +1,6 @@
 package sqgxy.xxydz.module;
 
+import org.springframework.util.StringUtils;
 import sqgxy.xxydz.exception.HintException;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.web.multipart.MultipartFile;
@@ -32,13 +33,14 @@ public class HeaderImgUpload {
     public static String headPortraitUpload(MultipartFile file) throws IOException, HintException {
 
         System.out.println(file.getContentType());
-        if (!FILE_TYPE.contains(file.getContentType())) {
-            throw new HintException("上传的文件类型只能是:" + FILE_TYPE.toString());
-        }
+//        if (!FILE_TYPE.contains(file.getContentType())) {
+//            throw new HintException("上传的文件类型只能是:" + FILE_TYPE.toString());
+//        }
+
 
         String path = System.getProperty("user.dir");
         File upload = new File(path, FILE_PATH_PREFIX);
-
+        String suffix = getFileExtension(file);
         log.info("目录upload: " + upload.toString());
         if (!upload.exists()) {
             log.info("目录不存在,开始创建: " + upload.toString());
@@ -52,12 +54,19 @@ public class HeaderImgUpload {
         if (!file.isEmpty()) {
             log.info("开始保存");
             //保存到文件服务器
-            file1 = new File(upload, fileName + FILE_PATH_SUFFIX);
+            file1 = new File(upload, fileName + "." + suffix);
             file.transferTo(file1);
         } else {
             throw new NullPointerException("文件不能为空");
         }
-        return FILE_PATH_PREFIX_READ + File.separator + fileName + FILE_PATH_SUFFIX;
+        return FILE_PATH_PREFIX_READ + File.separator + fileName + "."+ suffix;
+    }
+
+    public static String getFileExtension(MultipartFile file) {
+        String fileName = StringUtils.cleanPath(file.getOriginalFilename());
+        int dotIndex = fileName.lastIndexOf('.');
+        return (dotIndex == -1) ? "" : fileName.substring(dotIndex + 1);
     }
 
 }
+

+ 9 - 1
src/main/java/sqgxy/xxydz/service/impl/NewsServiceImpl.java

@@ -55,6 +55,12 @@ public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements Ne
             String s = HeaderImgUpload.headPortraitUpload(dto.getPictureFile());
             dto.setPicturePath(s);
         }
+
+        // 判断附件是否为空
+        if (dto.getAttachment() != null) {
+            String s = HeaderImgUpload.headPortraitUpload(dto.getAttachment());
+            dto.setAttachmentPath(s);
+        }
         return save(modelMapper.map(dto, News.class));
     }
 
@@ -95,7 +101,9 @@ public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements Ne
 
     @Override
     public NewsPaging getNewsListByNewsCategoryId(Integer newsCategoryId, Integer current, Integer size) {
-        Page<News> page = page(new Page<>(current, size), new QueryWrapper<News>().select(ID, TITLE, RELEASE_TIME, PICTURE_PATH).eq(NEWS_CATEGORY_ID, newsCategoryId).orderByDesc(RELEASE_TIME));
+        Page<News> page = page(new Page<>(current, size), new QueryWrapper<News>()
+                .select(ID, TITLE, RELEASE_TIME, PICTURE_PATH)
+                .eq(NEWS_CATEGORY_ID, newsCategoryId).orderByDesc(RELEASE_TIME));
         NewsPaging newsPaging = modelMapper.map(page, NewsPaging.class);
         List<News> records = page.getRecords();
         newsPaging.setRecords(modelMapper.map(records, new TypeToken<List<NewsQueryListDTO>>(){}.getType()));

+ 2 - 0
src/main/java/sqgxy/xxydz/vo/news/NewsAddVO.java

@@ -25,4 +25,6 @@ public class NewsAddVO {
     private String content;
 
     private Integer newsCategoryId;
+
+    private Integer sticky;
 }