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.
This commit is contained in:
@@ -25,13 +25,13 @@ func NewDeviceHandler(deviceService *service.DeviceService) *DeviceHandler {
|
|||||||
func (h *DeviceHandler) CreateDevice(c *gin.Context) {
|
func (h *DeviceHandler) CreateDevice(c *gin.Context) {
|
||||||
userID, ok := getUserIDFromContext(c)
|
userID, ok := getUserIDFromContext(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req service.CreateDeviceRequest
|
var req service.CreateDeviceRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ func (h *DeviceHandler) CreateDevice(c *gin.Context) {
|
|||||||
func (h *DeviceHandler) GetMyDevices(c *gin.Context) {
|
func (h *DeviceHandler) GetMyDevices(c *gin.Context) {
|
||||||
userID, ok := getUserIDFromContext(c)
|
userID, ok := getUserIDFromContext(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,12 +65,12 @@ func (h *DeviceHandler) GetMyDevices(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"message": "success",
|
"message": "success",
|
||||||
"data": gin.H{
|
"data": gin.H{
|
||||||
"items": devices,
|
"items": devices,
|
||||||
"total": total,
|
"total": total,
|
||||||
"page": page,
|
"page": page,
|
||||||
"page_size": pageSize,
|
"page_size": pageSize,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -79,7 +79,7 @@ func (h *DeviceHandler) GetMyDevices(c *gin.Context) {
|
|||||||
func (h *DeviceHandler) GetDevice(c *gin.Context) {
|
func (h *DeviceHandler) GetDevice(c *gin.Context) {
|
||||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,13 +99,13 @@ func (h *DeviceHandler) GetDevice(c *gin.Context) {
|
|||||||
func (h *DeviceHandler) UpdateDevice(c *gin.Context) {
|
func (h *DeviceHandler) UpdateDevice(c *gin.Context) {
|
||||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req service.UpdateDeviceRequest
|
var req service.UpdateDeviceRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -125,7 +125,7 @@ func (h *DeviceHandler) UpdateDevice(c *gin.Context) {
|
|||||||
func (h *DeviceHandler) DeleteDevice(c *gin.Context) {
|
func (h *DeviceHandler) DeleteDevice(c *gin.Context) {
|
||||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -143,7 +143,7 @@ func (h *DeviceHandler) DeleteDevice(c *gin.Context) {
|
|||||||
func (h *DeviceHandler) UpdateDeviceStatus(c *gin.Context) {
|
func (h *DeviceHandler) UpdateDeviceStatus(c *gin.Context) {
|
||||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -152,7 +152,7 @@ func (h *DeviceHandler) UpdateDeviceStatus(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,7 +163,7 @@ func (h *DeviceHandler) UpdateDeviceStatus(c *gin.Context) {
|
|||||||
case "inactive", "0":
|
case "inactive", "0":
|
||||||
status = domain.DeviceStatusInactive
|
status = domain.DeviceStatusInactive
|
||||||
default:
|
default:
|
||||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid status"})
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid status"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -182,7 +182,7 @@ func (h *DeviceHandler) GetUserDevices(c *gin.Context) {
|
|||||||
// IDOR 修复:检查当前用户是否有权限查看指定用户的设备
|
// IDOR 修复:检查当前用户是否有权限查看指定用户的设备
|
||||||
currentUserID, ok := getUserIDFromContext(c)
|
currentUserID, ok := getUserIDFromContext(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,13 +201,13 @@ func (h *DeviceHandler) GetUserDevices(c *gin.Context) {
|
|||||||
userIDParam := c.Param("id")
|
userIDParam := c.Param("id")
|
||||||
userID, err := strconv.ParseInt(userIDParam, 10, 64)
|
userID, err := strconv.ParseInt(userIDParam, 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 非管理员只能查看自己的设备
|
// 非管理员只能查看自己的设备
|
||||||
if !isAdmin && userID != currentUserID {
|
if !isAdmin && userID != currentUserID {
|
||||||
c.JSON(http.StatusForbidden, gin.H{"error": "无权访问该用户的设备列表"})
|
c.JSON(http.StatusForbidden, gin.H{"code": 403, "message": "无权访问该用户的设备列表"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -221,7 +221,7 @@ func (h *DeviceHandler) GetUserDevices(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"message": "success",
|
"message": "success",
|
||||||
"data": gin.H{
|
"data": gin.H{
|
||||||
"items": devices,
|
"items": devices,
|
||||||
@@ -236,7 +236,7 @@ func (h *DeviceHandler) GetUserDevices(c *gin.Context) {
|
|||||||
func (h *DeviceHandler) GetAllDevices(c *gin.Context) {
|
func (h *DeviceHandler) GetAllDevices(c *gin.Context) {
|
||||||
var req service.GetAllDevicesRequest
|
var req service.GetAllDevicesRequest
|
||||||
if err := c.ShouldBindQuery(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -263,12 +263,12 @@ func (h *DeviceHandler) GetAllDevices(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"message": "success",
|
"message": "success",
|
||||||
"data": gin.H{
|
"data": gin.H{
|
||||||
"items": devices,
|
"items": devices,
|
||||||
"total": total,
|
"total": total,
|
||||||
"page": req.Page,
|
"page": req.Page,
|
||||||
"page_size": req.PageSize,
|
"page_size": req.PageSize,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -283,13 +283,13 @@ type TrustDeviceRequest struct {
|
|||||||
func (h *DeviceHandler) TrustDevice(c *gin.Context) {
|
func (h *DeviceHandler) TrustDevice(c *gin.Context) {
|
||||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req TrustDeviceRequest
|
var req TrustDeviceRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -311,19 +311,19 @@ func (h *DeviceHandler) TrustDevice(c *gin.Context) {
|
|||||||
func (h *DeviceHandler) TrustDeviceByDeviceID(c *gin.Context) {
|
func (h *DeviceHandler) TrustDeviceByDeviceID(c *gin.Context) {
|
||||||
userID, ok := getUserIDFromContext(c)
|
userID, ok := getUserIDFromContext(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
deviceID := c.Param("deviceId")
|
deviceID := c.Param("deviceId")
|
||||||
if 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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req TrustDeviceRequest
|
var req TrustDeviceRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,7 +345,7 @@ func (h *DeviceHandler) TrustDeviceByDeviceID(c *gin.Context) {
|
|||||||
func (h *DeviceHandler) UntrustDevice(c *gin.Context) {
|
func (h *DeviceHandler) UntrustDevice(c *gin.Context) {
|
||||||
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -364,7 +364,7 @@ func (h *DeviceHandler) UntrustDevice(c *gin.Context) {
|
|||||||
func (h *DeviceHandler) GetMyTrustedDevices(c *gin.Context) {
|
func (h *DeviceHandler) GetMyTrustedDevices(c *gin.Context) {
|
||||||
userID, ok := getUserIDFromContext(c)
|
userID, ok := getUserIDFromContext(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -385,7 +385,7 @@ func (h *DeviceHandler) GetMyTrustedDevices(c *gin.Context) {
|
|||||||
func (h *DeviceHandler) LogoutAllOtherDevices(c *gin.Context) {
|
func (h *DeviceHandler) LogoutAllOtherDevices(c *gin.Context) {
|
||||||
userID, ok := getUserIDFromContext(c)
|
userID, ok := getUserIDFromContext(c)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
|
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -393,7 +393,7 @@ func (h *DeviceHandler) LogoutAllOtherDevices(c *gin.Context) {
|
|||||||
currentDeviceIDStr := c.GetHeader("X-Device-ID")
|
currentDeviceIDStr := c.GetHeader("X-Device-ID")
|
||||||
currentDeviceID, err := strconv.ParseInt(currentDeviceIDStr, 10, 64)
|
currentDeviceID, err := strconv.ParseInt(currentDeviceIDStr, 10, 64)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user