upload
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
package org.springblade.modules.kiosk.controller;
|
||||
|
||||
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import org.springblade.core.tool.api.R;
|
||||
import org.springblade.modules.kiosk.pojo.vo.CategoryApiVO;
|
||||
import org.springblade.modules.kiosk.service.IKioskApiService;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@@ -15,6 +16,7 @@ import java.util.List;
|
||||
@RestController
|
||||
@AllArgsConstructor
|
||||
@RequestMapping("kiosk")
|
||||
@Validated
|
||||
public class KioskApiController {
|
||||
|
||||
private final IKioskApiService kioskApiService;
|
||||
@@ -53,9 +55,24 @@ public class KioskApiController {
|
||||
*/
|
||||
@SneakyThrows
|
||||
@PostMapping("/upload")
|
||||
public R<Boolean> upload(@RequestParam String code, @RequestParam MultipartFile file) {
|
||||
Boolean result = kioskApiService.uploadByCode(code, file.getOriginalFilename(), file.getInputStream());
|
||||
public R<Boolean> upload(@RequestParam @NotBlank(message = "Invalid code") String code,
|
||||
@RequestParam @NotBlank(message = "Invalid machine") String machine,
|
||||
@RequestParam @NotBlank(message = "Invalid store") String store,
|
||||
@RequestParam MultipartFile file) {
|
||||
Boolean result = kioskApiService.uploadByCode(code, machine, store, file.getOriginalFilename(), file.getInputStream());
|
||||
return R.data(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 照片列表查询
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
@GetMapping("/photo-list")
|
||||
@Operation(summary = "生成上传地址")
|
||||
public R<List<String>> photoList() {
|
||||
List<String> list = kioskApiService.photoList();
|
||||
return R.data(list);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,8 +10,10 @@ public interface IKioskApiService {
|
||||
|
||||
List<CategoryApiVO> categoryList();
|
||||
|
||||
Boolean uploadByCode(String code, String fileName, InputStream stream);
|
||||
Boolean uploadByCode(String code, String machine, String store,String fileName, InputStream stream);
|
||||
|
||||
String generateUploadUrl();
|
||||
|
||||
List<String> photoList();
|
||||
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package org.springblade.modules.kiosk.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springblade.core.boot.file.BladeFileUtil;
|
||||
import org.springblade.core.log.exception.ServiceException;
|
||||
import org.springblade.core.oss.model.BladeFile;
|
||||
import org.springblade.core.redis.cache.BladeRedis;
|
||||
import org.springblade.core.secure.BladeUser;
|
||||
import org.springblade.core.secure.utils.AuthUtil;
|
||||
import org.springblade.core.tool.utils.DateUtil;
|
||||
import org.springblade.core.tool.utils.StringUtil;
|
||||
import org.springblade.core.tool.utils.WebUtil;
|
||||
import org.springblade.core.tool.utils.*;
|
||||
import org.springblade.modules.kiosk.pojo.entity.PhotoAttachmentEntity;
|
||||
import org.springblade.modules.kiosk.pojo.vo.CategoryApiVO;
|
||||
import org.springblade.modules.kiosk.service.IKioskApiService;
|
||||
@@ -19,8 +19,8 @@ import org.springblade.modules.system.service.IUserService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@@ -34,26 +34,32 @@ public class KioskServiceImpl implements IKioskApiService {
|
||||
|
||||
private final IPhotoAttachmentService photoAttachmentService;
|
||||
|
||||
private static final String codePrefix = "kiosk:code:";
|
||||
|
||||
@Override
|
||||
public List<CategoryApiVO> categoryList() {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean uploadByCode(String code, String fileName, InputStream stream) {
|
||||
public Boolean uploadByCode(String code, String machine, String store, String fileName, InputStream stream) {
|
||||
|
||||
String tenantId = WebUtil.getHeader("Tenant-Id");
|
||||
if (StringUtil.isBlank(tenantId)) {
|
||||
throw new ServiceException("Invalid tenantId.");
|
||||
}
|
||||
|
||||
if (!redis.exists(code) && redis.ttl(code) < 1) {
|
||||
String uploadKey = codePrefix + StringPool.COLON + store + StringPool.COLON + machine;
|
||||
|
||||
if (!redis.exists(uploadKey) || redis.ttl(uploadKey) < 1) {
|
||||
throw new ServiceException("Invalid parameters.");
|
||||
}
|
||||
|
||||
if (!StringUtil.equals(redis.get(uploadKey), code)) {
|
||||
throw new ServiceException("Invalid code.");
|
||||
}
|
||||
|
||||
String account = redis.get(code);
|
||||
|
||||
User user = userService.userByAccount(tenantId, account);
|
||||
User user = userService.userByAccount(tenantId, machine);
|
||||
|
||||
BladeFile bladeFile = ossBuilder.template(tenantId).putFile(fileName, stream);
|
||||
|
||||
@@ -72,6 +78,21 @@ public class KioskServiceImpl implements IKioskApiService {
|
||||
|
||||
@Override
|
||||
public String generateUploadUrl() {
|
||||
return "";
|
||||
BladeUser user = AuthUtil.getUser();
|
||||
String uploadKey = codePrefix + StringPool.COLON + user.getDeptId() + StringPool.COLON + user.getAccount();
|
||||
String code = StringUtil.random(8, RandomType.ALL);
|
||||
redis.setEx(uploadKey, code, 900L);
|
||||
return StringUtil.format("https://upload.tttzai.com?code=%s&machine=%s&store=%s", code, user.getAccount(), user.getDeptId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> photoList() {
|
||||
BladeUser user = AuthUtil.getUser();
|
||||
List<PhotoAttachmentEntity> list = photoAttachmentService.list(Wrappers
|
||||
.<PhotoAttachmentEntity>query()
|
||||
.lambda()
|
||||
.eq(PhotoAttachmentEntity::getCreateUser, user.getUserId())
|
||||
.eq(PhotoAttachmentEntity::getStatus, 1));
|
||||
return list.stream().map(PhotoAttachmentEntity::getLink).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user