优化接口返回值状态码

This commit is contained in:
2025-09-08 12:30:33 +08:00
parent c7e2aaee89
commit 6f3f7db870
4 changed files with 55 additions and 34 deletions

View File

@@ -3,8 +3,6 @@
package device
import (
"net/http"
"git.huangwc.com/pig/pig-farm-controller/internal/api/middleware"
"git.huangwc.com/pig/pig-farm-controller/internal/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/logs"
@@ -49,19 +47,19 @@ func (c *Controller) Switch(ctx *gin.Context) {
// 从上下文中获取用户信息
userValue, exists := ctx.Get("user")
if !exists {
controller.SendErrorResponse(ctx, http.StatusUnauthorized, "无法获取用户信息")
controller.SendErrorResponse(ctx, controller.UnauthorizedCode, "无法获取用户信息")
return
}
user, ok := userValue.(*middleware.AuthUser)
if !ok {
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "用户信息格式错误")
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "用户信息格式错误")
return
}
var req SwitchRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
controller.SendErrorResponse(ctx, http.StatusBadRequest, "请求参数错误")
controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "请求参数错误")
return
}
@@ -78,7 +76,7 @@ func (c *Controller) Switch(ctx *gin.Context) {
"设备控制成功",
); err != nil {
c.logger.Error("创建设备控制记录失败: " + err.Error())
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "设备控制失败")
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "设备控制失败")
return
}