首次提交:初始化项目代码
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"cloudnest/internal/application/captcha"
|
||||
"cloudnest/internal/pkg/response"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// CaptchaHandler handles HTTP requests for captcha operations.
|
||||
// CaptchaHandler 处理验证码操作的 HTTP 请求。
|
||||
type CaptchaHandler struct {
|
||||
service *captcha.CaptchaService
|
||||
}
|
||||
|
||||
// NewCaptchaHandler creates a new CaptchaHandler.
|
||||
// NewCaptchaHandler 创建一个新的 CaptchaHandler。
|
||||
func NewCaptchaHandler(service *captcha.CaptchaService) *CaptchaHandler {
|
||||
return &CaptchaHandler{service: service}
|
||||
}
|
||||
|
||||
// GenerateCaptcha handles GET /api/v1/captcha.
|
||||
// GenerateCaptcha 处理 GET /api/v1/captcha。
|
||||
//
|
||||
// Response:
|
||||
// - Success: 200 OK {"code": 0, "message": "success", "data": {"captcha_id": "string", "image_url": "string"}}
|
||||
func (h *CaptchaHandler) GenerateCaptcha(c *gin.Context) {
|
||||
captchaID, imageBytes, err := h.service.Generate()
|
||||
if err != nil {
|
||||
response.InternalError(c, "验证码生成失败")
|
||||
return
|
||||
}
|
||||
|
||||
imageUrl := "/api/v1/captcha/" + captchaID + "/image"
|
||||
// Store image bytes in context for potential direct use, but we return URL here
|
||||
_ = imageBytes
|
||||
|
||||
response.Success(c, gin.H{
|
||||
"captcha_id": captchaID,
|
||||
"image_url": imageUrl,
|
||||
})
|
||||
}
|
||||
|
||||
// GetCaptchaImage handles GET /api/v1/captcha/:id/image.
|
||||
// GetCaptchaImage 处理 GET /api/v1/captcha/:id/image。
|
||||
//
|
||||
// Returns the captcha PNG image directly.
|
||||
func (h *CaptchaHandler) GetCaptchaImage(c *gin.Context) {
|
||||
captchaID := c.Param("id")
|
||||
if captchaID == "" {
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
imageBytes, err := h.service.GetImage(captchaID)
|
||||
if err != nil {
|
||||
c.Status(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
c.Data(http.StatusOK, "image/png", imageBytes)
|
||||
}
|
||||
Reference in New Issue
Block a user