Files
starter/internal/middleware/utils.go
limitcool df066bc077 feat: Implement user and admin services with separate and simple modes
- Added middleware utility functions to retrieve user IDs from context.
- Created separate and simple routing structures for user and admin functionalities.
- Developed user and admin service interfaces with implementations for both separate and simple modes.
- Implemented user registration, login, password change, and user info retrieval functionalities.
- Integrated role, menu, and permission management within the admin services.
- Established lifecycle hooks for service initialization and shutdown logging.
- Enhanced error handling and logging throughout the service methods.
2025-04-22 21:31:49 +08:00

67 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package middleware
import (
"fmt"
"github.com/gin-gonic/gin"
)
// GetUserID 从上下文中获取用户ID
func GetUserID(c *gin.Context) uint {
userID, exists := c.Get("user_id")
if !exists {
return 0
}
// 尝试转换为float64
if id, ok := userID.(float64); ok {
return uint(id)
}
// 尝试转换为uint
if id, ok := userID.(uint); ok {
return id
}
// 尝试转换为int64
if id, ok := userID.(int64); ok {
return uint(id)
}
return 0
}
// GetUserIDInt64 从上下文中获取用户IDint64类型
func GetUserIDInt64(c *gin.Context) int64 {
userID, exists := c.Get("user_id")
if !exists {
return 0
}
// 尝试转换为float64
if id, ok := userID.(float64); ok {
return int64(id)
}
// 尝试转换为uint
if id, ok := userID.(uint); ok {
return int64(id)
}
// 尝试转换为int64
if id, ok := userID.(int64); ok {
return id
}
return 0
}
// GetUserIDString 从上下文中获取用户ID字符串类型
func GetUserIDString(c *gin.Context) string {
id := GetUserID(c)
if id == 0 {
return ""
}
return fmt.Sprintf("%d", id)
}