tydata-server/app/main/api/internal/logic/user/gettokenlogic.go

48 lines
1.2 KiB
Go
Raw Normal View History

2025-06-08 15:07:04 +08:00
package user
import (
"context"
"time"
"tydata-server/common/ctxdata"
"tydata-server/common/xerr"
"github.com/pkg/errors"
2025-06-08 15:14:34 +08:00
"tydata-server/app/main/api/internal/svc"
"tydata-server/app/main/api/internal/types"
2025-06-08 15:07:04 +08:00
"github.com/zeromicro/go-zero/core/logx"
)
type GetTokenLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTokenLogic {
return &GetTokenLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetTokenLogic) GetToken() (resp *types.MobileCodeLoginResp, err error) {
2025-06-19 13:09:16 +08:00
claims, err := ctxdata.GetClaimsFromCtx(l.ctx)
2025-06-08 15:07:04 +08:00
if err != nil {
2025-06-18 16:31:32 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", err)
2025-06-08 15:07:04 +08:00
}
2025-06-19 13:09:16 +08:00
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, claims.UserId, claims.UserType)
2025-06-18 16:31:32 +08:00
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", err)
2025-06-08 15:07:04 +08:00
}
// 获取当前时间戳
now := time.Now().Unix()
return &types.MobileCodeLoginResp{
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}