24 lines
485 B
Go
24 lines
485 B
Go
package commonservice
|
|
|
|
import "github.com/gin-gonic/gin"
|
|
|
|
// Gin context 中由 RequireJWT 写入的键名。
|
|
const (
|
|
CtxUserID = "user_id"
|
|
CtxUsername = "username"
|
|
)
|
|
|
|
// UserID 从 gin 上下文读取当前登录用户 ID。
|
|
func UserID(c *gin.Context) int64 {
|
|
v, _ := c.Get(CtxUserID)
|
|
id, _ := v.(int64)
|
|
return id
|
|
}
|
|
|
|
// Username 从 gin 上下文读取当前登录用户名。
|
|
func Username(c *gin.Context) string {
|
|
v, _ := c.Get(CtxUsername)
|
|
s, _ := v.(string)
|
|
return s
|
|
}
|