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,92 @@
package analytics
import (
analyticsservice "backend/internal/service/analytics"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// AnalyticsController 数据分析控制器 // Logan.Li
type AnalyticsController struct {
svc *analyticsservice.AnalyticsService
}
// NewAnalyticsController 创建控制器实例 // Logan.Li
func NewAnalyticsController(db *gorm.DB) *AnalyticsController {
return &AnalyticsController{svc: analyticsservice.NewAnalyticsService(db)}
}
// GetDashboardStats 仪表板统计 // Logan.Li
func (ctrl *AnalyticsController) GetDashboardStats(c *gin.Context) {
stats, err := ctrl.svc.GetDashboardStats()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": stats})
}
// GetExpiringLicenses 即将到期 License // Logan.Li
func (ctrl *AnalyticsController) GetExpiringLicenses(c *gin.Context) {
days, _ := strconv.Atoi(c.DefaultQuery("days", "30"))
licenses, err := ctrl.svc.GetExpiringLicenses(days)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": licenses})
}
// GetRecentActivities 最近活动 // Logan.Li
func (ctrl *AnalyticsController) GetRecentActivities(c *gin.Context) {
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
activities, err := ctrl.svc.GetRecentActivities(limit)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": activities})
}
// GetOnlineStats 在线统计 // Logan.Li
func (ctrl *AnalyticsController) GetOnlineStats(c *gin.Context) {
stats, err := ctrl.svc.GetOnlineStats()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": stats})
}
// GetRevenueStats 收入统计 // Logan.Li
func (ctrl *AnalyticsController) GetRevenueStats(c *gin.Context) {
stats, err := ctrl.svc.GetRevenueStats()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": stats})
}
// GetLicenseGrowth License 增长趋势 // Logan.Li
func (ctrl *AnalyticsController) GetLicenseGrowth(c *gin.Context) {
stats, err := ctrl.svc.GetLicenseGrowth()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": stats})
}
// GetProductDistribution 产品分布 // Logan.Li
func (ctrl *AnalyticsController) GetProductDistribution(c *gin.Context) {
stats, err := ctrl.svc.GetProductDistribution()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": stats})
}
@@ -0,0 +1,76 @@
package auth
import (
"backend/config"
authservice "backend/internal/service/auth"
"net/http"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// AuthController 认证控制器 // Logan.Li
type AuthController struct {
authService *authservice.AuthService
}
// NewAuthController 创建控制器实例 // Logan.Li
func NewAuthController(db *gorm.DB, cfg *config.Config) *AuthController {
return &AuthController{
authService: authservice.NewAuthService(db, cfg),
}
}
// Login 管理员登录 // Logan.Li
func (ctrl *AuthController) Login(c *gin.Context) {
var req struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "请求参数错误: " + err.Error()})
return
}
result, err := ctrl.authService.Login(req)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "登录成功", "data": result})
}
// Me 获取当前登录用户信息 // Logan.Li
func (ctrl *AuthController) Me(c *gin.Context) {
userID, _ := c.Get("user_id")
userInfo, err := ctrl.authService.GetCurrentUser(userID.(uint))
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": userInfo})
}
// ChangePassword 修改当前用户密码 // Logan.Li
func (ctrl *AuthController) ChangePassword(c *gin.Context) {
userID, _ := c.Get("user_id")
var req struct {
OldPassword string `json:"old_password" binding:"required"`
NewPassword string `json:"new_password" binding:"required,min=6"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "请求参数错误: " + err.Error()})
return
}
if err := ctrl.authService.ChangePassword(userID.(uint), req); err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "密码修改成功"})
}
@@ -0,0 +1,135 @@
package customer
import (
customerservice "backend/internal/service/customer"
"backend/utils"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// CustomerController 客户控制器 // Logan.Li
type CustomerController struct {
svc *customerservice.CustomerService
db *gorm.DB
}
// NewCustomerController 创建控制器实例 // Logan.Li
func NewCustomerController(db *gorm.DB) *CustomerController {
return &CustomerController{svc: customerservice.NewCustomerService(db), db: db}
}
// List 客户列表 // Logan.Li
func (ctrl *CustomerController) List(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
keyword := c.Query("keyword")
customers, 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": customers, "total": total, "page": page, "page_size": pageSize,
"total_pages": (total + int64(pageSize) - 1) / int64(pageSize),
}})
}
// GetAll 全部客户 // Logan.Li
func (ctrl *CustomerController) GetAll(c *gin.Context) {
customers, 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": customers})
}
// Get 客户详情 // Logan.Li
func (ctrl *CustomerController) Get(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
customer, 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": customer})
}
// Create 创建客户 // Logan.Li
func (ctrl *CustomerController) Create(c *gin.Context) {
var req struct {
Name string `json:"name" binding:"required"`
Company string `json:"company"`
Email string `json:"email" binding:"omitempty,email"`
Phone string `json:"phone"`
Address string `json:"address"`
Notes string `json:"notes"`
Status *int `json:"status"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
}
customer, 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", "customer", customer.ID, customer.Name, "", "", "创建客户")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "创建成功", "data": customer})
}
// Update 更新客户 // Logan.Li
func (ctrl *CustomerController) Update(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
var req struct {
Name string `json:"name"`
Company string `json:"company"`
Email string `json:"email" binding:"omitempty,email"`
Phone string `json:"phone"`
Address string `json:"address"`
Notes string `json:"notes"`
Status *int `json:"status"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
}
customer, 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", "customer", customer.ID, customer.Name, "", "", "更新客户")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "更新成功", "data": customer})
}
// Delete 删除客户 // Logan.Li
func (ctrl *CustomerController) 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", "customer", uint(id), "", "", "", "删除客户")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "删除成功"})
}
// UpdateStatus 切换状态 // Logan.Li
func (ctrl *CustomerController) 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", "customer", uint(id), "", "", "", "切换客户状态")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "状态更新成功"})
}
@@ -0,0 +1,96 @@
package entitlement
import (
entitlementservice "backend/internal/service/entitlement"
"backend/utils"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// EntitlementController 客户权益控制器 // Logan.Li
type EntitlementController struct {
svc *entitlementservice.EntitlementService
db *gorm.DB
}
// NewEntitlementController 创建控制器实例 // Logan.Li
func NewEntitlementController(db *gorm.DB) *EntitlementController {
return &EntitlementController{svc: entitlementservice.NewEntitlementService(db), db: db}
}
// List 权益列表 // Logan.Li
func (ctrl *EntitlementController) List(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
customerID, _ := strconv.ParseUint(c.Query("customer_id"), 10, 32)
productID, _ := strconv.ParseUint(c.Query("product_id"), 10, 32)
entitlements, total, err := ctrl.svc.List(page, pageSize, uint(customerID), 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": entitlements, "total": total, "page": page, "page_size": pageSize,
"total_pages": (total + int64(pageSize) - 1) / int64(pageSize),
}})
}
// Get 权益详情 // Logan.Li
func (ctrl *EntitlementController) Get(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
ent, 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": ent})
}
// Revoke 吊销权益 // Logan.Li
func (ctrl *EntitlementController) Revoke(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
if err := ctrl.svc.Revoke(uint(id)); err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
utils.LogAction(ctrl.db, c, "revoke", "entitlement", uint(id), "", "", "", "吊销客户权益")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "权益已吊销"})
}
// Renew 续费权益 // Logan.Li
func (ctrl *EntitlementController) Renew(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
var req struct {
PlanID uint `json:"plan_id" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
}
ent, err := ctrl.svc.Renew(uint(id), req.PlanID)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
utils.LogAction(ctrl.db, c, "renew", "entitlement", ent.ID, "", "", "", "续费客户权益")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "续费成功", "data": ent})
}
// GetByEmail 按邮箱查询客户权益(客户端接口) // Logan.Li
func (ctrl *EntitlementController) GetByEmail(c *gin.Context) {
email := c.Query("email")
if email == "" {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "缺少 email 参数"})
return
}
entitlements, err := ctrl.svc.GetByEmail(email)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": entitlements})
}
@@ -0,0 +1,79 @@
package license
import (
"net/http"
"github.com/gin-gonic/gin"
)
// Verify 客户端 License 验证 // Logan.Li
func (ctrl *LicenseController) Verify(c *gin.Context) {
var req struct {
LicenseKey string `json:"license_key" binding:"required"`
DeviceID string `json:"device_id"`
DeviceName string `json:"device_name"`
DeviceInfo string `json:"device_info"`
ProductID uint `json:"product_id"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
}
resp, _ := ctrl.svc.Verify(req)
c.JSON(http.StatusOK, resp)
}
// ActivateDevice 客户端激活(机器绑定) // Logan.Li
func (ctrl *LicenseController) ActivateDevice(c *gin.Context) {
var req struct {
LicenseKey string `json:"license_key" binding:"required"`
DeviceID string `json:"device_id" binding:"required"`
DeviceName string `json:"device_name"`
DeviceInfo string `json:"device_info"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
}
data, err := ctrl.svc.Activate(req, c.ClientIP())
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "激活成功", "data": data})
}
// Deactivate 客户端停用 // Logan.Li
func (ctrl *LicenseController) Deactivate(c *gin.Context) {
var req struct {
LicenseKey string `json:"license_key" binding:"required"`
DeviceID string `json:"device_id" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
}
if err := ctrl.svc.Deactivate(req); err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "停用成功"})
}
// Heartbeat 客户端心跳 // Logan.Li
func (ctrl *LicenseController) Heartbeat(c *gin.Context) {
var req struct {
LicenseKey string `json:"license_key" binding:"required"`
DeviceID string `json:"device_id" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
}
data, err := ctrl.svc.Heartbeat(req)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "心跳成功", "data": data})
}
@@ -0,0 +1,211 @@
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)
}
@@ -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})
}
@@ -0,0 +1,129 @@
package plan
import (
planservice "backend/internal/service/plan"
"backend/utils"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// PlanController 定价计划控制器 // Logan.Li
type PlanController struct {
svc *planservice.PlanService
db *gorm.DB
}
// NewPlanController 创建控制器实例 // Logan.Li
func NewPlanController(db *gorm.DB) *PlanController {
return &PlanController{svc: planservice.NewPlanService(db), db: db}
}
// List 计划列表 // Logan.Li
func (ctrl *PlanController) List(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
productID, _ := strconv.ParseUint(c.Query("product_id"), 10, 32)
plans, total, err := ctrl.svc.List(page, pageSize, 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": plans, "total": total, "page": page, "page_size": pageSize,
"total_pages": (total + int64(pageSize) - 1) / int64(pageSize),
}})
}
// GetByProduct 查询某产品的上架计划(公开接口) // Logan.Li
func (ctrl *PlanController) GetByProduct(c *gin.Context) {
productID, _ := strconv.ParseUint(c.Param("product_id"), 10, 32)
plans, err := ctrl.svc.GetByProduct(uint(productID))
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": plans})
}
// Create 创建计划 // Logan.Li
func (ctrl *PlanController) Create(c *gin.Context) {
var req struct {
ProductID uint `json:"product_id" binding:"required"`
Name string `json:"name" binding:"required"`
PlanType string `json:"plan_type" binding:"required"`
Price float64 `json:"price" binding:"required"`
Currency string `json:"currency"`
Duration int `json:"duration"`
MaxDevices int `json:"max_devices"`
Features []string `json:"features"`
SortOrder int `json:"sort_order"`
Status *int `json:"status"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
return
}
plan, 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", "plan", plan.ID, plan.Name, "", "", "创建定价计划")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "创建成功", "data": plan})
}
// Update 更新计划 // Logan.Li
func (ctrl *PlanController) Update(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
var req struct {
Name string `json:"name"`
Price *float64 `json:"price"`
Currency string `json:"currency"`
Duration *int `json:"duration"`
MaxDevices *int `json:"max_devices"`
Features []string `json:"features"`
SortOrder *int `json:"sort_order"`
Status *int `json:"status"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
}
plan, 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", "plan", plan.ID, plan.Name, "", "", "更新定价计划")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "更新成功", "data": plan})
}
// Delete 删除计划 // Logan.Li
func (ctrl *PlanController) 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", "plan", uint(id), "", "", "", "删除定价计划")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "删除成功"})
}
// ToggleStatus 切换上下架 // Logan.Li
func (ctrl *PlanController) ToggleStatus(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.ToggleStatus(uint(id), req.Status); err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
utils.LogAction(ctrl.db, c, "toggle_status", "plan", uint(id), "", "", "", "切换计划状态")
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "状态更新成功"})
}
@@ -0,0 +1,137 @@
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": "状态更新成功"})
}
@@ -0,0 +1,115 @@
package setting
import (
"backend/internal/model"
settingservice "backend/internal/service/setting"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// SettingController 系统设置控制器 // Logan.Li
type SettingController struct {
svc *settingservice.SettingService
}
// NewSettingController 创建控制器实例 // Logan.Li
func NewSettingController(db *gorm.DB) *SettingController {
return &SettingController{svc: settingservice.NewSettingService(db)}
}
// Get 获取系统设置 // Logan.Li
func (ctrl *SettingController) Get(c *gin.Context) {
setting, err := ctrl.svc.Get()
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 404, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": setting})
}
// Update 更新系统设置 // Logan.Li
func (ctrl *SettingController) Update(c *gin.Context) {
var req struct {
SystemName *string `json:"system_name"`
DefaultDuration *int `json:"default_duration"`
DefaultMaxActivations *int `json:"default_max_activations"`
ExpireReminderDays *int `json:"expire_reminder_days"`
APIRateLimit *int `json:"api_rate_limit"`
LogRetentionDays *int `json:"log_retention_days"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
}
setting, err := ctrl.svc.Update(req)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "更新成功", "data": setting})
}
// ListAuditLogs 审计日志列表 // Logan.Li
func (ctrl *SettingController) ListAuditLogs(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
action := c.Query("action")
targetType := c.Query("target_type")
logs, total, err := ctrl.svc.ListAuditLogs(page, pageSize, action, targetType)
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": logs, "total": total, "page": page, "page_size": pageSize,
"total_pages": (total + int64(pageSize) - 1) / int64(pageSize),
}})
}
// ClearAuditLogs 清空审计日志 // Logan.Li
func (ctrl *SettingController) ClearAuditLogs(c *gin.Context) {
if err := ctrl.svc.ClearAuditLogs(); err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "日志已清空"})
}
// ListLicenseTemplates 查询 License 模板 // Logan.Li
func (ctrl *SettingController) ListLicenseTemplates(c *gin.Context) {
templates, err := ctrl.svc.ListLicenseTemplates()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "data": templates})
}
// SaveLicenseTemplate 创建/更新 License 模板 // Logan.Li
func (ctrl *SettingController) SaveLicenseTemplate(c *gin.Context) {
var tmpl model.LicenseTemplate
if err := c.ShouldBindJSON(&tmpl); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
return
}
result, err := ctrl.svc.SaveLicenseTemplate(tmpl)
if err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "保存成功", "data": result})
}
// DeleteLicenseTemplate 删除 License 模板 // Logan.Li
func (ctrl *SettingController) DeleteLicenseTemplate(c *gin.Context) {
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
if err := ctrl.svc.DeleteLicenseTemplate(uint(id)); err != nil {
c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "删除成功"})
}