app-cloudep-member-server/internal/logic/account/bind_account_logic.go

86 lines
2.1 KiB
Go
Raw Normal View History

2024-12-30 03:58:14 +00:00
package accountlogic
import (
domain "app-cloudep-member-server/pkg/domain/member"
"app-cloudep-member-server/pkg/domain/usecase"
"code.30cm.net/digimon/library-go/errs"
"context"
"fmt"
"math"
"app-cloudep-member-server/gen_result/pb/member"
"app-cloudep-member-server/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type BindAccountLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewBindAccountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindAccountLogic {
return &BindAccountLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
type bindLoginUserReq struct {
Account string `json:"account" validate:"account"`
UID string `json:"uid" `
Type int64 `json:"type" validate:"required,oneof=1 2 3"`
}
// BindAccount 綁定帳號 -> account bind to UID
func (l *BindAccountLogic) BindAccount(in *member.BindingUserReq) (*member.BindingUserResp, error) {
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&bindLoginUserReq{
Account: in.GetLoginId(),
Type: in.GetType(),
}); err != nil {
return nil, errs.InvalidFormat(err.Error())
}
// 先確定有這個Account
if _, err := l.svcCtx.AccountUseCase.GetUserAccountInfo(l.ctx, usecase.GetUIDByAccountRequest{Account: in.GetLoginId()}); err != nil {
return nil, err
}
var err error
uid := in.GetUid()
// 有 UID 綁看看沒帶UID 近來,確認沒重複就直接綁一個給他
if in.GetUid() == "" {
uid, err = l.svcCtx.AccountUseCase.Generate(l.ctx)
if err != nil {
return nil, err
}
}
t, err := int64ToInt32Safe(in.GetType())
if err != nil {
return nil, err
}
if _, err := l.svcCtx.AccountUseCase.BindAccount(l.ctx, usecase.BindingUser{
LoginID: in.LoginId,
UID: uid,
Type: domain.AccountType(t),
}); err != nil {
return nil, err
}
return &member.BindingUserResp{
LoginId: in.LoginId,
Uid: uid,
Type: in.GetType(),
}, nil
}
func int64ToInt32Safe(value int64) (int32, error) {
if value > math.MaxInt32 || value < math.MinInt32 {
return 0, fmt.Errorf("value %d is out of int32 range", value)
}
return int32(value), nil
}