package member import ( "app-cloudep-portal-api-gateway/internal/domain" "app-cloudep-portal-api-gateway/internal/svc" "app-cloudep-portal-api-gateway/internal/types" "context" "time" ers "code.30cm.net/digimon/library-go/errors" accountRpc "code.30cm.net/digimon/proto-all/pkg/member" permissionRpc "code.30cm.net/digimon/proto-all/pkg/permission" "github.com/gogo/protobuf/proto" "github.com/zeromicro/go-zero/core/logx" ) type CreateAccountLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewCreateAccountLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateAccountLogic { return &CreateAccountLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *CreateAccountLogic) CreateAccount(req *types.CreateAccountRequest) (resp *types.CreateAccountResp, err error) { // 驗證密碼,兩次密碼要一致 if req.Token != req.TokenCheck { return nil, ers.InvalidFormat("password confirmation does not match") } // 平台驗證 p := domain.GetPlatformByPlatformCode(req.Platform) if p == domain.PlatformNone { return nil, ers.InvalidFormat("platform not found") } // 建立帳號 _, err = l.svcCtx.AccountRpc.CreateUserAccount(l.ctx, &accountRpc.CreateLoginUserReq{ LoginId: req.Account, Platform: p.ToInt64(), Token: req.Token, }) if err != nil { return nil, err } // 綁定 UID bindResp, err := l.svcCtx.AccountRpc.BindAccount(l.ctx, &accountRpc.BindingUserReq{ LoginId: req.Account, Type: req.AccountType, }) if err != nil { return nil, err } // 綁定使用者基礎資訊 if err := l.bindUserInfo(bindResp.Uid); err != nil { return nil, err } // 發 token token, err := l.generateToken(bindResp.Uid, req.DeviceID) if err != nil { return nil, err } // 建立回應 return &types.CreateAccountResp{ Status: types.Status{ Code: domain.SuccessCode, Message: domain.SuccessMsg, }, Data: types.CreateAccountItem{ UID: bindResp.Uid, AccessToken: token.AccessToken, RefreshToken: token.RefreshToken, TokenType: token.TokenType, }, }, nil } // bindUserInfo 綁定使用者基礎資訊 func (l *CreateAccountLogic) bindUserInfo(uid string) error { _, err := l.svcCtx.AccountRpc.BindUserInfo(l.ctx, &accountRpc.CreateUserInfoReq{ Uid: uid, Currency: domain.DefaultCurrency, Language: domain.DefaultLanguage, NickName: proto.String(uid), }) return err } // generateToken 生成 token func (l *CreateAccountLogic) generateToken(uid, deviceID string) (*permissionRpc.TokenResp, error) { return l.svcCtx.TokenRpc.NewToken(l.ctx, &permissionRpc.AuthorizationReq{ GrantType: domain.GrantTypeClientCredentials.ToString(), DeviceId: deviceID, Scope: domain.DefaultScope, IsRefreshToken: true, Expires: int32(time.Now().UTC().Add(l.svcCtx.Config.Token.Expired).Unix()), Data: map[string]string{ "uid": uid, }, }) }