基于 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
212 lines
7.3 KiB
Go
212 lines
7.3 KiB
Go
package license
|
|
|
|
import (
|
|
licenseservice "backend/internal/service/license"
|
|
"backend/utils"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// LicenseController License 控制器 // Logan.Li
|
|
type LicenseController struct {
|
|
svc *licenseservice.LicenseService
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewLicenseController 创建控制器实例 // Logan.Li
|
|
func NewLicenseController(db *gorm.DB) *LicenseController {
|
|
return &LicenseController{svc: licenseservice.NewLicenseService(db), db: db}
|
|
}
|
|
|
|
// parseListQuery 从请求解析筛选参数 // Logan.Li
|
|
func parseListQuery(c *gin.Context) licenseservice.ListQuery {
|
|
q := licenseservice.ListQuery{
|
|
Keyword: c.Query("keyword"),
|
|
Status: c.Query("status"),
|
|
}
|
|
if v, err := strconv.ParseUint(c.Query("customer_id"), 10, 32); err == nil {
|
|
q.CustomerID = uint(v)
|
|
}
|
|
if v, err := strconv.ParseUint(c.Query("product_id"), 10, 32); err == nil {
|
|
q.ProductID = uint(v)
|
|
}
|
|
return q
|
|
}
|
|
|
|
// List License 列表 // Logan.Li
|
|
func (ctrl *LicenseController) List(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
|
|
|
licenses, total, err := ctrl.svc.List(page, pageSize, parseListQuery(c))
|
|
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": licenses, "total": total, "page": page, "page_size": pageSize,
|
|
"total_pages": (total + int64(pageSize) - 1) / int64(pageSize),
|
|
}})
|
|
}
|
|
|
|
// Get License 详情 // Logan.Li
|
|
func (ctrl *LicenseController) Get(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
lic, activations, 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": gin.H{"license": lic, "activations": activations}})
|
|
}
|
|
|
|
// Create 创建 License // Logan.Li
|
|
func (ctrl *LicenseController) Create(c *gin.Context) {
|
|
var req struct {
|
|
CustomerID uint `json:"customer_id" binding:"required"`
|
|
ProductID uint `json:"product_id" binding:"required"`
|
|
Duration int `json:"duration" binding:"required"`
|
|
MaxActivations int `json:"max_activations"`
|
|
IsTrial bool `json:"is_trial"`
|
|
TrialDays int `json:"trial_days"`
|
|
Notes string `json:"notes"`
|
|
Count int `json:"count"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
|
|
return
|
|
}
|
|
licenses, 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", "license", licenses[0].ID, licenses[0].LicenseKey, "", "",
|
|
"创建 "+strconv.Itoa(len(licenses))+" 个 License")
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "创建成功", "data": licenses})
|
|
}
|
|
|
|
// Update 更新 License // Logan.Li
|
|
func (ctrl *LicenseController) Update(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
var updates map[string]interface{}
|
|
if err := c.ShouldBindJSON(&updates); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
|
|
return
|
|
}
|
|
lic, err := ctrl.svc.Update(uint(id), updates)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
|
return
|
|
}
|
|
utils.LogAction(ctrl.db, c, "update", "license", lic.ID, lic.LicenseKey, "", "", "更新 License")
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "更新成功", "data": lic})
|
|
}
|
|
|
|
// Delete 删除 License // Logan.Li
|
|
func (ctrl *LicenseController) Delete(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
lic, err := ctrl.svc.Delete(uint(id))
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
|
return
|
|
}
|
|
utils.LogAction(ctrl.db, c, "delete", "license", lic.ID, lic.LicenseKey, "", "", "删除 License")
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "删除成功"})
|
|
}
|
|
|
|
// Renew 续期 // Logan.Li
|
|
func (ctrl *LicenseController) Renew(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
var req struct {
|
|
Duration int `json:"duration" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
|
|
return
|
|
}
|
|
lic, err := ctrl.svc.Renew(uint(id), req.Duration)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
|
return
|
|
}
|
|
utils.LogAction(ctrl.db, c, "renew", "license", lic.ID, lic.LicenseKey, "", "", "续期 License")
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "续期成功", "data": lic})
|
|
}
|
|
|
|
// Revoke 吊销 // Logan.Li
|
|
func (ctrl *LicenseController) Revoke(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
var req struct {
|
|
Reason string `json:"reason"`
|
|
}
|
|
c.ShouldBindJSON(&req)
|
|
lic, err := ctrl.svc.Revoke(uint(id), req.Reason)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
|
return
|
|
}
|
|
utils.LogAction(ctrl.db, c, "revoke", "license", lic.ID, lic.LicenseKey, "", "", "吊销 License: "+req.Reason)
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "已吊销", "data": lic})
|
|
}
|
|
|
|
// Suspend 暂停 // Logan.Li
|
|
func (ctrl *LicenseController) Suspend(c *gin.Context) {
|
|
ctrl.setStatus(c, "suspended")
|
|
}
|
|
|
|
// Activate 激活(管理后台) // Logan.Li
|
|
func (ctrl *LicenseController) Activate(c *gin.Context) {
|
|
ctrl.setStatus(c, "active")
|
|
}
|
|
|
|
// setStatus 通用状态切换 // Logan.Li
|
|
func (ctrl *LicenseController) setStatus(c *gin.Context, status string) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
lic, err := ctrl.svc.SetStatus(uint(id), status)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
|
return
|
|
}
|
|
utils.LogAction(ctrl.db, c, status, "license", lic.ID, lic.LicenseKey, "", "", "License 状态变更为 "+status)
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "操作成功", "data": lic})
|
|
}
|
|
|
|
// GetActivations 获取激活记录 // Logan.Li
|
|
func (ctrl *LicenseController) GetActivations(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
activations, err := ctrl.svc.GetActivations(uint(id))
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "data": activations})
|
|
}
|
|
|
|
// ForceOffline 强制下线 // Logan.Li
|
|
func (ctrl *LicenseController) ForceOffline(c *gin.Context) {
|
|
licenseID, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
activationID, _ := strconv.ParseUint(c.Param("activationId"), 10, 32)
|
|
if err := ctrl.svc.ForceOffline(uint(licenseID), uint(activationID)); err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
|
return
|
|
}
|
|
utils.LogAction(ctrl.db, c, "force_offline", "activation", uint(activationID), "", "", "", "强制下线设备")
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "强制下线成功"})
|
|
}
|
|
|
|
// Export 导出 CSV // Logan.Li
|
|
func (ctrl *LicenseController) Export(c *gin.Context) {
|
|
csv, err := ctrl.svc.ExportCSV(parseListQuery(c))
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
|
|
return
|
|
}
|
|
c.Header("Content-Type", "text/csv; charset=utf-8")
|
|
c.Header("Content-Disposition", "attachment; filename=licenses.csv")
|
|
c.String(http.StatusOK, csv)
|
|
}
|