From db122950940faba23c7ffb45b8209ae8350f9298 Mon Sep 17 00:00:00 2001 From: SliverHorn <503551462@qq.com> Date: Tue, 22 Sep 2020 18:45:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dconfig=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF,=E8=B0=83=E6=95=B4=E8=AE=B0=E5=BD=95=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E4=B8=AD=E9=97=B4=E4=BB=B6=E7=9A=84=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=94=A8=E6=88=B7id=E9=80=BB=E8=BE=91,=E4=BC=98=E5=85=88?= =?UTF-8?q?=E4=BB=8Ejwt=E9=87=8C=E5=8F=96,=E6=96=B0=E5=A2=9E=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E7=A0=81=E4=B8=8D=E4=B8=BA200=E6=97=B6=E5=8F=91?= =?UTF-8?q?=E9=80=81=E9=82=AE=E4=BB=B6=E5=88=B0=E6=8C=87=E5=AE=9A=E9=82=AE?= =?UTF-8?q?=E7=AE=B1=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config.yaml | 3 ++- server/config/config.go | 1 + server/middleware/operation.go | 26 +++++++++++++++++++++++--- server/utils/email.go | 9 +++++++++ 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/server/config.yaml b/server/config.yaml index df88a549..65673dd7 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -77,6 +77,7 @@ system: addr: 8888 db-type: "mysql" # support mysql/postgresql/sqlite/sqlserver need-init-data: false + error-to-email: true # captcha configuration captcha: @@ -106,4 +107,4 @@ email: email-to: 'xxx@qq.com' email-host: 'smtp.163.com' email-port: 465 - email-isSSL: true \ No newline at end of file + email-is-ssl: true \ No newline at end of file diff --git a/server/config/config.go b/server/config/config.go index 4154cbcd..6d75ca78 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -22,6 +22,7 @@ type System struct { Addr int `mapstructure:"addr" json:"addr" yaml:"addr"` DbType string `mapstructure:"db-type" json:"dbType" yaml:"db-type"` NeedInitData bool `mapstructure:"need-init-data" json:"needInitData" yaml:"need-init-data"` + ErrorToEmail bool `mapstructure:"error-to-email" json:"errorToEmail" yaml:"error-to-email"` } type JWT struct { diff --git a/server/middleware/operation.go b/server/middleware/operation.go index f123466e..a8c61654 100644 --- a/server/middleware/operation.go +++ b/server/middleware/operation.go @@ -2,9 +2,12 @@ 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" @@ -16,6 +19,7 @@ import ( func OperationRecord() gin.HandlerFunc { return func(c *gin.Context) { var body []byte + var userId int if c.Request.Method != http.MethodGet { var err error body, err = ioutil.ReadAll(c.Request.Body) @@ -25,9 +29,15 @@ func OperationRecord() gin.HandlerFunc { c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(body)) } } - userId, err := strconv.Atoi(c.Request.Header.Get("x-user-id")) - if err != nil { - userId = 0 + 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 { + userId = 0 + } + userId = id } record := model.SysOperationRecord{ Ip: c.ClientIP(), @@ -52,6 +62,16 @@ 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/utils/email.go b/server/utils/email.go index f74e35dd..cccc8968 100644 --- a/server/utils/email.go +++ b/server/utils/email.go @@ -16,6 +16,15 @@ func Email(subject string, body string) error { return send(to, subject, body) } +// ErrorToEmail Error 发送邮件 +func ErrorToEmail(subject string, body string) error { + to := strings.Split(global.GVA_CONFIG.Email.EmailTo, ",") + if to[len(to)-1] == "" { // 判断切片的最后一个元素是否为空,为空则移除 + to = to[:len(to)-1] + } + return send(to, subject, body) +} + func EmailTest(subject string, body string) error { to := []string{global.GVA_CONFIG.Email.EmailFrom} return send(to, subject, body)