|
|
|
@ -0,0 +1,79 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* https://www.mall4j.com/
|
|
|
|
|
*
|
|
|
|
|
* 未经允许,不可做商业用途!
|
|
|
|
|
*
|
|
|
|
|
* 版权所有,侵权必究!
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.yami.shop.security.common.controller;
|
|
|
|
|
|
|
|
|
|
import com.anji.captcha.model.common.RepCodeEnum;
|
|
|
|
|
import com.anji.captcha.model.common.ResponseModel;
|
|
|
|
|
import com.anji.captcha.model.vo.CaptchaVO;
|
|
|
|
|
import com.anji.captcha.service.CaptchaService;
|
|
|
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
|
|
|
|
import com.yami.shop.common.response.ServerResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.annotation.PostMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* CaptchaController类是一个Spring RESTful风格的控制器类(通过@RestController注解标识),
|
|
|
|
|
* 主要负责处理与验证码相关的HTTP请求,如获取验证码、验证验证码等操作。
|
|
|
|
|
* 它被映射到"/captcha"路径下(通过@RequestMapping注解配置),并且在Swagger文档中被标记为"验证码"相关的接口(通过@Tag注解定义),方便接口文档的展示和查看。
|
|
|
|
|
*
|
|
|
|
|
* @author 菠萝凤梨
|
|
|
|
|
* @date 2022/3/25 17:33
|
|
|
|
|
*/
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/captcha")
|
|
|
|
|
@Tag(name = "验证码")
|
|
|
|
|
public class CaptchaController {
|
|
|
|
|
|
|
|
|
|
// 通过构造函数注入的方式引入CaptchaService,用于后续调用验证码相关的业务逻辑方法,比如生成验证码、验证验证码有效性等操作
|
|
|
|
|
private final CaptchaService captchaService;
|
|
|
|
|
|
|
|
|
|
public CaptchaController(CaptchaService captchaService) {
|
|
|
|
|
this.captchaService = captchaService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* get方法是一个处理HTTP POST请求的方法,用于获取验证码信息。
|
|
|
|
|
* 它接收一个CaptchaVO类型的请求体参数(通过@RequestBody注解标识),该参数包含了获取验证码时可能需要的一些配置信息或参数(具体取决于CaptchaVO的定义)。
|
|
|
|
|
* 方法内部调用captchaService的get方法来实际获取验证码相关的数据,并将获取到的结果包装在ServerResponseEntity中返回给客户端,
|
|
|
|
|
* 表示操作成功以及返回对应的验证码相关信息(如果成功获取的话)。
|
|
|
|
|
*
|
|
|
|
|
* @param captchaVO 包含获取验证码相关配置或参数的请求体对象。
|
|
|
|
|
* @return 返回一个ServerResponseEntity,其中包装了验证码服务获取到的ResponseModel类型的结果,向客户端表示操作成功并传递验证码相关数据。
|
|
|
|
|
*/
|
|
|
|
|
@PostMapping({ "/get" })
|
|
|
|
|
public ServerResponseEntity<ResponseModel> get(@RequestBody CaptchaVO captchaVO) {
|
|
|
|
|
return ServerResponseEntity.success(captchaService.get(captchaVO));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* check方法同样是一个处理HTTP POST请求的方法,用于验证客户端提交的验证码是否正确。
|
|
|
|
|
* 它也接收一个CaptchaVO类型的请求体参数,该参数包含了待验证的验证码以及可能相关的验证辅助信息(同样取决于CaptchaVO的定义)。
|
|
|
|
|
* 方法内部首先尝试调用captchaService的check方法来验证验证码,如果验证过程中没有出现异常,则将验证结果包装在ServerResponseEntity中返回给客户端,
|
|
|
|
|
* 表示验证操作成功以及返回对应的验证结果(如验证码是否正确等信息)。
|
|
|
|
|
* 如果在验证过程中出现异常,会捕获该异常,并返回一个表示验证码坐标错误的默认错误响应(通过ResponseModel.errorMsg方法构建对应错误消息的ResponseModel对象),
|
|
|
|
|
* 同样包装在ServerResponseEntity中返回给客户端,提示客户端验证码验证出现问题。
|
|
|
|
|
*
|
|
|
|
|
* @param captchaVO 包含待验证验证码及相关信息的请求体对象。
|
|
|
|
|
* @return 返回一个ServerResponseEntity,其中包装了验证码服务验证后的ResponseModel类型的结果,向客户端反馈验证操作的成功与否及相应信息。
|
|
|
|
|
*/
|
|
|
|
|
@PostMapping({ "/check" })
|
|
|
|
|
public ServerResponseEntity<ResponseModel> check(@RequestBody CaptchaVO captchaVO) {
|
|
|
|
|
ResponseModel responseModel;
|
|
|
|
|
try {
|
|
|
|
|
responseModel = captchaService.check(captchaVO);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
return ServerResponseEntity.success(ResponseModel.errorMsg(RepCodeEnum.API_CAPTCHA_COORDINATE_ERROR));
|
|
|
|
|
}
|
|
|
|
|
return ServerResponseEntity.success(responseModel);
|
|
|
|
|
}
|
|
|
|
|
}
|