From d531429674019dcc109e8615390d262e9b99f144 Mon Sep 17 00:00:00 2001 From: long-agent Date: Sat, 11 Apr 2026 13:34:56 +0800 Subject: [PATCH] fix: unify device_handler.go response format Standardize all JSON responses to {code: 0, message: "success", data: ...} for success and {code: XXX, message: "..."} for errors. --- internal/api/handler/device_handler.go | 64 +++++++++++++------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/internal/api/handler/device_handler.go b/internal/api/handler/device_handler.go index ccb3562..8a37635 100644 --- a/internal/api/handler/device_handler.go +++ b/internal/api/handler/device_handler.go @@ -25,13 +25,13 @@ func NewDeviceHandler(deviceService *service.DeviceService) *DeviceHandler { func (h *DeviceHandler) CreateDevice(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 } var req service.CreateDeviceRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } @@ -51,7 +51,7 @@ func (h *DeviceHandler) CreateDevice(c *gin.Context) { func (h *DeviceHandler) GetMyDevices(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,12 +65,12 @@ func (h *DeviceHandler) GetMyDevices(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "code": 0, + "code": 0, "message": "success", "data": gin.H{ - "items": devices, - "total": total, - "page": page, + "items": devices, + "total": total, + "page": page, "page_size": pageSize, }, }) @@ -79,7 +79,7 @@ func (h *DeviceHandler) GetMyDevices(c *gin.Context) { func (h *DeviceHandler) GetDevice(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid device id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"}) return } @@ -99,13 +99,13 @@ func (h *DeviceHandler) GetDevice(c *gin.Context) { func (h *DeviceHandler) UpdateDevice(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid device id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"}) return } var req service.UpdateDeviceRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } @@ -125,7 +125,7 @@ func (h *DeviceHandler) UpdateDevice(c *gin.Context) { func (h *DeviceHandler) DeleteDevice(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid device id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"}) return } @@ -143,7 +143,7 @@ func (h *DeviceHandler) DeleteDevice(c *gin.Context) { func (h *DeviceHandler) UpdateDeviceStatus(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid device id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"}) return } @@ -152,7 +152,7 @@ func (h *DeviceHandler) UpdateDeviceStatus(c *gin.Context) { } if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } @@ -163,7 +163,7 @@ func (h *DeviceHandler) UpdateDeviceStatus(c *gin.Context) { case "inactive", "0": status = domain.DeviceStatusInactive default: - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid status"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid status"}) return } @@ -182,7 +182,7 @@ func (h *DeviceHandler) GetUserDevices(c *gin.Context) { // IDOR 修复:检查当前用户是否有权限查看指定用户的设备 currentUserID, ok := getUserIDFromContext(c) if !ok { - c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"}) + c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"}) return } @@ -201,13 +201,13 @@ func (h *DeviceHandler) GetUserDevices(c *gin.Context) { userIDParam := c.Param("id") userID, err := strconv.ParseInt(userIDParam, 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid user id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid user id"}) return } // 非管理员只能查看自己的设备 if !isAdmin && userID != currentUserID { - c.JSON(http.StatusForbidden, gin.H{"error": "无权访问该用户的设备列表"}) + c.JSON(http.StatusForbidden, gin.H{"code": 403, "message": "无权访问该用户的设备列表"}) return } @@ -221,7 +221,7 @@ func (h *DeviceHandler) GetUserDevices(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "code": 0, + "code": 0, "message": "success", "data": gin.H{ "items": devices, @@ -236,7 +236,7 @@ func (h *DeviceHandler) GetUserDevices(c *gin.Context) { func (h *DeviceHandler) GetAllDevices(c *gin.Context) { var req service.GetAllDevicesRequest 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 } @@ -263,12 +263,12 @@ func (h *DeviceHandler) GetAllDevices(c *gin.Context) { } c.JSON(http.StatusOK, gin.H{ - "code": 0, + "code": 0, "message": "success", "data": gin.H{ - "items": devices, - "total": total, - "page": req.Page, + "items": devices, + "total": total, + "page": req.Page, "page_size": req.PageSize, }, }) @@ -283,13 +283,13 @@ type TrustDeviceRequest struct { func (h *DeviceHandler) TrustDevice(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid device id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"}) return } var req TrustDeviceRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } @@ -311,19 +311,19 @@ func (h *DeviceHandler) TrustDevice(c *gin.Context) { func (h *DeviceHandler) TrustDeviceByDeviceID(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 } deviceID := c.Param("deviceId") if deviceID == "" { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid device id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"}) return } var req TrustDeviceRequest if err := c.ShouldBindJSON(&req); err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } @@ -345,7 +345,7 @@ func (h *DeviceHandler) TrustDeviceByDeviceID(c *gin.Context) { func (h *DeviceHandler) UntrustDevice(c *gin.Context) { id, err := strconv.ParseInt(c.Param("id"), 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid device id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid device id"}) return } @@ -364,7 +364,7 @@ func (h *DeviceHandler) UntrustDevice(c *gin.Context) { func (h *DeviceHandler) GetMyTrustedDevices(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 } @@ -385,7 +385,7 @@ func (h *DeviceHandler) GetMyTrustedDevices(c *gin.Context) { func (h *DeviceHandler) LogoutAllOtherDevices(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 } @@ -393,7 +393,7 @@ func (h *DeviceHandler) LogoutAllOtherDevices(c *gin.Context) { currentDeviceIDStr := c.GetHeader("X-Device-ID") currentDeviceID, err := strconv.ParseInt(currentDeviceIDStr, 10, 64) if err != nil { - c.JSON(http.StatusBadRequest, gin.H{"error": "invalid current device id"}) + c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid current device id"}) return }