代码拉取完成,页面将自动刷新
package hashid
import (
	"errors"
	"gitee.com/wuzheng0709/backend-gopkg/infrastructure/pkg/conf"
	"github.com/speps/go-hashids"
)
// ID类型
const (
	ShareID  = iota // 分享
	UserID          // 用户
	FileID          // 文件ID
	FolderID        // 目录ID
	TagID           // 标签ID
	PolicyID        // 存储策略ID
)
var (
	// ErrTypeNotMatch ID类型不匹配
	ErrTypeNotMatch = errors.New("ID类型不匹配")
)
// HashEncode 对给定数据计算HashID
func HashEncode(v []int) (string, error) {
	hd := hashids.NewData()
	hd.Salt = conf.SystemConfig.HashIDSalt
	h, err := hashids.NewWithData(hd)
	if err != nil {
		return "", err
	}
	id, err := h.Encode(v)
	if err != nil {
		return "", err
	}
	return id, nil
}
// HashDecode 对给定数据计算原始数据
func HashDecode(raw string) ([]int, error) {
	hd := hashids.NewData()
	hd.Salt = conf.SystemConfig.HashIDSalt
	h, err := hashids.NewWithData(hd)
	if err != nil {
		return []int{}, err
	}
	return h.DecodeWithError(raw)
}
// HashID 计算数据库内主键对应的HashID
func HashID(id uint, t int) string {
	v, _ := HashEncode([]int{int(id), t})
	return v
}
// DecodeHashID 计算HashID对应的数据库ID
func DecodeHashID(id string, t int) (uint, error) {
	v, _ := HashDecode(id)
	if len(v) != 2 || v[1] != t {
		return 0, ErrTypeNotMatch
	}
	return uint(v[0]), nil
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
 马建仓 AI 助手
马建仓 AI 助手