tianyuan-api-server/apps/gateway/internal/logic/user/getsecretinfologic.go
2024-10-02 00:57:17 +08:00

45 lines
1003 B
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 user
import (
"context"
"errors"
"tianyuan-api/apps/sentinel/sentinel"
"tianyuan-api/apps/gateway/internal/svc"
"tianyuan-api/apps/gateway/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetSecretInfoLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetSecretInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetSecretInfoLogic {
return &GetSecretInfoLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetSecretInfoLogic) GetSecretInfo() (resp *types.SecretInfoResp, err error) {
// 从上下文中解析 JWT获取用户ID
userId, ok := l.ctx.Value("userId").(int64)
if !ok {
return nil, errors.New("无法获取 userId")
}
secret, err := l.svcCtx.SecretRpc.GetSecretByUserId(l.ctx, &sentinel.GetRecordByIdRequest{
Id: userId,
})
if err != nil {
return nil, err
}
return &types.SecretInfoResp{
AccessId: secret.SecretId,
AccessKey: secret.AesKey,
}, nil
}