84 lines
2.4 KiB
Go
84 lines
2.4 KiB
Go
package handler
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
|
||
"github.com/user-management-system/internal/service"
|
||
)
|
||
|
||
// CaptchaHandler handles captcha requests
|
||
type CaptchaHandler struct {
|
||
captchaService *service.CaptchaService
|
||
}
|
||
|
||
// NewCaptchaHandler creates a new CaptchaHandler
|
||
func NewCaptchaHandler(captchaService *service.CaptchaService) *CaptchaHandler {
|
||
return &CaptchaHandler{captchaService: captchaService}
|
||
}
|
||
|
||
// GenerateCaptcha 生成验证码
|
||
// @Summary 生成验证码
|
||
// @Description 生成图形验证码
|
||
// @Tags 验证码
|
||
// @Produce json
|
||
// @Success 200 {object} Response{data=CaptchaResponse} "验证码信息"
|
||
// @Router /api/v1/auth/captcha [get]
|
||
func (h *CaptchaHandler) GenerateCaptcha(c *gin.Context) {
|
||
result, err := h.captchaService.Generate(c.Request.Context())
|
||
if err != nil {
|
||
handleError(c, err)
|
||
return
|
||
}
|
||
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"code": 0,
|
||
"message": "success",
|
||
"data": gin.H{
|
||
"captcha_id": result.CaptchaID,
|
||
"image": result.ImageData,
|
||
},
|
||
})
|
||
}
|
||
|
||
// GetCaptchaImage 获取验证码图片
|
||
// @Summary 获取验证码图片
|
||
// @Description 根据captcha_id获取验证码图片(当前未实现)
|
||
// @Tags 验证码
|
||
// @Produce json
|
||
// @Param captcha_id query string false "验证码ID"
|
||
// @Success 200 {object} Response "验证码图片"
|
||
// @Router /api/v1/auth/captcha/image [get]
|
||
func (h *CaptchaHandler) GetCaptchaImage(c *gin.Context) {
|
||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
|
||
}
|
||
|
||
// VerifyCaptcha 验证验证码
|
||
// @Summary 验证验证码
|
||
// @Description 验证用户输入的验证码是否正确
|
||
// @Tags 验证码
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param request body VerifyCaptchaRequest true "验证码信息"
|
||
// @Success 200 {object} Response{data=VerifyResponse} "验证成功"
|
||
// @Failure 400 {object} Response "验证码无效"
|
||
// @Router /api/v1/auth/captcha/verify [post]
|
||
func (h *CaptchaHandler) VerifyCaptcha(c *gin.Context) {
|
||
var req struct {
|
||
CaptchaID string `json:"captcha_id" binding:"required"`
|
||
Answer string `json:"answer" binding:"required"`
|
||
}
|
||
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
|
||
return
|
||
}
|
||
|
||
if h.captchaService.Verify(c.Request.Context(), req.CaptchaID, req.Answer) {
|
||
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": gin.H{"verified": true}})
|
||
} else {
|
||
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid captcha"})
|
||
}
|
||
}
|