mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-21 10:13:01 +00:00
增加go-admin日志规范用法
This commit is contained in:
+19
-2
@@ -2,7 +2,10 @@ package apis
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/spf13/cast"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -11,15 +14,29 @@ type Api struct {
|
||||
|
||||
// GetOrm 获取orm连接
|
||||
func (e *Api) GetOrm(c *gin.Context) (*gorm.DB, error) {
|
||||
msgID := e.GenerateMsgIDFromContext(c)
|
||||
idb, exist := c.Get("db")
|
||||
if !exist {
|
||||
return nil, errors.New("db connect not exist")
|
||||
return nil, errors.New(fmt.Sprintf("msgID[%s], db connect not exist", msgID))
|
||||
}
|
||||
switch idb.(type) {
|
||||
case *gorm.DB:
|
||||
//新增操作
|
||||
return idb.(*gorm.DB), nil
|
||||
default:
|
||||
return nil, errors.New("db connect not exist")
|
||||
return nil, errors.New(fmt.Sprintf("msgID[%s], db connect not exist", msgID))
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateMsgIDFromContext 生成msgID
|
||||
func (e *Api) GenerateMsgIDFromContext(c *gin.Context) string {
|
||||
var msgID string
|
||||
data, ok := c.Get("msgID")
|
||||
if !ok {
|
||||
msgID = uuid.New().String()
|
||||
c.Set("msgID", msgID)
|
||||
return msgID
|
||||
}
|
||||
msgID = cast.ToString(data)
|
||||
return msgID
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user