50 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			50 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package user
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"time"
 | |
| 	"tyc-server/common/ctxdata"
 | |
| 	jwtx "tyc-server/common/jwt"
 | |
| 	"tyc-server/common/xerr"
 | |
| 
 | |
| 	"github.com/pkg/errors"
 | |
| 
 | |
| 	"tyc-server/app/main/api/internal/svc"
 | |
| 	"tyc-server/app/main/api/internal/types"
 | |
| 
 | |
| 	"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) {
 | |
| 	userID, err := ctxdata.GetUidFromCtx(l.ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %+v", err)
 | |
| 	}
 | |
| 	token, generaErr := jwtx.GenerateJwtToken(userID, l.svcCtx.Config.JwtAuth.AccessSecret, l.svcCtx.Config.JwtAuth.AccessExpire)
 | |
| 	if generaErr != nil {
 | |
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "更新token, 生成token失败 : %d", userID)
 | |
| 	}
 | |
| 	// 获取当前时间戳
 | |
| 	now := time.Now().Unix()
 | |
| 	return &types.MobileCodeLoginResp{
 | |
| 		AccessToken:  token,
 | |
| 		AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
 | |
| 		RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
 | |
| 	}, nil
 | |
| 
 | |
| }
 |