feat: bind tribally account
This commit is contained in:
64
internal/logic/task/bind_tribally_logic.go
Normal file
64
internal/logic/task/bind_tribally_logic.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package task
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"nova_task/internal/model"
|
||||
"nova_task/internal/pkg/errs"
|
||||
"nova_task/internal/pkg/tribally"
|
||||
"nova_task/internal/pkg/utils"
|
||||
"nova_task/internal/svc"
|
||||
"nova_task/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type BindTriballyLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 绑定Tribally账号
|
||||
|
||||
func NewBindTriballyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindTriballyLogic {
|
||||
return &BindTriballyLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *BindTriballyLogic) BindTribally() (resp *types.BindTriballyReply, err error) {
|
||||
uid := utils.GetUidUint(l.ctx)
|
||||
apiKey, err := l.svcCtx.ConfigModel.GetTriballyApiKey(l.ctx)
|
||||
if err != nil {
|
||||
l.Errorw("get tribally api key failed", logx.Field("err", err))
|
||||
return nil, errs.New(errs.ErrDatabaseOperate, err)
|
||||
}
|
||||
if apiKey == "" {
|
||||
return nil, errs.New(errs.ErrSystemConfig, "system config err")
|
||||
}
|
||||
u, err := l.svcCtx.UserModel.FindOne(l.ctx, uid)
|
||||
if err != nil {
|
||||
if errors.Is(err, model.ErrNotFound) {
|
||||
return nil, errs.New(errs.ErrUserNotFound, "user not found")
|
||||
}
|
||||
l.Errorw("get user failed", logx.Field("err", err), logx.Field("uid", uid))
|
||||
return nil, errs.New(errs.ErrDatabaseOperate, err)
|
||||
}
|
||||
hasBind, err := l.svcCtx.RoleModel.AccountExist(l.ctx, u.Email)
|
||||
if err != nil {
|
||||
return nil, errs.New(errs.ErrDatabaseOperate, err)
|
||||
}
|
||||
if !hasBind {
|
||||
return nil, errs.New(errs.ErrNotBindRole, "user not bind role")
|
||||
}
|
||||
authUrl, err := tribally.BindTribally(apiKey, u.Email)
|
||||
if err != nil {
|
||||
l.Errorw("bind tribally failed", logx.Field("err", err))
|
||||
return nil, errs.New(errs.ErrInternalServer, err)
|
||||
}
|
||||
|
||||
return &types.BindTriballyReply{AuthUrl: authUrl}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user