feat: 新增店铺时自动设置默认角色
All checks were successful
构建并部署到测试环境(无 SSH) / build-and-deploy (push) Successful in 7m1s

- CreateShopRequest 新增必填字段 default_role_id
- 创建店铺时验证默认角色(必须存在、是客户角色、已启用)
- 创建店铺后自动设置 ShopRole,初始账号立即拥有权限

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-02-25 14:33:13 +08:00
parent 18daeae65a
commit 25e9749564
2 changed files with 37 additions and 12 deletions

View File

@@ -11,18 +11,19 @@ type ShopListRequest struct {
}
type CreateShopRequest struct {
ShopName string `json:"shop_name" validate:"required,min=1,max=100" required:"true" minLength:"1" maxLength:"100" description:"店铺名称"`
ShopCode string `json:"shop_code" validate:"required,min=1,max=50" required:"true" minLength:"1" maxLength:"50" description:"店铺编号"`
ParentID *uint `json:"parent_id" validate:"omitempty,min=1" minimum:"1" description:"上级店铺ID一级店铺可不填"`
ContactName string `json:"contact_name" validate:"omitempty,max=50" maxLength:"50" description:"联系人姓名"`
ContactPhone string `json:"contact_phone" validate:"omitempty,len=11" minLength:"11" maxLength:"11" description:"联系人电话"`
Province string `json:"province" validate:"omitempty,max=50" maxLength:"50" description:"省份"`
City string `json:"city" validate:"omitempty,max=50" maxLength:"50" description:"城市"`
District string `json:"district" validate:"omitempty,max=50" maxLength:"50" description:"区县"`
Address string `json:"address" validate:"omitempty,max=255" maxLength:"255" description:"详细地址"`
InitPassword string `json:"init_password" validate:"required,min=8,max=32" required:"true" minLength:"8" maxLength:"32" description:"初始账号密码"`
InitUsername string `json:"init_username" validate:"required,min=3,max=50" required:"true" minLength:"3" maxLength:"50" description:"初始账号用户名"`
InitPhone string `json:"init_phone" validate:"required,len=11" required:"true" minLength:"11" maxLength:"11" description:"初始账号手机号"`
ShopName string `json:"shop_name" validate:"required,min=1,max=100" required:"true" minLength:"1" maxLength:"100" description:"店铺名称"`
ShopCode string `json:"shop_code" validate:"required,min=1,max=50" required:"true" minLength:"1" maxLength:"50" description:"店铺编号"`
ParentID *uint `json:"parent_id" validate:"omitempty,min=1" minimum:"1" description:"上级店铺ID一级店铺可不填"`
ContactName string `json:"contact_name" validate:"omitempty,max=50" maxLength:"50" description:"联系人姓名"`
ContactPhone string `json:"contact_phone" validate:"omitempty,len=11" minLength:"11" maxLength:"11" description:"联系人电话"`
Province string `json:"province" validate:"omitempty,max=50" maxLength:"50" description:"省份"`
City string `json:"city" validate:"omitempty,max=50" maxLength:"50" description:"城市"`
District string `json:"district" validate:"omitempty,max=50" maxLength:"50" description:"区县"`
Address string `json:"address" validate:"omitempty,max=255" maxLength:"255" description:"详细地址"`
DefaultRoleID uint `json:"default_role_id" validate:"required,min=1" required:"true" minimum:"1" description:"店铺默认角色ID必须是客户角色"`
InitPassword string `json:"init_password" validate:"required,min=8,max=32" required:"true" minLength:"8" maxLength:"32" description:"初始账号密码"`
InitUsername string `json:"init_username" validate:"required,min=3,max=50" required:"true" minLength:"3" maxLength:"50" description:"初始账号用户名"`
InitPhone string `json:"init_phone" validate:"required,len=11" required:"true" minLength:"11" maxLength:"11" description:"初始账号手机号"`
}
type UpdateShopRequest struct {

View File

@@ -68,6 +68,18 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopRequest) (*dto.
return nil, errors.New(errors.CodePhoneExists, "初始账号手机号已存在")
}
// 验证默认角色:必须存在、是客户角色且已启用
defaultRole, err := s.roleStore.GetByID(ctx, req.DefaultRoleID)
if err != nil {
return nil, errors.New(errors.CodeNotFound, "默认角色不存在")
}
if defaultRole.RoleType != constants.RoleTypeCustomer {
return nil, errors.New(errors.CodeInvalidParam, "店铺默认角色必须是客户角色")
}
if defaultRole.Status != constants.StatusEnabled {
return nil, errors.New(errors.CodeInvalidParam, "默认角色已禁用")
}
shop := &model.Shop{
ShopName: req.ShopName,
ShopCode: req.ShopCode,
@@ -108,6 +120,18 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopRequest) (*dto.
return nil, errors.Wrap(errors.CodeInternalError, err, "创建初始账号失败")
}
// 设置店铺默认角色
shopRole := &model.ShopRole{
ShopID: shop.ID,
RoleID: req.DefaultRoleID,
Status: constants.StatusEnabled,
Creator: currentUserID,
Updater: currentUserID,
}
if err := s.shopRoleStore.BatchCreate(ctx, []*model.ShopRole{shopRole}); err != nil {
return nil, errors.Wrap(errors.CodeInternalError, err, "设置店铺默认角色失败")
}
return &dto.ShopResponse{
ID: shop.ID,
ShopName: shop.ShopName,