Files
mir-godot/service/framework/utils/hashids.go

47 lines
760 B
Go
Raw Normal View History

2024-03-06 10:47:22 +08:00
/**
#*****************************************************************************
2024-08-06 18:30:21 +08:00
# @author MakerYang
# @site mir2.makeryang.com
2024-03-06 10:47:22 +08:00
#*****************************************************************************
*/
2024-03-03 22:59:18 +08:00
package Utils
import (
2024-08-06 18:30:21 +08:00
"Service/framework/config"
2024-03-03 22:59:18 +08:00
"github.com/speps/go-hashids"
)
func EncodeId(len int, id ...int) string {
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
hd := hashids.NewData()
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
hd.Salt = Config.Get.Hash.Salt
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
hd.MinLength = len
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
h := hashids.NewWithData(hd)
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
e, _ := h.Encode(id)
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
return e
}
func DecodeId(len int, encodedId string) ([]int, error) {
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
hd := hashids.NewData()
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
hd.Salt = Config.Get.Hash.Salt
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
hd.MinLength = len
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
h := hashids.NewWithData(hd)
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
d, err := h.DecodeWithError(encodedId)
if err != nil {
return nil, err
}
2024-08-06 18:30:21 +08:00
2024-03-03 22:59:18 +08:00
return d, nil
}