ark-member/internal/logic/bind_account_logic.go

74 lines
1.7 KiB
Go
Raw Normal View History

2024-07-21 15:57:56 +00:00
package logic
import (
"context"
"fmt"
"member/internal/domain"
ers "member/internal/lib/error"
"member/internal/lib/required"
"member/internal/model"
"strconv"
"member/gen_result/pb/member"
"member/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" `
}
// BindAccount 綁定帳號 -> account bind to UID
func (l *BindAccountLogic) BindAccount(in *member.BindingUserReq) (*member.Response, error) {
// 驗證資料
err := required.ValidateAll(l.svcCtx.Validate, &bindLoginUserReq{
Account: in.GetLoginId(),
})
if err != nil {
return nil, ers.InvalidFormat(err.Error())
}
uid := in.GetUid()
// 有UID 綁看看沒帶UID 近來,確認沒重複就直接綁一個給他
if in.GetUid() == "" {
uid = strconv.FormatInt(int64(l.svcCtx.SnackFlowGen.Generate()), 10)
}
// 先確定有這個Account
_, err = l.svcCtx.AccountModel.FindOneByAccount(l.ctx, in.GetLoginId())
if err != nil {
return nil, ers.ResourceNotFound(fmt.Sprintf("failed to get account : %s ", in.GetLoginId()))
}
_, err = l.svcCtx.AccountToUidModel.Insert(l.ctx, &model.AccountToUid{
Account: in.LoginId,
Uid: uid,
})
if err != nil {
return nil, ers.DBError(err.Error())
}
return &member.Response{
Status: &member.BaseResp{
Code: domain.CodeOk.ToString(),
Message: "success",
Error: "",
},
}, nil
}