From c1eec5d4f151990f8ba5509906cabf7f753ba99d Mon Sep 17 00:00:00 2001 From: huang Date: Wed, 25 Feb 2026 16:47:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=B0=E5=A2=9E=E5=BA=97=E9=93=BA?= =?UTF-8?q?=E6=97=B6=E4=B8=BA=E5=88=9D=E5=A7=8B=E8=B4=A6=E5=8F=B7=E5=88=86?= =?UTF-8?q?=E9=85=8D=E9=BB=98=E8=AE=A4=E8=A7=92=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:创建店铺时只创建了 shop_roles 记录(店铺可用角色), 但没有创建 account_roles 记录,导致初始账号没有任何权限。 修复:在创建初始账号后,立即为其分配默认角色到 account_roles 表。 Co-Authored-By: Claude Opus 4.5 --- internal/bootstrap/services.go | 2 +- internal/service/shop/service.go | 31 +++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/internal/bootstrap/services.go b/internal/bootstrap/services.go index 6387964..59269f3 100644 --- a/internal/bootstrap/services.go +++ b/internal/bootstrap/services.go @@ -96,7 +96,7 @@ func initServices(s *stores, deps *Dependencies) *services { Role: roleSvc.New(s.Role, s.Permission, s.RolePermission), Permission: permissionSvc.New(s.Permission, s.AccountRole, s.RolePermission, account, deps.Redis), PersonalCustomer: personalCustomerSvc.NewService(s.PersonalCustomer, s.PersonalCustomerPhone, deps.VerificationService, deps.JWTManager, deps.WechatOfficialAccount, deps.Logger), - Shop: shopSvc.New(s.Shop, s.Account, s.ShopRole, s.Role), + Shop: shopSvc.New(s.Shop, s.Account, s.ShopRole, s.Role, s.AccountRole), Auth: authSvc.New(s.Account, s.AccountRole, s.RolePermission, s.Permission, s.Shop, deps.TokenManager, deps.Logger), ShopCommission: shopCommissionSvc.New(s.Shop, s.Account, s.AgentWallet, s.CommissionWithdrawalRequest, s.CommissionRecord), CommissionWithdrawal: commissionWithdrawalSvc.New(deps.DB, s.Shop, s.Account, s.AgentWallet, s.AgentWalletTransaction, s.CommissionWithdrawalRequest), diff --git a/internal/service/shop/service.go b/internal/service/shop/service.go index 4e51045..1c31a7c 100644 --- a/internal/service/shop/service.go +++ b/internal/service/shop/service.go @@ -15,10 +15,11 @@ import ( ) type Service struct { - shopStore *postgres.ShopStore - accountStore *postgres.AccountStore - shopRoleStore *postgres.ShopRoleStore - roleStore *postgres.RoleStore + shopStore *postgres.ShopStore + accountStore *postgres.AccountStore + shopRoleStore *postgres.ShopRoleStore + roleStore *postgres.RoleStore + accountRoleStore *postgres.AccountRoleStore } func New( @@ -26,12 +27,14 @@ func New( accountStore *postgres.AccountStore, shopRoleStore *postgres.ShopRoleStore, roleStore *postgres.RoleStore, + accountRoleStore *postgres.AccountRoleStore, ) *Service { return &Service{ - shopStore: shopStore, - accountStore: accountStore, - shopRoleStore: shopRoleStore, - roleStore: roleStore, + shopStore: shopStore, + accountStore: accountStore, + shopRoleStore: shopRoleStore, + roleStore: roleStore, + accountRoleStore: accountRoleStore, } } @@ -120,6 +123,18 @@ func (s *Service) Create(ctx context.Context, req *dto.CreateShopRequest) (*dto. return nil, errors.Wrap(errors.CodeInternalError, err, "创建初始账号失败") } + // 为初始账号分配默认角色 + accountRole := &model.AccountRole{ + AccountID: account.ID, + RoleID: req.DefaultRoleID, + Status: constants.StatusEnabled, + Creator: currentUserID, + Updater: currentUserID, + } + if err := s.accountRoleStore.Create(ctx, accountRole); err != nil { + return nil, errors.Wrap(errors.CodeInternalError, err, "为初始账号分配角色失败") + } + // 设置店铺默认角色 shopRole := &model.ShopRole{ ShopID: shop.ID,