|
- package utils
-
- import (
- "github.com/sony/sonyflake"
-
- "applet/app/utils/logx"
- "fmt"
- "math/rand"
- "time"
- )
-
- const (
- KC_RAND_KIND_NUM = 0
- KC_RAND_KIND_LOWER = 1
- KC_RAND_KIND_UPPER = 2
- KC_RAND_KIND_ALL = 3
- )
-
- func newUUID() *[16]byte {
- u := &[16]byte{}
- rand.Read(u[:16])
- u[8] = (u[8] | 0x80) & 0xBf
- u[6] = (u[6] | 0x40) & 0x4f
- return u
- }
-
- func UUIDString() string {
- u := newUUID()
- return fmt.Sprintf("%x-%x-%x-%x-%x", u[:4], u[4:6], u[6:8], u[8:10], u[10:])
- }
-
- func UUIDHexString() string {
- u := newUUID()
- return fmt.Sprintf("%x%x%x%x%x", u[:4], u[4:6], u[6:8], u[8:10], u[10:])
- }
- func UUIDBinString() string {
- u := newUUID()
- return fmt.Sprintf("%s", [16]byte(*u))
- }
-
- func Krand(size int, kind int) []byte {
- ikind, kinds, result := kind, [][]int{[]int{10, 48}, []int{26, 97}, []int{26, 65}}, make([]byte, size)
- isAll := kind > 2 || kind < 0
- rand.Seed(time.Now().UnixNano())
- for i := 0; i < size; i++ {
- if isAll {
- ikind = rand.Intn(3)
- }
- scope, base := kinds[ikind][0], kinds[ikind][1]
- result[i] = uint8(base + rand.Intn(scope))
- }
- return result
- }
-
-
- func OrderUUID(uid int) string {
- ustr := IntToStr(uid)
- tstr := Int64ToStr(time.Now().Unix())
- ulen := len(ustr)
- tlen := len(tstr)
- rlen := 18 - ulen - tlen
- krb := Krand(rlen, KC_RAND_KIND_NUM)
- return ustr + tstr + string(krb)
- }
-
- var flake *sonyflake.Sonyflake
-
- func GenId() int64 {
-
- id, err := flake.NextID()
- if err != nil {
- _ = logx.Errorf("flake.NextID() failed with %s\n", err)
- panic(err)
- }
- return int64(id)
- }
|