164 lines
4.6 KiB
Java
164 lines
4.6 KiB
Java
package com.tiesheng.web.controller;
|
||
|
||
|
||
import cn.hutool.captcha.LineCaptcha;
|
||
import cn.hutool.core.collection.CollUtil;
|
||
import cn.hutool.core.util.IdUtil;
|
||
import com.tiesheng.annotation.token.TokenIgnore;
|
||
import com.tiesheng.util.service.TsCacheService;
|
||
import com.tiesheng.web.config.template.ToolTemplateHandler;
|
||
import com.tiesheng.web.pojos.dto.*;
|
||
import com.tiesheng.web.pojos.vo.TemplateInfoVO;
|
||
import com.tiesheng.web.pojos.vo.PicVerifyVo;
|
||
import com.tiesheng.web.service.FileUploadService;
|
||
import com.tiesheng.util.exception.ApiException;
|
||
import com.tiesheng.util.pojos.ApiResp;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.web.bind.annotation.*;
|
||
import org.springframework.web.multipart.MultipartFile;
|
||
|
||
import java.util.List;
|
||
import java.util.Objects;
|
||
import java.util.stream.Collectors;
|
||
|
||
/**
|
||
* 工具类
|
||
*
|
||
* @author hao
|
||
*/
|
||
@RestController
|
||
@RequestMapping("/tool")
|
||
public class ToolController {
|
||
|
||
@Autowired
|
||
TsCacheService tsCacheService;
|
||
@Autowired
|
||
FileUploadService fileUploadService;
|
||
@Autowired
|
||
List<ToolTemplateHandler> templateHandlerList;
|
||
|
||
|
||
/**
|
||
* 图片验证码
|
||
*
|
||
* @return
|
||
*/
|
||
@TokenIgnore
|
||
@GetMapping("/code/image")
|
||
public ApiResp<PicVerifyVo> picVerifyV2(ImageCodeDTO dto) {
|
||
|
||
LineCaptcha lineCaptcha = dto.lineCaptcha();
|
||
PicVerifyVo vo = new PicVerifyVo();
|
||
vo.setBase64(lineCaptcha.getImageBase64Data());
|
||
vo.setKey(IdUtil.simpleUUID());
|
||
tsCacheService.put(vo.getKey(), lineCaptcha.getCode());
|
||
|
||
return ApiResp.respOK(vo);
|
||
}
|
||
|
||
|
||
/**
|
||
* 上传整个文件
|
||
*
|
||
* @param file
|
||
* @return
|
||
*/
|
||
@TokenIgnore
|
||
@PostMapping("/file/whole_upload")
|
||
public ApiResp<String> fileWholeUpload(@RequestParam("file") MultipartFile file) {
|
||
String filePath = fileUploadService.saveMultipartFile(file);
|
||
return ApiResp.respOK(filePath);
|
||
}
|
||
|
||
|
||
/**
|
||
* 上传超大文件,建议超过20M的使用这个方法
|
||
*
|
||
* @return
|
||
*/
|
||
@TokenIgnore
|
||
@PostMapping(value = "/file/chunk_start")
|
||
public ApiResp<String> fileChunkStart(@RequestBody ChunkStartDTO dto) {
|
||
fileUploadService.chunkStart(dto.getFileExt());
|
||
return ApiResp.respOK("");
|
||
}
|
||
|
||
|
||
/**
|
||
* 校验文件块
|
||
*
|
||
* @return
|
||
*/
|
||
@TokenIgnore
|
||
@PostMapping("/file/chunk_check")
|
||
public ApiResp<Boolean> fileChunkCheck(@RequestBody ChunkCheckDTO dto) {
|
||
boolean exist = fileUploadService.chunkCheck(dto.getFileMd5(), dto.getChunk());
|
||
return ApiResp.respOK(exist);
|
||
}
|
||
|
||
|
||
/**
|
||
* 上传文件块
|
||
*
|
||
* @return
|
||
*/
|
||
@TokenIgnore
|
||
@PostMapping("/file/chunk_upload")
|
||
public ApiResp<String> fileChunkUpload(@RequestParam("file") MultipartFile file, String fileMd5, Integer chunk) {
|
||
fileUploadService.chunkUpload(file, fileMd5, chunk);
|
||
return ApiResp.respOK("");
|
||
}
|
||
|
||
|
||
/**
|
||
* 合并文件
|
||
*
|
||
* @param dto
|
||
* @return
|
||
*/
|
||
@TokenIgnore
|
||
@PostMapping("/file/chunk_merge")
|
||
public ApiResp<String> fileChunkMerge(@RequestBody ChunkMergeDTO dto) {
|
||
String path = fileUploadService.chunkMerge(dto.getFileMd5(), dto.getFileExt());
|
||
return ApiResp.respOK(path);
|
||
}
|
||
|
||
|
||
@TokenIgnore
|
||
@GetMapping("/template/info")
|
||
public ApiResp<TemplateInfoVO> templateInfo(TemplateInfoDTO dto) {
|
||
|
||
List<ToolTemplateHandler> collect = templateHandlerList.stream()
|
||
.filter(it -> Objects.equals(it.getAction(), dto.getAction()))
|
||
.sorted((it, it2) -> it2.getSort() - it.getSort())
|
||
.collect(Collectors.toList());
|
||
if (CollUtil.isEmpty(collect)) {
|
||
throw new ApiException("没有找到对应的模版");
|
||
}
|
||
|
||
ToolTemplateHandler toolTemplateHandler = collect.get(0);
|
||
|
||
TemplateInfoVO template = new TemplateInfoVO();
|
||
template.setTemplateUrl(toolTemplateHandler.getTemplateUrl());
|
||
template.setParams(toolTemplateHandler.getParms(dto.getParams()));
|
||
template.setTemplateId(toolTemplateHandler.getTeamplateId());
|
||
|
||
return ApiResp.respOK(template);
|
||
}
|
||
|
||
@TokenIgnore
|
||
@PostMapping("/template/deal")
|
||
public ApiResp<String> templateDeal(@RequestBody TemplateDealDTO dto) {
|
||
|
||
List<ToolTemplateHandler> collect = templateHandlerList.stream().
|
||
filter(it -> Objects.equals(it.getTeamplateId(), dto.getTemplateId()))
|
||
.collect(Collectors.toList());
|
||
if (CollUtil.isEmpty(collect)) {
|
||
throw new ApiException("模版ID不存在");
|
||
}
|
||
|
||
return ApiResp.respOK(collect.get(0).handler(dto));
|
||
}
|
||
|
||
}
|