diff --git a/api/v1/system/enter.go b/api/v1/system/enter.go index 0ad22fb..b63ef81 100644 --- a/api/v1/system/enter.go +++ b/api/v1/system/enter.go @@ -8,12 +8,14 @@ type ApiGroup struct { ClientApi RoleApi MenuApi + OperationRecordApi } var ( - jwtService = service.ServiceGroupApp.SystemServiceGroup.JwtService - userService = service.ServiceGroupApp.SystemServiceGroup.UserService - clientService = service.ServiceGroupApp.SystemServiceGroup.ClientService - roleService = service.ServiceGroupApp.SystemServiceGroup.RoleService - menuService = service.ServiceGroupApp.SystemServiceGroup.MenuService + jwtService = service.ServiceGroupApp.SystemServiceGroup.JwtService + userService = service.ServiceGroupApp.SystemServiceGroup.UserService + clientService = service.ServiceGroupApp.SystemServiceGroup.ClientService + roleService = service.ServiceGroupApp.SystemServiceGroup.RoleService + menuService = service.ServiceGroupApp.SystemServiceGroup.MenuService + operationRecordService = service.ServiceGroupApp.SystemServiceGroup.OperationRecordService ) diff --git a/api/v1/system/sys_operation_record.go b/api/v1/system/sys_operation_record.go new file mode 100644 index 0000000..6d06d92 --- /dev/null +++ b/api/v1/system/sys_operation_record.go @@ -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) +} diff --git a/model/system/request/sys_operation_record.go b/model/system/request/sys_operation_record.go new file mode 100644 index 0000000..a6359c4 --- /dev/null +++ b/model/system/request/sys_operation_record.go @@ -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"` +} diff --git a/model/system/sys_operation_record.go b/model/system/sys_operation_record.go index 240f255..3787f0d 100644 --- a/model/system/sys_operation_record.go +++ b/model/system/sys_operation_record.go @@ -13,9 +13,9 @@ type SysOperationRecord struct { Status int `json:"status" form:"status" gorm:"column:status;comment:请求状态"` // 请求状态 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:代理"` // 代理 - 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 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"` } diff --git a/router/system/enter.go b/router/system/enter.go index 2a4a608..1a133ed 100644 --- a/router/system/enter.go +++ b/router/system/enter.go @@ -8,13 +8,15 @@ type RouterGroup struct { ClientRouter RoleRouter MenuRouter + OperationRecordRouter } // 初始化路由 var ( - authApi = v1.ApiGroupApp.SystemApiGroup.AuthApi - userApi = v1.ApiGroupApp.SystemApiGroup.UserApi - clientApi = v1.ApiGroupApp.SystemApiGroup.ClientApi - roleApi = v1.ApiGroupApp.SystemApiGroup.RoleApi - menuApi = v1.ApiGroupApp.SystemApiGroup.MenuApi + authApi = v1.ApiGroupApp.SystemApiGroup.AuthApi + userApi = v1.ApiGroupApp.SystemApiGroup.UserApi + clientApi = v1.ApiGroupApp.SystemApiGroup.ClientApi + roleApi = v1.ApiGroupApp.SystemApiGroup.RoleApi + menuApi = v1.ApiGroupApp.SystemApiGroup.MenuApi + operationRecordApi = v1.ApiGroupApp.SystemApiGroup.OperationRecordApi ) diff --git a/router/system/menu_router.go b/router/system/menu_router.go index d3475dc..0bf678a 100644 --- a/router/system/menu_router.go +++ b/router/system/menu_router.go @@ -5,7 +5,7 @@ import "github.com/gin-gonic/gin" type MenuRouter struct { } -func (m MenuRouter) InitMenuRouter(Router *gin.RouterGroup) { +func (m *MenuRouter) InitMenuRouter(Router *gin.RouterGroup) { menuRouter := Router.Group("menu") { menuRouter.GET("route", menuApi.Route) diff --git a/router/system/operation_record.go b/router/system/operation_record.go new file mode 100644 index 0000000..d752b13 --- /dev/null +++ b/router/system/operation_record.go @@ -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) // 批量删除操作记录 + } +} diff --git a/service/system/enter.go b/service/system/enter.go index 5643153..aaa776c 100644 --- a/service/system/enter.go +++ b/service/system/enter.go @@ -6,4 +6,5 @@ type ServiceGroup struct { ClientService RoleService MenuService + OperationRecordService } diff --git a/service/system/sys_jwt.go b/service/system/sys_jwt.go index ceb7508..572340a 100644 --- a/service/system/sys_jwt.go +++ b/service/system/sys_jwt.go @@ -11,7 +11,7 @@ type JwtService struct{} var JwtServiceApp = new(JwtService) // 登出,禁用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) if err != nil { return err @@ -20,7 +20,7 @@ func (s JwtService) PutBlacklist(userId string, token string) (err error) { 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() return err == nil && val == token } diff --git a/service/system/sys_operation_record.go b/service/system/sys_operation_record.go new file mode 100644 index 0000000..5b6fd43 --- /dev/null +++ b/service/system/sys_operation_record.go @@ -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 +}