1. 前端改为vue单体程序

2. 增加设备管理相关接口
This commit is contained in:
2025-09-08 18:16:40 +08:00
parent e5383ce268
commit 7112a16ca8
853 changed files with 546690 additions and 620 deletions

View File

@@ -178,6 +178,26 @@ func (a *API) setupRoutes() {
userGroup.POST("/login", a.userController.Login)
}
// 配置静态文件服务
a.engine.Static("/assets/", "./frontend/dist/assets/")
// 使用NoRoute处理器处理前端路由
a.engine.NoRoute(func(c *gin.Context) {
path := c.Request.URL.Path
// 判断是否为API路径
if strings.HasPrefix(path, "/api/") || strings.HasPrefix(path, "/ws/") || path == "/health" {
// API路径返回404
c.JSON(http.StatusNotFound, gin.H{
"error": "API路径未找到",
})
return
}
// 对于前端路由提供index.html支持Vue Router的history模式
c.File("./frontend/dist/index.html")
})
// 需要鉴权的路由组
protectedGroup := a.engine.Group("/api/v1")
protectedGroup.Use(a.authMiddleware.Handle())
@@ -194,6 +214,10 @@ func (a *API) setupRoutes() {
deviceGroup := protectedGroup.Group("/device")
{
deviceGroup.POST("/switch", a.deviceController.Switch)
deviceGroup.GET("/list", a.deviceController.List)
deviceGroup.POST("/create", a.deviceController.Create)
deviceGroup.POST("/update", a.deviceController.Update)
deviceGroup.POST("/delete", a.deviceController.Delete)
}
// 远程控制相关路由
@@ -204,26 +228,6 @@ func (a *API) setupRoutes() {
}
}
// 使用NoRoute处理器处理前端路由
a.engine.NoRoute(func(c *gin.Context) {
path := c.Request.URL.Path
// 判断是否为API路径
if strings.HasPrefix(path, "/api/") || strings.HasPrefix(path, "/ws/") {
// API路径返回404
c.JSON(http.StatusNotFound, gin.H{
"error": "API路径未找到",
})
return
}
// 其他路径提供前端静态文件服务
c.File("./frontend/index.html")
})
// 静态文件服务 - 提供前端静态资源
a.engine.Static("/static", "./frontend/static")
// TODO: 添加更多路由
}

View File

@@ -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

View File

@@ -31,6 +31,9 @@ type DeviceRepo interface {
// Delete 删除设备
Delete(id uint) error
// ListAll 获取所有设备列表
ListAll() ([]model.Device, error)
}
// DeviceControlRepo 设备控制仓库接口
@@ -103,6 +106,15 @@ func (r *deviceRepo) FindByIDString(id string) (*model.Device, error) {
return &device, nil
}
// ListAll 获取所有设备列表
func (r *deviceRepo) ListAll() ([]model.Device, error) {
var devices []model.Device
if err := r.db.Find(&devices).Error; err != nil {
return nil, err
}
return devices, nil
}
// FindByParentID 根据上级设备ID查找设备
func (r *deviceRepo) FindByParentID(parentID uint) ([]*model.Device, error) {
var devices []*model.Device