Bläddra i källkod

1.新增退出接口
2.新增上传资产测算表和报告模板接口
3.新增文件上传工具类

GouGengquan 9 månader sedan
förälder
incheckning
e2d4feb185

+ 12 - 0
biz-base/src/main/java/com/dayou/controller/AuthController.java

@@ -1,5 +1,7 @@
 package com.dayou.controller;
 
+import cn.dev33.satoken.annotation.SaCheckLogin;
+import cn.dev33.satoken.stp.StpUtil;
 import cn.hutool.core.util.ObjectUtil;
 import com.dayou.result.Result;
 import com.dayou.result.Status;
@@ -33,4 +35,14 @@ public class AuthController {
         }
     }
 
+    /**
+     * 退出登录
+     */
+    @SaCheckLogin
+    @PostMapping("/logout")
+    public Result<String> logout(){
+        StpUtil.logout();
+        return Result.build(Status.SUCCESS);
+    }
+
 }

+ 61 - 0
biz-base/src/main/java/com/dayou/controller/FileUploadController.java

@@ -0,0 +1,61 @@
+package com.dayou.controller;
+
+import cn.dev33.satoken.annotation.SaCheckLogin;
+import cn.dev33.satoken.annotation.SaCheckRole;
+import cn.hutool.core.lang.UUID;
+import com.dayou.config.FileNetConfig;
+import com.dayou.result.Result;
+import com.dayou.result.Status;
+import com.dayou.utils.FileNetUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.CrossOrigin;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.util.Map;
+import java.util.Objects;
+
+@RestController
+@RequestMapping("/file")
+@Slf4j
+@CrossOrigin
+@SaCheckRole("ADMIN")
+public class FileUploadController {
+
+    @Autowired
+    private FileNetConfig fileNetConfig;
+
+    /**
+     * 上传资产测算表模板
+     * @param multipartFile 文件
+     * @return Result<Map<String, String>> 文件信息
+     */
+    @PostMapping("/assets/uploadCalculateTmpl")
+    public Result<Map<String, String>> uploadAssetsCalculateTmpl(MultipartFile multipartFile){
+        // 判断文件类型
+        if (Objects.equals(multipartFile.getContentType(), "application/vnd.ms-excel") || Objects.equals(multipartFile.getContentType(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
+            return Result.build(FileNetUtil.uploadToServer(multipartFile, fileNetConfig.getBaseDir(), fileNetConfig.getAssetTemplateCalculatePath(), UUID.randomUUID() + "_" +multipartFile.getOriginalFilename()));
+        }else {
+            return Result.build(Status.PARAM_ERROR, "请上传xls或xlsx类型的excel文档!");
+        }
+    }
+
+    /**
+     * 上传资产报告模板
+     * @param multipartFile 文件
+     * @return Result<Map<String, String>> 文件信息
+     */
+    @PostMapping("/assets/uploadReportTmpl")
+    public Result<Map<String, String>> uploadAssetsReportTmpl(MultipartFile multipartFile){
+        // 判断文件类型
+        if (Objects.equals(multipartFile.getContentType(), "application/msword") || Objects.equals(multipartFile.getContentType(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document")){
+            return Result.build(FileNetUtil.uploadToServer(multipartFile, fileNetConfig.getBaseDir(), fileNetConfig.getAssetTemplateReportPath(), UUID.randomUUID() + "_" + multipartFile.getOriginalFilename()));
+        }else {
+            return Result.build(Status.PARAM_ERROR, "请上传doc或docx类型的word文档!");
+        }
+    }
+
+}

+ 5 - 0
biz-base/src/main/java/com/dayou/handler/GlobalException.java

@@ -4,6 +4,7 @@ import cn.dev33.satoken.exception.DisableServiceException;
 import cn.dev33.satoken.exception.NotLoginException;
 import cn.dev33.satoken.exception.NotPermissionException;
 import cn.dev33.satoken.exception.NotRoleException;
+import com.dayou.exception.BusinessException;
 import com.dayou.result.Result;
 import com.dayou.result.Status;
 import lombok.extern.slf4j.Slf4j;
@@ -55,6 +56,10 @@ public class GlobalException {
             // MyBatis抛出异常
             log.error("Exception Message:",e);
             return Result.build(Status.MYBATIS_ERROR);
+        } else if(e instanceof BusinessException){
+            // 其他异常
+            log.error("Exception Message:",e);
+            return Result.build(Status.SYSTEM_ERROR,e.getMessage());
         }
         else {
             // 其他异常

+ 12 - 1
biz-base/src/main/resources/application-local.yaml

@@ -64,4 +64,15 @@ sa-token:
 # oa系统的api url
 oa-api-url: localhost:8088/api/
 
-tableSavePath: /Users/wuwei/opt/temp/
+tableSavePath: E:\test\word\
+
+file-net:
+  # 基础配置
+  base-dir: E:\productivity-platform-files\
+  max-file-size: 102400
+  # 资产业务配置
+  asset-template-report-path: assets\templates\report\
+  asset-template-calculate-path: assets\templates\calculate\
+  asset-output-report-path: assets\output\report\
+  asset-output-calculate-path: assets\output\calculate\
+

+ 2 - 0
biz-base/src/test/java/com/dayou/WordSimpleTests.java

@@ -12,6 +12,8 @@ import org.junit.jupiter.api.Test;
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.Arrays;

+ 42 - 0
common/src/main/java/com/dayou/config/FileNetConfig.java

@@ -0,0 +1,42 @@
+package com.dayou.config;
+
+import lombok.Data;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@ConfigurationProperties(prefix = "file-net")
+@Data
+public class FileNetConfig {
+
+    /**
+     * 根目录
+     */
+    private String baseDir;
+
+    /**
+     * 资产报告模板存储位置
+     */
+    private String assetTemplateReportPath;
+
+    /**
+     * 资产测算表模板存储位置
+     */
+    private String assetTemplateCalculatePath;
+
+    /**
+     * 资产输出的报告文件位置
+     */
+    private String assetOutputReportPath;
+
+    /**
+     * 资产输出的测算表文件位置
+     */
+    private String assetOutputCalculatePath;
+
+    /**
+     * 文件大小
+     */
+    private int maxFileSize;
+
+}

+ 104 - 0
common/src/main/java/com/dayou/utils/FileNetUtil.java

@@ -0,0 +1,104 @@
+package com.dayou.utils;
+
+import com.dayou.exception.BusinessException;
+import org.springframework.util.FileCopyUtils;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import java.io.BufferedInputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+public class FileNetUtil {
+
+    /**
+     * 上传文件到服务器本地磁盘
+     *
+     * @param multiFile      文件
+     * @param baseDir        根路径
+     * @param uploadPath     服务器上要存储文件的路径
+     * @param uploadFileName 服务器上要存储的文件的名称
+     * @return Map<String, String> 文件信息
+     */
+    public static Map<String, String> uploadToServer(MultipartFile multiFile, String baseDir, String uploadPath, String uploadFileName) {
+        //构建文件对象
+        File file = new File(baseDir + uploadPath);
+        //文件目录不存在则递归创建目录
+        if (!file.exists()) {
+            boolean mkdirs = file.mkdirs();
+            if (!mkdirs) {
+                throw new BusinessException("创建文件夹异常");
+            }
+        }
+        try {
+            //获取文件输入流
+            InputStream inputStream = multiFile.getInputStream();
+            //构建文件输出流
+            FileOutputStream outputStream = new FileOutputStream(baseDir + uploadPath + uploadFileName);
+            // SpringBoot自带工具类上传
+            FileCopyUtils.copy(inputStream, outputStream);
+            Map<String, String> fileInfo = new HashMap<>();
+            fileInfo.put("fileName", uploadFileName);
+            fileInfo.put("filePath", uploadPath);
+            return fileInfo;
+        } catch (IOException e) {
+            throw new BusinessException("文件上传异常", e);
+        }
+    }
+
+    /**
+     * 通过url从远端下载文件到服务器本地磁盘
+     *
+     * @param downloadUrl      要下载的文件的地址
+     * @param downloadPath     服务器上存储的文件路径
+     * @param downloadFileName 服务器上存储的文件名称
+     * @return Map<String, String> 文件信息
+     */
+    public static Map<String, String> downloadToServer(String downloadUrl, String downloadPath, String downloadFileName) {
+        FileOutputStream fos = null;
+        BufferedInputStream bis = null;
+        try {
+            URL url = new URL(downloadUrl);
+            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+            connection.connect();
+            bis = new BufferedInputStream(connection.getInputStream());
+            File file = new File(downloadPath);
+            if (!file.exists()) {
+                boolean mkdirs = file.mkdirs();
+                if (!mkdirs) {
+                    throw new BusinessException("创建文件目录失败");
+                }
+            }
+            String filePathName = downloadPath + File.separator + downloadFileName;
+            byte[] buf = new byte[1024];
+            int size;
+            fos = new FileOutputStream(filePathName);
+            while ((size = bis.read(buf)) != -1) {
+                fos.write(buf, 0, size);
+            }
+            Map<String, String> fileInfo = new HashMap<>();
+            fileInfo.put(downloadFileName, downloadPath);
+            return fileInfo;
+        } catch (Exception e) {
+            throw new BusinessException("下载文件异常", e);
+        } finally {
+            try {
+                if (bis != null) {
+                    bis.close();
+                }
+                if (fos != null) {
+                    fos.close();
+                }
+            } catch (IOException e) {
+                throw new BusinessException("关流异常", e);
+            }
+        }
+    }
+
+}