refactor platform menus and entity statuses

This commit is contained in:
david
2026-07-29 13:18:52 +08:00
parent 978dc446d9
commit afd9dbdeb5
48 changed files with 510 additions and 421 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors"
"os"
"git.apinb.com/heqiapp/platforms/backend/api/internal/logic/common"
"git.apinb.com/heqiapp/platforms/backend/api/internal/models"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
@@ -18,44 +19,16 @@ const (
PlatformRootRoleCode = "root"
)
// InitPlatformAccess 幂等初始化 root 角色菜单和 root 的全菜单授权
// InitPlatformAccess 幂等初始化 root 角色菜单定义位于逻辑层静态数据中
func InitPlatformAccess(database *gorm.DB) error {
rootRole := models.PlatformRole{
Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1},
Entity: models.Entity{Identity: models.NewIdentity(), Status: common.StatusEnable, Version: 1},
RoleCode: PlatformRootRoleCode,
Name: "系统管理员",
DataScope: "global",
IsSystem: true,
}
if err := database.Where("role_code = ?", rootRole.RoleCode).FirstOrCreate(&rootRole).Error; err != nil {
return err
}
menus := []models.PlatformMenu{
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "dashboard", Name: "工作台", Icon: "icon-dashboard", Path: "/dashboard", SortNo: 10},
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "gas", Name: "可燃气体站管理", Icon: "icon-fire", Path: "/gas/basic", SortNo: 20},
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "delivery", Name: "配送管理", Icon: "icon-car", Path: "/delivery/basic", SortNo: 30},
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "staff", Name: "服务人员", Icon: "icon-user", Path: "/staff/list", SortNo: 40},
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "user", Name: "业主客户", Icon: "icon-user-group", Path: "/user/list", SortNo: 50},
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "device", Name: "设备管理", Icon: "icon-storage", Path: "/device", SortNo: 60},
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "ec", Name: "电商管理", Icon: "icon-shopping", Path: "/ec/product", SortNo: 80},
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "finance", Name: "财务管理", Icon: "icon-safe", Path: "/finance/payment", SortNo: 90},
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "wallet", Name: "钱包中心", Icon: "icon-wallet", Path: "/wallet/list", SortNo: 100},
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "content", Name: "内容管理", Icon: "icon-file", Path: "/content", SortNo: 120},
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "customer_service", Name: "客户服务", Icon: "icon-customer-service", Path: "/customer_service", SortNo: 140},
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "platform", Name: "平台配置", Icon: "icon-settings", Path: "/platform/account", SortNo: 160},
}
for index := range menus {
menu := menus[index]
if err := database.Where("menu_code = ?", menu.MenuCode).FirstOrCreate(&menu).Error; err != nil {
return err
}
relation := models.PlatformRoleMenu{PlatformRoleID: rootRole.ID, PlatformMenuID: menu.ID}
if err := database.Where("platform_role_id = ? AND platform_menu_id = ?", rootRole.ID, menu.ID).FirstOrCreate(&relation).Error; err != nil {
return err
}
}
return nil
return database.Where("role_code = ?", rootRole.RoleCode).FirstOrCreate(&rootRole).Error
}
// InitPlatformRoot 幂等创建平台总后台 root 账号。
@@ -75,7 +48,7 @@ func InitPlatformRoot(database *gorm.DB) error {
}
account = models.PlatformAccount{
Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled"},
Entity: models.Entity{Identity: models.NewIdentity(), Status: common.StatusEnable},
Username: PlatformRootUsername,
DisplayName: "平台根管理员",
PasswordHash: string(passwordHash),

View File

@@ -9,7 +9,7 @@ import (
"gorm.io/gorm"
)
func TestInitPlatformAccessSeedsEveryProtectedFrontendDomain(t *testing.T) {
func TestInitPlatformAccessSeedsRootRole(t *testing.T) {
sqlDatabase, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
@@ -23,23 +23,7 @@ func TestInitPlatformAccessSeedsEveryProtectedFrontendDomain(t *testing.T) {
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "platform_role" WHERE role_code = $1 ORDER BY "platform_role"."id" LIMIT $2`)).
WithArgs("root", 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "identity", "status", "version", "role_code", "name", "data_scope", "is_system"}).
AddRow(uint64(1), "root-role", "enabled", 1, "root", "Root", "global", true))
domains := []string{
"dashboard", "gas", "delivery", "staff", "user", "device",
"ec", "finance", "wallet", "content", "customer_service", "platform",
}
for index, domain := range domains {
menuID := uint64(index + 10)
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "platform_menu" WHERE menu_code = $1 ORDER BY "platform_menu"."id" LIMIT $2`)).
WithArgs(domain, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "identity", "status", "version", "parent_id", "menu_code", "name", "icon", "path", "sort_no"}).
AddRow(menuID, domain+"-menu", "enabled", 1, uint64(0), domain, domain, "", "/"+domain, index))
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "platform_role_menu" WHERE platform_role_id = $1 AND platform_menu_id = $2 ORDER BY "platform_role_menu"."id" LIMIT $3`)).
WithArgs(uint64(1), menuID, 1).
WillReturnRows(sqlmock.NewRows([]string{"id", "platform_role_id", "platform_menu_id"}).
AddRow(uint64(index+100), uint64(1), menuID))
}
AddRow(uint64(1), "root-role", 1, 1, "root", "Root", "global", true))
if err := InitPlatformAccess(database); err != nil {
t.Fatal(err)