mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-02-07 09:14:06 +08:00
fix: 管理员活跃时间记录
This commit is contained in:
@@ -6,9 +6,10 @@ import (
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/chaitin/MonkeyCode/backend/consts"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"github.com/chaitin/MonkeyCode/backend/consts"
|
||||
)
|
||||
|
||||
type ActiveMiddleware struct {
|
||||
@@ -23,14 +24,24 @@ func NewActiveMiddleware(redis *redis.Client, logger *slog.Logger) *ActiveMiddle
|
||||
}
|
||||
}
|
||||
|
||||
func (a *ActiveMiddleware) Active() echo.MiddlewareFunc {
|
||||
func (a *ActiveMiddleware) Active(scope string) echo.MiddlewareFunc {
|
||||
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
||||
return func(c echo.Context) error {
|
||||
if apikey := GetApiKey(c); apikey != nil {
|
||||
if err := a.redis.Set(context.Background(), fmt.Sprintf(consts.UserActiveKeyFmt, apikey.UserID), time.Now().Unix(), 0).Err(); err != nil {
|
||||
a.logger.With("error", err).ErrorContext(c.Request().Context(), "failed to set user active status in Redis")
|
||||
switch scope {
|
||||
case "admin":
|
||||
if user := GetUser(c); user != nil {
|
||||
if err := a.redis.Set(context.Background(), fmt.Sprintf(consts.AdminActiveKeyFmt, user.ID), time.Now().Unix(), 0).Err(); err != nil {
|
||||
a.logger.With("error", err).ErrorContext(c.Request().Context(), "failed to set admin active status in Redis")
|
||||
}
|
||||
}
|
||||
case "user":
|
||||
if apikey := GetApiKey(c); apikey != nil {
|
||||
if err := a.redis.Set(context.Background(), fmt.Sprintf(consts.UserActiveKeyFmt, apikey.UserID), time.Now().Unix(), 0).Err(); err != nil {
|
||||
a.logger.With("error", err).ErrorContext(c.Request().Context(), "failed to set user active status in Redis")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return next(c)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user