feat(device): 添加设备二维码导出功能
- 新增设备二维码导出接口,支持单个或多个设备二维码的导出 -优化二维码生成逻辑,增加边框和文本的处理
This commit is contained in:
parent
b78c9fe3bf
commit
02c8b8f089
|
|
@ -20,6 +20,7 @@ import com.nflg.mobilebroken.common.pojo.ApiResult;
|
|||
import com.nflg.mobilebroken.common.pojo.PageData;
|
||||
import com.nflg.mobilebroken.common.pojo.dto.DeviceAddImportDTO;
|
||||
import com.nflg.mobilebroken.common.pojo.dto.DeviceUpdateImportDTO;
|
||||
import com.nflg.mobilebroken.common.pojo.request.IdPostRequest;
|
||||
import com.nflg.mobilebroken.common.pojo.vo.DeviceQRCodeVO;
|
||||
import com.nflg.mobilebroken.common.util.*;
|
||||
import com.nflg.mobilebroken.repository.entity.*;
|
||||
|
|
@ -29,6 +30,7 @@ import com.nflg.mobilebroken.starter.service.FileUploadService;
|
|||
import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
|
@ -41,14 +43,19 @@ import javax.validation.Valid;
|
|||
import javax.validation.constraints.NotEmpty;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
/**
|
||||
* 设备管理
|
||||
|
|
@ -687,11 +694,11 @@ public class DeviceController extends ControllerBase {
|
|||
|
||||
/**
|
||||
* 获取设备二维码
|
||||
* @param ids 设备id列表
|
||||
* @param request 请求参数
|
||||
*/
|
||||
@PostMapping("getQRCode")
|
||||
public ApiResult<List<DeviceQRCodeVO>> getQRCode(@Valid @RequestBody @NotEmpty List<Integer> ids){
|
||||
List<Device> devices=deviceService.listByIds(ids);
|
||||
public ApiResult<List<DeviceQRCodeVO>> getQRCode(@Valid @RequestBody IdPostRequest request){
|
||||
List<Device> devices=deviceService.listByIds(request.getIds());
|
||||
List<DeviceQRCodeVO> vos=new ArrayList<>();
|
||||
devices.forEach(device -> {
|
||||
try {
|
||||
|
|
@ -706,4 +713,49 @@ public class DeviceController extends ControllerBase {
|
|||
});
|
||||
return ApiResult.success(vos);
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出设备二维码图片
|
||||
* @param request 请求参数
|
||||
*/
|
||||
@PostMapping("exportImages")
|
||||
public ResponseEntity<byte[]> exportImages(@Valid @RequestBody IdPostRequest request){
|
||||
List<Device> devices=deviceService.listByIds(request.getIds());
|
||||
if (CollUtil.isNotEmpty(devices)){
|
||||
try {
|
||||
if (devices.size()==1){
|
||||
byte[] bytes=deviceQRCodeService.generate(devices.get(0).getDeviceNo());
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+ devices.get(0).getDeviceNo()+".jpg");
|
||||
return ResponseEntity.ok()
|
||||
.headers(headers)
|
||||
.contentType(MediaType.IMAGE_JPEG)
|
||||
.body(bytes);
|
||||
}else {
|
||||
Path tempZipFile = Files.createTempFile("files", ".zip");
|
||||
try (FileOutputStream fos = new FileOutputStream(tempZipFile.toFile());
|
||||
ZipOutputStream zos = new ZipOutputStream(fos)) {
|
||||
for (Device device : devices) {
|
||||
ZipEntry zipEntry = new ZipEntry(device.getDeviceNo()+".jpg");
|
||||
zos.putNextEntry(zipEntry);
|
||||
byte[] bytes=deviceQRCodeService.generate(device.getDeviceNo());
|
||||
zos.write(bytes, 0, bytes.length);
|
||||
zos.closeEntry();
|
||||
}
|
||||
}
|
||||
byte[] zipContent = Files.readAllBytes(tempZipFile);
|
||||
Files.deleteIfExists(tempZipFile);
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+ DateTimeUtil.format(LocalDateTime.now(),"yyyyMMddHHmmss")+".zip");
|
||||
return ResponseEntity.ok()
|
||||
.headers(headers)
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.body(zipContent);
|
||||
}
|
||||
}catch (Exception e) {
|
||||
VUtils.trueThrowBusinessError(true).throwMessage("生成二维码出错");
|
||||
}
|
||||
}
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
package com.nflg.mobilebroken.common.pojo.request;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class IdPostRequest {
|
||||
|
||||
/**
|
||||
* id集合
|
||||
*/
|
||||
@NotEmpty
|
||||
private List<Integer> ids;
|
||||
}
|
||||
|
|
@ -55,8 +55,9 @@ public class QRCodeUtil {
|
|||
*/
|
||||
private static BufferedImage addBorderAndText(BufferedImage qrImage, String text) {
|
||||
int borderSize = 1;
|
||||
int newWidth = qrImage.getWidth();
|
||||
int newHeight = qrImage.getHeight() + 10;
|
||||
int extend=10;
|
||||
int newWidth = qrImage.getWidth() + extend;
|
||||
int newHeight = qrImage.getHeight() + extend;
|
||||
// 创建新画布(扩大尺寸以容纳边框)
|
||||
BufferedImage newImage = new BufferedImage(
|
||||
newWidth,
|
||||
|
|
@ -68,7 +69,7 @@ public class QRCodeUtil {
|
|||
g.setColor(Color.WHITE);
|
||||
g.fillRect(0, 0, newWidth, newHeight);
|
||||
// 绘制原始二维码(居中)
|
||||
g.drawImage(qrImage, borderSize, borderSize, null);
|
||||
g.drawImage(qrImage, borderSize + extend / 2, borderSize, null);
|
||||
// 绘制边框
|
||||
g.setColor(Color.BLACK);
|
||||
g.setStroke(new BasicStroke(borderSize));
|
||||
|
|
|
|||
Loading…
Reference in New Issue