修改生成二维码的逻辑, 将保存目录设置成项目根目录下, 同时将旧的二维码图片删除
This commit is contained in:
parent
78c6b5bead
commit
ebd915c0a3
|
|
@ -26,13 +26,7 @@ import com.mdd.common.mapper.admin.AdminMapper;
|
|||
import com.mdd.common.mapper.system.SystemRoleMapper;
|
||||
import com.mdd.common.exception.OperateException;
|
||||
import com.mdd.common.service.RegisterService;
|
||||
import com.mdd.common.util.ConfigUtils;
|
||||
import com.mdd.common.util.QrCodeUtil;
|
||||
import com.mdd.common.util.RandomUtil;
|
||||
import com.mdd.common.util.TimeUtils;
|
||||
import com.mdd.common.util.ToolUtils;
|
||||
import com.mdd.common.util.UrlUtils;
|
||||
import com.mdd.common.util.YmlUtils;
|
||||
import com.mdd.common.util.*;
|
||||
import com.google.zxing.WriterException;
|
||||
|
||||
import java.io.File;
|
||||
|
|
@ -393,6 +387,9 @@ public class TeacherServiceImpl implements ITeacherService {
|
|||
// 生成二维码内容URL(预报名页面地址)
|
||||
String qrCodeContent = buildQrCodeUrl(invitationCode);
|
||||
|
||||
// 删除旧的二维码文件(如果存在)
|
||||
deleteOldQrCodeFile(teacher.getQrcodeUrl());
|
||||
|
||||
// 生成二维码图片并保存
|
||||
String qrcodeUrl = generateAndSaveQrCode(invitationCode, qrCodeContent, teacherId);
|
||||
|
||||
|
|
@ -480,13 +477,8 @@ public class TeacherServiceImpl implements ITeacherService {
|
|||
|
||||
if ("local".equals(engine)) {
|
||||
// ========== 方案B:本地存储 ==========
|
||||
String directory = YmlUtils.get("like.upload-directory");
|
||||
if (directory == null || directory.isEmpty()) {
|
||||
throw new OperateException("请配置上传目录like.upload-directory");
|
||||
}
|
||||
|
||||
String folder = "qrcode/teacher";
|
||||
String savePath = (directory + folder + "/" + date).replace("\\", "/");
|
||||
String savePath = (folder + "/" + date).replace("\\", "/");
|
||||
File saveDir = new File(savePath);
|
||||
if (!saveDir.exists()) {
|
||||
if (!saveDir.mkdirs()) {
|
||||
|
|
@ -594,13 +586,8 @@ public class TeacherServiceImpl implements ITeacherService {
|
|||
if (!"local".equals(engine)) {
|
||||
return null;
|
||||
}
|
||||
String directory = YmlUtils.get("like.upload-directory");
|
||||
if (directory == null || directory.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
String path = teacher.getQrcodeUrl().startsWith("/") ? teacher.getQrcodeUrl() : "/" + teacher.getQrcodeUrl();
|
||||
String fullPath = (directory + path).replace("\\", "/");
|
||||
java.io.File file = new java.io.File(fullPath);
|
||||
String path = teacher.getQrcodeUrl().startsWith("/") ? teacher.getQrcodeUrl().substring(1) : teacher.getQrcodeUrl();
|
||||
java.io.File file = new java.io.File(path);
|
||||
if (!file.exists() || !file.isFile()) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -610,4 +597,26 @@ public class TeacherServiceImpl implements ITeacherService {
|
|||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除旧的二维码文件
|
||||
*
|
||||
* @param oldQrcodeUrl 旧的二维码相对路径
|
||||
*/
|
||||
private void deleteOldQrCodeFile(String oldQrcodeUrl) {
|
||||
if (StringUtils.isBlank(oldQrcodeUrl)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
String path = oldQrcodeUrl.startsWith("/") ? oldQrcodeUrl.substring(1) : oldQrcodeUrl;
|
||||
File oldFile = new File(path);
|
||||
if (oldFile.exists() && oldFile.isFile()) {
|
||||
if (!oldFile.delete()) {
|
||||
System.err.println("删除旧二维码文件失败: " + path);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.err.println("删除旧二维码文件时发生异常: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue