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
This commit is contained in:
Logan.Li committed 2026-08-07 15:02:00 +08:00
commit 4ae318335c
88 files changed
+10562

No files matched your search

@@ -0,0 +1,164 @@
package order
import (
"backend/internal/dto"
orderservice "backend/internal/service/order"
"backend/utils"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// OrderController 订单控制器 // Logan.Li
type OrderController struct {
svc *orderservice.OrderService
db *gorm.DB
}
// NewOrderController 创建控制器实例 // Logan.Li
func NewOrderController(db *gorm.DB) *OrderController {
return &OrderController{svc: orderservice.NewOrderService(db), db: db}
}
// List 订单列表(管理后台) // Logan.Li
func (ctrl *OrderController) List(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
status := c.Query("status")
productID, _ := strconv.ParseUint(c.Query("product_id"), 10, 32)
orders, total, err := ctrl.svc.List(page, pageSize, status, uint(productID))
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": orders, "total": total, "page": page, "page_size": pageSize,
"total_pages": (total + int64(pageSize) - 1) / int64(pageSize),
}})
}
// GetDetail 订单详情 // Logan.Li
func (ctrl *OrderController) GetDetail(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
order, 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": order})
}
// CreateOrder 客户端下单(公开接口) // Logan.Li
func (ctrl *OrderController) CreateOrder(c *gin.Context) {
var req struct {
CustomerEmail string `json:"customer_email" binding:"required,email"`
CustomerName string `json:"customer_name"`
ProductID uint `json:"product_id" binding:"required"`
PlanID uint `json:"plan_id" binding:"required"`
DeviceID string `json:"device_id"`
ReturnURL string `json:"return_url"`
CancelURL string `json:"cancel_url"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
}
resp, err := ctrl.svc.CreateOrder(req, utils.GetClientIP(c), c.GetHeader("User-Agent"))
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 2001, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "订单创建成功", "data": resp})
}
// PaymentWebhook 支付回调(公开接口,供支付渠道调用) // Logan.Li
func (ctrl *OrderController) PaymentWebhook(c *gin.Context) {
var req struct {
EventType string `json:"event_type" binding:"required"`
OrderNo string `json:"order_no" binding:"required"`
GatewayOrderNo string `json:"gateway_order_no"`
Status string `json:"status" binding:"required"`
Amount float64 `json:"amount"`
Currency string `json:"currency"`
CustomerEmail string `json:"customer_email"`
Gateway string `json:"gateway" binding:"required"`
Signature string `json:"signature"`
Payload string `json:"payload"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "Invalid payload"})
return
}
if err := ctrl.svc.PaymentWebhook(req, utils.GetClientIP(c)); err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "Webhook processed"})
}
// ManualGrant 手动发放 License(管理后台) // Logan.Li
func (ctrl *OrderController) ManualGrant(c *gin.Context) {
var req struct {
CustomerEmail string `json:"customer_email" binding:"required,email"`
CustomerName string `json:"customer_name"`
ProductID uint `json:"product_id" binding:"required"`
PlanID uint `json:"plan_id" binding:"required"`
DeviceID string `json:"device_id"`
Notes string `json:"notes"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
}
userID, _ := c.Get("user_id")
username, _ := c.Get("username")
data, err := ctrl.svc.ManualGrant(req, userID.(uint), username.(string), utils.GetClientIP(c))
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "License 发放成功", "data": data})
}
// CheckDuplicatePurchase 防重复购买检测(公开接口) // Logan.Li
func (ctrl *OrderController) CheckDuplicatePurchase(c *gin.Context) {
var req struct {
CustomerEmail string `json:"customer_email" binding:"required,email"`
ProductID uint `json:"product_id" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
}
// 通过 CheckPurchaseStatus 间接判断:有记录且有效则不允许重复购买 // Logan.Li
status, _ := ctrl.svc.CheckPurchaseStatus(req.CustomerEmail, req.ProductID)
result := dto.DuplicatePurchaseCheckResponse{AllowPurchase: true}
if status.HasActiveLicense {
result.AllowPurchase = false
result.Reason = status.Message
if result.Reason == "" {
result.Reason = "您已拥有该产品的有效授权"
}
result.ExistingLicenseKey = status.LicenseKey
result.ExistingPlanType = status.PlanType
result.CanUpgrade = true
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": result})
}
// CheckPurchaseStatus 查询购买状态(公开接口) // Logan.Li
func (ctrl *OrderController) CheckPurchaseStatus(c *gin.Context) {
var req struct {
CustomerEmail string `json:"customer_email" binding:"required,email"`
ProductID uint `json:"product_id" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
}
resp, _ := ctrl.svc.CheckPurchaseStatus(req.CustomerEmail, req.ProductID)
c.JSON(http.StatusOK, gin.H{"code": 0, "data": resp})
}