|
@ -2,9 +2,12 @@ package middleware |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"bytes" |
|
|
"bytes" |
|
|
|
|
|
"encoding/json" |
|
|
"gin-vue-admin/global" |
|
|
"gin-vue-admin/global" |
|
|
"gin-vue-admin/model" |
|
|
"gin-vue-admin/model" |
|
|
|
|
|
"gin-vue-admin/model/request" |
|
|
"gin-vue-admin/service" |
|
|
"gin-vue-admin/service" |
|
|
|
|
|
"gin-vue-admin/utils" |
|
|
"github.com/gin-gonic/gin" |
|
|
"github.com/gin-gonic/gin" |
|
|
"go.uber.org/zap" |
|
|
"go.uber.org/zap" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
@ -16,6 +19,7 @@ import ( |
|
|
func OperationRecord() gin.HandlerFunc { |
|
|
func OperationRecord() gin.HandlerFunc { |
|
|
return func(c *gin.Context) { |
|
|
return func(c *gin.Context) { |
|
|
var body []byte |
|
|
var body []byte |
|
|
|
|
|
var userId int |
|
|
if c.Request.Method != http.MethodGet { |
|
|
if c.Request.Method != http.MethodGet { |
|
|
var err error |
|
|
var err error |
|
|
body, err = ioutil.ReadAll(c.Request.Body) |
|
|
body, err = ioutil.ReadAll(c.Request.Body) |
|
@ -25,10 +29,16 @@ func OperationRecord() gin.HandlerFunc { |
|
|
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) |
|
|
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
userId, err := strconv.Atoi(c.Request.Header.Get("x-user-id")) |
|
|
|
|
|
|
|
|
if claims, ok := c.Get("claims"); ok { |
|
|
|
|
|
waitUse := claims.(*request.CustomClaims) |
|
|
|
|
|
userId = int(waitUse.ID) |
|
|
|
|
|
}else { |
|
|
|
|
|
id, err := strconv.Atoi(c.Request.Header.Get("x-user-id")) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
userId = 0 |
|
|
userId = 0 |
|
|
} |
|
|
} |
|
|
|
|
|
userId = id |
|
|
|
|
|
} |
|
|
record := model.SysOperationRecord{ |
|
|
record := model.SysOperationRecord{ |
|
|
Ip: c.ClientIP(), |
|
|
Ip: c.ClientIP(), |
|
|
Method: c.Request.Method, |
|
|
Method: c.Request.Method, |
|
@ -52,6 +62,16 @@ func OperationRecord() gin.HandlerFunc { |
|
|
record.Latency = latency |
|
|
record.Latency = latency |
|
|
record.Resp = writer.body.String() |
|
|
record.Resp = writer.body.String() |
|
|
|
|
|
|
|
|
|
|
|
if global.GVA_CONFIG.System.ErrorToEmail { |
|
|
|
|
|
if record.Status != 200 { |
|
|
|
|
|
subject := record.Ip+"调用了"+record.Path+"报错了" |
|
|
|
|
|
body, _ := json.Marshal(record) |
|
|
|
|
|
if err := utils.ErrorToEmail(subject, string(body)); err != nil{ |
|
|
|
|
|
global.GVA_LOG.Error("ErrorToEmail Failed, err:", zap.Any("err", err)) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if err := service.CreateSysOperationRecord(record); err != nil { |
|
|
if err := service.CreateSysOperationRecord(record); err != nil { |
|
|
global.GVA_LOG.Error("create operation record error:", zap.Any("err", err)) |
|
|
global.GVA_LOG.Error("create operation record error:", zap.Any("err", err)) |
|
|
} |
|
|
} |
|
|