-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.go
46 lines (40 loc) · 970 Bytes
/
box.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
package tools
import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/satori/go.uuid"
"hash/crc32"
"io"
"strings"
"time"
)
func GenUUID() string {
return strings.ToUpper(strings.Replace(uuid.Must(uuid.NewV4(), nil).String(), "-", "", -1))
}
func MD4(data string) string {
m := md5.New()
_, _ = io.WriteString(m, data)
x := hex.EncodeToString(m.Sum(nil))
return strings.ToUpper(x)
}
func String(v interface{}) string {
x, _ := json.Marshal(v)
return string(x)
}
func GetNow() string {
return time.Now().Format("2006-01-02 15:04:05")
}
func GenNo(prefix, suffix string) string {
//这里根据自身业务生成单号
orderNo := prefix + time.Now().Format("20060102150405")
// 3.生成uuid的hashCode值
hashCodeV := int(crc32.ChecksumIEEE([]byte(uuid.Must(uuid.NewV4(), nil).String())))
// 4.可能为负数
if hashCodeV < 0 {
hashCodeV = -hashCodeV
}
orderNo += fmt.Sprintf("%s%010d", suffix, hashCodeV)
return orderNo
}