feat(sms): 支持双站点独立配置登录短信
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package tenantsms
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"hfb_sys/backend/internal/auditlog"
|
||||
"hfb_sys/backend/internal/middleware"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
"hfb_sys/backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
func NewHandler(repo *Repository) *Handler {
|
||||
return &Handler{repo: repo}
|
||||
}
|
||||
|
||||
func (h *Handler) Get(c *gin.Context) {
|
||||
if tenancy.IDFromContext(c.Request.Context()) == 0 {
|
||||
response.BadRequest(c, "当前后台缺少经营站点上下文")
|
||||
return
|
||||
}
|
||||
item, err := h.repo.GetDTO(c.Request.Context())
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, item)
|
||||
}
|
||||
|
||||
func (h *Handler) Update(c *gin.Context) {
|
||||
adminID, ok := c.Get(middleware.ContextAdminID)
|
||||
actorID, valid := adminID.(uint64)
|
||||
if !ok || !valid || actorID == 0 {
|
||||
response.Unauthorized(c, "缺少管理员上下文")
|
||||
return
|
||||
}
|
||||
var req UpdateRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "短信配置格式无效")
|
||||
return
|
||||
}
|
||||
item, err := h.repo.Update(c.Request.Context(), actorID, req, auditlog.Meta{
|
||||
IP: c.ClientIP(),
|
||||
UserAgent: c.GetHeader("User-Agent"),
|
||||
RequestID: middleware.GetRequestID(c),
|
||||
})
|
||||
if err != nil {
|
||||
writeError(c, err)
|
||||
return
|
||||
}
|
||||
response.OK(c, item)
|
||||
}
|
||||
|
||||
func writeError(c *gin.Context, err error) {
|
||||
response.RecordError(c, err)
|
||||
if errors.Is(err, ErrInvalidConfig) {
|
||||
response.BadRequest(c, "启用独立短信时,AccessKey、签名和登录模板不能为空")
|
||||
return
|
||||
}
|
||||
response.Error(c, http.StatusInternalServerError, "internal_error", "登录短信配置服务暂时不可用")
|
||||
}
|
||||
Reference in New Issue
Block a user