refactor: Implement dependency injection for handlers and services
- Refactored AuthHandler, UserHandler, TextureHandler, ProfileHandler, CaptchaHandler, and YggdrasilHandler to use dependency injection. - Removed direct instantiation of services and repositories within handlers, replacing them with constructor injection. - Updated the container to initialize service instances and provide them to handlers. - Enhanced code structure for better testability and adherence to Go best practices.
This commit is contained in:
@@ -1,47 +1,77 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"carrotskin/internal/container"
|
||||
"carrotskin/internal/service"
|
||||
"carrotskin/pkg/redis"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// CaptchaHandler 验证码处理器
|
||||
type CaptchaHandler struct {
|
||||
container *container.Container
|
||||
logger *zap.Logger
|
||||
}
|
||||
|
||||
// NewCaptchaHandler 创建CaptchaHandler实例
|
||||
func NewCaptchaHandler(c *container.Container) *CaptchaHandler {
|
||||
return &CaptchaHandler{
|
||||
container: c,
|
||||
logger: c.Logger,
|
||||
}
|
||||
}
|
||||
|
||||
// CaptchaVerifyRequest 验证码验证请求
|
||||
type CaptchaVerifyRequest struct {
|
||||
CaptchaID string `json:"captchaId" binding:"required"`
|
||||
Dx int `json:"dx" binding:"required"`
|
||||
}
|
||||
|
||||
// Generate 生成验证码
|
||||
func Generate(c *gin.Context) {
|
||||
// 调用验证码服务生成验证码数据
|
||||
redisClient := redis.MustGetClient()
|
||||
masterImg, tileImg, captchaID, y, err := service.GenerateCaptchaData(c.Request.Context(), redisClient)
|
||||
// @Summary 生成滑动验证码
|
||||
// @Description 生成滑动验证码图片
|
||||
// @Tags captcha
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} map[string]interface{} "生成成功"
|
||||
// @Failure 500 {object} map[string]interface{} "生成失败"
|
||||
// @Router /api/v1/captcha/generate [get]
|
||||
func (h *CaptchaHandler) Generate(c *gin.Context) {
|
||||
masterImg, tileImg, captchaID, y, err := service.GenerateCaptchaData(c.Request.Context(), h.container.Redis)
|
||||
if err != nil {
|
||||
h.logger.Error("生成验证码失败", zap.Error(err))
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"code": 500,
|
||||
"msg": "生成验证码失败: " + err.Error(),
|
||||
"msg": "生成验证码失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 返回验证码数据给前端
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"data": gin.H{
|
||||
"masterImage": masterImg, // 主图(base64格式)
|
||||
"tileImage": tileImg, // 滑块图(base64格式)
|
||||
"captchaId": captchaID, // 验证码唯一标识(用于后续验证)
|
||||
"y": y, // 滑块Y坐标(前端可用于定位滑块初始位置)
|
||||
"masterImage": masterImg,
|
||||
"tileImage": tileImg,
|
||||
"captchaId": captchaID,
|
||||
"y": y,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Verify 验证验证码
|
||||
func Verify(c *gin.Context) {
|
||||
// 定义请求参数结构体
|
||||
var req struct {
|
||||
CaptchaID string `json:"captchaId" binding:"required"` // 验证码唯一标识
|
||||
Dx int `json:"dx" binding:"required"` // 用户滑动的X轴偏移量
|
||||
}
|
||||
|
||||
// 解析并校验请求参数
|
||||
// @Summary 验证滑动验证码
|
||||
// @Description 验证用户滑动的偏移量是否正确
|
||||
// @Tags captcha
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body CaptchaVerifyRequest true "验证请求"
|
||||
// @Success 200 {object} map[string]interface{} "验证结果"
|
||||
// @Failure 400 {object} map[string]interface{} "参数错误"
|
||||
// @Router /api/v1/captcha/verify [post]
|
||||
func (h *CaptchaHandler) Verify(c *gin.Context) {
|
||||
var req CaptchaVerifyRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"code": 400,
|
||||
@@ -50,18 +80,19 @@ func Verify(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 调用验证码服务验证偏移量
|
||||
redisClient := redis.MustGetClient()
|
||||
valid, err := service.VerifyCaptchaData(c.Request.Context(), redisClient, req.Dx, req.CaptchaID)
|
||||
valid, err := service.VerifyCaptchaData(c.Request.Context(), h.container.Redis, req.Dx, req.CaptchaID)
|
||||
if err != nil {
|
||||
h.logger.Error("验证码验证失败",
|
||||
zap.String("captcha_id", req.CaptchaID),
|
||||
zap.Error(err),
|
||||
)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"code": 500,
|
||||
"msg": "验证失败: " + err.Error(),
|
||||
"msg": "验证失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 根据验证结果返回响应
|
||||
if valid {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
@@ -74,3 +105,5 @@ func Verify(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user