fix: unify handler response format in log, permission, webhook handlers

- log_handler.go: Fix GetMyLoginLogs/GetMyOperationLogs/GetLoginLogs/GetOperationLogs to use {code, message, data}
- permission_handler.go: Fix all error responses to use {code, message}
- webhook_handler.go: Add missing "message" field in success responses, wrap data in data object with list/total/page/page_size
- webhook_handler_test.go: Update test to match new response format

Standardize all JSON responses to {code: 0, message: "success", data: ...} for success
and {code: XXX, message: "..."} for errors.
This commit is contained in:
2026-04-11 13:12:27 +08:00
parent b6aff65975
commit e00af0bce4
4 changed files with 66 additions and 48 deletions

View File

@@ -27,7 +27,7 @@ func NewLogHandler(loginLogService *service.LoginLogService, operationLogService
func (h *LogHandler) GetMyLoginLogs(c *gin.Context) {
userID, ok := getUserIDFromContext(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
return
}
@@ -41,17 +41,21 @@ func (h *LogHandler) GetMyLoginLogs(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"list": logs,
"total": total,
"page": page,
"page_size": pageSize,
"code": 0,
"message": "success",
"data": gin.H{
"list": logs,
"total": total,
"page": page,
"page_size": pageSize,
},
})
}
func (h *LogHandler) GetMyOperationLogs(c *gin.Context) {
userID, ok := getUserIDFromContext(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
return
}
@@ -65,17 +69,21 @@ func (h *LogHandler) GetMyOperationLogs(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"list": logs,
"total": total,
"page": page,
"page_size": pageSize,
"code": 0,
"message": "success",
"data": gin.H{
"list": logs,
"total": total,
"page": page,
"page_size": pageSize,
},
})
}
func (h *LogHandler) GetLoginLogs(c *gin.Context) {
var req service.ListLoginLogRequest
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
@@ -102,17 +110,21 @@ func (h *LogHandler) GetLoginLogs(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"list": logs,
"total": total,
"page": req.Page,
"page_size": req.PageSize,
"code": 0,
"message": "success",
"data": gin.H{
"list": logs,
"total": total,
"page": req.Page,
"page_size": req.PageSize,
},
})
}
func (h *LogHandler) GetOperationLogs(c *gin.Context) {
var req service.ListOperationLogRequest
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
@@ -139,17 +151,21 @@ func (h *LogHandler) GetOperationLogs(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"list": logs,
"total": total,
"page": req.Page,
"page_size": req.PageSize,
"code": 0,
"message": "success",
"data": gin.H{
"list": logs,
"total": total,
"page": req.Page,
"page_size": req.PageSize,
},
})
}
func (h *LogHandler) ExportLoginLogs(c *gin.Context) {
var req service.ExportLoginLogRequest
if err := c.ShouldBindQuery(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}