feat: 完善平台总后台模块
This commit is contained in:
@@ -6,6 +6,9 @@ import "gorm.io/gorm"
|
||||
// New 在同一事务中初始化平台基础数据。
|
||||
func New(database *gorm.DB) error {
|
||||
return database.Transaction(func(tx *gorm.DB) error {
|
||||
if err := InitPlatformAccess(tx); err != nil {
|
||||
return err
|
||||
}
|
||||
return InitPlatformRoot(tx)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -15,12 +15,50 @@ const (
|
||||
// PlatformRootPassword 是仅用于首次启动的初始密码,首次登录后必须修改。
|
||||
PlatformRootPassword = "Heqi@Root2026"
|
||||
// PlatformRootRoleCode 表示根账号的平台角色。
|
||||
PlatformRootRoleCode = "platform_root"
|
||||
PlatformRootRoleCode = "root"
|
||||
)
|
||||
|
||||
// InitPlatformAccess 幂等初始化 root 角色、菜单和 root 的全菜单授权。
|
||||
func InitPlatformAccess(database *gorm.DB) error {
|
||||
rootRole := models.PlatformRole{
|
||||
Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", 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: "ec", Name: "电商管理", Icon: "icon-shopping", Path: "/ec/product", SortNo: 60},
|
||||
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "finance", Name: "财务管理", Icon: "icon-safe", Path: "/finance/payment", SortNo: 70},
|
||||
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "wallet", Name: "钱包中心", Icon: "icon-wallet", Path: "/wallet/list", SortNo: 80},
|
||||
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "report", Name: "统计报表", Icon: "icon-bar-chart", Path: "/report/list", SortNo: 90},
|
||||
{Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled", Version: 1}, MenuCode: "platform", Name: "平台配置", Icon: "icon-settings", Path: "/platform/account", SortNo: 100},
|
||||
}
|
||||
for index := range menus {
|
||||
menu := menus[index]
|
||||
if err := database.Where("menu_code = ?", menu.MenuCode).FirstOrCreate(&menu).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
relation := models.PlatformRoleMenuRelation{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
|
||||
}
|
||||
|
||||
// InitPlatformRoot 幂等创建平台总后台 root 账号。
|
||||
func InitPlatformRoot(database *gorm.DB) error {
|
||||
var account models.IdnAccount
|
||||
var account models.PlatfromAccount
|
||||
err := database.Where("username = ?", PlatformRootUsername).First(&account).Error
|
||||
if err == nil {
|
||||
return nil
|
||||
@@ -34,16 +72,13 @@ func InitPlatformRoot(database *gorm.DB) error {
|
||||
return err
|
||||
}
|
||||
|
||||
account = models.IdnAccount{
|
||||
Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled"},
|
||||
Username: PlatformRootUsername,
|
||||
DisplayName: "平台根管理员",
|
||||
PasswordHash: string(passwordHash),
|
||||
RoleCode: PlatformRootRoleCode,
|
||||
MustChangePassword: true,
|
||||
Phone: "",
|
||||
AccountType: "operator",
|
||||
ServiceArea: "全国",
|
||||
account = models.PlatfromAccount{
|
||||
Entity: models.Entity{Identity: models.NewIdentity(), Status: "enabled"},
|
||||
Username: PlatformRootUsername,
|
||||
DisplayName: "平台根管理员",
|
||||
PasswordHash: string(passwordHash),
|
||||
PlatformRoleCode: PlatformRootRoleCode,
|
||||
Phone: "",
|
||||
}
|
||||
return database.Create(&account).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user