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

86 lines
2.4 KiB
Go
Raw Permalink Normal View History

2024-08-22 14:27:13 +00:00
package accountlogic
import (
"app-cloudep-member-server/internal/model"
"context"
"errors"
"time"
ers "code.30cm.net/digimon/library-go/errors"
"github.com/go-sql-driver/mysql"
"app-cloudep-member-server/gen_result/pb/member"
"app-cloudep-member-server/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type BindUserInfoLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
type createUserInfo struct {
Uid string `validate:"required"`
VerifyType int32 `validate:"required,oneof=0 1 2 3"`
AlarmType int32 `validate:"required,oneof=0 1 2"`
Status int32 `validate:"required,oneof=1 2 3 4 5 6"`
RoleId string `validate:"required"`
Language string `validate:"required"`
Currency string `validate:"required"`
NickName string `validate:"required"`
}
func NewBindUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BindUserInfoLogic {
return &BindUserInfoLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
type createUserInfoReq struct {
Uid string `validate:"required"` // 唯一辨識碼
Language string `validate:"required"`
Currency string `validate:"required"`
}
// BindUserInfo 初次,綁定 User Info
func (l *BindUserInfoLogic) BindUserInfo(in *member.CreateUserInfoReq) (*member.OKResp, error) {
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&createUserInfoReq{
Uid: in.GetUid(),
Language: in.GetLanguage(),
Currency: in.GetCurrency(),
}); err != nil {
return nil, ers.InvalidFormat(err.Error())
}
// 有些應為預設資料,不應該被新增
now := time.Now().UTC().Unix()
if _, err := l.svcCtx.UserModel.Insert(l.ctx, &model.UserTable{
Uid: in.GetUid(),
VerifyType: int64(member.VerifyType_VERIFY_NOT.Number()), // 預設為沒有設定兩步驟驗證
AlarmType: int64(member.AlarmType_ALARM_NOT.Number()), // 預設狀態為沒有被系統告警過
Status: int64(member.MemberStatus_STATUS_VERIFY.Number()), // 預設狀態為尚未驗證
Language: in.GetLanguage(),
Currency: in.GetCurrency(),
NickName: in.GetNickName(),
Avatar: in.GetAvatar(),
CreateTime: now,
UpdateTime: now,
}); err != nil {
// 新增進去
var mysqlErr *mysql.MySQLError
if errors.As(err, &mysqlErr) && mysqlErr.Number == 1062 {
// 處理重複條目錯誤
return nil, ers.DBDuplicate(in.GetUid())
}
return nil, ers.DBError(err.Error())
}
return &member.OKResp{}, nil
}