Skip to content

Commit

Permalink
Merge pull request #272 from ArtisanCloud/dev/michaelhu
Browse files Browse the repository at this point in the history
Dev/michaelhu
  • Loading branch information
Matrix-X committed Oct 22, 2023
2 parents 772dc2f + 129463d commit b6ec99a
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 43 deletions.
1 change: 1 addition & 0 deletions api/admin/crm/customerdomain/customer.api
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type (
Name string `json:"name"`
Mobile string `json:"mobile"`
Email string `json:"email,optional"`
UUID string `json:"uuid,optional"`
Inviter *CustomerInviter `json:"inviter,optional"`
InviterId int64 `json:"inviter,optional"`
Source int `json:"source,optional"`
Expand Down
1 change: 1 addition & 0 deletions api/web/customerdomain/auth.api
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type (
Phone string `json:"phone"`
Password string `json:"password"`
VerifyCode string `json:"verifyCode"`
InviteCode string `json:"inviteCode,optional"`
}

CustomerRegisterByPhoneReply {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"PowerX/internal/model/crm/customerdomain"
"PowerX/internal/svc"
"PowerX/internal/types"
"PowerX/pkg/securityx"
"context"

"github.com/zeromicro/go-zero/core/logx"
)

Expand All @@ -29,6 +29,7 @@ func (l *CreateCustomerLogic) CreateCustomer(req *types.CreateCustomerRequest) (
Name: req.Name,
Mobile: req.Mobile,
Email: req.Email,
Uuid: securityx.GenerateUUID(),
InviterId: req.InviterId,
Source: req.Source,
Type: req.Type,
Expand All @@ -46,19 +47,20 @@ func (l *CreateCustomerLogic) CreateCustomer(req *types.CreateCustomerRequest) (
func TransformRequestToCustomer(customerRequest *types.Customer) (mdlCustomer *customerdomain.Customer) {

mdlCustomer = &customerdomain.Customer{
Name: customerRequest.Name,
Mobile: customerRequest.Mobile,
Name: customerRequest.Name,
//Mobile: customerRequest.Mobile,
Email: customerRequest.Email,
InviterId: customerRequest.InviterId,
Source: customerRequest.Source,
Type: customerRequest.Type,
IsActivated: customerRequest.IsActivated,
ExternalId: customerdomain.ExternalId{
OpenIdInMiniProgram: customerRequest.CustomerExternalId.OpenIdInMiniProgram,
OpenIdInWeChatOfficialAccount: customerRequest.CustomerExternalId.OpenIdInWeChatOfficialAccount,
OpenIdInWeCom: customerRequest.CustomerExternalId.OpenIdInWeCom,
},
//ExternalId: customerdomain.ExternalId{
// OpenIdInMiniProgram: customerRequest.CustomerExternalId.OpenIdInMiniProgram,
// OpenIdInWeChatOfficialAccount: customerRequest.CustomerExternalId.OpenIdInWeChatOfficialAccount,
// OpenIdInWeCom: customerRequest.CustomerExternalId.OpenIdInWeCom,
//},
}
mdlCustomer.Id = customerRequest.Id

return mdlCustomer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package customer
import (
"PowerX/internal/model/crm/customerdomain"
"PowerX/internal/types/errorx"
"PowerX/pkg/securityx"
"context"

"PowerX/internal/svc"
Expand Down Expand Up @@ -51,16 +50,17 @@ func TransformCustomerToReply(svcCtx *svc.ServiceContext, mdlCustomer *customerd

mobile := mdlCustomer.Mobile
openIdInMiniProgram := mdlCustomer.OpenIdInMiniProgram
if svcCtx.Config.PowerXDatabase.SeedCommerceData {
mobile = securityx.MaskMobile(mobile)
openIdInMiniProgram = securityx.MaskName(openIdInMiniProgram, 20)
}
//if svcCtx.Config.PowerXDatabase.SeedCommerceData {
// mobile = securityx.MaskMobile(mobile)
// openIdInMiniProgram = securityx.MaskName(openIdInMiniProgram, 20)
//}

return &types.Customer{
Id: mdlCustomer.Id,
Name: mdlCustomer.Name,
Mobile: mobile,
Email: mdlCustomer.Email,
UUID: mdlCustomer.Uuid,
InviterId: mdlCustomer.InviterId,
Source: mdlCustomer.Source,
Type: mdlCustomer.Type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func NewPatchCustomerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Pat
func (l *PatchCustomerLogic) PatchCustomer(req *types.PatchCustomerRequest) (resp *types.PatchCustomerReply, err error) {

mdlCustomer := &customerdomain.Customer{
Name: req.Name,
Email: req.Email,
InviterId: req.InviterId,
Source: req.Source,
Type: req.Type,
Name: req.Name,
Email: req.Email,
//InviterId: req.InviterId,
//Source: req.Source,
//Type: req.Type,
IsActivated: req.IsActivated,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package customer

import (
"context"

"PowerX/internal/svc"
"PowerX/internal/types"
"PowerX/pkg/securityx"
"context"

"github.com/zeromicro/go-zero/core/logx"
)
Expand All @@ -26,6 +26,15 @@ func NewPutCustomerLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PutCu
func (l *PutCustomerLogic) PutCustomer(req *types.PutCustomerRequest) (resp *types.PutCustomerReply, err error) {
mdlCustomer := TransformRequestToCustomer(&(req.Customer))

cCustomer, err := l.svcCtx.PowerX.Customer.GetCustomer(l.ctx, req.Id)
if err != nil {
return nil, err
}
// 如果当前数据库的用户已经有了UUID
if cCustomer.Uuid == "" {
mdlCustomer.Uuid = securityx.GenerateUUID()
}

// 更新产品对象
err = l.svcCtx.PowerX.Customer.UpdateCustomer(l.ctx, req.CustomerId, mdlCustomer)

Expand Down
5 changes: 2 additions & 3 deletions internal/logic/mp/crm/customer/auth/authbyphonelogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"PowerX/internal/svc"
"PowerX/internal/types"
customerdomain2 "PowerX/internal/uc/powerx/crm/customerdomain"
fmt2 "PowerX/pkg/printx"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -50,15 +49,15 @@ func (l *AuthByPhoneLogic) AuthByPhone(req *types.MPCustomerAuthRequest) (resp *
// SessionKey: "+CG6t0FMK1QMLP4IKWNPUw==",
//}

fmt2.Dump(rs, req)
//fmt2.Dump(rs, req)
// 解码手机授权信息
msgData, errEncrypt := l.svcCtx.PowerX.WechatMP.App.Encryptor.DecryptData(req.EncryptedData, rs.SessionKey, req.IV)

if errEncrypt != nil {
return nil, errors.New(errEncrypt.ErrMsg)
}

println(string(msgData))
//println(string(msgData))
// 解析手机信息
mpPhoneInfo := &wechat.MPPhoneInfo{}
err = object.JsonDecode(msgData, mpPhoneInfo)
Expand Down
54 changes: 50 additions & 4 deletions internal/logic/web/customer/auth/registercustomerbyphonelogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package auth
import (
"PowerX/internal/model"
"PowerX/internal/model/crm/customerdomain"
"PowerX/internal/model/crm/market"
"PowerX/internal/svc"
"PowerX/internal/types"
"PowerX/internal/types/errorx"
"PowerX/pkg/securityx"
"context"

"PowerX/internal/svc"
"PowerX/internal/types"

"github.com/zeromicro/go-zero/core/logx"
)

Expand Down Expand Up @@ -46,19 +46,65 @@ func (l *RegisterCustomerByPhoneLogic) RegisterCustomerByPhone(req *types.Custom
customerTypeId := l.svcCtx.PowerX.DataDictionary.GetCachedDDId(l.ctx, customerdomain.TypeCustomerType, customerdomain.CustomerPersonal)

// upsert 客户
uuid := securityx.GenerateUUID()
inviteCode := securityx.GenerateInviteCode(uuid)
customer := &customerdomain.Customer{
Mobile: req.Phone,
Password: hashedPassword,
Source: customerSourceId,
Type: customerTypeId,
Uuid: uuid,
InviteCode: inviteCode,
IsActivated: true,
}

// 如果邀请码存在,需要绑定邀请人的UUID
var record *market.InviteRecord
if req.InviteCode != "" {
customer, record, err = l.BindCustomerByInviteCode(l.ctx, req.InviteCode, customer)
if err != nil {
return nil, errorx.WithCause(errorx.ErrBadRequest, "邀请码无效")
}
}

// 创建新注册用户
err = l.svcCtx.PowerX.Customer.CreateCustomer(l.ctx, customer)
if err != nil {
return nil, err
return nil, errorx.WithCause(errorx.ErrCreateObject, "创建注册永固失败")
}

// 更新邀请记录的受邀者的ID
if req.InviteCode != "" {
record.InviteeID = customer.Id
l.svcCtx.PowerX.MGM.UpdateInviteRecord(l.ctx, record)
}

return &types.CustomerRegisterByPhoneReply{
CustomerId: customer.Id,
}, nil
}

func (l *RegisterCustomerByPhoneLogic) BindCustomerByInviteCode(ctx context.Context,
inviteCode string, customer *customerdomain.Customer,
) (*customerdomain.Customer, *market.InviteRecord, error) {

// 通过邀请码,获取邀请人
inviter, err := l.svcCtx.PowerX.Customer.GetCustomerByInviteCode(ctx, inviteCode)
if err != nil {
return nil, nil, err
}

// 保存邀请记录
// 使用默认的MGM规则,直系关系
mgmSceneId := l.svcCtx.PowerX.DataDictionary.GetCachedDDId(ctx, market.TypeMGMScene, market.MGMSceneDirectRecruitment)
inviteCode = securityx.GenerateInviteCode(inviter.Uuid)
record, err := l.svcCtx.PowerX.MGM.CreateInviteRecord(ctx, inviter, customer, inviteCode, mgmSceneId)

// 绑定邀请人和注册人的关系
customer.InviterId = inviter.Id
if err != nil {
return nil, nil, err
}

return customer, record, err
}
3 changes: 2 additions & 1 deletion internal/model/crm/customerdomain/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ type Customer struct {
powermodel.PowerModel
Name string `gorm:"comment:客户名称" json:"name"`
Mobile string `gorm:"unique;not null;comment:店长Id" json:"mobile"`
Uuid string `gorm:"comment:识别码;index" json:"uuid"`
Password string `gorm:"comment:客户密码" json:"password"`
Email string `gorm:"comment:邮箱地址" json:"email"`
InviterId int64 `gorm:"comment:邀请方" json:"inviterId"`
MgmId int `gorm:"comment:MGM Id" json:"mgmId"`
InviteCode string `gorm:"comment:裂变邀请码" json:"inviteCode"`
Source int `gorm:"comment:注册来源" json:"source"`
Uuid string `gorm:"comment:识别码;index" json:"uuid"`
Type int `gorm:"comment:类型:个人,企业" json:"type"`
IsActivated bool `gorm:"comment:激活状态" json:"isActivated"`
ExternalId
Expand Down
4 changes: 2 additions & 2 deletions internal/model/powermodel/powermodel.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package powermodel

import (
"PowerX/pkg/securityx"
"database/sql"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
Expand Down Expand Up @@ -57,7 +57,7 @@ var ArrayModelFields *object.HashMap = &object.HashMap{}
func NewPowerUUIDModel() *PowerUUIDModel {
now := time.Now()
return &PowerUUIDModel{
UUID: uuid.New().String(),
UUID: securityx.GenerateUUID(),
CreatedAt: now,
UpdatedAt: now,
}
Expand Down
2 changes: 2 additions & 0 deletions internal/types/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 48 additions & 13 deletions internal/uc/powerx/crm/customerdomain/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"PowerX/internal/model/powermodel"
"PowerX/internal/types"
"PowerX/internal/types/errorx"
"PowerX/pkg/securityx"
"context"
"github.com/pkg/errors"
"gorm.io/gorm"
Expand Down Expand Up @@ -100,10 +101,22 @@ func (uc *CustomerUseCase) UpsertCustomer(ctx context.Context, customer *custome

customers := []*customerdomain.Customer{customer}

_, err := uc.UpsertCustomers(ctx, customers)
if err != nil {
panic(errors.Wrap(err, "upsert customerdomain failed"))
}
err := uc.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
err := powermodel.UpsertModelsOnUniqueID(tx, &customerdomain.Customer{}, customerdomain.CustomerUniqueId, customers, nil, false)

if err != nil {
panic(errors.Wrap(err, "upsert customerdomain failed"))
}
// 如果是新增用户,那么需要给一个唯一识别号
if customer.Uuid == "" {
customer.Uuid = securityx.GenerateUUID()
err = powermodel.UpsertModelsOnUniqueID(tx, &customerdomain.Customer{}, customerdomain.CustomerUniqueId, customer, []string{"uuid"}, false)
if err != nil {
return err
}
}
return err
})

return customer, err
}
Expand All @@ -120,15 +133,12 @@ func (uc *CustomerUseCase) UpsertCustomers(ctx context.Context, customers []*cus
}

func (uc *CustomerUseCase) UpdateCustomer(ctx context.Context, id int64, customer *customerdomain.Customer) error {
if err := uc.db.WithContext(ctx).Model(&customerdomain.Customer{}).
//Debug().
Where(id).Updates(&customer).Error; err != nil {
if strings.Contains(err.Error(), "duplicate key value violates unique constraint") {
return errorx.WithCause(errorx.ErrDuplicatedInsert, "该对象不能重复创建")
}
panic(err)
}
return nil
//fmt.Dump(customer)
err := uc.db.WithContext(ctx).Model(&customerdomain.Customer{}).
Debug().
Where(id).Updates(&customer).Error

return err
}

func (uc *CustomerUseCase) GetCustomer(ctx context.Context, id int64) (*customerdomain.Customer, error) {
Expand All @@ -155,6 +165,31 @@ func (uc *CustomerUseCase) GetCustomerByMobile(ctx context.Context, mobile strin
return &customer, nil
}

func (uc *CustomerUseCase) GetCustomerByUUID(ctx context.Context, uuid string) (*customerdomain.Customer, error) {
var customer customerdomain.Customer
if err := uc.db.WithContext(ctx).
Where("uuid", uuid).
First(&customer).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errorx.WithCause(errorx.ErrBadRequest, "未找到客户")
}
panic(err)
}
return &customer, nil
}
func (uc *CustomerUseCase) GetCustomerByInviteCode(ctx context.Context, inviteCode string) (*customerdomain.Customer, error) {
var customer customerdomain.Customer
if err := uc.db.WithContext(ctx).
Where("invite_code", inviteCode).
First(&customer).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, errorx.WithCause(errorx.ErrBadRequest, "未找到客户")
}
panic(err)
}
return &customer, nil
}

func (uc *CustomerUseCase) DeleteCustomer(ctx context.Context, id int64) error {
result := uc.db.WithContext(ctx).Delete(&customerdomain.Customer{}, id)
if err := result.Error; err != nil {
Expand Down
Loading

0 comments on commit b6ec99a

Please sign in to comment.