forked from go-admin-team/go-admin-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
83 lines (75 loc) · 1.89 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package pkg
import (
"errors"
"log"
"runtime"
"strconv"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
const (
TrafficKey = "X-Request-Id"
LoggerKey = "_go-admin-logger-request"
)
func CompareHashAndPassword(e string, p string) (bool, error) {
err := bcrypt.CompareHashAndPassword([]byte(e), []byte(p))
if err != nil {
return false, err
}
return true, nil
}
// Assert 条件断言
// 当断言条件为 假 时触发 panic
// 对于当前请求不会再执行接下来的代码,并且返回指定格式的错误信息和错误码
func Assert(condition bool, msg string, code ...int) {
if !condition {
statusCode := 200
if len(code) > 0 {
statusCode = code[0]
}
panic("CustomError#" + strconv.Itoa(statusCode) + "#" + msg)
}
}
// HasError 错误断言
// 当 error 不为 nil 时触发 panic
// 对于当前请求不会再执行接下来的代码,并且返回指定格式的错误信息和错误码
// 若 msg 为空,则默认为 error 中的内容
func HasError(err error, msg string, code ...int) {
if err != nil {
statusCode := 200
if len(code) > 0 {
statusCode = code[0]
}
if msg == "" {
msg = err.Error()
}
_, file, line, _ := runtime.Caller(1)
log.Printf("%s:%v error: %#v", file, line, err)
panic("CustomError#" + strconv.Itoa(statusCode) + "#" + msg)
}
}
// GenerateMsgIDFromContext 生成msgID
func GenerateMsgIDFromContext(c *gin.Context) string {
requestId := c.GetHeader(TrafficKey)
if requestId == "" {
requestId = uuid.New().String()
c.Header(TrafficKey, requestId)
}
return requestId
}
// GetOrm 获取orm连接
func GetOrm(c *gin.Context) (*gorm.DB, error) {
idb, exist := c.Get("db")
if !exist {
return nil, errors.New("db connect not exist")
}
switch idb.(type) {
case *gorm.DB:
//新增操作
return idb.(*gorm.DB), nil
default:
return nil, errors.New("db connect not exist")
}
}