This commit is contained in:
toom1996
2026-08-31 00:44:23 +08:00
parent c97a001a76
commit a325e438be
6 changed files with 99 additions and 7 deletions

View File

@ -124,3 +124,29 @@ func Decode(s string) (uint32, error) {
}
return decodeStr(strings.TrimSpace(s))
}
// 类型前缀:让同一数字主键在不同实体下得到不同的编码串,前端 /item/[id] 可凭前缀区分实体类型,
// 从而把 runway / street snap 等详情统一收口到同一路由而无需担心 id 撞车。
const (
TypeRunway = 'r' // 走秀 / 文章 lookbook(brand_runway 表)
TypeSnap = 's' // 街拍(street_snap 表)
)
// EncodeTyped 在编码串前拼一个类型字符前缀(如 "r"+base62 串)。前缀字符取自 base62 字母表,
// 不参与 Feistel 还原、仅作类型标识。DecodeTyped 负责剥离前缀并还原数字主键。
func EncodeTyped(t byte, id uint32) string {
ensure()
return string(t) + encodeNum(id)
}
// DecodeTyped 还原带类型前缀的编码串,返回 (类型字符, 数字主键, error)。
// 调用方通常忽略类型(路由已确定实体),只取数字主键查库;旧的无前缀串仍用 Decode。
func DecodeTyped(s string) (byte, uint32, error) {
s = strings.TrimSpace(s)
if len(s) < 2 {
return 0, 0, errors.New("invalid typed hashid")
}
t := s[0]
n, err := decodeStr(s[1:])
return t, n, err
}