|
@@ -1,5 +1,6 @@
|
|
package org.dromara.system.utils;
|
|
package org.dromara.system.utils;
|
|
|
|
|
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.BarcodeFormat;
|
|
import com.google.zxing.EncodeHintType;
|
|
import com.google.zxing.EncodeHintType;
|
|
import com.google.zxing.WriterException;
|
|
import com.google.zxing.WriterException;
|
|
@@ -7,14 +8,19 @@ import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
import com.google.zxing.common.BitMatrix;
|
|
import com.google.zxing.common.BitMatrix;
|
|
import com.google.zxing.qrcode.QRCodeWriter;
|
|
import com.google.zxing.qrcode.QRCodeWriter;
|
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
|
|
|
|
+import jakarta.annotation.Resource;
|
|
|
|
+import org.dromara.system.domain.app.QRCodeReferee;
|
|
|
|
+import org.dromara.system.domain.vo.GameEventProjectVo;
|
|
|
|
+import org.dromara.system.domain.vo.GameEventVo;
|
|
|
|
+import org.dromara.system.domain.vo.GameRefereeVo;
|
|
|
|
+import org.dromara.system.service.IGameEventProjectService;
|
|
|
|
+import org.dromara.system.service.IGameEventService;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.IOException;
|
|
-import java.util.Base64;
|
|
|
|
-import java.util.HashMap;
|
|
|
|
-import java.util.Map;
|
|
|
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
/**
|
|
* 二维码生成工具类
|
|
* 二维码生成工具类
|
|
@@ -25,6 +31,15 @@ import java.util.Map;
|
|
@Component
|
|
@Component
|
|
public class QRCodeUtils {
|
|
public class QRCodeUtils {
|
|
|
|
|
|
|
|
+ @Resource
|
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private IGameEventProjectService gameEventProjectService;
|
|
|
|
+
|
|
|
|
+ @Resource
|
|
|
|
+ private IGameEventService gameEventService;
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 生成二维码并返回Base64编码的图片数据
|
|
* 生成二维码并返回Base64编码的图片数据
|
|
*
|
|
*
|
|
@@ -37,9 +52,9 @@ public class QRCodeUtils {
|
|
try {
|
|
try {
|
|
// 设置二维码参数
|
|
// 设置二维码参数
|
|
Map<EncodeHintType, Object> hints = new HashMap<>();
|
|
Map<EncodeHintType, Object> hints = new HashMap<>();
|
|
- hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
|
|
|
|
|
|
+ hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 提高容错级别
|
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
|
|
- hints.put(EncodeHintType.MARGIN, 1);
|
|
|
|
|
|
+ hints.put(EncodeHintType.MARGIN, 2);// 增加边距
|
|
|
|
|
|
// 生成二维码
|
|
// 生成二维码
|
|
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
|
QRCodeWriter qrCodeWriter = new QRCodeWriter();
|
|
@@ -62,12 +77,118 @@ public class QRCodeUtils {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 生成二维码并返回Base64编码的图片数据(默认尺寸200x200)
|
|
|
|
|
|
+ * 生成二维码并返回Base64编码的图片数据(默认尺寸300x300)
|
|
*
|
|
*
|
|
* @param content 二维码内容
|
|
* @param content 二维码内容
|
|
* @return Base64编码的图片数据
|
|
* @return Base64编码的图片数据
|
|
*/
|
|
*/
|
|
public String generateQRCodeBase64(String content) {
|
|
public String generateQRCodeBase64(String content) {
|
|
- return generateQRCodeBase64(content, 200, 200);
|
|
|
|
|
|
+ return generateQRCodeBase64(content, 300, 300);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据裁判信息构建二维码载荷对象(返回 QRCodeReferee)
|
|
|
|
+ */
|
|
|
|
+ public QRCodeReferee buildRefereeData(GameRefereeVo referee) {
|
|
|
|
+ if (referee == null) {
|
|
|
|
+ return null;
|
|
|
|
+ }
|
|
|
|
+ QRCodeReferee data = new QRCodeReferee();
|
|
|
|
+ data.setEventId(referee.getEventId());
|
|
|
|
+ // eventName
|
|
|
|
+ try {
|
|
|
|
+ if (referee.getEventId() != null) {
|
|
|
|
+ GameEventVo eventVo = gameEventService.queryById(referee.getEventId());
|
|
|
|
+ if (eventVo != null) {
|
|
|
|
+ data.setEventName(eventVo.getEventName());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception ignore) {}
|
|
|
|
+ data.setRefereeId(referee.getRefereeId());
|
|
|
|
+ data.setName(referee.getName());
|
|
|
|
+ data.setAccount(referee.getAccount());
|
|
|
|
+ data.setPassword(referee.getPassword());
|
|
|
|
+
|
|
|
|
+ // 将项目ID列表转换为项目VO列表
|
|
|
|
+ List<Long> projectIds = referee.getProjectList2();
|
|
|
|
+ if (projectIds != null && !projectIds.isEmpty()) {
|
|
|
|
+ List<GameEventProjectVo> projects = new ArrayList<>();
|
|
|
|
+ for (Long pid : projectIds) {
|
|
|
|
+ if (pid == null) continue;
|
|
|
|
+ GameEventProjectVo pvo = gameEventProjectService.queryById(pid);
|
|
|
|
+ if (pvo != null) {
|
|
|
|
+ projects.add(pvo);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ data.setProjectList(projects);
|
|
|
|
+ } else {
|
|
|
|
+ data.setProjectList(Collections.emptyList());
|
|
|
|
+ }
|
|
|
|
+ return data;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public String buildRefereeData2(GameRefereeVo referee) {
|
|
|
|
+ try {
|
|
|
|
+ // 创建简化的数据结构,只包含必要信息
|
|
|
|
+ Map<String, Object> simpleData = new HashMap<>();
|
|
|
|
+ simpleData.put("eventId", referee.getEventId());
|
|
|
|
+ try {
|
|
|
|
+ if (referee.getEventId() != null) {
|
|
|
|
+ GameEventVo eventVo = gameEventService.queryById(referee.getEventId());
|
|
|
|
+ if (eventVo != null) {
|
|
|
|
+ simpleData.put("eventName", eventVo.getEventName());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception ignore) {}
|
|
|
|
+ simpleData.put("refereeId", referee.getRefereeId());
|
|
|
|
+ simpleData.put("refereeName", referee.getName());
|
|
|
|
+ simpleData.put("account", referee.getAccount());
|
|
|
|
+ simpleData.put("password", referee.getPassword());
|
|
|
|
+
|
|
|
|
+ // 将项目ID列表转换为项目VO列表
|
|
|
|
+ List<Long> projectIds = referee.getProjectList2();
|
|
|
|
+ if (projectIds != null && !projectIds.isEmpty()) {
|
|
|
|
+ Map<String, Object> projectList = new HashMap<>();
|
|
|
|
+ for (Long pid : projectIds) {
|
|
|
|
+ if (pid == null) continue;
|
|
|
|
+ GameEventProjectVo pvo = gameEventProjectService.queryById(pid);
|
|
|
|
+ if (pvo != null) {
|
|
|
|
+ Map<String, Object> project = new HashMap<>();
|
|
|
|
+ project.put("projectType", pvo.getProjectType());
|
|
|
|
+ project.put("projectName", pvo.getProjectName());
|
|
|
|
+ project.put("classification", pvo.getClassification());
|
|
|
|
+ project.put("orderType", pvo.getOrderType());
|
|
|
|
+ project.put("gradeRule", pvo.getScoreRule());
|
|
|
|
+ project.put("admissionRank",pvo.getRoundType());
|
|
|
|
+ project.put("admissionPoint",pvo.getScoreValue());
|
|
|
|
+ projectList.put(pvo.getProjectId().toString(), project);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ simpleData.put("projectList", projectList);
|
|
|
|
+ } else {
|
|
|
|
+ simpleData.put("projectList", Collections.emptyMap());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ String content = objectMapper.writeValueAsString(simpleData);
|
|
|
|
+ System.out.println("二维码内容长度: " + content.length());
|
|
|
|
+ System.out.println("二维码内容: " + content);
|
|
|
|
+
|
|
|
|
+ return generateQRCodeBase64(content, 400, 400); // 使用更大的尺寸
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException("生成裁判二维码失败", e);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成裁判二维码(载荷为 QRCodeReferee 的 JSON)并返回Base64
|
|
|
|
+ */
|
|
|
|
+ public String generateRefereeQRCodeBase64(GameRefereeVo referee) {
|
|
|
|
+ try {
|
|
|
|
+ QRCodeReferee payload = buildRefereeData(referee);
|
|
|
|
+ String content = objectMapper.writeValueAsString(payload);
|
|
|
|
+ return generateQRCodeBase64(content);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ throw new RuntimeException("生成裁判二维码失败", e);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|