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

+42
View File
@@ -0,0 +1,42 @@
package model
import "time"
// AuditLog 操作日志模型 // Logan.Li
type AuditLog struct {
ID uint `json:"id" gorm:"primaryKey"`
UserID uint `json:"user_id"`
Username string `json:"username" gorm:"size:100"`
Action string `json:"action" gorm:"size:50;not null"` // create / update / delete / activate / renew / revoke ...
TargetType string `json:"target_type" gorm:"size:50"` // license / customer / product / order ...
TargetID uint `json:"target_id"`
TargetName string `json:"target_name" gorm:"size:200"`
OldData string `json:"old_data" gorm:"type:text"` // JSON
NewData string `json:"new_data" gorm:"type:text"` // JSON
Detail string `json:"detail" gorm:"type:text"`
Result string `json:"result" gorm:"size:20"` // success / fail
ErrorMessage string `json:"error_message" gorm:"type:text"`
IP string `json:"ip" gorm:"size:50"`
CreatedAt time.Time `json:"created_at"`
}
// TableName 指定表名 // Logan.Li
func (AuditLog) TableName() string {
return "audit_logs"
}
// LicenseTemplate License 模板(用于快速创建) // Logan.Li
type LicenseTemplate struct {
ID uint `json:"id" gorm:"primaryKey"`
Name string `json:"name" gorm:"size:100;not null"`
Duration int `json:"duration" gorm:"default:365"` // 天数,0 表示永久
MaxActivations int `json:"max_activations" gorm:"default:1"`
Description string `json:"description" gorm:"type:text"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName 指定表名 // Logan.Li
func (LicenseTemplate) TableName() string {
return "license_templates"
}
+28
View File
@@ -0,0 +1,28 @@
package model
import (
"time"
"gorm.io/gorm"
)
// Customer 客户模型 // Logan.Li
type Customer struct {
ID uint `json:"id" gorm:"primaryKey"`
Name string `json:"name" gorm:"size:100;not null"`
Company string `json:"company" gorm:"size:200"`
Email string `json:"email" gorm:"size:100;index"`
Phone string `json:"phone" gorm:"size:20"`
Address string `json:"address" gorm:"size:500"`
Notes string `json:"notes" gorm:"type:text"`
Status int `json:"status" gorm:"default:1"` // 1: 启用, 0: 禁用
Licenses []License `json:"licenses,omitempty" gorm:"foreignKey:CustomerID"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"index"`
}
// TableName 指定表名 // Logan.Li
func (Customer) TableName() string {
return "customers"
}
+28
View File
@@ -0,0 +1,28 @@
package model
import "time"
// CustomerEntitlement 客户权益记录(防重复购买的核心表) // Logan.Li
type CustomerEntitlement struct {
ID uint `json:"id" gorm:"primaryKey"`
CustomerID uint `json:"customer_id"`
Customer Customer `json:"customer,omitempty" gorm:"foreignKey:CustomerID"`
ProductID uint `json:"product_id"`
Product Product `json:"product,omitempty" gorm:"foreignKey:ProductID"`
PlanID uint `json:"plan_id"`
Plan Plan `json:"plan,omitempty" gorm:"foreignKey:PlanID"`
LicenseID uint `json:"license_id"`
LicenseKey string `json:"license_key" gorm:"size:100"`
PlanType string `json:"plan_type" gorm:"size:50"` // perpetual / monthly / yearly / lifetime
Status string `json:"status" gorm:"size:20;default:'active'"` // active / expired / revoked
ValidFrom time.Time `json:"valid_from"`
ValidUntil *time.Time `json:"valid_until,omitempty"` // 买断/永久为 null
Renewable bool `json:"renewable" gorm:"default:true"` // 订阅到期后是否可续费
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName 指定表名 // Logan.Li
func (CustomerEntitlement) TableName() string {
return "customer_entitlements"
}
+83
View File
@@ -0,0 +1,83 @@
package model
import (
"time"
"gorm.io/gorm"
)
// License 期限常量(天数,0 表示永久) // Logan.Li
const (
Duration1Month = 30
Duration3Months = 90
Duration6Months = 180
Duration1Year = 365
Duration2Years = 730
Duration3Years = 1095
Duration5Years = 1825
DurationForever = 0
)
// License 状态常量 // Logan.Li
const (
LicenseStatusInactive = "inactive"
LicenseStatusActive = "active"
LicenseStatusExpired = "expired"
LicenseStatusRevoked = "revoked"
LicenseStatusSuspended = "suspended"
)
// PermanentEndDate 永久授权的到期时间(2099-12-31) // Logan.Li
var PermanentEndDate = time.Date(2099, 12, 31, 23, 59, 59, 0, time.UTC)
// License 许可证模型 // Logan.Li
type License struct {
ID uint `json:"id" gorm:"primaryKey"`
LicenseKey string `json:"license_key" gorm:"size:100;uniqueIndex;not null"`
CustomerID uint `json:"customer_id"`
Customer Customer `json:"customer,omitempty" gorm:"foreignKey:CustomerID"`
ProductID uint `json:"product_id"`
Product Product `json:"product,omitempty" gorm:"foreignKey:ProductID"`
Status string `json:"status" gorm:"size:20;default:'inactive'"`
StartDate time.Time `json:"start_date"`
EndDate time.Time `json:"end_date"`
Duration int `json:"duration" gorm:"default:365"` // 天数,0 表示永久
MaxActivations int `json:"max_activations" gorm:"default:1"`
CurrentActivations int `json:"current_activations" gorm:"default:0"`
IsTrial bool `json:"is_trial" gorm:"default:false"`
TrialDays int `json:"trial_days" gorm:"default:0"`
RevokeReason string `json:"revoke_reason" gorm:"type:text"`
Notes string `json:"notes" gorm:"type:text"`
Activations []Activation `json:"activations,omitempty" gorm:"foreignKey:LicenseID"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"index"`
}
// TableName 指定表名 // Logan.Li
func (License) TableName() string {
return "licenses"
}
// Activation 激活记录模型(机器绑定) // Logan.Li
type Activation struct {
ID uint `json:"id" gorm:"primaryKey"`
LicenseID uint `json:"license_id"`
License License `json:"license,omitempty" gorm:"foreignKey:LicenseID"`
DeviceID string `json:"device_id" gorm:"size:100"`
DeviceName string `json:"device_name" gorm:"size:200"`
DeviceInfo string `json:"device_info" gorm:"type:text"` // JSON 格式存储设备信息
IP string `json:"ip" gorm:"size:50"`
Status string `json:"status" gorm:"size:20;default:'active'"` // active / inactive
IsOnline bool `json:"is_online" gorm:"default:false"`
ActivatedAt time.Time `json:"activated_at"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
LastSeenAt time.Time `json:"last_seen_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName 指定表名 // Logan.Li
func (Activation) TableName() string {
return "activations"
}
+54
View File
@@ -0,0 +1,54 @@
package model
import "time"
// Order 订单模型 // Logan.Li
type Order struct {
ID uint `json:"id" gorm:"primaryKey"`
OrderNo string `json:"order_no" gorm:"size:100;uniqueIndex;not null"` // 系统唯一订单号(幂等键)
CustomerID uint `json:"customer_id"`
Customer Customer `json:"customer,omitempty" gorm:"foreignKey:CustomerID"`
ProductID uint `json:"product_id"`
Product Product `json:"product,omitempty" gorm:"foreignKey:ProductID"`
PlanID uint `json:"plan_id"`
Plan Plan `json:"plan,omitempty" gorm:"foreignKey:PlanID"`
LicenseID uint `json:"license_id"` // 关联的 License ID
Amount float64 `json:"amount" gorm:"type:decimal(10,2);not null"`
Currency string `json:"currency" gorm:"size:3;default:'USD'"`
Status string `json:"status" gorm:"size:20;default:'pending'"` // pending / paid / failed / refunded / cancelled
PaymentMethod string `json:"payment_method" gorm:"size:50"` // paddle / stripe / creem / waffo / alipay / wechat
PaymentGateway string `json:"payment_gateway" gorm:"size:50"`
GatewayOrderNo string `json:"gateway_order_no" gorm:"size:200"`
PaidAt *time.Time `json:"paid_at,omitempty"`
RefundAt *time.Time `json:"refund_at,omitempty"`
ClientIP string `json:"client_ip" gorm:"size:50"`
UserAgent string `json:"user_agent" gorm:"size:500"`
Notes string `json:"notes" gorm:"type:text"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName 指定表名 // Logan.Li
func (Order) TableName() string {
return "orders"
}
// PaymentGatewayConfig 支付网关配置 // Logan.Li
type PaymentGatewayConfig struct {
Gateway string `json:"gateway" gorm:"size:50;primaryKey"` // paddle / stripe / creem / waffo
Enabled bool `json:"enabled" gorm:"default:false"`
PublicKey string `json:"public_key" gorm:"type:text"`
SecretKey string `json:"secret_key" gorm:"type:text"` // 加密存储
WebhookSecret string `json:"webhook_secret" gorm:"type:text"` // Webhook 验签密钥
SandboxMode bool `json:"sandbox_mode" gorm:"default:true"`
Currency string `json:"currency" gorm:"size:3;default:'USD'"`
Region string `json:"region" gorm:"size:50"`
Priority int `json:"priority" gorm:"default:0"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName 指定表名 // Logan.Li
func (PaymentGatewayConfig) TableName() string {
return "payment_gateway_configs"
}
+26
View File
@@ -0,0 +1,26 @@
package model
import "time"
// Plan 定价计划模型(支持买断/订阅多档位) // Logan.Li
type Plan struct {
ID uint `json:"id" gorm:"primaryKey"`
ProductID uint `json:"product_id"`
Product Product `json:"product,omitempty" gorm:"foreignKey:ProductID"`
Name string `json:"name" gorm:"size:100;not null"` // 如:个人版、专业版、企业版
PlanType string `json:"plan_type" gorm:"size:50;not null"` // perpetual / monthly / yearly / lifetime
Price float64 `json:"price" gorm:"type:decimal(10,2);not null"`
Currency string `json:"currency" gorm:"size:3;default:'USD'"` // USD / CNY
Duration int `json:"duration" gorm:"default:0"` // 天数:0=永久/买断,30=月,365=年
MaxDevices int `json:"max_devices" gorm:"default:1"` // 允许激活设备数
Features string `json:"features" gorm:"type:text"` // JSON 功能清单
SortOrder int `json:"sort_order" gorm:"default:0"`
Status int `json:"status" gorm:"default:1"` // 1: 上架, 0: 下架
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName 指定表名 // Logan.Li
func (Plan) TableName() string {
return "plans"
}
+30
View File
@@ -0,0 +1,30 @@
package model
import (
"time"
"gorm.io/gorm"
)
// Product 产品模型 // Logan.Li
type Product struct {
ID uint `json:"id" gorm:"primaryKey"`
Name string `json:"name" gorm:"size:100;not null"`
Code string `json:"code" gorm:"size:50;uniqueIndex"`
Version string `json:"version" gorm:"size:50"`
Description string `json:"description" gorm:"type:text"`
Features string `json:"features" gorm:"type:text"` // JSON 格式存储功能列表
Price float64 `json:"price" gorm:"type:decimal(10,2)"`
PriceModel string `json:"price_model" gorm:"size:50;default:'subscription'"` // subscription / perpetual / trial
Status int `json:"status" gorm:"default:1"` // 1: 上架, 0: 下架
Licenses []License `json:"licenses,omitempty" gorm:"foreignKey:ProductID"`
Plans []Plan `json:"plans,omitempty" gorm:"foreignKey:ProductID"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"index"`
}
// TableName 指定表名 // Logan.Li
func (Product) TableName() string {
return "products"
}
+27
View File
@@ -0,0 +1,27 @@
package model
import "time"
// Setting 系统设置模型(单行配置,后续阶段启用) // Logan.Li
type Setting struct {
ID uint `json:"id" gorm:"primaryKey"`
SystemName string `json:"system_name" gorm:"size:100;default:'opsx-licence'"`
SystemLogo string `json:"system_logo" gorm:"size:500"`
DefaultDuration int `json:"default_duration" gorm:"default:365"` // 默认 License 有效天数
DefaultMaxActivations int `json:"default_max_activations" gorm:"default:1"` // 默认最大激活数
ExpireReminderDays int `json:"expire_reminder_days" gorm:"default:7"` // 到期提醒天数
SMTPServer string `json:"smtp_server" gorm:"size:200"`
SMTPPort int `json:"smtp_port"`
SMTPUsername string `json:"smtp_username" gorm:"size:100"`
SMTPPassword string `json:"smtp_password" gorm:"size:100"`
SenderEmail string `json:"sender_email" gorm:"size:100"`
SenderName string `json:"sender_name" gorm:"size:100"`
APIRateLimit int `json:"api_rate_limit" gorm:"default:1000"`
LogRetentionDays int `json:"log_retention_days" gorm:"default:365"`
UpdatedAt time.Time `json:"updated_at"`
}
// TableName 指定表名 // Logan.Li
func (Setting) TableName() string {
return "settings"
}
+27
View File
@@ -0,0 +1,27 @@
package model
import (
"time"
"gorm.io/gorm"
)
// User 管理员用户模型(单 Admin 表,无 RBAC 多角色) // Logan.Li
type User struct {
ID uint `json:"id" gorm:"primaryKey"`
Username string `json:"username" gorm:"size:100;uniqueIndex;not null"`
Password string `json:"-" gorm:"size:255;not null"` // bcrypt 哈希,JSON 响应时隐藏
Email string `json:"email" gorm:"size:100"`
Avatar string `json:"avatar" gorm:"size:500"`
Role string `json:"role" gorm:"size:20;default:'admin'"` // 固定 admin,预留扩展
IsActive bool `json:"is_active" gorm:"default:true"`
LastLogin *time.Time `json:"last_login,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at,omitempty" gorm:"index"`
}
// TableName 指定表名 // Logan.Li
func (User) TableName() string {
return "users"
}