Files
user-system/internal/api/handler/webhook_handler.go
long-agent a85d822419 fix: 统一API响应格式并修复前端测试
- 所有Handler方法使用标准{code:0,message:"success",data:...}响应格式
- 修复Cursor分页响应包装(GetAllDevices,GetLoginLogs,ListUsers等)
- 修复AuthHandler和SMSHandler认证方法响应格式
- 修复operation_log.go admin用户operation_type前缀问题
- 修复DashboardPage嵌套stats结构
- 修复LoginLogsPage reset功能stale closure问题
- 修复UsersPage批量操作API调用
- 修复多个前端测试(mock格式、按钮选择、断言逻辑)
- 添加OAuth测试域名白名单
- 新增代码审查流程文档
2026-04-08 20:06:54 +08:00

126 lines
3.4 KiB
Go

package handler
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/user-management-system/internal/service"
)
// WebhookHandler handles webhook requests
type WebhookHandler struct {
webhookService *service.WebhookService
}
// NewWebhookHandler creates a new WebhookHandler
func NewWebhookHandler(webhookService *service.WebhookService) *WebhookHandler {
return &WebhookHandler{webhookService: webhookService}
}
func (h *WebhookHandler) CreateWebhook(c *gin.Context) {
var req service.CreateWebhookRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
userID, _ := c.Get("user_id")
creatorID, _ := userID.(int64)
webhook, err := h.webhookService.CreateWebhook(c.Request.Context(), &req, creatorID)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "创建 Webhook 失败"})
return
}
c.JSON(http.StatusCreated, gin.H{"code": 0, "data": webhook})
}
func (h *WebhookHandler) ListWebhooks(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
if page < 1 {
page = 1
}
if pageSize < 1 || pageSize > 100 {
pageSize = 20
}
offset := (page - 1) * pageSize
userID, _ := c.Get("user_id")
creatorID, _ := userID.(int64)
webhooks, total, err := h.webhookService.ListWebhooksPaginated(c.Request.Context(), creatorID, offset, pageSize)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "获取 Webhook 列表失败"})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": webhooks,
"total": total,
"page": page,
"page_size": pageSize,
})
}
func (h *WebhookHandler) UpdateWebhook(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的 Webhook ID"})
return
}
var req service.UpdateWebhookRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
if err := h.webhookService.UpdateWebhook(c.Request.Context(), id, &req); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "更新 Webhook 失败"})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "更新成功"})
}
func (h *WebhookHandler) DeleteWebhook(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的 Webhook ID"})
return
}
if err := h.webhookService.DeleteWebhook(c.Request.Context(), id); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "删除 Webhook 失败"})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "删除成功"})
}
func (h *WebhookHandler) GetWebhookDeliveries(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "无效的 Webhook ID"})
return
}
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "20"))
if limit < 1 || limit > 100 {
limit = 20
}
deliveries, err := h.webhookService.GetWebhookDeliveries(c.Request.Context(), id, limit)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": "获取投递记录失败"})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": deliveries})
}