mirror of
https://github.com/flipped-aurora/gin-vue-admin.git
synced 2026-09-21 12:32:25 +00:00
add operation primary realization
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gin-vue-admin/global/response"
|
||||
"gin-vue-admin/model"
|
||||
"gin-vue-admin/model/request"
|
||||
resp "gin-vue-admin/model/response"
|
||||
"gin-vue-admin/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// @Tags SysOperationRecord
|
||||
// @Summary 创建SysOperationRecord
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.SysOperationRecord true "创建SysOperationRecord"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
|
||||
// @Router /sysOperationRecord/createSysOperationRecord [post]
|
||||
func CreateSysOperationRecord(c *gin.Context) {
|
||||
var sysOperationRecord model.SysOperationRecord
|
||||
_ = c.ShouldBindJSON(&sysOperationRecord)
|
||||
err := service.CreateSysOperationRecord(sysOperationRecord)
|
||||
if err != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("创建失败,%v", err), c)
|
||||
} else {
|
||||
response.OkWithMessage("创建成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags SysOperationRecord
|
||||
// @Summary 删除SysOperationRecord
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.SysOperationRecord true "删除SysOperationRecord"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
|
||||
// @Router /sysOperationRecord/deleteSysOperationRecord [delete]
|
||||
func DeleteSysOperationRecord(c *gin.Context) {
|
||||
var sysOperationRecord model.SysOperationRecord
|
||||
_ = c.ShouldBindJSON(&sysOperationRecord)
|
||||
err := service.DeleteSysOperationRecord(sysOperationRecord)
|
||||
if err != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
|
||||
} else {
|
||||
response.OkWithMessage("删除成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags SysOperationRecord
|
||||
// @Summary 更新SysOperationRecord
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.SysOperationRecord true "更新SysOperationRecord"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
|
||||
// @Router /sysOperationRecord/updateSysOperationRecord [put]
|
||||
func UpdateSysOperationRecord(c *gin.Context) {
|
||||
var sysOperationRecord model.SysOperationRecord
|
||||
_ = c.ShouldBindJSON(&sysOperationRecord)
|
||||
err := service.UpdateSysOperationRecord(&sysOperationRecord)
|
||||
if err != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("更新失败,%v", err), c)
|
||||
} else {
|
||||
response.OkWithMessage("更新成功", c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags SysOperationRecord
|
||||
// @Summary 用id查询SysOperationRecord
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body model.SysOperationRecord true "用id查询SysOperationRecord"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
|
||||
// @Router /sysOperationRecord/findSysOperationRecord [get]
|
||||
func FindSysOperationRecord(c *gin.Context) {
|
||||
var sysOperationRecord model.SysOperationRecord
|
||||
_ = c.ShouldBindQuery(&sysOperationRecord)
|
||||
err, resysOperationRecord := service.GetSysOperationRecord(sysOperationRecord.ID)
|
||||
if err != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("查询失败,%v", err), c)
|
||||
} else {
|
||||
response.OkWithData(gin.H{"resysOperationRecord": resysOperationRecord}, c)
|
||||
}
|
||||
}
|
||||
|
||||
// @Tags SysOperationRecord
|
||||
// @Summary 分页获取SysOperationRecord列表
|
||||
// @Security ApiKeyAuth
|
||||
// @accept application/json
|
||||
// @Produce application/json
|
||||
// @Param data body request.SysOperationRecordSearch true "分页获取SysOperationRecord列表"
|
||||
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
|
||||
// @Router /sysOperationRecord/getSysOperationRecordList [get]
|
||||
func GetSysOperationRecordList(c *gin.Context) {
|
||||
var pageInfo request.SysOperationRecordSearch
|
||||
_ = c.ShouldBindQuery(&pageInfo)
|
||||
err, list, total := service.GetSysOperationRecordInfoList(pageInfo)
|
||||
if err != nil {
|
||||
response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
|
||||
} else {
|
||||
response.OkWithData(resp.PageResult{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: pageInfo.Page,
|
||||
PageSize: pageInfo.PageSize,
|
||||
}, c)
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -62,4 +62,5 @@ log:
|
||||
operation:
|
||||
skip_paths:
|
||||
- '/base/login'
|
||||
- '/base/register'
|
||||
- '/base/register'
|
||||
- '/sysOperationRecord/getSysOperationRecordList'
|
||||
@@ -6,7 +6,6 @@ require (
|
||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
|
||||
github.com/casbin/casbin v1.9.1
|
||||
github.com/casbin/gorm-adapter v1.0.0
|
||||
github.com/dchest/captcha v0.0.0-20170622155422-6a29415a8364
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||
github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect
|
||||
github.com/fsnotify/fsnotify v1.4.9
|
||||
|
||||
@@ -21,6 +21,7 @@ func DBTables() {
|
||||
model.ExaFile{},
|
||||
model.ExaFileChunk{},
|
||||
model.ExaCustomer{},
|
||||
model.SysOperationRecord{},
|
||||
)
|
||||
global.GVA_LOG.Debug("register table success")
|
||||
}
|
||||
|
||||
@@ -14,10 +14,9 @@ import (
|
||||
|
||||
func Routers() *gin.Engine {
|
||||
var Router = gin.Default()
|
||||
// 操作记录
|
||||
Router.Use(middleware.RecordRequestBody(), middleware.OperationRecord())
|
||||
// Router.Use(middleware.LoadTls()) // 打开就能玩https了
|
||||
global.GVA_LOG.Debug("use middleware logger")
|
||||
Router.Use(middleware.RecordRequestBody(), middleware.OperationRecord())
|
||||
// 跨域
|
||||
Router.Use(middleware.Cors())
|
||||
global.GVA_LOG.Debug("use middleware cors")
|
||||
@@ -39,6 +38,7 @@ func Routers() *gin.Engine {
|
||||
router.InitAutoCodeRouter(ApiGroup) // 创建自动化代码
|
||||
router.InitSysDictionaryDetailRouter(ApiGroup) // 字典详情管理
|
||||
router.InitSysDictionaryRouter(ApiGroup) // 字典管理
|
||||
router.InitSysOperationRecordRouter(ApiGroup)
|
||||
global.GVA_LOG.Info("router register success")
|
||||
return Router
|
||||
}
|
||||
|
||||
@@ -4,13 +4,16 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"gin-vue-admin/global"
|
||||
"gin-vue-admin/model"
|
||||
"gin-vue-admin/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var body []byte
|
||||
var userId uint
|
||||
|
||||
func RecordRequestBody() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
@@ -20,30 +23,39 @@ func RecordRequestBody() gin.HandlerFunc {
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error(err)
|
||||
}
|
||||
|
||||
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
|
||||
} else {
|
||||
body = nil
|
||||
}
|
||||
|
||||
//TODO parse token , userId <-
|
||||
}
|
||||
}
|
||||
|
||||
func OperationRecord() gin.HandlerFunc {
|
||||
return gin.LoggerWithConfig(gin.LoggerConfig{
|
||||
Formatter: func(param gin.LogFormatterParams) string {
|
||||
|
||||
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" \"%s\" %s\"\n",
|
||||
param.ClientIP,
|
||||
param.TimeStamp.Format(time.RFC1123),
|
||||
param.Method,
|
||||
param.Path,
|
||||
param.Request.Proto,
|
||||
param.StatusCode,
|
||||
param.Latency,
|
||||
param.Request.UserAgent(),
|
||||
string(body),
|
||||
param.ErrorMessage,
|
||||
)
|
||||
fmt.Println(global.GVA_CONFIG.Operation.SkipPaths)
|
||||
for _, v := range global.GVA_CONFIG.Operation.SkipPaths {
|
||||
if strings.Contains(param.Path, v) {
|
||||
fmt.Println(param.Path)
|
||||
return ""
|
||||
}
|
||||
}
|
||||
err := service.CreateSysOperationRecord(model.SysOperationRecord{
|
||||
Ip: param.ClientIP,
|
||||
Method: param.Method,
|
||||
Path: param.Path,
|
||||
Status: param.StatusCode,
|
||||
Latency: param.Latency,
|
||||
Agent: param.Request.UserAgent(),
|
||||
ErrorMessage: string(body),
|
||||
UserId: int(userId),
|
||||
})
|
||||
if err != nil {
|
||||
global.GVA_LOG.Error(err)
|
||||
}
|
||||
return ""
|
||||
},
|
||||
// 暂时没考虑好
|
||||
Output: nil,
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package request
|
||||
|
||||
import "gin-vue-admin/model"
|
||||
|
||||
type SysOperationRecordSearch struct {
|
||||
model.SysOperationRecord
|
||||
PageInfo
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// 自动生成模板SysOperationRecord
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/jinzhu/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 如果含有time.Time 请自行import time包
|
||||
type SysOperationRecord struct {
|
||||
gorm.Model
|
||||
Ip string `json:"ip" form:"ip" gorm:"column:ip;comment:'请求ip'"`
|
||||
Method string `json:"method" form:"method" gorm:"column:method;comment:''"`
|
||||
Path string `json:"path" form:"path" gorm:"column:path;comment:''"`
|
||||
Status int `json:"status" form:"status" gorm:"column:status;comment:''"`
|
||||
Latency time.Duration `json:"latency" form:"latency" gorm:"column:latency;comment:''"`
|
||||
Agent string `json:"agent" form:"agent" gorm:"column:agent;comment:''"`
|
||||
ErrorMessage string `json:"error_message" form:"error_message" gorm:"column:error_message;comment:''"`
|
||||
UserId int `json:"user_id" form:"user_id" gorm:"column:user_id;comment:''"`
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"gin-vue-admin/api/v1"
|
||||
"gin-vue-admin/middleware"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func InitSysOperationRecordRouter(Router *gin.RouterGroup) {
|
||||
SysOperationRecordRouter := Router.Group("sysOperationRecord").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
|
||||
{
|
||||
SysOperationRecordRouter.POST("createSysOperationRecord", v1.CreateSysOperationRecord) // 新建SysOperationRecord
|
||||
SysOperationRecordRouter.DELETE("deleteSysOperationRecord", v1.DeleteSysOperationRecord) // 删除SysOperationRecord
|
||||
SysOperationRecordRouter.PUT("updateSysOperationRecord", v1.UpdateSysOperationRecord) // 更新SysOperationRecord
|
||||
SysOperationRecordRouter.GET("findSysOperationRecord", v1.FindSysOperationRecord) // 根据ID获取SysOperationRecord
|
||||
SysOperationRecordRouter.GET("getSysOperationRecordList", v1.GetSysOperationRecordList) // 获取SysOperationRecord列表
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"gin-vue-admin/global"
|
||||
"gin-vue-admin/model"
|
||||
"gin-vue-admin/model/request"
|
||||
)
|
||||
|
||||
// @title CreateSysOperationRecord
|
||||
// @description create a SysOperationRecord
|
||||
// @param sysOperationRecord model.SysOperationRecord
|
||||
// @auth (2020/04/05 20:22)
|
||||
// @return err error
|
||||
|
||||
func CreateSysOperationRecord(sysOperationRecord model.SysOperationRecord) (err error) {
|
||||
err = global.GVA_DB.Create(&sysOperationRecord).Error
|
||||
return err
|
||||
}
|
||||
|
||||
// @title DeleteSysOperationRecord
|
||||
// @description delete a SysOperationRecord
|
||||
// @auth (2020/04/05 20:22)
|
||||
// @param sysOperationRecord model.SysOperationRecord
|
||||
// @return error
|
||||
|
||||
func DeleteSysOperationRecord(sysOperationRecord model.SysOperationRecord) (err error) {
|
||||
err = global.GVA_DB.Delete(sysOperationRecord).Error
|
||||
return err
|
||||
}
|
||||
|
||||
// @title UpdateSysOperationRecord
|
||||
// @description update a SysOperationRecord
|
||||
// @param sysOperationRecord *model.SysOperationRecord
|
||||
// @auth (2020/04/05 20:22)
|
||||
// @return error
|
||||
|
||||
func UpdateSysOperationRecord(sysOperationRecord *model.SysOperationRecord) (err error) {
|
||||
err = global.GVA_DB.Save(sysOperationRecord).Error
|
||||
return err
|
||||
}
|
||||
|
||||
// @title GetSysOperationRecord
|
||||
// @description get the info of a SysOperationRecord
|
||||
// @auth (2020/04/05 20:22)
|
||||
// @param id uint
|
||||
// @return error
|
||||
// @return SysOperationRecord SysOperationRecord
|
||||
|
||||
func GetSysOperationRecord(id uint) (err error, sysOperationRecord model.SysOperationRecord) {
|
||||
err = global.GVA_DB.Where("id = ?", id).First(&sysOperationRecord).Error
|
||||
return
|
||||
}
|
||||
|
||||
// @title GetSysOperationRecordInfoList
|
||||
// @description get SysOperationRecord list by pagination, 分页获取用户列表
|
||||
// @auth (2020/04/05 20:22)
|
||||
// @param info PageInfo
|
||||
// @return error
|
||||
|
||||
func GetSysOperationRecordInfoList(info request.SysOperationRecordSearch) (err error, list interface{}, total int) {
|
||||
limit := info.PageSize
|
||||
offset := info.PageSize * (info.Page - 1)
|
||||
// 创建db
|
||||
db := global.GVA_DB.Model(&model.SysOperationRecord{})
|
||||
var sysOperationRecords []model.SysOperationRecord
|
||||
// 如果有条件搜索 下方会自动创建搜索语句
|
||||
if info.Method != "" {
|
||||
db = db.Where("method = ?", info.Method)
|
||||
}
|
||||
if info.Path != "" {
|
||||
db = db.Where("path LIKE ?", "%"+info.Path+"%")
|
||||
}
|
||||
if info.Status != 0 {
|
||||
db = db.Where("status = ?", info.Status)
|
||||
}
|
||||
err = db.Count(&total).Error
|
||||
err = db.Limit(limit).Offset(offset).Find(&sysOperationRecords).Error
|
||||
return err, sysOperationRecords, total
|
||||
}
|
||||
Reference in New Issue
Block a user