Skip to content

Commit

Permalink
Merge pull request lilishop#131
Browse files Browse the repository at this point in the history
fix: 增加添加验证码时,检查验证码是否可以正常生成问题
  • Loading branch information
LeiGaoRobot authored Oct 31, 2024
2 parents dc1d1a7 + d4855b7 commit 3a615a3
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,13 @@ public interface VerificationService {
* @return 操作结果
*/
boolean check(String uuid, VerificationEnums verificationEnums);


/**
* 检测是否可以生成验证码
*
* @param type 验证码类型
* @param filePath 文件路径
*/
Boolean checkCreateVerification(String type, String filePath);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<VerificationSource> verificationResources = verificationDTO.getVerificationResources();

List<VerificationSource> 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;
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,9 @@ public class VerificationSourceController {
@Autowired
private VerificationSourceService verificationSourceService;

@Autowired
private VerificationService verificationService;

@GetMapping(value = "/{id}")
@ApiOperation(value = "查看验证码资源维护详情")
public ResultMessage<VerificationSource> get(@PathVariable String id) {
Expand All @@ -53,6 +57,7 @@ public ResultMessage<IPage<VerificationSource>> getByPage(VerificationSource ent
@DemoSite
public ResultMessage<VerificationSource> save(VerificationSource verificationSource) {

verificationService.checkCreateVerification(verificationSource.getType(), verificationSource.getResource());
verificationSourceService.save(verificationSource);
verificationSourceService.initCache();
return ResultUtil.data(verificationSource);
Expand All @@ -63,6 +68,7 @@ public ResultMessage<VerificationSource> save(VerificationSource verificationSou
@DemoSite
public ResultMessage<VerificationSource> update(@PathVariable String id, VerificationSource verificationSource) {
verificationSource.setId(id);
verificationService.checkCreateVerification(verificationSource.getType(), verificationSource.getResource());
verificationSourceService.updateById(verificationSource);
verificationSourceService.initCache();
return ResultUtil.data(verificationSource);
Expand Down

0 comments on commit 3a615a3

Please sign in to comment.