refactor🎨 : 系统登录日志和操作日志代码优化
This commit is contained in:
1 parent
55112db749
commit
199fc180b7
23 files changed
+1081
-915
No files matched your search
@@ -1,6 +1,8 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"go-admin/app/admin/models/system"
|
||||
"go-admin/app/admin/service"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -98,7 +100,8 @@ func Authenticator(c *gin.Context) (interface{}, error) {
|
||||
// Write log to database
|
||||
func LoginLogToDB(c *gin.Context, status string, msg string, username string) {
|
||||
if config.LoggerConfig.EnabledDB {
|
||||
var loginlog models.LoginLog
|
||||
var loginlog system.SysLoginLog
|
||||
serviceLoginLog :=service.SysLoginlog{}
|
||||
ua := user_agent.New(c.Request.UserAgent())
|
||||
loginlog.Ipaddr = c.ClientIP()
|
||||
loginlog.Username = username
|
||||
@@ -112,7 +115,7 @@ func LoginLogToDB(c *gin.Context, status string, msg string, username string) {
|
||||
loginlog.Os = ua.OS()
|
||||
loginlog.Msg = msg
|
||||
loginlog.Platform = ua.Platform()
|
||||
_, _ = loginlog.Create()
|
||||
_ = serviceLoginLog.InsertSysLoginlog(loginlog.Generate())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,7 +129,7 @@ func LoginLogToDB(c *gin.Context, status string, msg string, username string) {
|
||||
// @Router /logout [post]
|
||||
// @Security Bearer
|
||||
func LogOut(c *gin.Context) {
|
||||
var loginlog models.LoginLog
|
||||
var loginlog system.SysLoginLog
|
||||
ua := user_agent.New(c.Request.UserAgent())
|
||||
loginlog.Ipaddr = c.ClientIP()
|
||||
location := tools.GetLocation(c.ClientIP())
|
||||
@@ -140,7 +143,8 @@ func LogOut(c *gin.Context) {
|
||||
loginlog.Platform = ua.Platform()
|
||||
loginlog.Username = tools.GetUserName(c)
|
||||
loginlog.Msg = "退出成功"
|
||||
loginlog.Create()
|
||||
serviceLoginLog:=service.SysLoginlog{}
|
||||
_ = serviceLoginLog.InsertSysLoginlog(loginlog.Generate())
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
|
||||
@@ -2,6 +2,8 @@ package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-admin/app/admin/models/system"
|
||||
"go-admin/app/admin/service"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -66,45 +68,46 @@ func SetDBOperLog(c *gin.Context, clientIP string, statusCode int, reqUri string
|
||||
menu.Path = reqUri
|
||||
menu.Action = reqMethod
|
||||
menuList, _ := menu.Get()
|
||||
sysOperLog := models.SysOperLog{}
|
||||
sysOperLog.OperIp = clientIP
|
||||
sysOperLog.OperLocation = tools.GetLocation(clientIP)
|
||||
sysOperLog.Status = tools.IntToString(statusCode)
|
||||
sysOperLog.OperName = tools.GetUserName(c)
|
||||
sysOperLog.RequestMethod = c.Request.Method
|
||||
sysOperLog.OperUrl = reqUri
|
||||
sysOperaLog := system.SysOperaLog{}
|
||||
sysOperaLog.OperIp = clientIP
|
||||
sysOperaLog.OperLocation = tools.GetLocation(clientIP)
|
||||
sysOperaLog.Status = tools.IntToString(statusCode)
|
||||
sysOperaLog.OperName = tools.GetUserName(c)
|
||||
sysOperaLog.RequestMethod = c.Request.Method
|
||||
sysOperaLog.OperUrl = reqUri
|
||||
if reqUri == "/login" {
|
||||
sysOperLog.BusinessType = "10"
|
||||
sysOperLog.Title = "用户登录"
|
||||
sysOperLog.OperName = "-"
|
||||
sysOperaLog.BusinessType = "10"
|
||||
sysOperaLog.Title = "用户登录"
|
||||
sysOperaLog.OperName = "-"
|
||||
} else if strings.Contains(reqUri, "/api/v1/logout") {
|
||||
sysOperLog.BusinessType = "11"
|
||||
sysOperaLog.BusinessType = "11"
|
||||
} else if strings.Contains(reqUri, "/api/v1/getCaptcha") {
|
||||
sysOperLog.BusinessType = "12"
|
||||
sysOperLog.Title = "验证码"
|
||||
sysOperaLog.BusinessType = "12"
|
||||
sysOperaLog.Title = "验证码"
|
||||
} else {
|
||||
if reqMethod == "POST" {
|
||||
sysOperLog.BusinessType = "1"
|
||||
sysOperaLog.BusinessType = "1"
|
||||
} else if reqMethod == "PUT" {
|
||||
sysOperLog.BusinessType = "2"
|
||||
sysOperaLog.BusinessType = "2"
|
||||
} else if reqMethod == "DELETE" {
|
||||
sysOperLog.BusinessType = "3"
|
||||
sysOperaLog.BusinessType = "3"
|
||||
}
|
||||
}
|
||||
sysOperLog.Method = reqMethod
|
||||
sysOperaLog.Method = reqMethod
|
||||
if len(menuList) > 0 {
|
||||
sysOperLog.Title = menuList[0].Title
|
||||
sysOperaLog.Title = menuList[0].Title
|
||||
}
|
||||
b, _ := c.Get("body")
|
||||
sysOperLog.OperParam, _ = tools.StructToJsonStr(b)
|
||||
sysOperLog.CreateBy = tools.GetUserName(c)
|
||||
sysOperLog.OperTime = tools.GetCurrentTime()
|
||||
sysOperLog.LatencyTime = (latencyTime).String()
|
||||
sysOperLog.UserAgent = c.Request.UserAgent()
|
||||
sysOperaLog.OperParam, _ = tools.StructToJsonStr(b)
|
||||
sysOperaLog.CreateBy = tools.GetUserIdUint(c)
|
||||
sysOperaLog.OperTime = tools.GetCurrentTime()
|
||||
sysOperaLog.LatencyTime = (latencyTime).String()
|
||||
sysOperaLog.UserAgent = c.Request.UserAgent()
|
||||
if c.Err() == nil {
|
||||
sysOperLog.Status = "0"
|
||||
sysOperaLog.Status = "0"
|
||||
} else {
|
||||
sysOperLog.Status = "1"
|
||||
sysOperaLog.Status = "1"
|
||||
}
|
||||
_, _ = sysOperLog.Create()
|
||||
serviceOperaLog:=service.SysOperaLog{}
|
||||
_ = serviceOperaLog.InsertSysOperaLog(sysOperaLog.Generate())
|
||||
}
|
||||
Reference in new issue
Block a user