mirror of
https://github.com/limitcool/starter.git
synced 2025-10-07 01:02:45 +08:00

- 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.
67 lines
1.1 KiB
Go
67 lines
1.1 KiB
Go
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 从上下文中获取用户ID(int64类型)
|
||
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)
|
||
}
|