Browse Source

修复config的错误,调整记录日志中间件的获取用户id逻辑,优先从jwt里取,新增状态码不为200时发送邮件到指定邮箱功能

SliverHorn 4 years ago
parent
commit
db12295094
4 changed files with 35 additions and 4 deletions
  1. 2 1
      server/config.yaml
  2. 1 0
      server/config/config.go
  3. 23 3
      server/middleware/operation.go
  4. 9 0
      server/utils/email.go

+ 2 - 1
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: '[email protected]'
   email-host: 'smtp.163.com'
   email-port: 465
-  email-isSSL: true
+  email-is-ssl: true

+ 1 - 0
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 {

+ 23 - 3
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))
 		}

+ 9 - 0
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)