[bugfix/backend] /users/me handling
This commit is contained in:
parent
823bedd1fa
commit
d8d8e4b0d7
3 changed files with 228 additions and 156 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
"tss-rocks-be/internal/types"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type UpdateCurrentUserRequest struct {
|
||||
|
@ -160,24 +161,39 @@ func (h *Handler) DeleteUser(c *gin.Context) {
|
|||
|
||||
// GetCurrentUser returns the current user's information
|
||||
func (h *Handler) GetCurrentUser(c *gin.Context) {
|
||||
// 从上下文中获取用户ID(由认证中间件设置)
|
||||
userID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Unauthorized"})
|
||||
return
|
||||
}
|
||||
|
||||
// 将用户ID转换为int64
|
||||
var id int64
|
||||
switch v := userID.(type) {
|
||||
case int64:
|
||||
id = v
|
||||
case int:
|
||||
id = int64(v)
|
||||
case float64:
|
||||
id = int64(v)
|
||||
default:
|
||||
log.Error().
|
||||
Str("type", fmt.Sprintf("%T", userID)).
|
||||
Interface("value", userID).
|
||||
Msg("Invalid user_id type")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid user_id type"})
|
||||
return
|
||||
}
|
||||
|
||||
// 获取用户信息
|
||||
user, err := h.service.GetUser(c.Request.Context(), userID.(int))
|
||||
user, err := h.service.GetUser(c.Request.Context(), int(id))
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to get user")
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get user information"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": user,
|
||||
})
|
||||
c.JSON(http.StatusOK, user)
|
||||
}
|
||||
|
||||
// UpdateCurrentUser updates the current user's information
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue