[feat] 短信验证码获取接口

This commit is contained in:
hsdllcw
2024-08-07 10:56:50 +08:00
parent a265ca8303
commit 25613442a4
5 changed files with 98 additions and 69 deletions

View File

@@ -118,11 +118,6 @@
<version>${kotlin.version}</version> <version>${kotlin.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies> </dependencies>

View File

@@ -1,41 +1,51 @@
package com.ruoyi.gateway.handler; package com.ruoyi.gateway.handler;
import java.io.IOException; import java.io.IOException;
import org.springframework.beans.factory.annotation.Autowired; import java.util.Objects;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.reactive.function.BodyInserters; import org.springframework.http.HttpStatus;
import org.springframework.web.reactive.function.server.HandlerFunction; import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerResponse; import org.springframework.web.reactive.function.server.HandlerFunction;
import com.ruoyi.common.core.exception.CaptchaException; import org.springframework.web.reactive.function.server.ServerRequest;
import com.ruoyi.common.core.web.domain.AjaxResult; import org.springframework.web.reactive.function.server.ServerResponse;
import com.ruoyi.gateway.service.ValidateCodeService; import com.ruoyi.common.core.exception.CaptchaException;
import reactor.core.publisher.Mono; import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.gateway.service.ValidateCodeService;
/** import reactor.core.publisher.Mono;
* 验证码获取
* /**
* @author ruoyi * 验证码获取
*/ *
@Component * @author ruoyi
public class ValidateCodeHandler implements HandlerFunction<ServerResponse> */
{ @Component
@Autowired public class ValidateCodeHandler implements HandlerFunction<ServerResponse>
private ValidateCodeService validateCodeService; {
@Autowired
@Override private ValidateCodeService validateCodeService;
public Mono<ServerResponse> handle(ServerRequest serverRequest)
{ @Override
AjaxResult ajax; public Mono<ServerResponse> handle(ServerRequest serverRequest)
try {
{ AjaxResult ajax;
ajax = validateCodeService.createCaptcha(); try
} {
catch (CaptchaException | IOException e) switch (serverRequest.queryParam("type").orElse("image")) {
{ case "sms":
return Mono.error(e); if (Objects.nonNull(serverRequest.queryParam("receiver").orElse(null))) {
} ajax = validateCodeService.createSMSCaptcha(serverRequest.queryParam("receiver").orElse(null));
return ServerResponse.status(HttpStatus.OK).body(BodyInserters.fromValue(ajax)); break;
} }
} default:
ajax = validateCodeService.createCaptcha();
}
}
catch (CaptchaException | IOException e)
{
return Mono.error(e);
}
return ServerResponse.status(HttpStatus.OK).body(BodyInserters.fromValue(ajax));
}
}

View File

@@ -1,23 +1,28 @@
package com.ruoyi.gateway.service; package com.ruoyi.gateway.service;
import java.io.IOException; import java.io.IOException;
import com.ruoyi.common.core.exception.CaptchaException; import com.ruoyi.common.core.exception.CaptchaException;
import com.ruoyi.common.core.web.domain.AjaxResult; import com.ruoyi.common.core.web.domain.AjaxResult;
/** /**
* 验证码处理 * 验证码处理
* *
* @author ruoyi * @author ruoyi
*/ */
public interface ValidateCodeService public interface ValidateCodeService
{ {
/** /**
* 生成验证码 * 生成图片验证码
*/ */
public AjaxResult createCaptcha() throws IOException, CaptchaException; public AjaxResult createCaptcha() throws IOException, CaptchaException;
/** /**
* 校验验证码 * 生成短信验证码
*/ */
public void checkCaptcha(String key, String value) throws CaptchaException; public AjaxResult createSMSCaptcha(String receiver) throws IOException, CaptchaException;
}
/**
* 校验验证码
*/
public void checkCaptcha(String key, String value) throws CaptchaException;
}

View File

@@ -103,6 +103,25 @@ public class ValidateCodeServiceImpl implements ValidateCodeService
return ajax; return ajax;
} }
@Override
public AjaxResult createSMSCaptcha(String receiver) throws IOException, CaptchaException {
AjaxResult ajax = AjaxResult.success();
boolean captchaEnabled = captchaProperties.getEnabled();
ajax.put("captchaEnabled", captchaEnabled);
if (!captchaEnabled)
{
return ajax;
}
// 保存验证码信息
String uuid = IdUtils.simpleUUID();
String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
String code = captchaProducerNumber.createText();
redisService.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
ajax.put("code", code);
ajax.put("uuid", uuid);
return ajax;
}
/** /**
* 校验验证码 * 校验验证码
*/ */