feat: 操作日志

This commit is contained in:
Blizzard
2025-09-15 21:49:55 +08:00
parent 6beb915adf
commit 79c19bc47c
10 changed files with 186 additions and 15 deletions
+2
View File
@@ -8,6 +8,7 @@ type ApiGroup struct {
ClientApi ClientApi
RoleApi RoleApi
MenuApi MenuApi
OperationRecordApi
} }
var ( var (
@@ -16,4 +17,5 @@ var (
clientService = service.ServiceGroupApp.SystemServiceGroup.ClientService clientService = service.ServiceGroupApp.SystemServiceGroup.ClientService
roleService = service.ServiceGroupApp.SystemServiceGroup.RoleService roleService = service.ServiceGroupApp.SystemServiceGroup.RoleService
menuService = service.ServiceGroupApp.SystemServiceGroup.MenuService menuService = service.ServiceGroupApp.SystemServiceGroup.MenuService
operationRecordService = service.ServiceGroupApp.SystemServiceGroup.OperationRecordService
) )
+79
View File
@@ -0,0 +1,79 @@
package system
import (
"sundynix-go/global"
"sundynix-go/model/commom/request"
"sundynix-go/model/commom/response"
"sundynix-go/model/system"
systemReq "sundynix-go/model/system/request"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
type OperationRecordApi struct {
}
func (s *OperationRecordApi) CreateOperationRecord(c *gin.Context) {
var sysOperationRecord system.SysOperationRecord
err := c.ShouldBindJSON(&sysOperationRecord)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
err = operationRecordService.CreateOperationRecord(sysOperationRecord)
if err != nil {
global.Logger.Error("创建操作记录失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("创建操作记录成功!", c)
}
func (s *OperationRecordApi) GetRecordList(c *gin.Context) {
var pageInfo systemReq.GetOperationRecordList
err := c.ShouldBindJSON(&pageInfo)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
list, total, err := operationRecordService.GetRecordList(pageInfo)
if err != nil {
global.Logger.Error("获取操作记录列表失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(response.PageResult{
List: list,
Total: total,
Page: pageInfo.Current,
PageSize: pageInfo.PageSize,
}, c)
}
func (s *OperationRecordApi) GetRecordById(c *gin.Context) {
id := c.Query("id")
record, err := operationRecordService.GetRecordById(id)
if err != nil {
global.Logger.Error("获取操作记录详情失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithData(record, c)
}
func (s *OperationRecordApi) DeleteRecordsByIds(c *gin.Context) {
var ids request.IdsReq
err := c.ShouldBindJSON(&ids)
if err != nil {
response.FailWithMsg(err.Error(), c)
return
}
err = operationRecordService.DeleteRecordsByIds(ids)
if err != nil {
global.Logger.Error("删除操作记录失败!", zap.Error(err))
response.FailWithMsg(err.Error(), c)
return
}
response.OkWithMsg("删除操作记录成功!", c)
}
@@ -0,0 +1,14 @@
package request
import (
common "sundynix-go/model/commom/request"
)
type GetOperationRecordList struct {
common.PageInfo
Ip string `json:"ip" form:"ip"`
Method string `json:"method" form:"method"`
Path string `json:"path" form:"path"`
UserId string `json:"userId" form:"userId"`
Status int `json:"status" form:"status"`
}
+2 -2
View File
@@ -13,9 +13,9 @@ type SysOperationRecord struct {
Status int `json:"status" form:"status" gorm:"column:status;comment:请求状态"` // 请求状态 Status int `json:"status" form:"status" gorm:"column:status;comment:请求状态"` // 请求状态
Latency time.Duration `json:"latency" form:"latency" gorm:"column:latency;comment:延迟" swaggertype:"string"` // 延迟 Latency time.Duration `json:"latency" form:"latency" gorm:"column:latency;comment:延迟" swaggertype:"string"` // 延迟
Agent string `json:"agent" form:"agent" gorm:"type:text;column:agent;comment:代理"` // 代理 Agent string `json:"agent" form:"agent" gorm:"type:text;column:agent;comment:代理"` // 代理
ErrorMessage string `json:"error_message" form:"error_message" gorm:"column:error_message;comment:错误信息"` // 错误信息 ErrorMessage string `json:"erroMessage" form:"error_message" gorm:"column:error_message;comment:错误信息"` // 错误信息
Body string `json:"body" form:"body" gorm:"type:text;column:body;comment:请求Body"` // 请求Body Body string `json:"body" form:"body" gorm:"type:text;column:body;comment:请求Body"` // 请求Body
Resp string `json:"resp" form:"resp" gorm:"type:text;column:resp;comment:响应Body"` // 响应Body Resp string `json:"resp" form:"resp" gorm:"type:text;column:resp;comment:响应Body"` // 响应Body
UserID int `json:"user_id" form:"user_id" gorm:"column:user_id;comment:用户id"` // 用户id UserID int `json:"userId" form:"user_id" gorm:"column:user_id;comment:用户id"` // 用户id
User User `json:"user"` User User `json:"user"`
} }
+2
View File
@@ -8,6 +8,7 @@ type RouterGroup struct {
ClientRouter ClientRouter
RoleRouter RoleRouter
MenuRouter MenuRouter
OperationRecordRouter
} }
// 初始化路由 // 初始化路由
@@ -17,4 +18,5 @@ var (
clientApi = v1.ApiGroupApp.SystemApiGroup.ClientApi clientApi = v1.ApiGroupApp.SystemApiGroup.ClientApi
roleApi = v1.ApiGroupApp.SystemApiGroup.RoleApi roleApi = v1.ApiGroupApp.SystemApiGroup.RoleApi
menuApi = v1.ApiGroupApp.SystemApiGroup.MenuApi menuApi = v1.ApiGroupApp.SystemApiGroup.MenuApi
operationRecordApi = v1.ApiGroupApp.SystemApiGroup.OperationRecordApi
) )
+1 -1
View File
@@ -5,7 +5,7 @@ import "github.com/gin-gonic/gin"
type MenuRouter struct { type MenuRouter struct {
} }
func (m MenuRouter) InitMenuRouter(Router *gin.RouterGroup) { func (m *MenuRouter) InitMenuRouter(Router *gin.RouterGroup) {
menuRouter := Router.Group("menu") menuRouter := Router.Group("menu")
{ {
menuRouter.GET("route", menuApi.Route) menuRouter.GET("route", menuApi.Route)
+16
View File
@@ -0,0 +1,16 @@
package system
import "github.com/gin-gonic/gin"
type OperationRecordRouter struct {
}
func (o *OperationRecordRouter) InitOperationRecordRouter(Router *gin.RouterGroup) {
operationRecordRouter := Router.Group("operationRecord")
{
operationRecordRouter.POST("createOperationRecord", operationRecordApi.CreateOperationRecord) // 新增操作记录
operationRecordRouter.GET("getOperationRecordList", operationRecordApi.GetRecordList) // 获取操作记录列表
operationRecordRouter.GET("getOperationRecordById", operationRecordApi.GetRecordById) // 获取操作记录
operationRecordRouter.DELETE("delete", operationRecordApi.DeleteRecordsByIds) // 批量删除操作记录
}
}
+1
View File
@@ -6,4 +6,5 @@ type ServiceGroup struct {
ClientService ClientService
RoleService RoleService
MenuService MenuService
OperationRecordService
} }
+2 -2
View File
@@ -11,7 +11,7 @@ type JwtService struct{}
var JwtServiceApp = new(JwtService) var JwtServiceApp = new(JwtService)
// 登出,禁用jwt // 登出,禁用jwt
func (s JwtService) PutBlacklist(userId string, token string) (err error) { func (s *JwtService) PutBlacklist(userId string, token string) (err error) {
expire, err := utils.ParseDuration(global.Config.JWT.ExpiresTime) expire, err := utils.ParseDuration(global.Config.JWT.ExpiresTime)
if err != nil { if err != nil {
return err return err
@@ -20,7 +20,7 @@ func (s JwtService) PutBlacklist(userId string, token string) (err error) {
return err return err
} }
func (s JwtService) IsInBlacklist(userId string, token string) bool { func (s *JwtService) IsInBlacklist(userId string, token string) bool {
val, err := global.Redis.Get(context.Background(), userId).Result() val, err := global.Redis.Get(context.Background(), userId).Result()
return err == nil && val == token return err == nil && val == token
} }
+57
View File
@@ -0,0 +1,57 @@
package system
import (
"sundynix-go/global"
common "sundynix-go/model/commom/request"
"sundynix-go/model/system"
systemReq "sundynix-go/model/system/request"
)
type OperationRecordService struct{}
var OperationRecordServiceApp = new(OperationRecordService)
func (o *OperationRecordService) CreateOperationRecord(operationRecord system.SysOperationRecord) (err error) {
return global.DB.Create(&operationRecord).Error
}
func (o *OperationRecordService) GetRecordList(info systemReq.GetOperationRecordList) (list interface{}, total int64, err error) {
limit := info.PageSize
offset := info.PageSize * (info.Current - 1)
db := global.DB.Model(&system.SysOperationRecord{})
var operationRecordList []system.SysOperationRecord
if info.Ip != "" {
db = db.Where("ip = ?", info.Method)
}
if info.Method != "" {
db = db.Where("method = ?", info.Method)
}
if info.Path != "" {
db = db.Where("path = ?", info.Path)
}
if info.UserId != "" {
db = db.Where("status = ?", info.UserId)
}
if info.Status != 0 {
db = db.Where("status = ?", info.Status)
}
err = db.Count(&total).Error
if err != nil {
return
}
err = db.Limit(limit).Offset(offset).Find(&operationRecordList).Error
return operationRecordList, total, err
}
func (o *OperationRecordService) GetRecordById(id string) (record *system.SysOperationRecord, err error) {
var r system.SysOperationRecord
err = global.DB.Where("id = ?", id).First(&r).Error
return &r, err
}
func (o *OperationRecordService) DeleteRecordsByIds(ids common.IdsReq) (err error) {
// Unscoped()禁用软删除 --> 永久物理删除
err = global.DB.Where("id in ?", ids.Ids).Unscoped().Delete(&system.SysOperationRecord{}).Error
return err
}