作者 yangfu

1.子账号登录修改

... ... @@ -2,6 +2,7 @@ package auth
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/query"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/application/partnerInfo/service"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
... ... @@ -17,12 +18,34 @@ func Login(header *protocol.RequestHeader, request *protocol.LoginRequest) (rsp
PartnerInfoService = service.NewPartnerInfoService(nil)
partnerInfo *domain.PartnerInfo
result bool = false
transactionContext, _ = factory.CreateTransactionContext(nil)
PartnerSubAccountRepository, _ = factory.CreatePartnerSubAccountRepository(transactionContext)
partnerSubAccount *domain.PartnerSubAccount
)
if err = transactionContext.StartTransaction(); err != nil {
return nil, err
}
defer func() {
if err != nil {
transactionContext.RollbackTransaction()
return
}
err = transactionContext.CommitTransaction()
}()
rsp = &protocol.LoginResponse{}
if partnerInfo, err = PartnerInfoService.GetPartnerInfo(&query.GetPartnerInfoQuery{Account: request.Phone}); err != nil {
//子账号
if partnerSubAccount, err = PartnerSubAccountRepository.FindOne(map[string]interface{}{"account": request.Phone}); err == nil {
partnerInfo = &domain.PartnerInfo{
Id: partnerSubAccount.PartnerId,
Account: partnerSubAccount.Account,
Password: partnerSubAccount.Password,
}
} else {
err = protocol.NewErrWithMessage(502, err) //账号不存在
return
}
}
switch request.GrantType {
case protocol.LoginByPassword:
if len(partnerInfo.Password) == 0 {
... ...
... ... @@ -37,3 +37,7 @@ func CreateOrderPaymentRepository(transactionContext *transaction.TransactionCon
func CreatePartnerInfoRepositoryIn(transactionContext *transaction.TransactionContext) (domain.PartnerInfoRepository, error) {
return repository.NewPartnerInfoRepository(transactionContext)
}
func CreatePartnerSubAccountRepository(transactionContext *transaction.TransactionContext) (domain.PartnerSubAccountRepository, error) {
return repository.NewPartnerSubAccountRepository(transactionContext)
}
... ...
package domain
//合伙人子账号
type PartnerSubAccount struct {
Id int64 `json:"id"`
Account string `json:"account"`
Password string `json:"password"`
PartnerId int64 `json:"partnerId"` //绑定合伙人账号
}
type PartnerSubAccountRepository interface {
Save(dm *PartnerSubAccount) (*PartnerSubAccount, error)
Remove(dm *PartnerSubAccount) (*PartnerSubAccount, error)
FindOne(queryOptions map[string]interface{}) (*PartnerSubAccount, error)
Find(queryOptions map[string]interface{}) (int64, []*PartnerSubAccount, error)
}
func (m *PartnerSubAccount) Identify() interface{} {
if m.Id == 0 {
return nil
}
return m.Id
}
... ...
... ... @@ -27,6 +27,7 @@ func init() {
(*models.PartnerInfo)(nil),
(*models.Order)(nil),
(*models.OrderPayment)(nil),
(*models.PartnerSubAccount)(nil),
} {
err := DB.CreateTable(model, &orm.CreateTableOptions{
Temp: false,
... ...
package models
type PartnerSubAccount struct {
tableName struct{} `pg:"partner_sub_account"`
Id int64 `pg:",pk"`
Account string
Password string
PartnerId int64 //绑定合伙人账号
}
... ...
package repository
import (
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/pg/transaction"
. "gitlab.fjmaimaimai.com/mmm-go/partner/pkg/infrastructure/utils"
)
type PartnerSubAccountRepository struct {
transactionContext *transaction.TransactionContext
}
func (repository *PartnerSubAccountRepository) Save(dm *domain.PartnerSubAccount) (*domain.PartnerSubAccount, error) {
var (
err error
m = &models.PartnerSubAccount{}
tx = repository.transactionContext.PgTx
)
if err = GobModelTransform(m, dm); err != nil {
return nil, err
}
if dm.Identify() == nil {
if dm.Id, err = NewSnowflakeId(); err != nil {
return dm, err
}
m.Id = dm.Id
if err = tx.Insert(m); err != nil {
return nil, err
}
return dm, nil
}
if err = tx.Update(m); err != nil {
return nil, err
}
return dm, nil
}
func (repository *PartnerSubAccountRepository) Remove(PartnerSubAccount *domain.PartnerSubAccount) (*domain.PartnerSubAccount, error) {
var (
tx = repository.transactionContext.PgTx
PartnerSubAccountModel = &models.PartnerSubAccount{Id: PartnerSubAccount.Identify().(int64)}
)
if _, err := tx.Model(PartnerSubAccountModel).Where("id = ?", PartnerSubAccount.Id).Delete(); err != nil {
return PartnerSubAccount, err
}
return PartnerSubAccount, nil
}
func (repository *PartnerSubAccountRepository) FindOne(queryOptions map[string]interface{}) (*domain.PartnerSubAccount, error) {
tx := repository.transactionContext.PgTx
PartnerSubAccountModel := new(models.PartnerSubAccount)
query := NewQuery(tx.Model(PartnerSubAccountModel), queryOptions)
query.SetWhere("id = ?", "id")
query.SetWhere("account = ?", "account")
if err := query.First(); err != nil {
return nil, query.HandleError(err, "没有此合伙人")
}
if PartnerSubAccountModel.Id == 0 {
return nil, nil
}
return repository.transformPgModelToDomainModel(PartnerSubAccountModel)
}
func (repository *PartnerSubAccountRepository) Find(queryOptions map[string]interface{}) (int64, []*domain.PartnerSubAccount, error) {
tx := repository.transactionContext.PgTx
var PartnerSubAccountModels []*models.PartnerSubAccount
PartnerSubAccounts := make([]*domain.PartnerSubAccount, 0)
query := NewQuery(tx.Model(&PartnerSubAccountModels), queryOptions)
query.
SetWhere("account = ?", "account").
SetLimit()
var err error
if query.AffectRow, err = query.SelectAndCount(); err != nil {
return 0, PartnerSubAccounts, err
}
for _, PartnerSubAccountModel := range PartnerSubAccountModels {
if PartnerSubAccount, err := repository.transformPgModelToDomainModel(PartnerSubAccountModel); err != nil {
return 0, PartnerSubAccounts, err
} else {
PartnerSubAccounts = append(PartnerSubAccounts, PartnerSubAccount)
}
}
return int64(query.AffectRow), PartnerSubAccounts, nil
}
func (repository *PartnerSubAccountRepository) transformPgModelToDomainModel(PartnerSubAccountModel *models.PartnerSubAccount) (*domain.PartnerSubAccount, error) {
m := &domain.PartnerSubAccount{}
err := GobModelTransform(m, PartnerSubAccountModel)
return m, err
}
func NewPartnerSubAccountRepository(transactionContext *transaction.TransactionContext) (*PartnerSubAccountRepository, error) {
if transactionContext == nil {
return nil, ERR_EMPTY_TC
}
return &PartnerSubAccountRepository{transactionContext: transactionContext}, nil
}
... ...