Files
opsx-licence/backend/utils/audit.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

63 lines
1.5 KiB
Go

package utils
import (
"backend/internal/model"
"time"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// LogAction 记录操作日志(成功) // Logan.Li
func LogAction(db *gorm.DB, c *gin.Context, action, targetType string, targetID uint, targetName, oldData, newData, detail string) {
logEntry := model.AuditLog{
Action: action,
TargetType: targetType,
TargetID: targetID,
TargetName: targetName,
OldData: oldData,
NewData: newData,
Detail: detail,
Result: "success",
IP: c.ClientIP(),
CreatedAt: time.Now(),
}
// 从上下文中获取操作人信息(管理后台调用时存在) // Logan.Li
if c != nil {
if userID, exists := c.Get("user_id"); exists {
if id, ok := userID.(uint); ok {
logEntry.UserID = id
}
}
if username, exists := c.Get("username"); exists {
if name, ok := username.(string); ok {
logEntry.Username = name
}
}
}
db.Create(&logEntry)
}
// LogSystemAction 记录系统操作日志(无 HTTP 上下文,如支付回调) // Logan.Li
func LogSystemAction(db *gorm.DB, action, targetType string, targetID uint, targetName, detail, ip string) {
db.Create(&model.AuditLog{
UserID: 0,
Username: "system",
Action: action,
TargetType: targetType,
TargetID: targetID,
TargetName: targetName,
Detail: detail,
Result: "success",
IP: ip,
CreatedAt: time.Now(),
})
}
// GetClientIP 获取客户端真实 IP(兼容代理) // Logan.Li
func GetClientIP(c *gin.Context) string {
return c.ClientIP()
}