feat: 设备二维码优化

This commit is contained in:
曹鹏飞 2025-05-08 16:55:32 +08:00
parent 96d52dcdb2
commit 8371104912
3 changed files with 22 additions and 14 deletions

View File

@ -708,7 +708,7 @@ public class DeviceController extends ControllerBase {
devices.forEach(device -> {
try {
DeviceQRCodeVO vo=new DeviceQRCodeVO();
byte[] bytes=deviceQRCodeService.generate(device.getDeviceNo());
byte[] bytes=deviceQRCodeService.generate(device.getDeviceNo(),device.getModelNo());
vo.setDeviceNo(device.getDeviceNo());
vo.setUrl(fileUploadService.upload("temp/qrcode/device/"+IdUtil.fastUUID()+".png",new ByteArrayInputStream(bytes)));
vos.add(vo);
@ -725,14 +725,14 @@ public class DeviceController extends ControllerBase {
*/
@PostMapping("exportImages")
public ResponseEntity<byte[]> exportImages(@Valid @RequestBody IdPostRequest request){
Collection<String> deviceNos=deviceService.listByIds(request.getIds()).stream().map(Device::getDeviceNo).collect(Collectors.toSet());
if (CollUtil.isNotEmpty(deviceNos)){
List<Device> devices=deviceService.listByIds(request.getIds());
if (CollUtil.isNotEmpty(devices)){
try {
if (deviceNos.size()==1){
String deviceNo=deviceNos.stream().findFirst().get();
byte[] bytes=deviceQRCodeService.generate(deviceNo);
if (devices.size()==1){
Device device=devices.get(0);
byte[] bytes=deviceQRCodeService.generate(device.getDeviceNo(),device.getModelNo());
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+ deviceNo+".jpg");
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename="+ device.getDeviceNo()+".jpg");
return ResponseEntity.ok()
.headers(headers)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
@ -741,10 +741,15 @@ public class DeviceController extends ControllerBase {
Path tempZipFile = Files.createTempFile("files", ".zip");
try (FileOutputStream fos = new FileOutputStream(tempZipFile.toFile());
ZipOutputStream zos = new ZipOutputStream(fos)) {
for (String deviceNo : deviceNos) {
ZipEntry zipEntry = new ZipEntry(deviceNo+".jpg");
List<Device> tds=new ArrayList<>();
for (Device device : devices) {
if (tds.stream().anyMatch(td -> StrUtil.equals(td.getDeviceNo(), device.getDeviceNo()))){
continue;
}
tds.add(device);
ZipEntry zipEntry = new ZipEntry(device.getDeviceNo()+".jpg");
zos.putNextEntry(zipEntry);
byte[] bytes=deviceQRCodeService.generate(deviceNo);
byte[] bytes=deviceQRCodeService.generate(device.getDeviceNo(),device.getModelNo());
zos.write(bytes, 0, bytes.length);
zos.closeEntry();
}

View File

@ -26,9 +26,11 @@ public class TestController extends ControllerBase{
* @param deviceNo 设备编号
*/
@GetMapping("showDeviceQRCode")
public void showDeviceQRCode(HttpServletResponse response, @RequestParam(defaultValue = "dsadwe3d3cdsd") String deviceNo){
public void showDeviceQRCode(HttpServletResponse response
, @RequestParam(defaultValue = "dsadwe3d3cdsd") String deviceNo
, @RequestParam(defaultValue = "sadasd") String modelNo){
try {
byte[] bytes=deviceQRCodeService.generate(deviceNo);
byte[] bytes=deviceQRCodeService.generate(deviceNo,modelNo);
response.setContentType("image/png");
response.setHeader("Content-Disposition", "attachment;filename=qrcode.png");
try (OutputStream outputStream = response.getOutputStream()) {

View File

@ -1,5 +1,6 @@
package com.nflg.mobilebroken.admin.service;
import cn.hutool.core.util.StrUtil;
import com.nflg.mobilebroken.common.util.QRCodeUtil;
import com.nflg.mobilebroken.repository.entity.ParamConfig;
import com.nflg.mobilebroken.repository.service.IParamConfigService;
@ -15,8 +16,8 @@ public class DeviceQRCodeService {
@Resource
private IParamConfigService paramConfigService;
public byte[] generate(String deviceNo) throws Exception {
public byte[] generate(String deviceNo, String modelNo) throws Exception {
ParamConfig config = paramConfigService.lambdaQuery().eq(ParamConfig::getCode, "DeviceQRCodeHost").one();
return QRCodeUtil.generateDeviceQRCode(deviceNo ,config.getValue() + deviceNo,200, 200);
return QRCodeUtil.generateDeviceQRCode(deviceNo ,StrUtil.format(config.getValue(), deviceNo,modelNo),200, 200);
}
}