将状态码改成自定义码
This commit is contained in:
@@ -2,7 +2,6 @@ package device
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -112,7 +111,7 @@ func (c *Controller) CreateDevice(ctx *gin.Context) {
|
||||
var req CreateDeviceRequest
|
||||
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
|
||||
}
|
||||
|
||||
@@ -127,11 +126,11 @@ func (c *Controller) CreateDevice(ctx *gin.Context) {
|
||||
|
||||
if err := c.repo.Create(device); err != nil {
|
||||
c.logger.Errorf("创建设备: 数据库操作失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "创建设备失败")
|
||||
controller.SendErrorResponse(ctx, controller.CodeInternalError, "创建设备失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendResponse(ctx, http.StatusCreated, "设备创建成功", newDeviceResponse(device))
|
||||
controller.SendResponse(ctx, controller.CodeCreated, "设备创建成功", newDeviceResponse(device))
|
||||
}
|
||||
|
||||
// GetDevice godoc
|
||||
@@ -149,19 +148,19 @@ func (c *Controller) GetDevice(ctx *gin.Context) {
|
||||
device, err := c.repo.FindByIDString(deviceID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
controller.SendErrorResponse(ctx, http.StatusNotFound, "设备未找到")
|
||||
controller.SendErrorResponse(ctx, controller.CodeNotFound, "设备未找到")
|
||||
return
|
||||
}
|
||||
if strings.Contains(err.Error(), "无效的设备ID格式") {
|
||||
controller.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
c.logger.Errorf("获取设备: 数据库操作失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "获取设备信息失败")
|
||||
controller.SendErrorResponse(ctx, controller.CodeInternalError, "获取设备信息失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendResponse(ctx, http.StatusOK, "获取设备信息成功", newDeviceResponse(device))
|
||||
controller.SendResponse(ctx, controller.CodeSuccess, "获取设备信息成功", newDeviceResponse(device))
|
||||
}
|
||||
|
||||
// ListDevices godoc
|
||||
@@ -176,11 +175,11 @@ func (c *Controller) ListDevices(ctx *gin.Context) {
|
||||
devices, err := c.repo.ListAll()
|
||||
if err != nil {
|
||||
c.logger.Errorf("获取设备列表: 数据库操作失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "获取设备列表失败")
|
||||
controller.SendErrorResponse(ctx, controller.CodeInternalError, "获取设备列表失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendResponse(ctx, http.StatusOK, "获取设备列表成功", newListDeviceResponse(devices))
|
||||
controller.SendResponse(ctx, controller.CodeSuccess, "获取设备列表成功", newListDeviceResponse(devices))
|
||||
}
|
||||
|
||||
// UpdateDevice godoc
|
||||
@@ -201,15 +200,15 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) {
|
||||
existingDevice, err := c.repo.FindByIDString(deviceID)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
controller.SendErrorResponse(ctx, http.StatusNotFound, "设备未找到")
|
||||
controller.SendErrorResponse(ctx, controller.CodeNotFound, "设备未找到")
|
||||
return
|
||||
}
|
||||
if strings.Contains(err.Error(), "无效的设备ID格式") {
|
||||
controller.SendErrorResponse(ctx, http.StatusBadRequest, err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.CodeBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
c.logger.Errorf("更新设备: 查找设备失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "更新设备失败")
|
||||
controller.SendErrorResponse(ctx, controller.CodeInternalError, "更新设备失败")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -217,7 +216,7 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) {
|
||||
var req UpdateDeviceRequest
|
||||
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
|
||||
}
|
||||
|
||||
@@ -232,11 +231,11 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) {
|
||||
// 4. 将修改后的 existingDevice 对象保存回数据库
|
||||
if err := c.repo.Update(existingDevice); err != nil {
|
||||
c.logger.Errorf("更新设备: 数据库操作失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "更新设备失败")
|
||||
controller.SendErrorResponse(ctx, controller.CodeInternalError, "更新设备失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendResponse(ctx, http.StatusOK, "设备更新成功", newDeviceResponse(existingDevice))
|
||||
controller.SendResponse(ctx, controller.CodeSuccess, "设备更新成功", newDeviceResponse(existingDevice))
|
||||
}
|
||||
|
||||
// DeleteDevice godoc
|
||||
@@ -254,15 +253,15 @@ func (c *Controller) DeleteDevice(ctx *gin.Context) {
|
||||
// 我们需要先将字符串ID转换为uint,因为Delete方法需要uint类型
|
||||
idUint, err := strconv.ParseUint(deviceID, 10, 64)
|
||||
if err != nil {
|
||||
controller.SendErrorResponse(ctx, http.StatusBadRequest, "无效的设备ID格式")
|
||||
controller.SendErrorResponse(ctx, controller.CodeBadRequest, "无效的设备ID格式")
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.repo.Delete(uint(idUint)); err != nil {
|
||||
c.logger.Errorf("删除设备: 数据库操作失败: %v", err)
|
||||
controller.SendErrorResponse(ctx, http.StatusInternalServerError, "删除设备失败")
|
||||
controller.SendErrorResponse(ctx, controller.CodeInternalError, "删除设备失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendResponse(ctx, http.StatusOK, "设备删除成功", nil)
|
||||
controller.SendResponse(ctx, controller.CodeSuccess, "设备删除成功", nil)
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package plan
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
||||
@@ -72,7 +70,7 @@ func NewController(logger *logs.Logger, planRepo repository.PlanRepository) *Con
|
||||
func (pc *Controller) CreatePlan(c *gin.Context) {
|
||||
// 占位符:此处应调用服务层或仓库层来创建计划
|
||||
pc.logger.Infof("收到创建计划请求 (占位符)")
|
||||
controller.SendResponse(c, http.StatusCreated, "创建计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
|
||||
controller.SendResponse(c, controller.CodeCreated, "创建计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
|
||||
}
|
||||
|
||||
// GetPlan godoc
|
||||
@@ -87,7 +85,7 @@ func (pc *Controller) CreatePlan(c *gin.Context) {
|
||||
func (pc *Controller) GetPlan(c *gin.Context) {
|
||||
// 占位符:此处应调用服务层或仓库层来获取计划
|
||||
pc.logger.Infof("收到获取计划 (占位符)")
|
||||
controller.SendResponse(c, http.StatusOK, "获取计划接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
|
||||
controller.SendResponse(c, controller.CodeSuccess, "获取计划接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
|
||||
|
||||
}
|
||||
|
||||
@@ -102,7 +100,7 @@ func (pc *Controller) GetPlan(c *gin.Context) {
|
||||
func (pc *Controller) ListPlans(c *gin.Context) {
|
||||
// 占位符:此处应调用服务层或仓库层来获取计划列表
|
||||
pc.logger.Infof("收到获取计划列表请求 (占位符)")
|
||||
controller.SendResponse(c, http.StatusOK, "获取计划列表接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
|
||||
controller.SendResponse(c, controller.CodeSuccess, "获取计划列表接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
|
||||
}
|
||||
|
||||
// UpdatePlan godoc
|
||||
@@ -119,7 +117,7 @@ func (pc *Controller) ListPlans(c *gin.Context) {
|
||||
func (pc *Controller) UpdatePlan(c *gin.Context) {
|
||||
// 占位符:此处应调用服务层或仓库层来更新计划
|
||||
pc.logger.Infof("收到更新计划请求 (占位符)")
|
||||
controller.SendResponse(c, http.StatusOK, "更新计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
|
||||
controller.SendResponse(c, controller.CodeSuccess, "更新计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
|
||||
}
|
||||
|
||||
// DeletePlan godoc
|
||||
@@ -134,7 +132,7 @@ func (pc *Controller) UpdatePlan(c *gin.Context) {
|
||||
func (pc *Controller) DeletePlan(c *gin.Context) {
|
||||
// 占位符:此处应调用服务层或仓库层来删除计划
|
||||
pc.logger.Infof("收到删除计划请求 (占位符)")
|
||||
controller.SendResponse(c, http.StatusOK, "删除计划接口占位符", nil)
|
||||
controller.SendResponse(c, controller.CodeSuccess, "删除计划接口占位符", nil)
|
||||
}
|
||||
|
||||
// StartPlan godoc
|
||||
@@ -149,7 +147,7 @@ func (pc *Controller) DeletePlan(c *gin.Context) {
|
||||
func (pc *Controller) StartPlan(c *gin.Context) {
|
||||
// 占位符:此处应调用服务层或仓库层来启动计划
|
||||
pc.logger.Infof("收到启动计划请求 (占位符)")
|
||||
controller.SendResponse(c, http.StatusOK, "启动计划接口占位符", nil)
|
||||
controller.SendResponse(c, controller.CodeSuccess, "启动计划接口占位符", nil)
|
||||
}
|
||||
|
||||
// StopPlan godoc
|
||||
@@ -164,5 +162,5 @@ func (pc *Controller) StartPlan(c *gin.Context) {
|
||||
func (pc *Controller) StopPlan(c *gin.Context) {
|
||||
// 占位符:此处应调用服务层或仓库层来停止计划
|
||||
pc.logger.Infof("收到停止计划请求 (占位符)")
|
||||
controller.SendResponse(c, http.StatusOK, "停止计划接口占位符", nil)
|
||||
controller.SendResponse(c, controller.CodeSuccess, "停止计划接口占位符", nil)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,23 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// --- 业务状态码 ---
|
||||
const (
|
||||
// 成功状态码 (2000-2999)
|
||||
CodeSuccess = 2000 // 操作成功
|
||||
CodeCreated = 2001 // 创建成功
|
||||
|
||||
// 客户端错误状态码 (4000-4999)
|
||||
CodeBadRequest = 4000 // 请求参数错误
|
||||
CodeUnauthorized = 4001 // 未授权
|
||||
CodeNotFound = 4004 // 资源未找到
|
||||
CodeConflict = 4009 // 资源冲突
|
||||
|
||||
// 服务器错误状态码 (5000-5999)
|
||||
CodeInternalError = 5000 // 服务器内部错误
|
||||
CodeServiceUnavailable = 5003 // 服务不可用
|
||||
)
|
||||
|
||||
// --- 通用响应结构 ---
|
||||
|
||||
// Response 定义统一的API响应结构体
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user