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

75 lines
1.9 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package accountlogic
import (
"app-cloudep-member-server/internal/model"
"context"
"fmt"
ers "code.30cm.net/digimon/library-go/errors"
"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, ers.InvalidFormat(err.Error())
}
// 先確定有這個Account
if _, err := l.svcCtx.AccountModel.FindOneByAccount(l.ctx, in.GetLoginId()); err != nil {
return nil, ers.ResourceNotFound(fmt.Sprintf("failed to get account : %s ", in.GetLoginId()))
}
var err error
uid := in.GetUid()
// 有 UID 綁看看沒帶UID 近來,確認沒重複就直接綁一個給他
if in.GetUid() == "" {
uid, err = l.svcCtx.GenUIDUseCase.Generate(l.ctx)
if err != nil {
return nil, ers.ArkInternal(fmt.Sprintf("[GenUIDUseCase.Generate] failed to get account : %s ", in.GetLoginId()))
}
}
if _, err := l.svcCtx.AccountToUidModel.Insert(l.ctx, &model.AccountToUid{
Account: in.LoginId,
Uid: uid,
Type: in.GetType(),
}); err != nil {
return nil, ers.DBError(err.Error())
}
return &member.BindingUserResp{
LoginId: in.LoginId,
Uid: uid,
Type: in.GetType(),
}, nil
}