-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.go
113 lines (91 loc) · 2.11 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package utils
import (
"encoding/json"
"fmt"
"os"
"strings"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
func init() {
initLogger()
initConfig()
initDB()
initCron()
}
// createApp
func CreateApp() *gin.Engine {
// 设置mode
mode := GetConfig().Server.Mode
gin.SetMode(mode)
app := gin.New()
app.SetTrustedProxies([]string{"127.0.0.1"})
return app
}
// CreateSafeFilePath 创建安全的文件路径
func CreateSafeFilePath(dirNames []string, fileName string) string {
if len(dirNames) < 1 {
return ""
}
var basePath string
for index, dir := range dirNames {
if index == 0 {
basePath = dir
} else {
basePath = fmt.Sprintf("%s/%s", basePath, dir)
}
if _, err := os.Stat(basePath); os.IsNotExist(err) {
os.MkdirAll(basePath, os.ModePerm)
os.Chmod(basePath, os.ModePerm)
}
}
return strings.Join([]string{basePath, fileName}, "/")
}
// MergeJson 合并json
func MergeJson(args ...any) (map[string]any, error) {
var jsonMap map[string]any
for _, item := range args {
itemJson, err := json.Marshal(item)
if err != nil {
return nil, err
}
json.Unmarshal(itemJson, &jsonMap)
}
return jsonMap, nil
}
// UUID 生成唯一id
func UUID() string {
uuid := uuid.New().String()
return strings.ReplaceAll(uuid, "-", "")
}
// GetCurrentUserId 获取当前登陆的userId
func GetCurrentUserId(c *gin.Context) (uid string) {
if userInfo, has := c.Get("claims"); has {
if claims, ok := ToClaims(userInfo); ok {
return claims.UserId
} else {
return
}
} else {
return
}
}
// CheckAuthByUserId 通过userId判断当前有无权限
func CheckAuthByUserId(c *gin.Context, userId string) (isAuth bool) {
currentUid := GetCurrentUserId(c)
return currentUid == userId
}
// ToHashFileName 转唯一文件名
func ToHashFileName(fileName string) (hashFileName string) {
if filenames := strings.Split(fileName, "."); len(filenames) < 2 {
return fmt.Sprintf("%s__%s", fileName, UUID())
}
return strings.Join(strings.Split(fileName, "."), fmt.Sprintf("__%s.", UUID()))
}
// If 模拟三元运算符
func If[T any](is bool, v1 T, v2 T) T {
if is {
return v1
}
return v2
}