Files
go-sms-V2/internal/api/http/v1/route_handler.go
T

128 lines
4.0 KiB
Go

// route_handler.go Day 38:导流规则管理端点(channel:write 门禁)。
package v1
import (
"strconv"
"github.com/gin-gonic/gin"
"go-sms/internal/middleware"
"go-sms/internal/pkg/errors"
"go-sms/internal/pkg/response"
"go-sms/internal/service/router"
)
// steerHandler 导流规则接口。
type steerHandler struct{ svc *router.Router }
// registerSteerRoutes 注册导流规则端点(channel:write 门禁)。
func registerSteerRoutes(protected *gin.RouterGroup, perm middleware.PermissionChecker, svc *router.Router) {
h := &steerHandler{svc: svc}
gate := func(obj, act string) gin.HandlerFunc {
return middleware.RequirePermission(perm, obj, act)
}
protected.POST("/route-rules", gate("channel", "write"), h.create)
protected.GET("/route-rules", h.list)
protected.PUT("/route-rules/:id", gate("channel", "write"), h.update)
protected.DELETE("/route-rules/:id", gate("channel", "write"), h.remove)
// Day 63:账号级通道绑定(账号详情页「发送通道」;绑定/原子切换/解绑,
// GET 快照含通道名/状态,前端免二次查询)。
protected.GET("/accounts/:id/channel-steer", h.accountSteer)
protected.PUT("/accounts/:id/channel-steer",
gate("channel", "write"), h.setAccountSteer)
}
// accountSteer GET /accounts/:id/channel-steer 查账号当前绑定(rule_id=0=未绑定)。
func (h *steerHandler) accountSteer(c *gin.Context) {
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
resp, err := h.svc.GetAccountSteer(c.Request.Context(), id)
if err != nil {
bizResp(c, err)
return
}
response.Success(c, resp)
}
// setAccountSteer PUT /accounts/:id/channel-steer body {channel_id}:
// >0=绑定/切换(事务原子);0=解绑回退四步智能路由。保存即生效(引擎实时查库)。
func (h *steerHandler) setAccountSteer(c *gin.Context) {
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
var req struct {
ChannelID int64 `json:"channel_id"`
}
if err := c.ShouldBindJSON(&req); err != nil {
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
return
}
if req.ChannelID < 0 {
response.ErrorWithMsg(c, errors.InvalidParam, "channel_id 须为非负整数")
return
}
rule, err := h.svc.SetAccountSteer(c.Request.Context(), id, req.ChannelID)
if err != nil {
bizResp(c, err)
return
}
if rule == nil { // 解绑成功(无新规则)
response.Success(c, gin.H{"account_id": id, "rule_id": 0})
return
}
response.Success(c, gin.H{
"account_id": id, "rule_id": rule.ID, "channel_id": rule.ChannelID,
})
}
// create POST /route-rules。
func (h *steerHandler) create(c *gin.Context) {
var req router.CreateRuleReq
if err := c.ShouldBindJSON(&req); err != nil {
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
return
}
rule, err := h.svc.CreateRule(c.Request.Context(), &req)
if err != nil {
bizResp(c, err)
return
}
response.Success(c, gin.H{
"id": rule.ID, "account_id": rule.AccountID, "channel_id": rule.ChannelID,
"rule_type": rule.RuleType, "rule_config": rule.RuleConfig,
"priority": rule.Priority, "status": rule.Status,
})
}
// list GET /route-rules。
func (h *steerHandler) list(c *gin.Context) {
list, err := h.svc.ListRules(c.Request.Context())
if err != nil {
bizResp(c, err)
return
}
response.Success(c, gin.H{"total": len(list), "list": list})
}
// update PUT /route-rules/:id。
func (h *steerHandler) update(c *gin.Context) {
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
var fields map[string]any
if err := c.ShouldBindJSON(&fields); err != nil {
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
return
}
if err := h.svc.UpdateRule(c.Request.Context(), id, fields); err != nil {
bizResp(c, err)
return
}
response.Success(c, gin.H{"id": id})
}
// remove DELETE /route-rules/:id。
func (h *steerHandler) remove(c *gin.Context) {
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
if err := h.svc.DeleteRule(c.Request.Context(), id); err != nil {
bizResp(c, err)
return
}
response.Success(c, gin.H{"id": id})
}