Files
opsx-licence/backend/internal/controller/product/product_controller.go
T
Logan.Li 4ae318335c feat: 完成 opsx-licence 许可证管理平台全栈实现
基于 opsx-web-lite 框架(Go+Gin+Vue3+Element Plus)构建许可证管理服务器,
业务域参考 Licence 服务器 Web 版。

【后端 - 4层架构】
- 认证:单 Admin 表 + JWT(HS256)
- 核心授权域:Product / Customer / License / Activation
  · License 生成/验证/激活/机器绑定/心跳/续期/吊销
  · 客户端契约接口:/api/v1/license/{verify,activate,deactivate,heartbeat}
- 定价订单:Plan / Order / 支付网关配置
- 防重复购买:CustomerEntitlement + 幂等支付 webhook + 手动发放
- 分析审计:Dashboard 统计 / Analytics / AuditLog / Setting 管理

【前端】
- Dashboard(ECharts 图表)、产品、客户、License、计划、订单、权益、日志、设置
- 9 个完整 CRUD 业务页面 + 登录/布局

【部署】
- SQLite + 单二进制(//go:embed 嵌入前端)
- 开发:./run.sh start(前端5181/后端9001)
- 生产:./run.sh build && ./run.sh binary start
- 默认账号:admin/admin

Author: Logan.Li
2026-08-07 15:02:00 +08:00

138 lines
4.5 KiB
Go

package product
import (
productservice "backend/internal/service/product"
"backend/utils"
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// ProductController 产品控制器 // Logan.Li
type ProductController struct {
svc *productservice.ProductService
db *gorm.DB
}
// NewProductController 创建控制器实例 // Logan.Li
func NewProductController(db *gorm.DB) *ProductController {
return &ProductController{svc: productservice.NewProductService(db), db: db}
}
// List 产品列表 // Logan.Li
func (ctrl *ProductController) List(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
keyword := c.Query("keyword")
products, total, err := ctrl.svc.List(page, pageSize, keyword)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{
"list": products, "total": total, "page": page, "page_size": pageSize,
"total_pages": (total + int64(pageSize) - 1) / int64(pageSize),
}})
}
// GetAll 全部产品(下拉用) // Logan.Li
func (ctrl *ProductController) GetAll(c *gin.Context) {
products, err := ctrl.svc.GetAll()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": products})
}
// Get 产品详情 // Logan.Li
func (ctrl *ProductController) Get(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
product, err := ctrl.svc.GetByID(uint(id))
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 404, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": product})
}
// Create 创建产品 // Logan.Li
func (ctrl *ProductController) Create(c *gin.Context) {
var req struct {
Name string `json:"name" binding:"required"`
Code string `json:"code" binding:"required"`
Version string `json:"version"`
Description string `json:"description"`
Features []string `json:"features"`
Price float64 `json:"price"`
PriceModel string `json:"price_model"`
Status *int `json:"status"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
}
product, err := ctrl.svc.Create(req)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
utils.LogAction(ctrl.db, c, "create", "product", product.ID, product.Name, "", "", "创建产品")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "创建成功", "data": product})
}
// Update 更新产品 // Logan.Li
func (ctrl *ProductController) Update(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
var req struct {
Name string `json:"name"`
Version string `json:"version"`
Description string `json:"description"`
Features []string `json:"features"`
Price *float64 `json:"price"`
PriceModel string `json:"price_model"`
Status *int `json:"status"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
}
product, err := ctrl.svc.Update(uint(id), req)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
utils.LogAction(ctrl.db, c, "update", "product", product.ID, product.Name, "", "", "更新产品")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "更新成功", "data": product})
}
// Delete 删除产品 // Logan.Li
func (ctrl *ProductController) Delete(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
if err := ctrl.svc.Delete(uint(id)); err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
utils.LogAction(ctrl.db, c, "delete", "product", uint(id), "", "", "", "删除产品")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "删除成功"})
}
// UpdateStatus 切换上下架 // Logan.Li
func (ctrl *ProductController) UpdateStatus(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
var req struct {
Status int `json:"status"`
}
c.ShouldBindJSON(&req)
if err := ctrl.svc.UpdateStatus(uint(id), req.Status); err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
utils.LogAction(ctrl.db, c, "update_status", "product", uint(id), "", "", "", fmt.Sprintf("切换状态为 %d", req.Status))
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "状态更新成功"})
}