forked from LyricTian/gin-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
148 lines (123 loc) · 2.71 KB
/
util.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package util
import (
"bytes"
"crypto/rand"
"encoding/hex"
"fmt"
"reflect"
"strings"
"github.com/fatih/structs"
)
// MustUUID 创建一个UUID,如果有错误,则抛出panic
func MustUUID() string {
uuid, err := NewUUID()
if err != nil {
panic("创建UUID发生错误: " + err.Error())
}
return uuid
}
// NewUUID 创建UUID,参考:https://github.com/google/uuid
func NewUUID() (string, error) {
var buf [16]byte
_, err := rand.Read(buf[:])
if err != nil {
return "", err
}
buf[6] = (buf[6] & 0x0f) | 0x40
buf[8] = (buf[8] & 0x3f) | 0x80
dst := make([]byte, 36)
hex.Encode(dst, buf[:4])
dst[8] = '-'
hex.Encode(dst[9:13], buf[4:6])
dst[13] = '-'
hex.Encode(dst[14:18], buf[6:8])
dst[18] = '-'
hex.Encode(dst[19:23], buf[8:10])
dst[23] = '-'
hex.Encode(dst[24:], buf[10:])
return string(dst), nil
}
// StructToMap 将结构体转换为字典
func StructToMap(s interface{}) map[string]interface{} {
return structs.Map(s)
}
// StructsToMapSlice 将结构体切片转换为字典切片
func StructsToMapSlice(v interface{}) []map[string]interface{} {
iVal := reflect.Indirect(reflect.ValueOf(v))
if iVal.IsNil() || !iVal.IsValid() || iVal.Type().Kind() != reflect.Slice {
return make([]map[string]interface{}, 0)
}
l := iVal.Len()
result := make([]map[string]interface{}, l)
for i := 0; i < l; i++ {
result[i] = structs.Map(iVal.Index(i).Interface())
}
return result
}
// Trim 去除空格
func Trim(s string) string {
return strings.TrimSpace(s)
}
// GetLevelCode 获取分级码
func GetLevelCode(orderLevelCodes []string) string {
l := len(orderLevelCodes)
if l == 0 {
return "01"
} else if l == 1 {
return orderLevelCodes[0] + "01"
}
root := orderLevelCodes[0]
toValue := func(i int) string {
if i < 10 {
return fmt.Sprintf("%s0%d", root, i)
}
return fmt.Sprintf("%s%d", root, i)
}
for i := 1; i < 100; i++ {
code := toValue(i)
if i < l &&
orderLevelCodes[i] == code {
continue
}
return code
}
return ""
}
// ParseLevelCodes 解析分级码(去重)
func ParseLevelCodes(levelCodes ...string) []string {
var allCodes []string
for _, levelCode := range levelCodes {
codes := parseLevelCode(levelCode)
for _, code := range codes {
var exists bool
for _, c := range allCodes {
if code == c {
exists = true
break
}
}
if !exists {
allCodes = append(allCodes, code)
}
}
}
return allCodes
}
func parseLevelCode(levelCode string) []string {
if len(levelCode) < 2 {
return nil
}
var (
codes []string
root bytes.Buffer
)
for i := range levelCode {
idx := i + 1
if idx%2 == 0 {
root.WriteString(levelCode[idx-2 : idx])
codes = append(codes, root.String())
}
}
root.Reset()
return codes
}