修复 导航栏重复初始化的问题

This commit is contained in:
2026-04-17 04:54:54 +08:00
parent ec508e6a32
commit 6d6b1c00c4
6 changed files with 150 additions and 14 deletions

View File

@@ -8,6 +8,43 @@ import (
"gorm.io/gorm"
)
// NeedSeedDefaultPortalNavigation 判断是否需要修复默认门户导航。
// 仅在门户导航表缺失、关键字段缺失、没有任何数据或存在旧版脏数据时返回 true。
func NeedSeedDefaultPortalNavigation() (bool, error) {
db, err := GetDB()
if err != nil {
return false, err
}
if !db.Migrator().HasTable(&models.PortalNavigation{}) {
return true, nil
}
requiredColumns := []string{"type", "parent_id", "is_home", "is_hidden", "is_external"}
for _, column := range requiredColumns {
if !db.Migrator().HasColumn(&models.PortalNavigation{}, column) {
return true, nil
}
}
var count int64
if err := db.Model(&models.PortalNavigation{}).Count(&count).Error; err != nil {
return false, err
}
if count == 0 {
return true, nil
}
if err := db.Model(&models.PortalNavigation{}).Where("type = '' OR type IS NULL").Count(&count).Error; err != nil {
return false, err
}
if count > 0 {
return true, nil
}
return false, nil
}
// SeedDefaultPortalNavigation 初始化默认门户导航
// 当系统首次安装或升级后缺少默认入口时,自动补充首页和管理员登录入口
func SeedDefaultPortalNavigation() error {
@@ -19,6 +56,8 @@ func SeedDefaultPortalNavigation() error {
defaultItems := []models.PortalNavigation{
{
Name: "首页",
Type: "link",
ParentID: 0,
Path: "/home/index",
Sort: 0,
IsHome: true,
@@ -27,6 +66,8 @@ func SeedDefaultPortalNavigation() error {
},
{
Name: "管理员登录",
Type: "link",
ParentID: 0,
Path: "admin",
Sort: 999,
IsHome: false,
@@ -52,6 +93,8 @@ func SeedDefaultPortalNavigation() error {
case true:
if err := db.Model(&models.PortalNavigation{}).Where("id = ?", exists.ID).Updates(map[string]interface{}{
"name": "管理员登录",
"type": "link",
"parent_id": 0,
"path": "admin",
"sort": 999,
"is_home": false,
@@ -65,6 +108,13 @@ func SeedDefaultPortalNavigation() error {
}
}
if err := db.Model(&models.PortalNavigation{}).Where("type = '' OR type IS NULL").Updates(map[string]interface{}{
"type": "link",
"parent_id": 0,
}).Error; err != nil {
return err
}
logrus.Info("默认门户导航初始化完成")
return nil
}