389 lines
13 KiB
Go
389 lines
13 KiB
Go
// channel_handler.go Day 35:通道 CRUD/测试工具/拦截配置端点。
|
|
package v1
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go-sms/internal/middleware"
|
|
"go-sms/internal/pkg/errors"
|
|
"go-sms/internal/pkg/response"
|
|
"go-sms/internal/service/channel"
|
|
"go-sms/internal/service/channel/pool"
|
|
)
|
|
|
|
// channelHandler 通道接口。
|
|
type channelHandler struct{ svc *channel.Service }
|
|
|
|
// registerChannelRoutes 注册通道路由(channel:write 门禁)。
|
|
func registerChannelRoutes(protected *gin.RouterGroup, perm middleware.PermissionChecker, svc *channel.Service, ups *channel.UpstreamService) {
|
|
h := &channelHandler{svc: svc}
|
|
gate := func(obj, act string) gin.HandlerFunc {
|
|
return middleware.RequirePermission(perm, obj, act)
|
|
}
|
|
protected.POST("/channels", gate("channel", "write"), h.create)
|
|
protected.GET("/channels", h.list)
|
|
// Day 40:实时监控(WS 2s)+通道日志(A-3.13/A-3.14)——静态段须先于 /channels/:id 注册,
|
|
// 否则 gin 树将 monitor 吞入 :id 参数段(h.get("monitor") 404)。
|
|
registerMonitorRoutes(protected, svc)
|
|
// Day 41(清债③):上家管理——同样须先于 /channels/:id(静态段优先)。
|
|
if ups != nil {
|
|
uh := &upstreamHandler{svc: ups}
|
|
protected.GET("/channels/upstreams", uh.list)
|
|
protected.POST("/channels/upstreams", gate("channel", "write"), uh.create)
|
|
protected.PUT("/channels/upstreams/:id", gate("channel", "write"), uh.update)
|
|
protected.DELETE("/channels/upstreams/:id", gate("channel", "write"), uh.remove)
|
|
}
|
|
protected.GET("/channels/:id", h.get)
|
|
protected.PUT("/channels/:id", gate("channel", "write"), h.update)
|
|
protected.PUT("/channels/:id/status", gate("channel", "write"), h.setStatus)
|
|
protected.DELETE("/channels/:id", gate("channel", "write"), h.remove)
|
|
protected.POST("/channels/:id/test", gate("channel", "write"), h.test)
|
|
protected.GET("/channels/:id/intercept", h.getIntercept)
|
|
protected.PUT("/channels/:id/intercept", gate("channel", "write"), h.setIntercept)
|
|
// Day 38:价格时间线(分时成本价)
|
|
protected.POST("/channels/:id/prices", gate("channel", "write"), h.setPrice)
|
|
protected.GET("/channels/:id/prices", h.listPrices)
|
|
protected.GET("/channels/:id/prices/current", h.currentPrice)
|
|
protected.DELETE("/channels/:id/prices/:pid", gate("channel", "write"), h.removePrice)
|
|
// Day 39:链路状态与回执复核(发送真实性 WEB 验收,数据源自内存)
|
|
protected.GET("/channels/:id/link-status", h.linkStatus)
|
|
protected.GET("/channels/:id/receipts", h.linkReceipts)
|
|
// Day 36:通道组与成员(channel:write)
|
|
protected.POST("/channel-groups", gate("channel", "write"), h.createGroup)
|
|
protected.GET("/channel-groups", h.listGroups)
|
|
protected.GET("/channel-groups/:id", h.getGroup)
|
|
protected.PUT("/channel-groups/:id", gate("channel", "write"), h.updateGroup)
|
|
protected.DELETE("/channel-groups/:id", gate("channel", "write"), h.removeGroup)
|
|
protected.POST("/channel-groups/:id/items", gate("channel", "write"), h.addItem)
|
|
protected.DELETE("/channel-groups/:id/items/:itemId", gate("channel", "write"), h.removeItem)
|
|
}
|
|
|
|
// createGroup POST /channel-groups {name}。
|
|
func (h *channelHandler) createGroup(c *gin.Context) {
|
|
var req struct {
|
|
Name string `json:"name" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
g, err := h.svc.CreateGroup(c.Request.Context(), req.Name)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": g.ID, "name": g.Name, "status": g.Status})
|
|
}
|
|
|
|
// listGroups GET /channel-groups。
|
|
func (h *channelHandler) listGroups(c *gin.Context) {
|
|
list, err := h.svc.ListGroups(c.Request.Context())
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"total": len(list), "list": list})
|
|
}
|
|
|
|
// getGroup GET /channel-groups/:id(含成员与通道摘要)。
|
|
func (h *channelHandler) getGroup(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
d, err := h.svc.GetGroup(c.Request.Context(), id)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, d)
|
|
}
|
|
|
|
// updateGroup PUT /channel-groups/:id。
|
|
func (h *channelHandler) updateGroup(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.UpdateGroup(c.Request.Context(), id, fields); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id})
|
|
}
|
|
|
|
// removeGroup DELETE /channel-groups/:id(须先清空成员)。
|
|
func (h *channelHandler) removeGroup(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err := h.svc.DeleteGroup(c.Request.Context(), id); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id, "deleted": true})
|
|
}
|
|
|
|
// addItem POST /channel-groups/:id/items。
|
|
func (h *channelHandler) addItem(c *gin.Context) {
|
|
gid, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req channel.AddItemReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
it, err := h.svc.AddItem(c.Request.Context(), gid, &req)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, it)
|
|
}
|
|
|
|
// removeItem DELETE /channel-groups/:id/items/:itemId。
|
|
func (h *channelHandler) removeItem(c *gin.Context) {
|
|
gid, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
itemID, _ := strconv.ParseInt(c.Param("itemId"), 10, 64)
|
|
if err := h.svc.RemoveItem(c.Request.Context(), gid, itemID); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": itemID, "deleted": true})
|
|
}
|
|
|
|
// bizResp 业务错误透传。
|
|
func bizResp(c *gin.Context, err error) {
|
|
if bizErr, ok := err.(*errors.Error); ok {
|
|
response.Error(c, bizErr)
|
|
return
|
|
}
|
|
c.Set("biz_error", err.Error())
|
|
response.Error(c, errors.InternalError)
|
|
}
|
|
|
|
// create POST /channels:创建通道。
|
|
func (h *channelHandler) create(c *gin.Context) {
|
|
var req channel.CreateReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
ch, err := h.svc.Create(c.Request.Context(), &req)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": ch.ID, "code": ch.Code, "protocol": ch.Protocol, "status": ch.Status})
|
|
}
|
|
|
|
// list GET /channels:分页过滤(name/protocol/status)。
|
|
func (h *channelHandler) list(c *gin.Context) {
|
|
q := &channel.ListQuery{
|
|
Page: intQuery(c, "page", 1), PageSize: intQuery(c, "page_size", 20),
|
|
Name: c.Query("name"), Protocol: c.Query("protocol"),
|
|
}
|
|
if v := c.Query("status"); v != "" {
|
|
st, _ := strconv.Atoi(v)
|
|
q.Status = int8(st)
|
|
} else {
|
|
q.Status = -1
|
|
}
|
|
list, total, err := h.svc.List(c.Request.Context(), q)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"total": total, "list": list})
|
|
}
|
|
|
|
// get GET /channels/:id。
|
|
func (h *channelHandler) get(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
ch, err := h.svc.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, ch)
|
|
}
|
|
|
|
// update PUT /channels/:id:白名单字段。
|
|
func (h *channelHandler) 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.Update(c.Request.Context(), id, fields); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id})
|
|
}
|
|
|
|
// setStatus PUT /channels/:id/status {status}。
|
|
func (h *channelHandler) setStatus(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req struct {
|
|
Status int8 `json:"status"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
if err := h.svc.SetStatus(c.Request.Context(), id, req.Status); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id, "status": req.Status})
|
|
}
|
|
|
|
// remove DELETE /channels/:id(须先停用)。
|
|
func (h *channelHandler) remove(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if err := h.svc.Delete(c.Request.Context(), id); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id, "deleted": true})
|
|
}
|
|
|
|
// test POST /channels/:id/test {mobile, content}:测试工具(真实发送,返回上游 MsgID)。
|
|
func (h *channelHandler) test(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req struct {
|
|
Mobile string `json:"mobile" binding:"required"`
|
|
Content string `json:"content" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
res, err := h.svc.TestChannel(c.Request.Context(), id, req.Mobile, req.Content)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, res)
|
|
}
|
|
|
|
// getIntercept GET /channels/:id/intercept。
|
|
func (h *channelHandler) getIntercept(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
cfg, err := h.svc.GetIntercept(c.Request.Context(), id)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id, "intercept": cfg})
|
|
}
|
|
|
|
// setIntercept PUT /channels/:id/intercept:夜发/日限/屏蔽地区。
|
|
func (h *channelHandler) setIntercept(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var cfg channel.InterceptConfig
|
|
if err := c.ShouldBindJSON(&cfg); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
if err := h.svc.SetIntercept(c.Request.Context(), id, &cfg); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id, "intercept": cfg})
|
|
}
|
|
|
|
// ---- 价格时间线(Day 38) ----
|
|
|
|
// setPrice POST /channels/:id/prices {price, effective_at}:写入价格时间线。
|
|
func (h *channelHandler) setPrice(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
var req channel.SetPriceReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
response.ErrorWithMsg(c, errors.InvalidParam, "参数错误: "+err.Error())
|
|
return
|
|
}
|
|
p, err := h.svc.SetPrice(c.Request.Context(), id, &req)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{
|
|
"id": p.ID, "channel_id": p.ChannelID, "price": p.Price,
|
|
"effective_at": p.EffectiveAt.Format("2006-01-02 15:04:05"),
|
|
})
|
|
}
|
|
|
|
// listPrices GET /channels/:id/prices:价格时间线(生效时间升序)。
|
|
func (h *channelHandler) listPrices(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
list, err := h.svc.ListPrices(c.Request.Context(), id)
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"total": len(list), "list": list})
|
|
}
|
|
|
|
// currentPrice GET /channels/:id/prices/current:当前时间点单价(无时间线回退实体 Price)。
|
|
func (h *channelHandler) currentPrice(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
price, err := h.svc.CurrentPrice(c.Request.Context(), id, time.Now())
|
|
if err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"channel_id": id, "price": price})
|
|
}
|
|
|
|
// removePrice DELETE /channels/:id/prices/:pid。
|
|
func (h *channelHandler) removePrice(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
pid, _ := strconv.ParseInt(c.Param("pid"), 10, 64)
|
|
if err := h.svc.DeletePrice(c.Request.Context(), id, pid); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
response.Success(c, gin.H{"id": id, "price_id": pid, "deleted": true})
|
|
}
|
|
|
|
// ---- 链路状态与回执复核(Day 39) ----
|
|
|
|
// linkStatus GET /channels/:id/link-status:常驻链路状态(未建链返回 running=false 视图)。
|
|
func (h *channelHandler) linkStatus(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if _, err := h.svc.Get(c.Request.Context(), id); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
st := h.svc.LinkStatus(id)
|
|
if st == nil {
|
|
st = &pool.LinkStatus{ChannelID: id}
|
|
}
|
|
response.Success(c, st)
|
|
}
|
|
|
|
// linkReceipts GET /channels/:id/receipts?limit=50:该通道最近回执/上行(倒序;含状态报告与上行短信)。
|
|
// Day 56:优先读持久层(message_report_seg+message_uplink,重启后回执可查);
|
|
// 查询失败降级内存环形缓冲(链路刚建立/DB 抖动窗口仍可观测)。
|
|
func (h *channelHandler) linkReceipts(c *gin.Context) {
|
|
id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
|
|
if _, err := h.svc.Get(c.Request.Context(), id); err != nil {
|
|
bizResp(c, err)
|
|
return
|
|
}
|
|
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "50"))
|
|
if limit <= 0 || limit > 100 {
|
|
limit = 50
|
|
}
|
|
list, err := h.svc.LinkReceiptsPersist(c.Request.Context(), id, limit)
|
|
if err != nil {
|
|
list = h.svc.LinkReceipts(id, limit) // 降级:内存环形缓冲(最近 receiptCap 条)
|
|
}
|
|
if list == nil {
|
|
list = []pool.Receipt{}
|
|
}
|
|
response.Success(c, gin.H{"channel_id": id, "total": len(list), "list": list})
|
|
}
|