From d4855b702bb73406d0b545f347297cc838ead469 Mon Sep 17 00:00:00 2001 From: misworga831 Date: Thu, 31 Oct 2024 11:47:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E6=97=B6,=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E7=A0=81=E6=98=AF=E5=90=A6=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E6=AD=A3=E5=B8=B8=E7=94=9F=E6=88=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/VerificationService.java | 9 ++++ .../service/impl/VerificationServiceImpl.java | 44 +++++++++++++++++++ .../other/VerificationSourceController.java | 6 +++ 3 files changed, 59 insertions(+) diff --git a/framework/src/main/java/cn/lili/modules/verification/service/VerificationService.java b/framework/src/main/java/cn/lili/modules/verification/service/VerificationService.java index 40d709c59..df36afd1e 100644 --- a/framework/src/main/java/cn/lili/modules/verification/service/VerificationService.java +++ b/framework/src/main/java/cn/lili/modules/verification/service/VerificationService.java @@ -40,4 +40,13 @@ public interface VerificationService { * @return 操作结果 */ boolean check(String uuid, VerificationEnums verificationEnums); + + + /** + * 检测是否可以生成验证码 + * + * @param type 验证码类型 + * @param filePath 文件路径 + */ + Boolean checkCreateVerification(String type, String filePath); } diff --git a/framework/src/main/java/cn/lili/modules/verification/service/impl/VerificationServiceImpl.java b/framework/src/main/java/cn/lili/modules/verification/service/impl/VerificationServiceImpl.java index edf3c9f51..155478b28 100644 --- a/framework/src/main/java/cn/lili/modules/verification/service/impl/VerificationServiceImpl.java +++ b/framework/src/main/java/cn/lili/modules/verification/service/impl/VerificationServiceImpl.java @@ -1,5 +1,6 @@ package cn.lili.modules.verification.service.impl; +import cn.hutool.core.text.CharSequenceUtil; import cn.lili.cache.Cache; import cn.lili.cache.CachePrefix; import cn.lili.common.enums.ResultCode; @@ -11,6 +12,7 @@ import cn.lili.modules.verification.entity.dos.VerificationSource; import cn.lili.modules.verification.entity.dto.VerificationDTO; import cn.lili.modules.verification.entity.enums.VerificationEnums; +import cn.lili.modules.verification.entity.enums.VerificationSourceEnum; import cn.lili.modules.verification.service.VerificationService; import cn.lili.modules.verification.service.VerificationSourceService; import lombok.extern.slf4j.Slf4j; @@ -185,5 +187,47 @@ public static String cacheResult(VerificationEnums verificationEnums, String uui return CachePrefix.VERIFICATION_RESULT.getPrefix() + verificationEnums.name() + uuid; } + + @Override + public Boolean checkCreateVerification(String type, String filePath) { + if (CharSequenceUtil.isBlank(type) || CharSequenceUtil.isBlank(filePath)) { + throw new ServiceException(ResultCode.ILLEGAL_REQUEST_ERROR); + } + + //获取验证码配置 + VerificationDTO verificationDTO = verificationSourceService.getVerificationCache(); + + List verificationResources = verificationDTO.getVerificationResources(); + + List verificationSlider = verificationDTO.getVerificationSlider(); + + //随机选择需要切的图片地址 + String originalResource = verificationResources.get(0).getResource(); + //随机选择剪切模版图片地址 + String sliderResource = verificationSlider.get(0).getResource(); + + + if (VerificationSourceEnum.RESOURCE.name().equals(type)) { + originalResource = filePath; + } else if (VerificationSourceEnum.SLIDER.name().equals(type)) { + sliderResource = filePath; + } + + try { + //获取缓存中的资源 + SerializableStream originalFile = getInputStream(originalResource); + SerializableStream sliderFile = getInputStream(sliderResource); + //生成数据 + SliderImageUtil.pictureTemplatesCut( + sliderFile, sliderFile, originalFile, + verificationCodeProperties.getWatermark(), + verificationCodeProperties.getInterfereNum() + ); + } catch (Exception e) { + throw new ServiceException("当前图片不符合规则,请上传正确格式的图片!"); + } + return true; + } + } diff --git a/manager-api/src/main/java/cn/lili/controller/other/VerificationSourceController.java b/manager-api/src/main/java/cn/lili/controller/other/VerificationSourceController.java index c67a0962a..bfb849c3a 100644 --- a/manager-api/src/main/java/cn/lili/controller/other/VerificationSourceController.java +++ b/manager-api/src/main/java/cn/lili/controller/other/VerificationSourceController.java @@ -6,6 +6,7 @@ import cn.lili.common.vo.ResultMessage; import cn.lili.common.vo.SearchVO; import cn.lili.modules.verification.entity.dos.VerificationSource; +import cn.lili.modules.verification.service.VerificationService; import cn.lili.modules.verification.service.VerificationSourceService; import cn.lili.mybatis.util.PageUtil; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -31,6 +32,9 @@ public class VerificationSourceController { @Autowired private VerificationSourceService verificationSourceService; + @Autowired + private VerificationService verificationService; + @GetMapping(value = "/{id}") @ApiOperation(value = "查看验证码资源维护详情") public ResultMessage get(@PathVariable String id) { @@ -53,6 +57,7 @@ public ResultMessage> getByPage(VerificationSource ent @DemoSite public ResultMessage save(VerificationSource verificationSource) { + verificationService.checkCreateVerification(verificationSource.getType(), verificationSource.getResource()); verificationSourceService.save(verificationSource); verificationSourceService.initCache(); return ResultUtil.data(verificationSource); @@ -63,6 +68,7 @@ public ResultMessage save(VerificationSource verificationSou @DemoSite public ResultMessage update(@PathVariable String id, VerificationSource verificationSource) { verificationSource.setId(id); + verificationService.checkCreateVerification(verificationSource.getType(), verificationSource.getResource()); verificationSourceService.updateById(verificationSource); verificationSourceService.initCache(); return ResultUtil.data(verificationSource);