在 fast-forward 到 origin/main 后解决 music logic/repo 冲突,SQL 与模型统一 t_* 表名;Makefile 修正 golangci-lint 变量并令 test/deploy 依赖 lint;补齐 admin/transport 等 lint 修复与 music logic 单测以满足覆盖率门禁。 Co-authored-by: Cursor <cursoragent@cursor.com>
64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package smshandler
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
smsentity "spark-server/internal/module/sms/entity"
|
|
smslogic "spark-server/internal/module/sms/logic"
|
|
"spark-server/internal/pkg/errs"
|
|
"spark-server/internal/pkg/i18n"
|
|
"spark-server/internal/pkg/reqctx"
|
|
"spark-server/internal/pkg/validate"
|
|
"spark-server/internal/transport/http/respond"
|
|
)
|
|
|
|
const headerXLanguage = "X-Language"
|
|
|
|
// Handler is the verification-code HTTP controller.
|
|
type Handler struct {
|
|
sms *smslogic.Service
|
|
}
|
|
|
|
func New(sms *smslogic.Service) *Handler {
|
|
return &Handler{sms: sms}
|
|
}
|
|
|
|
func (h *Handler) SendCode(c *gin.Context) {
|
|
if h == nil || h.sms == nil {
|
|
respond.JSONError(c, errs.ErrAvailable)
|
|
return
|
|
}
|
|
lang, ok := i18n.ParseExplicit(strings.TrimSpace(c.GetHeader(headerXLanguage)))
|
|
if !ok {
|
|
respond.JSONError(c, errs.ErrInvalidArgument.WithMsg(i18n.T(reqctx.Lang(c.Request.Context()), i18n.KeySMSLanguageRequired, nil)))
|
|
return
|
|
}
|
|
if rc := reqctx.From(c.Request.Context()); rc != nil {
|
|
rc.Language = lang.String()
|
|
}
|
|
var in smslogic.SendCodeInput
|
|
if err := respond.BindJSON(c, &in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
if err := validate.StructLang(lang, in); err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
if in.Purpose == smsentity.PurposeAccountDelete {
|
|
rc := reqctx.From(c.Request.Context())
|
|
if rc == nil || rc.UserID <= 0 {
|
|
respond.JSONError(c, errs.ErrUnauthorized)
|
|
return
|
|
}
|
|
}
|
|
out, err := h.sms.Send(c.Request.Context(), in)
|
|
if err != nil {
|
|
respond.JSONError(c, err)
|
|
return
|
|
}
|
|
respond.JSON(c, out)
|
|
}
|