2026-03-13 08:35:54 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
|
"myschools.me/heritage/heritage-api/model"
|
|
|
|
|
"myschools.me/heritage/heritage-api/mysql"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func Bootstrap() {
|
|
|
|
|
menuCount, err := mysql.MenuCount()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
|
|
|
|
}).Warnf("mysql.MenuCount: %v", err)
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
if *menuCount == 0 {
|
|
|
|
|
menus := []model.Menu{
|
|
|
|
|
{ID: 1, Name: "首页", Icon: "home", Path: "/dashboard", Sort: 1, PermissionCode: "dashboard"},
|
|
|
|
|
{ID: 2, Name: "系统管理", Icon: "setting", Path: "/system", Sort: 2, PermissionCode: "system"},
|
2026-03-18 09:18:06 +00:00
|
|
|
{ID: 3, Name: "组织机构", Icon: "team", Path: "/org", Sort: 1, PermissionCode: "org:list"},
|
|
|
|
|
{ID: 4, Name: "项目管理", Icon: "project", Path: "/project", Sort: 2, PermissionCode: "project:list"},
|
|
|
|
|
{ID: 5, Name: "任务管理", Icon: "profile", Path: "/task", Sort: 3, PermissionCode: "task:list"},
|
|
|
|
|
{ID: 6, Name: "监测点位", Icon: "environment", Path: "/point", Sort: 4, PermissionCode: "point:list"},
|
|
|
|
|
{ID: 7, Name: "数据记录", Icon: "database", Path: "/data", Sort: 5, PermissionCode: "data:list"},
|
|
|
|
|
{ID: 8, Name: "设备管理", Icon: "cluster", Path: "/device", Sort: 6, PermissionCode: "device:list"},
|
2026-03-13 08:35:54 +00:00
|
|
|
}
|
|
|
|
|
if err := mysql.MenuCreateBatch(menus); err != nil {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
|
|
|
|
}).Warnf("mysql.MenuCreateBatch: %v", err)
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
roleCount, err := mysql.RoleCount()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
|
|
|
|
}).Warnf("mysql.RoleCount: %v", err)
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var defaultRole *model.Role
|
|
|
|
|
if *roleCount == 0 {
|
|
|
|
|
defaultRole = &model.Role{
|
|
|
|
|
ID: newUserID(),
|
|
|
|
|
Code: "admin",
|
|
|
|
|
Name: "管理员",
|
|
|
|
|
}
|
|
|
|
|
if _, err := mysql.RoleCreate(defaultRole); err != nil {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
|
|
|
|
}).Warnf("mysql.RoleCreate: %v", err)
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
var found bool
|
|
|
|
|
defaultRole, found, err = mysql.RoleByCode("admin")
|
|
|
|
|
if err != nil || !found {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
|
|
|
|
}).Warnf("mysql.RoleByCode: %v", err)
|
|
|
|
|
panic("admin role not found")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
permissionCount, err := mysql.PermissionCount()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
|
|
|
|
}).Warnf("mysql.PermissionCount: %v", err)
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
if *permissionCount == 0 {
|
|
|
|
|
p := &model.Permission{
|
|
|
|
|
RoleID: defaultRole.ID,
|
|
|
|
|
Code: "*",
|
|
|
|
|
Name: "全部权限",
|
|
|
|
|
}
|
|
|
|
|
if _, err := mysql.PermissionCreate(p); err != nil {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
|
|
|
|
}).Warnf("mysql.PermissionCreate: %v", err)
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-19 09:35:11 +00:00
|
|
|
// 初始化管理机构
|
|
|
|
|
orgCount, err := mysql.OrgCount()
|
2026-03-13 08:35:54 +00:00
|
|
|
if err != nil {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
2026-03-19 09:35:11 +00:00
|
|
|
}).Warnf("mysql.OrgCount: %v", err)
|
2026-03-13 08:35:54 +00:00
|
|
|
panic(err)
|
|
|
|
|
}
|
2026-03-19 09:35:11 +00:00
|
|
|
if *orgCount == 0 {
|
|
|
|
|
orgid := newOrgID()
|
|
|
|
|
// 创建管理机构
|
|
|
|
|
adminOrg := &model.Org{
|
|
|
|
|
ID: orgid,
|
|
|
|
|
Code: "admin",
|
|
|
|
|
Name: "管理机构",
|
|
|
|
|
OrgType: "admin",
|
|
|
|
|
ParentID: "0",
|
|
|
|
|
}
|
|
|
|
|
if err := mysql.OrgCreate(adminOrg); err != nil {
|
2026-03-13 08:35:54 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
2026-03-19 09:35:11 +00:00
|
|
|
}).Warnf("mysql.OrgCreate: %v", err)
|
2026-03-13 08:35:54 +00:00
|
|
|
panic(err)
|
|
|
|
|
}
|
2026-03-19 09:35:11 +00:00
|
|
|
|
|
|
|
|
userCount, err := mysql.UserCount()
|
|
|
|
|
if err != nil {
|
2026-03-13 08:35:54 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
2026-03-19 09:35:11 +00:00
|
|
|
}).Warnf("mysql.UserCount: %v", err)
|
2026-03-13 08:35:54 +00:00
|
|
|
panic(err)
|
|
|
|
|
}
|
2026-03-19 09:35:11 +00:00
|
|
|
if *userCount == 0 {
|
|
|
|
|
defaultPwd := "admin"
|
|
|
|
|
h, err := bcrypt.GenerateFromPassword([]byte(defaultPwd), bcrypt.DefaultCost)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
|
|
|
|
}).Warnf("bcrypt.GenerateFromPassword: %v", err)
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
u := &model.User{
|
|
|
|
|
ID: newUserID(),
|
|
|
|
|
UserName: "admin",
|
|
|
|
|
PasswordHash: string(h),
|
|
|
|
|
RoleID: defaultRole.ID,
|
|
|
|
|
OrgID: orgid,
|
|
|
|
|
}
|
|
|
|
|
if _, err := mysql.UserCreate(u); err != nil {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
|
|
|
|
}).Warnf("mysql.UserCreate: %v", err)
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-13 08:35:54 +00:00
|
|
|
}
|
2026-03-18 09:18:06 +00:00
|
|
|
|
|
|
|
|
// 初始化组织选项
|
|
|
|
|
optionCount, err := mysql.OptionCount()
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
|
|
|
|
}).Warnf("mysql.OptionCount: %v", err)
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
if *optionCount == 0 {
|
|
|
|
|
orgOptions := []model.Option{
|
|
|
|
|
{OptionName: "level", OptionValue: "国家级"},
|
|
|
|
|
{OptionName: "level", OptionValue: "省级"},
|
|
|
|
|
{OptionName: "level", OptionValue: "市级"},
|
|
|
|
|
{OptionName: "type", OptionValue: "古建筑"},
|
|
|
|
|
{OptionName: "type", OptionValue: "古遗址"},
|
|
|
|
|
{OptionName: "type", OptionValue: "古墓葬"},
|
|
|
|
|
{OptionName: "batch", OptionValue: "第一批"},
|
|
|
|
|
{OptionName: "batch", OptionValue: "第二批"},
|
|
|
|
|
}
|
|
|
|
|
if err := mysql.OptionCreateBatch(orgOptions); err != nil {
|
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
|
"func": "Bootstrap",
|
|
|
|
|
}).Warnf("mysql.OptionCreateBatch: %v", err)
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-03-13 08:35:54 +00:00
|
|
|
}
|