提交 0de3d606 authored 作者: mooncake9527's avatar mooncake9527

1. uid

上级 f8b1262b
......@@ -82,19 +82,19 @@ func CtxTraceIDField(c context.Context) zap.Field {
}
func GetGinUserID(c *gin.Context) xsf.ID {
return getGinSfID(c, KeyUID)
return getGinVal[xsf.ID](c, KeyUID)
}
func GetGinShopID(c *gin.Context) xsf.ID {
return getGinSfID(c, KeyShopID)
return getGinVal[xsf.ID](c, KeyShopID)
}
func GetCtxUserID(c context.Context) xsf.ID {
return getCtxSfID(c, KeyUID)
return getCtxVal[xsf.ID](c, KeyUID)
}
func GetCtxShopID(c context.Context) xsf.ID {
return getCtxSfID(c, KeyShopID)
return getCtxVal[xsf.ID](c, KeyShopID)
}
func GetCtxString(c context.Context, key string) string {
val := c.Value(key)
......@@ -168,52 +168,35 @@ func ToGinCtx(ctx context.Context) (*gin.Context, error) {
}
func GetCtxUID(c context.Context) uint {
uid := c.Value(KeyUID)
if uid != nil {
if v, ok := uid.(string); ok {
return cast.ToUint(v)
}
}
return 0
return getCtxVal[uint](c, KeyUID)
}
func GetCtxUType(c context.Context) int {
val := c.Value(KeyUType)
if val != nil {
if v, ok := val.(string); ok {
return cast.ToInt(v)
}
}
return 0
func GetCtxUType(c context.Context) uint {
return getCtxVal[uint](c, KeyUType)
}
func GetCtxCompanyID(c context.Context) uint {
val := c.Value(KeyCompanyID)
if val != nil {
v, ok := val.(string)
if ok {
return cast.ToUint(v)
}
}
return 0
return getCtxVal[uint](c, KeyCompanyID)
}
func getGinSfID(c *gin.Context, key string) xsf.ID {
func getGinVal[T any](c *gin.Context, key string) T {
val, exists := c.Get(key)
if exists {
if ret, ok := val.(xsf.ID); ok {
if ret, ok := val.(T); ok {
return ret
}
}
return xsf.ID(0)
var t T
return t
}
func getCtxSfID(c context.Context, key string) xsf.ID {
func getCtxVal[T any](c context.Context, key string) T {
val := c.Value(key)
if val != nil {
if ret, ok := val.(xsf.ID); ok {
if ret, ok := val.(T); ok {
return ret
}
}
return xsf.ID(0)
var t T
return t
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论