first commit
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// Orm 全局 ORM 对象,供业务层和控制器使用
|
||||
var Orm orm.Ormer
|
||||
|
||||
// Init 初始化模型层资源(数据库连接、模型注册等)。
|
||||
func Init(_ string) {
|
||||
// 从配置读取数据库连接信息
|
||||
user, _ := beego.AppConfig.String("mysqluser")
|
||||
pass, _ := beego.AppConfig.String("mysqlpass")
|
||||
urls, _ := beego.AppConfig.String("mysqlurls")
|
||||
dbname, _ := beego.AppConfig.String("mysqldb")
|
||||
|
||||
if user == "" || urls == "" || dbname == "" {
|
||||
panic("数据库配置(mysqluser/mysqlurls/mysqldb) 未正确设置")
|
||||
}
|
||||
|
||||
// 组装 DSN:user:pass@tcp(host:port)/dbname?charset=utf8mb4&parseTime=True&loc=Local
|
||||
dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, pass, urls, dbname)
|
||||
|
||||
// 注册默认数据库
|
||||
if err := orm.RegisterDataBase("default", "mysql", dsn); err != nil {
|
||||
panic("注册数据库失败: " + err.Error())
|
||||
}
|
||||
|
||||
// 注册模型
|
||||
orm.RegisterModel(
|
||||
new(SystemTenant),
|
||||
new(SystemTenantUser),
|
||||
new(BackendErpOrganization),
|
||||
new(BackendErpEmployee),
|
||||
new(BackendErpPosition),
|
||||
new(SystemMenu),
|
||||
new(AdminUser),
|
||||
new(AdminRole),
|
||||
new(SystemFile),
|
||||
new(SystemFilesCategory),
|
||||
new(SystemEmail),
|
||||
new(SystemSMS),
|
||||
new(SystemSMSTask),
|
||||
new(SystemOperationLog),
|
||||
new(SystemDomainPool),
|
||||
new(SystemTenantDomain),
|
||||
new(SystemModules),
|
||||
new(PlatformLoginVerify),
|
||||
new(StorageConfig),
|
||||
new(TenantSiteSetting),
|
||||
new(ComplaintCategory),
|
||||
new(PlatformComplaint),
|
||||
new(SystemSoftwareUpgrade),
|
||||
new(PlatformAccountPoolKiro),
|
||||
new(PlatformAccountPoolWindsurf),
|
||||
new(PlatformAccountPoolCursor),
|
||||
)
|
||||
|
||||
// 创建全局 Ormer
|
||||
Orm = orm.NewOrm()
|
||||
}
|
||||
Reference in New Issue
Block a user