移动端和 PC 端登录页都加了“图形验证码
This commit is contained in:
@@ -15,7 +15,9 @@ type Handler struct {
|
||||
}
|
||||
|
||||
type SendSMSRequest struct {
|
||||
Phone string `json:"phone" binding:"required"`
|
||||
Phone string `json:"phone" binding:"required"`
|
||||
CaptchaID string `json:"captcha_id" binding:"required"`
|
||||
CaptchaCode string `json:"captcha_code" binding:"required"`
|
||||
}
|
||||
|
||||
type LoginRequest struct {
|
||||
@@ -31,6 +33,15 @@ func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
}
|
||||
|
||||
func (h *Handler) Captcha(c *gin.Context) {
|
||||
item, err := h.service.Captcha(c.Request.Context())
|
||||
if err != nil {
|
||||
writeAuthError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, item)
|
||||
}
|
||||
|
||||
// SendSMS 发送短信验证码
|
||||
// @Summary 发送短信验证码
|
||||
// @Description 发送登录验证码到指定手机号
|
||||
@@ -45,10 +56,10 @@ func NewHandler(service *Service) *Handler {
|
||||
func (h *Handler) SendSMS(c *gin.Context) {
|
||||
var req SendSMSRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "手机号不能为空")
|
||||
response.BadRequest(c, "手机号和图形验证码不能为空")
|
||||
return
|
||||
}
|
||||
if err := h.service.SendSMSCode(c.Request.Context(), strings.TrimSpace(req.Phone)); err != nil {
|
||||
if err := h.service.SendSMSCode(c.Request.Context(), strings.TrimSpace(req.Phone), strings.TrimSpace(req.CaptchaID), strings.TrimSpace(req.CaptchaCode)); err != nil {
|
||||
writeAuthError(c, err)
|
||||
return
|
||||
}
|
||||
@@ -125,6 +136,8 @@ func writeAuthError(c *gin.Context, err error) {
|
||||
response.ServiceUnavailable(c, "数据库或 Redis 未连接")
|
||||
case errors.Is(err, ErrInvalidPhone):
|
||||
response.BadRequest(c, "手机号格式不正确")
|
||||
case errors.Is(err, ErrCaptchaInvalid):
|
||||
response.BadRequest(c, "图形验证码错误或已过期")
|
||||
case errors.Is(err, ErrCodeRateLimited):
|
||||
response.Error(c, http.StatusTooManyRequests, "rate_limited", "验证码发送过于频繁")
|
||||
case errors.Is(err, ErrSMSSendFailed):
|
||||
|
||||
Reference in New Issue
Block a user