Răsfoiți Sursa

通用下载任务接口

wangliang 5 ani în urmă
părinte
comite
70212bb1af

+ 53 - 0
themis-backend/src/main/java/com/qmth/themis/backend/api/SysController.java

@@ -1,5 +1,6 @@
 package com.qmth.themis.backend.api;
 
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.qmth.themis.backend.config.DictionaryConfig;
 import com.qmth.themis.backend.constant.BackendConstant;
@@ -7,10 +8,13 @@ import com.qmth.themis.backend.util.ServletUtil;
 import com.qmth.themis.business.constant.SystemConstant;
 import com.qmth.themis.business.entity.TBAttachment;
 import com.qmth.themis.business.entity.TBPrivilege;
+import com.qmth.themis.business.entity.TBTaskHistory;
 import com.qmth.themis.business.entity.TBUser;
+import com.qmth.themis.business.enums.DownloadFileEnum;
 import com.qmth.themis.business.enums.RoleEnum;
 import com.qmth.themis.business.service.TBAttachmentService;
 import com.qmth.themis.business.service.TBPrivilegeService;
+import com.qmth.themis.business.service.TBTaskHistoryService;
 import com.qmth.themis.common.enums.ExceptionResultEnum;
 import com.qmth.themis.common.exception.BusinessException;
 import com.qmth.themis.common.util.Result;
@@ -24,6 +28,7 @@ import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
+import java.io.File;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.List;
@@ -48,6 +53,9 @@ public class SysController {
     @Resource
     TBAttachmentService tbAttachmentService;
 
+    @Resource
+    TBTaskHistoryService tbTaskHistoryService;
+
     @Resource
     DictionaryConfig dictionaryConfig;
 
@@ -106,4 +114,49 @@ public class SysController {
         map.put(SystemConstant.TYPE, tbAttachment.getType());
         return ResultUtil.ok(map);
     }
+
+    @ApiOperation(value = "文件下载接口")
+    @RequestMapping(value = "/file/download", method = RequestMethod.GET)
+    @ApiResponses({@ApiResponse(code = 200, message = "{\"url\":string}", response = Result.class)})
+    public Result fileDownload(@ApiParam(value = "任务id", required = true) @RequestParam Long id, @ApiParam(value = "下载文件类型", required = true) @RequestParam Integer type) {
+        if (Objects.isNull(id) || Objects.equals(id, "")) {
+            throw new BusinessException(ExceptionResultEnum.TASK_ID_IS_NULL);
+        }
+        if (Objects.isNull(type) || Objects.equals(type, "")) {
+            throw new BusinessException(ExceptionResultEnum.DOWNLOAD_FILE_TYPE_IS_NULL);
+        }
+        if (Objects.isNull(DownloadFileEnum.convertToName(type))) {
+            throw new BusinessException(ExceptionResultEnum.DOWNLOAD_FILE_TYPE_ERROR);
+        }
+        TBTaskHistory tbTaskHistory = tbTaskHistoryService.getById(id);
+        if (Objects.isNull(tbTaskHistory)) {
+            throw new BusinessException(ExceptionResultEnum.TASK_NO);
+        }
+        boolean oss = dictionaryConfig.sysDomain().isOss();
+        JSONObject jsonObject = null;
+        switch (type) {
+            case 0:
+                jsonObject = JSONObject.parseObject(tbTaskHistory.getImportFilePath());
+                break;
+            case 1:
+                jsonObject = JSONObject.parseObject(tbTaskHistory.getErrorFilePath());
+                break;
+            default:
+                jsonObject = JSONObject.parseObject(tbTaskHistory.getResultFilePath());
+                break;
+        }
+        if (Objects.isNull(jsonObject) || Objects.isNull(jsonObject.get("path"))) {
+            throw new BusinessException("下载文件地址不存在");
+        }
+        String filePath = String.valueOf(jsonObject.get("path"));
+        String url = null;
+        if (oss) {
+            url = dictionaryConfig.aliYunOssDomain().getUrl() + File.separator + filePath;
+        } else {
+            url = "http://" + dictionaryConfig.sysDomain().getFileHost() + File.separator + filePath;
+        }
+        Map map = new HashMap();
+        map.put("url", url);
+        return ResultUtil.ok(map);
+    }
 }

+ 3 - 3
themis-backend/src/main/resources/application.properties

@@ -124,8 +124,8 @@ sys.config.serverUpload=/Users/king/git/themis-server/
 #sys.config.gatewayAccessKey=LTAI4FhEmrrhh27vzPGh25xe
 #sys.config.gatewayAccessSecret=lgnWDUMRAhWBIn4bvAEg2ZC9ECB0Of
 #sys.config.deviceId=1
-#sys.config.fileHost=localhost:7001
-#sys.config.serverHost=localhost:7001
+sys.config.fileHost=localhost:6001${server.servlet.context-path}
+sys.config.serverHost=localhost:6001${server.servlet.context-path}
 spring.resources.static-locations=file:${sys.config.serverUpload},classpath:/META-INF/resources/,classpath:/resources/
 
 #============================================================================
@@ -205,5 +205,5 @@ mq.config.taskConsumerRoomCodeExportGroup=${mq.config.taskConsumerGroup}-${mq.co
 prefix.url.admin=api/admin
 
 #\u65E0\u9700\u9274\u6743\u7684url
-no.auth.urls=/webjars/**,/druid/**,/swagger-ui.html,/doc.html,/swagger-resources/**,/v2/api-docs,/webjars/springfox-swagger-ui/**,/api/admin/user/login/account
+no.auth.urls=/webjars/**,/druid/**,/swagger-ui.html,/doc.html,/swagger-resources/**,/v2/api-docs,/webjars/springfox-swagger-ui/**,/api/admin/user/login/account,/file/**,/upload/**,/client/**,/base_photo/**
 common.system.urls=/api/admin/sys/getMenu,/api/admin/user/logout,/api/admin/sys/env

+ 20 - 0
themis-business/src/main/java/com/qmth/themis/business/domain/SysDomain.java

@@ -17,6 +17,26 @@ public class SysDomain {
 
     String serverUpload;
 
+    String fileHost;
+
+    String serverHost;
+
+    public String getFileHost() {
+        return fileHost;
+    }
+
+    public void setFileHost(String fileHost) {
+        this.fileHost = fileHost;
+    }
+
+    public String getServerHost() {
+        return serverHost;
+    }
+
+    public void setServerHost(String serverHost) {
+        this.serverHost = serverHost;
+    }
+
     public boolean isOss() {
         return oss;
     }

+ 69 - 0
themis-business/src/main/java/com/qmth/themis/business/enums/DownloadFileEnum.java

@@ -0,0 +1,69 @@
+package com.qmth.themis.business.enums;
+
+import java.util.Objects;
+
+/**
+ * @Description: 下载文件类型
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2020/7/15
+ */
+public enum DownloadFileEnum {
+    /**
+     * 导入文件
+     */
+    importFile(0),
+
+    /**
+     * 错误文件
+     */
+    error(1),
+
+    /**
+     * result
+     */
+    result(2);
+
+    private int id;
+
+    private DownloadFileEnum(int id) {
+        this.id = id;
+    }
+
+    public int getId() {
+        return id;
+    }
+
+    /**
+     * 状态转换 toId
+     *
+     * @param value
+     * @return
+     */
+    public static int convertToId(String value) {
+        if (Objects.equals(value.trim(), importFile.name())) {
+            return importFile.getId();
+        } else if (Objects.equals(value.trim(), error.name())) {
+            return error.getId();
+        } else {
+            return result.getId();
+        }
+    }
+
+    /**
+     * 状态转换 toName
+     *
+     * @param value
+     * @return
+     */
+    public static String convertToName(int value) {
+        if (value == importFile.getId()) {
+            return importFile.name();
+        } else if (value == error.getId()) {
+            return error.name();
+        } else {
+            return result.name();
+        }
+    }
+}

+ 12 - 4
themis-business/src/main/java/com/qmth/themis/business/enums/UploadFileEnum.java

@@ -10,13 +10,21 @@ import java.util.Objects;
  * @Date: 2020/7/15
  */
 public enum UploadFileEnum {
-
+    /**
+     * 客户端
+     */
     client(0),
-
+    /**
+     * 学生底照
+     */
     base_photo(1),
-
+    /**
+     * 系统相关
+     */
     upload(2),
-
+    /**
+     * 导入导出
+     */
     file(3);
 
     private int id;

+ 8 - 0
themis-common/src/main/java/com/qmth/themis/common/enums/ExceptionResultEnum.java

@@ -24,6 +24,8 @@ public enum ExceptionResultEnum {
 
     PARAMS_ILLEGALITY("102", "参数不能为空"),
 
+    TASK_ID_IS_NULL("102", "任务id不能为空"),
+
     EXAM_ID_IS_NULL("102", "考试批次id不能为空"),
 
     EXAM_INFO_IS_NULL("102", "考试批次信息不能为空"),
@@ -60,8 +62,14 @@ public enum ExceptionResultEnum {
 
     ATTACHMENT_TYPE_IS_NULL("102", "请上传文件类型"),
 
+    DOWNLOAD_FILE_TYPE_IS_NULL("102", "下载文件类型不能为空"),
+
+    DOWNLOAD_FILE_TYPE_ERROR("102", "下载文件类型错误"),
+
     PASSWORD_NO("102", "密码不正确"),
 
+    TASK_NO("102", "任务不存在"),
+
     USER_NO("102", "用户不存在"),
 
     EXAM_NO("102", "考试批次信息不存在"),