将状态码改成自定义码

This commit is contained in:
2025-09-14 13:23:16 +08:00
parent b0ce191aff
commit b3322fe367
4 changed files with 52 additions and 40 deletions

View File

@@ -1,8 +1,6 @@
package user
import (
"net/http"
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/app/service/token"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
@@ -67,7 +65,7 @@ func (c *Controller) CreateUser(ctx *gin.Context) {
var req CreateUserRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
c.logger.Errorf("创建用户: 参数绑定失败: %v", err)
controller.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
return
}
@@ -82,16 +80,16 @@ func (c *Controller) CreateUser(ctx *gin.Context) {
// 尝试查询用户,以判断是否是用户名重复导致的错误
_, findErr := c.userRepo.FindByUsername(req.Username)
if findErr == nil { // 如果能找到用户,说明是用户名重复
controller.SendErrorResponse(ctx, http.StatusConflict, "用户名已存在")
controller.SendErrorResponse(ctx, controller.CodeConflict, "用户名已存在")
return
}
// 其他创建失败的情况
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "创建用户失败")
controller.SendErrorResponse(ctx, controller.CodeInternalError, "创建用户失败")
return
}
controller.SendResponse(ctx, http.StatusCreated, "用户创建成功", CreateUserResponse{
controller.SendResponse(ctx, controller.CodeCreated, "用户创建成功", CreateUserResponse{
Username: user.Username,
ID: user.ID,
})
@@ -111,23 +109,23 @@ func (c *Controller) Login(ctx *gin.Context) {
var req LoginRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
c.logger.Errorf("登录: 参数绑定失败: %v", err)
controller.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
return
}
user, err := c.userRepo.FindByUsername(req.Username)
if err != nil {
if err == gorm.ErrRecordNotFound {
controller.SendErrorResponse(ctx, http.StatusUnauthorized, "用户名或密码不正确")
controller.SendErrorResponse(ctx, controller.CodeUnauthorized, "用户名或密码不正确")
return
}
c.logger.Errorf("登录: 查询用户失败: %v", err)
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "登录失败")
controller.SendErrorResponse(ctx, controller.CodeInternalError, "登录失败")
return
}
if !user.CheckPassword(req.Password) {
controller.SendErrorResponse(ctx, http.StatusUnauthorized, "用户名或密码不正确")
controller.SendErrorResponse(ctx, controller.CodeUnauthorized, "用户名或密码不正确")
return
}
@@ -135,11 +133,11 @@ func (c *Controller) Login(ctx *gin.Context) {
tokenString, err := c.tokenService.GenerateToken(user.ID)
if err != nil {
c.logger.Errorf("登录: 生成令牌失败: %v", err)
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "登录失败,无法生成认证信息")
controller.SendErrorResponse(ctx, controller.CodeInternalError, "登录失败,无法生成认证信息")
return
}
controller.SendResponse(ctx, http.StatusOK, "登录成功", LoginResponse{
controller.SendResponse(ctx, controller.CodeSuccess, "登录成功", LoginResponse{
Username: user.Username,
ID: user.ID,
Token: tokenString,