init: init refactor
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"sundynix-micro-go/app/user/rpc/internal/svc"
|
||||
"sundynix-micro-go/app/user/rpc/user"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type VerifyTokenLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewVerifyTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VerifyTokenLogic {
|
||||
return &VerifyTokenLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
// 验证Token有效性
|
||||
func (l *VerifyTokenLogic) VerifyToken(in *user.VerifyTokenReq) (*user.VerifyTokenResp, error) {
|
||||
claims, err := l.svcCtx.JWT.ParseToken(in.Token)
|
||||
if err != nil {
|
||||
return &user.VerifyTokenResp{
|
||||
Valid: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 检查Redis黑名单
|
||||
if l.svcCtx.Redis != nil {
|
||||
blacklistKey := "jwt:blacklist:" + claims.BaseClaims.ID
|
||||
val, _ := l.svcCtx.Redis.Get(l.ctx, blacklistKey).Result()
|
||||
if val == in.Token {
|
||||
return &user.VerifyTokenResp{
|
||||
Valid: false,
|
||||
}, nil
|
||||
}
|
||||
}
|
||||
|
||||
return &user.VerifyTokenResp{
|
||||
Valid: true,
|
||||
UserId: claims.BaseClaims.ID,
|
||||
Account: claims.BaseClaims.Account,
|
||||
ExpiresAt: claims.ExpiresAt.Unix(),
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user