完成任务2
This commit is contained in:
@@ -4,21 +4,21 @@ import (
|
||||
"errors"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrUserNotFoundInContext 表示在 gin.Context 中未找到用户信息。
|
||||
// ErrUserNotFoundInContext 表示在 context 中未找到用户信息。
|
||||
ErrUserNotFoundInContext = errors.New("context中未找到用户信息")
|
||||
// ErrInvalidUserType 表示从 gin.Context 中获取的用户信息类型不正确。
|
||||
// ErrInvalidUserType 表示从 context 中获取的用户信息类型不正确。
|
||||
ErrInvalidUserType = errors.New("context中用户信息类型不正确")
|
||||
)
|
||||
|
||||
// GetOperatorIDFromContext 从 gin.Context 中提取操作者ID。
|
||||
// GetOperatorIDFromContext 从 echo.Context 中提取操作者ID。
|
||||
// 假设操作者ID是由 AuthMiddleware 存储到 context 中的 *models.User 对象的 ID 字段。
|
||||
func GetOperatorIDFromContext(c *gin.Context) (uint, error) {
|
||||
userVal, exists := c.Get(models.ContextUserKey.String())
|
||||
if !exists {
|
||||
func GetOperatorIDFromContext(c echo.Context) (uint, error) {
|
||||
userVal := c.Get(models.ContextUserKey.String())
|
||||
if userVal == nil {
|
||||
return 0, ErrUserNotFoundInContext
|
||||
}
|
||||
|
||||
@@ -30,11 +30,11 @@ func GetOperatorIDFromContext(c *gin.Context) (uint, error) {
|
||||
return user.ID, nil
|
||||
}
|
||||
|
||||
// GetOperatorFromContext 从 gin.Context 中提取操作者。
|
||||
// 假设操作者是由 AuthMiddleware 存储到 context 中的 *models.User 对象的 字段。
|
||||
func GetOperatorFromContext(c *gin.Context) (*models.User, error) {
|
||||
userVal, exists := c.Get(models.ContextUserKey.String())
|
||||
if !exists {
|
||||
// GetOperatorFromContext 从 echo.Context 中提取操作者。
|
||||
// 假设操作者是由 AuthMiddleware 存储到 context 中的 *models.User 对象的字段。
|
||||
func GetOperatorFromContext(c echo.Context) (*models.User, error) {
|
||||
userVal := c.Get(models.ContextUserKey.String())
|
||||
if userVal == nil {
|
||||
return nil, ErrUserNotFoundInContext
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user