kra/internal/service/system/jwt_blacklist.go

41 lines
1.1 KiB
Go
Raw Permalink 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 system
import (
"kra/internal/biz/system"
"kra/internal/server/middleware"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/transport/http"
)
// JwtBlacklistService JWT黑名单服务
type JwtBlacklistService struct {
uc *system.JwtBlacklistUsecase
}
// NewJwtBlacklistService 创建JWT黑名单服务
func NewJwtBlacklistService(uc *system.JwtBlacklistUsecase) *JwtBlacklistService {
return &JwtBlacklistService{uc: uc}
}
// JoinBlacklist 加入黑名单(登出时调用)
func (s *JwtBlacklistService) JoinBlacklist(ctx http.Context) error {
token := middleware.GetToken(ctx)
if token == "" {
return errors.BadRequest("TOKEN_EMPTY", "token为空")
}
if err := s.uc.JoinBlacklist(ctx, token); err != nil {
return errors.InternalServer("BLACKLIST_ERROR", err.Error())
}
return ctx.Result(200, map[string]any{
"code": 0,
"msg": "登出成功",
})
}
// RegisterRoutes 注册路由
func (s *JwtBlacklistService) RegisterRoutes(rg *RouterGroup) {
// JWT黑名单不需要操作记录
rg.Private.POST("/jwt/jsonInBlacklist", s.JoinBlacklist)
}