diff --git a/server/config.yaml b/server/config.yaml index 65673dd7..4f01e1a1 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -77,7 +77,7 @@ system: addr: 8888 db-type: "mysql" # support mysql/postgresql/sqlite/sqlserver need-init-data: false - error-to-email: true + error-to-email: false # captcha configuration captcha: diff --git a/server/middleware/email.go b/server/middleware/email.go new file mode 100644 index 00000000..bbed77fa --- /dev/null +++ b/server/middleware/email.go @@ -0,0 +1,55 @@ +package middleware + +import ( + "gin-vue-admin/global" + "gin-vue-admin/model" + "gin-vue-admin/model/request" + "gin-vue-admin/service" + "gin-vue-admin/utils" + "github.com/gin-gonic/gin" + "go.uber.org/zap" + "io/ioutil" + "strconv" + "time" +) + +func ErrorToEmail() gin.HandlerFunc { + return func(c *gin.Context) { + var username string + if claims, ok := c.Get("claims"); ok { + waitUse := claims.(*request.CustomClaims) + username = waitUse.Username + }else { + id, _ := strconv.Atoi(c.Request.Header.Get("x-user-id")) + err, user := service.FindUserById(id) + if err != nil{ + username = "Unknown" + } + username = user.Username + } + body, _ := ioutil.ReadAll(c.Request.Body) + record := model.SysOperationRecord{ + Ip: c.ClientIP(), + Method: c.Request.Method, + Path: c.Request.URL.Path, + Agent: c.Request.UserAgent(), + Body: string(body), + } + now := time.Now() + + c.Next() + + latency := time.Now().Sub(now) + status := c.Writer.Status() + record.ErrorMessage = c.Errors.ByType(gin.ErrorTypePrivate).String() + str := "接收到的请求为" + record.Body + "\n" + "请求方式为" + record.Path + "\n" + "报错信息如下" + record.ErrorMessage + "\n" + "耗时" + latency.String() + "\n" + if global.GVA_CONFIG.System.ErrorToEmail { + if status != 200 { + subject := username + "" +record.Ip + "调用了" + record.Path + "报错了" + if err := utils.ErrorToEmail(subject, str); err != nil { + global.GVA_LOG.Error("ErrorToEmail Failed, err:", zap.Any("err", err)) + } + } + } + } +} diff --git a/server/middleware/operation.go b/server/middleware/operation.go index a8c61654..72a7fb79 100644 --- a/server/middleware/operation.go +++ b/server/middleware/operation.go @@ -2,12 +2,10 @@ package middleware import ( "bytes" - "encoding/json" "gin-vue-admin/global" "gin-vue-admin/model" "gin-vue-admin/model/request" "gin-vue-admin/service" - "gin-vue-admin/utils" "github.com/gin-gonic/gin" "go.uber.org/zap" "io/ioutil" @@ -62,16 +60,6 @@ func OperationRecord() gin.HandlerFunc { record.Latency = latency 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 { global.GVA_LOG.Error("create operation record error:", zap.Any("err", err)) } diff --git a/server/service/sys_user.go b/server/service/sys_user.go index 1c21e811..a657cce7 100644 --- a/server/service/sys_user.go +++ b/server/service/sys_user.go @@ -112,3 +112,16 @@ func SetUserInfo(reqUser model.SysUser) (err error, user model.SysUser) { err = global.GVA_DB.Updates(&reqUser).Error return err, reqUser } + +// @title FindUserById +// @description Get user information by id, 通过id获取用户信息 +// @auth (2020/04/05 20:22) +// @param id int +// @return err error +// @return user *model.SysUser + +func FindUserById(id int) (err error, user *model.SysUser) { + var u model.SysUser + err = global.GVA_DB.Where("`id` = ?", id).First(&u).Error + return err, &u +}