mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
增加 自定义导航栏模块
This commit is contained in:
@@ -229,7 +229,7 @@ func initSQLite(sqliteConfig *appconfig.SQLiteConfig, logLevel string) error {
|
||||
sqlDB.SetMaxIdleConns(1)
|
||||
}
|
||||
dbInstance = db
|
||||
logrus.WithField("path", path).Info("SQLite 连接已建立")
|
||||
logrus.WithField("path", utils.DisplayPath(path)).Info("SQLite 连接已建立")
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,32 +1,33 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"NetworkAuth/models"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// AutoMigrate 自动迁移数据库模型
|
||||
// - 会确保必要的数据表结构存在
|
||||
// - 不会破坏已有数据
|
||||
func AutoMigrate() error {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.AutoMigrate(
|
||||
&models.Settings{},
|
||||
&models.OperationLog{},
|
||||
&models.LoginLog{},
|
||||
&models.User{},
|
||||
&models.App{},
|
||||
&models.API{},
|
||||
&models.Variable{},
|
||||
&models.Function{},
|
||||
); err != nil {
|
||||
logrus.WithError(err).Error("AutoMigrate 执行失败")
|
||||
return err
|
||||
}
|
||||
logrus.Info("AutoMigrate 执行完成")
|
||||
return nil
|
||||
}
|
||||
package database
|
||||
|
||||
import (
|
||||
"NetworkAuth/models"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// AutoMigrate 自动迁移数据库模型
|
||||
// - 会确保必要的数据表结构存在
|
||||
// - 不会破坏已有数据
|
||||
func AutoMigrate() error {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.AutoMigrate(
|
||||
&models.Settings{},
|
||||
&models.PortalNavigation{},
|
||||
&models.OperationLog{},
|
||||
&models.LoginLog{},
|
||||
&models.User{},
|
||||
&models.App{},
|
||||
&models.API{},
|
||||
&models.Variable{},
|
||||
&models.Function{},
|
||||
); err != nil {
|
||||
logrus.WithError(err).Error("AutoMigrate 执行失败")
|
||||
return err
|
||||
}
|
||||
logrus.Info("AutoMigrate 执行完成")
|
||||
return nil
|
||||
}
|
||||
|
||||
70
database/portal_navigation.go
Normal file
70
database/portal_navigation.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"NetworkAuth/models"
|
||||
"errors"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// SeedDefaultPortalNavigation 初始化默认门户导航
|
||||
// 当系统首次安装或升级后缺少默认入口时,自动补充首页和管理员登录入口
|
||||
func SeedDefaultPortalNavigation() error {
|
||||
db, err := GetDB()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defaultItems := []models.PortalNavigation{
|
||||
{
|
||||
Name: "首页",
|
||||
Path: "/home/index",
|
||||
Sort: 0,
|
||||
IsHome: true,
|
||||
IsHidden: false,
|
||||
IsExternal: false,
|
||||
},
|
||||
{
|
||||
Name: "管理员登录",
|
||||
Path: "admin",
|
||||
Sort: 999,
|
||||
IsHome: false,
|
||||
IsHidden: false,
|
||||
IsExternal: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, item := range defaultItems {
|
||||
var exists models.PortalNavigation
|
||||
if err := db.Where("path = ?", item.Path).First(&exists).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return err
|
||||
}
|
||||
if err := db.Create(&item).Error; err != nil {
|
||||
logrus.WithError(err).WithField("path", item.Path).Error("创建默认门户导航失败")
|
||||
return err
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
switch exists.Path == "admin" {
|
||||
case true:
|
||||
if err := db.Model(&models.PortalNavigation{}).Where("id = ?", exists.ID).Updates(map[string]interface{}{
|
||||
"name": "管理员登录",
|
||||
"path": "admin",
|
||||
"sort": 999,
|
||||
"is_home": false,
|
||||
"is_external": false,
|
||||
}).Error; err != nil {
|
||||
logrus.WithError(err).WithField("path", item.Path).Error("更新默认门户导航失败")
|
||||
return err
|
||||
}
|
||||
default:
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
logrus.Info("默认门户导航初始化完成")
|
||||
return nil
|
||||
}
|
||||
@@ -51,11 +51,6 @@ func SeedDefaultSettings() error {
|
||||
Value: "0",
|
||||
Description: "维护模式,0=关闭维护模式,1=开启维护模式",
|
||||
},
|
||||
{
|
||||
Name: "hide_login_entrance",
|
||||
Value: "0",
|
||||
Description: "隐藏登录入口,0=显示,1=隐藏(门户中不显示管理员或子账号登录入口)",
|
||||
},
|
||||
{
|
||||
Name: "encryption_key",
|
||||
Value: encryptionKey,
|
||||
@@ -329,6 +324,11 @@ func SeedDefaultSettings() error {
|
||||
}
|
||||
}
|
||||
|
||||
// 移除已废弃的旧设置项,管理员登录入口改由门户导航控制
|
||||
if err := db.Where("name = ?", "hide_login_entrance").Delete(&models.Settings{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logrus.Info("系统设置初始化完成")
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user