feat:验证码(通用数字字母验证码)接口开发

This commit is contained in:
xiang
2025-09-04 21:31:21 +08:00
parent 5f3282164f
commit 95252f3775
2 changed files with 46 additions and 1 deletions

View File

@@ -0,0 +1,41 @@
package com.xiang.xservice.auth.server.controller;
import com.xiang.xservice.auth.service.enums.CaptchaTypeEnum;
import com.xiang.xservice.auth.service.service.CaptchaGenerateFactory;
import com.xiang.xservice.basic.common.resp.Result;
import com.xiang.xservice.basic.utils.RandomCodeUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequiredArgsConstructor
public class CaptchaImageController {
private final CaptchaGenerateFactory captchaGenerateFactory;
@PostMapping("/public/captchaImage")
public Result<String> getCaptchaImage() {
try {
String randomCode = RandomCodeUtils.getNumberRandomCode(1);
String captchaImage;
if (StringUtils.isBlank(randomCode)) {
captchaImage = captchaGenerateFactory.get(CaptchaTypeEnum.NORMAL_CAPTCHA_IMAGE.getType()).getCaptchaImage();
} else {
try {
int i = Integer.parseInt(randomCode);
captchaImage = captchaGenerateFactory.get(i).getCaptchaImage();
} catch (Exception e) {
captchaImage = captchaGenerateFactory.get(CaptchaTypeEnum.NORMAL_CAPTCHA_IMAGE.getType()).getCaptchaImage();
}
}
return Result.success(captchaImage);
} catch (Exception e) {
log.error("获取验证码失败", e);
}
return Result.error();
}
}

View File

@@ -23,6 +23,10 @@ public class CaptchaGenerateFactory {
if (Objects.isNull(type)) {
return strategies.get(CaptchaTypeEnum.NORMAL_CAPTCHA_IMAGE.getType());
}
return strategies.get(type);
ICaptchaService captchaService = strategies.get(type);
if (Objects.isNull(captchaService)) {
return strategies.get(CaptchaTypeEnum.NORMAL_CAPTCHA_IMAGE.getType());
}
return captchaService;
}
}