perf(auth): 降低会话校验请求与重复查库

This commit is contained in:
yml
2026-11-20 19:18:25 +08:00
parent 9242f44281
commit ec6f79f196
10 changed files with 208 additions and 30 deletions
+10 -1
View File
@@ -26,6 +26,10 @@ func NewHandler(users *auth.UserRepository) *Handler {
}
func (h *Handler) Me(c *gin.Context) {
if cached, ok := middleware.AuthenticatedUser(c); ok {
response.OK(c, cached)
return
}
if h.users == nil {
response.ServiceUnavailable(c, "数据库未连接")
return
@@ -35,7 +39,12 @@ func (h *Handler) Me(c *gin.Context) {
response.Unauthorized(c, "缺少用户上下文")
return
}
user, err := h.users.FindByID(c.Request.Context(), userID.(uint64))
id, ok := userID.(uint64)
if !ok {
response.Unauthorized(c, "用户上下文无效")
return
}
user, err := h.users.FindByID(c.Request.Context(), id)
if errors.Is(err, gorm.ErrRecordNotFound) {
response.Unauthorized(c, "用户不存在")
return