1. 前端改为vue单体程序
2. 增加设备管理相关接口
This commit is contained in:
@@ -12,6 +12,19 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// ListResponse 设备列表响应结构体
|
||||
type ListResponse struct {
|
||||
Devices []model.Device `json:"devices"`
|
||||
}
|
||||
|
||||
// DeviceRequest 设备创建/更新请求结构体
|
||||
type DeviceRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Type model.DeviceType `json:"type" binding:"required"`
|
||||
ParentID *uint `json:"parent_id"`
|
||||
Status string `json:"status" binding:"required"`
|
||||
}
|
||||
|
||||
// Controller 设备控制控制器
|
||||
type Controller struct {
|
||||
deviceControlRepo repository.DeviceControlRepo
|
||||
@@ -30,6 +43,103 @@ func NewController(deviceControlRepo repository.DeviceControlRepo, deviceRepo re
|
||||
}
|
||||
}
|
||||
|
||||
// List 获取设备列表
|
||||
func (c *Controller) List(ctx *gin.Context) {
|
||||
devices, err := c.deviceRepo.ListAll()
|
||||
if err != nil {
|
||||
c.logger.Error("获取设备列表失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "获取设备列表失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendSuccessResponse(ctx, "获取设备列表成功", ListResponse{Devices: devices})
|
||||
}
|
||||
|
||||
// Create 创建设备
|
||||
func (c *Controller) Create(ctx *gin.Context) {
|
||||
var req DeviceRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "请求参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
device := &model.Device{
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
ParentID: req.ParentID,
|
||||
Status: req.Status,
|
||||
}
|
||||
|
||||
if err := c.deviceRepo.Create(device); err != nil {
|
||||
c.logger.Error("创建设备失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "创建设备失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendSuccessResponse(ctx, "创建设备成功", device)
|
||||
}
|
||||
|
||||
// Update 更新设备
|
||||
func (c *Controller) Update(ctx *gin.Context) {
|
||||
var req struct {
|
||||
ID uint `json:"id" binding:"required"`
|
||||
DeviceRequest
|
||||
}
|
||||
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "请求参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
device, err := c.deviceRepo.FindByID(req.ID)
|
||||
if err != nil {
|
||||
c.logger.Error("查找设备失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.NotFoundCode, "设备不存在")
|
||||
return
|
||||
}
|
||||
|
||||
device.Name = req.Name
|
||||
device.Type = req.Type
|
||||
device.ParentID = req.ParentID
|
||||
device.Status = req.Status
|
||||
|
||||
if err := c.deviceRepo.Update(device); err != nil {
|
||||
c.logger.Error("更新设备失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "更新设备失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendSuccessResponse(ctx, "更新设备成功", device)
|
||||
}
|
||||
|
||||
// Delete 删除设备
|
||||
func (c *Controller) Delete(ctx *gin.Context) {
|
||||
var req struct {
|
||||
ID uint `json:"id" binding:"required"`
|
||||
}
|
||||
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "请求参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 先检查设备是否存在
|
||||
_, err := c.deviceRepo.FindByID(req.ID)
|
||||
if err != nil {
|
||||
c.logger.Error("查找设备失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.NotFoundCode, "设备不存在")
|
||||
return
|
||||
}
|
||||
|
||||
if err := c.deviceRepo.Delete(req.ID); err != nil {
|
||||
c.logger.Error("删除设备失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "删除设备失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendSuccessResponse(ctx, "删除设备成功", nil)
|
||||
}
|
||||
|
||||
// SwitchRequest 设备控制请求结构体
|
||||
type SwitchRequest struct {
|
||||
ParentID *uint `json:"parent_id"` // 区域主控ID
|
||||
|
||||
Reference in New Issue
Block a user