优化接口返回值状态码
This commit is contained in:
		| @@ -3,7 +3,6 @@ | ||||
| package operation | ||||
|  | ||||
| import ( | ||||
| 	"net/http" | ||||
| 	"strconv" | ||||
|  | ||||
| 	"git.huangwc.com/pig/pig-farm-controller/internal/api/middleware" | ||||
| @@ -42,19 +41,19 @@ func (c *Controller) Create(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 CreateRequest | ||||
| 	if err := ctx.ShouldBindJSON(&req); err != nil { | ||||
| 		controller.SendErrorResponse(ctx, http.StatusBadRequest, "请求参数错误") | ||||
| 		controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "请求参数错误") | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| @@ -70,7 +69,7 @@ func (c *Controller) Create(ctx *gin.Context) { | ||||
|  | ||||
| 	if err := c.operationHistoryRepo.Create(operation); err != nil { | ||||
| 		c.logger.Error("创建操作历史记录失败: " + err.Error()) | ||||
| 		controller.SendErrorResponse(ctx, http.StatusInternalServerError, "创建操作历史记录失败") | ||||
| 		controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "创建操作历史记录失败") | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| @@ -82,13 +81,13 @@ func (c *Controller) ListByUser(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 | ||||
| 	} | ||||
|  | ||||
| @@ -111,7 +110,7 @@ func (c *Controller) ListByUser(ctx *gin.Context) { | ||||
| 	operations, err := c.operationHistoryRepo.ListByUserID(user.ID, offset, limit) | ||||
| 	if err != nil { | ||||
| 		c.logger.Error("获取操作历史记录失败: " + err.Error()) | ||||
| 		controller.SendErrorResponse(ctx, http.StatusInternalServerError, "获取操作历史记录失败") | ||||
| 		controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "获取操作历史记录失败") | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| @@ -123,7 +122,7 @@ func (c *Controller) Get(ctx *gin.Context) { | ||||
| 	// 获取操作历史记录ID | ||||
| 	id, err := strconv.ParseUint(ctx.Param("id"), 10, 64) | ||||
| 	if err != nil { | ||||
| 		controller.SendErrorResponse(ctx, http.StatusBadRequest, "无效的操作历史记录ID") | ||||
| 		controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "无效的操作历史记录ID") | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| @@ -131,26 +130,26 @@ func (c *Controller) Get(ctx *gin.Context) { | ||||
| 	operation, err := c.operationHistoryRepo.FindByID(uint(id)) | ||||
| 	if err != nil { | ||||
| 		c.logger.Error("获取操作历史记录失败: " + err.Error()) | ||||
| 		controller.SendErrorResponse(ctx, http.StatusInternalServerError, "获取操作历史记录失败") | ||||
| 		controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "获取操作历史记录失败") | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
| 	// 从上下文中获取用户信息 | ||||
| 	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 | ||||
| 	} | ||||
|  | ||||
| 	// 检查操作历史记录是否属于当前用户 | ||||
| 	if operation.UserID != user.ID { | ||||
| 		controller.SendErrorResponse(ctx, http.StatusForbidden, "无权访问该操作历史记录") | ||||
| 		controller.SendErrorResponse(ctx, controller.ForbiddenCode, "无权访问该操作历史记录") | ||||
| 		return | ||||
| 	} | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user