63 lines
1.8 KiB
Go
63 lines
1.8 KiB
Go
package user
|
||
|
||
import (
|
||
"context"
|
||
"time"
|
||
|
||
"bdrp-server/app/main/api/internal/svc"
|
||
"bdrp-server/app/main/api/internal/types"
|
||
"bdrp-server/app/main/model"
|
||
"bdrp-server/common/ctxdata"
|
||
"bdrp-server/common/xerr"
|
||
|
||
"github.com/pkg/errors"
|
||
"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) {
|
||
claims, err := ctxdata.GetClaimsFromCtx(l.ctx)
|
||
if err != nil {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", err)
|
||
}
|
||
// 被封禁用户禁止刷新 token(临时用户 ID 在 user_temp 表)
|
||
if claims.UserType == model.UserTypeTemp {
|
||
_, err := l.svcCtx.UserTempModel.FindOne(l.ctx, claims.UserId)
|
||
if err != nil {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", err)
|
||
}
|
||
} else {
|
||
user, err := l.svcCtx.UserModel.FindOne(l.ctx, claims.UserId)
|
||
if err != nil {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", err)
|
||
}
|
||
if user.Disable == 1 {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.USER_DISABLED), "账号已被封禁")
|
||
}
|
||
}
|
||
token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, claims.UserId, claims.UserType)
|
||
if err != nil {
|
||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", err)
|
||
}
|
||
// 获取当前时间戳
|
||
now := time.Now().Unix()
|
||
return &types.MobileCodeLoginResp{
|
||
AccessToken: token,
|
||
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
|
||
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
|
||
}, nil
|
||
}
|