浏览代码

修改file查询接口返回格式,解决返回总数为0问题,删除传参改为id

zhaojinyu 2 周之前
父节点
当前提交
bb27f8d732

+ 23 - 10
base-modules/service-file/pom.xml

@@ -1,10 +1,8 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-         xmlns="http://maven.apache.org/POM/4.0.0"
-         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <parent>
-        <groupId>com.usky</groupId>
         <artifactId>base-modules</artifactId>
+        <groupId>com.usky</groupId>
         <version>0.0.1</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
@@ -48,11 +46,11 @@
         </dependency>
         
         <!-- Minio -->
-        <dependency>
-            <groupId>io.minio</groupId>
-            <artifactId>minio</artifactId>
-            <version>${minio.version}</version>
-        </dependency>
+<!--        <dependency>-->
+<!--            <groupId>io.minio</groupId>-->
+<!--            <artifactId>minio</artifactId>-->
+<!--            <version>${minio.version}</version>-->
+<!--        </dependency>-->
         
         <!-- RuoYi Api System -->
         <dependency>
@@ -108,7 +106,22 @@
                     </execution>
                 </executions>
             </plugin>
+            <plugin>
+                <groupId>com.github.shalousun</groupId>
+                <artifactId>smart-doc-maven-plugin</artifactId>
+                <version>2.1.1</version>
+                <configuration>
+                    <!--指定生成文档的使用的配置文件,配置文件放在自己的项目中-->
+                    <configFile>./src/main/resources/smart-doc.json</configFile>
+                    <!--指定项目名称-->
+                    <projectName>test</projectName>
+                    <!--                    <excludes>-->
+                    <!--                        <exclude>com.bizmatics:product-service-provider</exclude>-->
+                    <!--                        <exclude>cn.afterturn:easypoi-web</exclude>-->
+                    <!--                    </excludes>-->
+                </configuration>
+            </plugin>
         </plugins>
     </build>
-   
+
 </project>

+ 19 - 0
base-modules/service-file/src/main/java/com/ruoyi/file/config/WebConfig.java

@@ -0,0 +1,19 @@
+package com.ruoyi.file.config;
+
+import org.springframework.context.annotation.Configuration;
+import org.springframework.format.FormatterRegistry;
+import org.springframework.format.datetime.standard.DateTimeFormatterRegistrar;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
+
+import java.time.format.DateTimeFormatter;
+
+@Configuration
+public class WebConfig implements WebMvcConfigurer {
+
+    @Override
+    public void addFormatters(FormatterRegistry registry) {
+        DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
+        registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+        registrar.registerFormatters(registry);
+    }
+}

+ 9 - 12
base-modules/service-file/src/main/java/com/ruoyi/file/controller/FilesController.java

@@ -5,6 +5,7 @@ import com.ruoyi.common.core.domain.R;
 import com.ruoyi.file.service.FileUploadResponse;
 import com.ruoyi.file.service.FilesUpload;
 import com.ruoyi.file.service.FilesService;
+import com.usky.common.core.bean.CommonPage;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
@@ -27,17 +28,11 @@ public class FilesController {
         return R.ok(response);
     }
 
-    @GetMapping("/download/{filesUUID}")
-    public void download(@PathVariable String filesUUID, HttpServletResponse response) {
-        //下载文件
-        filesService.download(filesUUID, response);
-    }
-
 
-    @DeleteMapping("/delete/{filesUUID}")
-    public R<Void> delete(@PathVariable String filesUUID) {
+    @DeleteMapping("/delete/{id}")
+    public R<Void> delete(@PathVariable Integer id) {
         try {
-            filesService.deleteFile(filesUUID);
+            filesService.deleteFile(id);
             return R.ok();
         } catch (Exception e) {
             return R.fail(e.getMessage()); // 删除失败,返回错误信息
@@ -45,7 +40,7 @@ public class FilesController {
     }
 
     @GetMapping("/query")
-    public R<Page<FilesUpload>> queryFiles(
+    public R<CommonPage<FilesUpload>> queryFiles(
             @RequestParam(required = false) String filesName,
             @RequestParam(required = false) LocalDateTime startTime,
             @RequestParam(required = false) LocalDateTime endTime,
@@ -54,8 +49,10 @@ public class FilesController {
             @RequestParam int current,
             @RequestParam int size) {
 
-        Page<FilesUpload> page = new Page<>(current, size);
-        Page<FilesUpload> resultPage = filesService.queryFiles(filesName, startTime, endTime, isDeleted, fileType, page);
+        // 调用服务层方法
+        CommonPage<FilesUpload> resultPage = filesService.queryFiles(filesName, startTime, endTime, isDeleted, fileType, current, size);
+
+        // 返回封装好的分页结果
         return R.ok(resultPage);
     }
 }

+ 3 - 6
base-modules/service-file/src/main/java/com/ruoyi/file/service/FilesService.java

@@ -2,6 +2,7 @@ package com.ruoyi.file.service;
 
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.IService;
+import com.usky.common.core.bean.CommonPage;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.servlet.http.HttpServletResponse;
@@ -11,13 +12,9 @@ public interface FilesService extends IService<FilesUpload> {
     // 修改返回类型为 FileUploadResponse
     FileUploadResponse upload(MultipartFile file);
 
-    // 下载文件
-    void download(String filesUUID, HttpServletResponse response);
-
     // 删除文件
-    void deleteFile(String filesUUID);
+    void deleteFile(Integer id);
 
     // 查询方法
-    Page<FilesUpload> queryFiles(String filesName, LocalDateTime startTime, LocalDateTime endTime, Boolean isDeleted, String fileType, Page<FilesUpload> page);
-
+    CommonPage<FilesUpload> queryFiles(String filesName, LocalDateTime startTime, LocalDateTime endTime, Boolean isDeleted, String fileType, int current, int size);
 }

+ 22 - 55
base-modules/service-file/src/main/java/com/ruoyi/file/service/FilesServiceImpl.java

@@ -1,11 +1,12 @@
 package com.ruoyi.file.service;
 
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.file.mapper.FilesMapper;
+import com.usky.common.core.bean.CommonPage;
 import com.usky.common.security.utils.SecurityUtils;
-import com.usky.system.model.LoginUser;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -35,7 +36,7 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, FilesUpload> impl
     public FileUploadResponse upload(MultipartFile file) {
 
         // 获取当前登录用户昵称(如果可用)
-        String userName = getUserNameFromSecurityContext();
+        String userName = SecurityUtils.getUsername();
 
         // 文件夹路径名称
         String originalFilename = file.getOriginalFilename();
@@ -94,7 +95,7 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, FilesUpload> impl
             save(saveFile);
 
             // 返回 FileUploadResponse 对象
-            return new FileUploadResponse(fileUuid, url);
+            return new FileUploadResponse(originalFilename, url);
         } catch (IOException e) {
             e.printStackTrace();
             return null;
@@ -127,58 +128,11 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, FilesUpload> impl
         return files.length + 1;
     }
 
-    //将文件以流的形式一次性读取到内存,通过响应输出流输出到前端
     @Override
-    public void download(String filesUUID, HttpServletResponse response) {
+    public void deleteFile(Integer id) {
         try {
-
-            // 忽略 favicon.ico 请求
-            if ("favicon.ico".equals(filesUUID)) {
-                // 直接返回,不做任何处理
-                return;
-            }
-
-            // 确保 filesUUID 是文件名,而不是完整的路径
-            // 如果 filesUUID 包含路径分隔符,需要从最后的路径分隔符开始截取文件名
-            String fileName = filesUUID.substring(filesUUID.lastIndexOf('/') + 1);
-
-            // 确保路径拼接时使用正确的分隔符
-            LocalDateTime now = LocalDateTime.now();
-            String yearMonth = now.format(DateTimeFormatter.ofPattern("yyyyMM"));
-            File uploadFile = new File(filesUploadPath + "/" + yearMonth, fileName);
-
-            // 检查文件是否存在
-            if (!uploadFile.exists()) {
-                throw new FileNotFoundException("File not found: " + uploadFile.getAbsolutePath());
-            }
-
-            // 设置响应头
-            String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
-            response.addHeader("Content-Disposition", "attachment; filename*=UTF-8''" + encodedFileName);
-            response.setContentType("application/octet-stream");
-
-            // 使用 try-with-resources 确保流正确关闭
-            try (InputStream inputStream = new BufferedInputStream(new FileInputStream(uploadFile));
-                 ServletOutputStream os = response.getOutputStream()) {
-
-                byte[] buffer = new byte[1024];
-                int length;
-                while ((length = inputStream.read(buffer)) != -1) {
-                    os.write(buffer, 0, length);
-                }
-            }
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
-    }
-
-    @Override
-    public void deleteFile(String filesUUID) {
-        try {
-
-            // 根据文件名查询文件信息
-            FilesUpload file = getOne(new QueryWrapper<FilesUpload>().eq("name", filesUUID));
-
+            // 根据 id 查询文件信息
+            FilesUpload file = getById(id);
             if (file == null) {
                 throw new RuntimeException("文件不存在,无法删除");
             }
@@ -216,7 +170,7 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, FilesUpload> impl
     }
 
     @Override
-    public Page<FilesUpload> queryFiles(String filesName, LocalDateTime startTime, LocalDateTime endTime, Boolean isDeleted, String fileType, Page<FilesUpload> page) {
+    public CommonPage<FilesUpload> queryFiles(String filesName, LocalDateTime startTime, LocalDateTime endTime, Boolean isDeleted, String fileType, int current, int size) {
         QueryWrapper<FilesUpload> queryWrapper = new QueryWrapper<>();
 
         // 默认查询未删除的文件
@@ -246,7 +200,20 @@ public class FilesServiceImpl extends ServiceImpl<FilesMapper, FilesUpload> impl
         // 按照创建时间倒序排列
         queryWrapper.orderByDesc("create_time");
 
-        return page(page, queryWrapper);
+        // 创建分页请求对象
+        Page<FilesUpload> page = new Page<>(current, size);
+
+        // 执行分页查询
+        IPage<FilesUpload> iPage = baseMapper.selectPage(page, queryWrapper);
+
+        // 将 MyBatis Plus 的 IPage 转换为 CommonPage
+        CommonPage<FilesUpload> commonPage = new CommonPage<>();
+        commonPage.setRecords(iPage.getRecords());
+        commonPage.setTotal(iPage.getRecords().size());
+        commonPage.setSize(iPage.getSize());
+        commonPage.setCurrent(iPage.getCurrent());
+
+        return commonPage;
     }
 
     private String getUserNameFromSecurityContext() {