You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
6.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
"github.com/han-joker/moo-layout/api/moo/confm"
"github.com/han-joker/moo-layout/api/moo/toolm"
"github.com/han-joker/moo-layout/api/tables"
"log"
"time"
)
import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func main() {
// connect
db, err := gorm.Open(sqlite.Open(confm.Get().String("sqlite.DSN")), &gorm.Config{})
if err != nil {
log.Fatal(err)
}
// all tables
tbs := []interface{}{
&tables.User{},
&tables.Role{},
&tables.Category{},
&tables.Content{},
}
// drop tables
if err := db.Migrator().DropTable(tbs...); err != nil {
log.Fatal(err)
}
// create tables
if err := db.AutoMigrate(tbs...); err != nil {
log.Fatal(err)
}
// basic seed
// role
root := tables.Role{Name: "根", Key: "root"}
if result := db.Create(&root); result.Error != nil {
log.Fatal(result.Error)
}
// user
users := []tables.User{
{Username: "admin", Password: "", PasswordSalt: "", Name: "Admin", Roles: []tables.Role{root}},
}
for i, _ := range users {
users[i].PasswordSalt = toolm.RandString(6, toolm.CharsAll)
users[i].Password = toolm.Sha256HMacString("admin", users[i].PasswordSalt)
}
if result := db.Create(&users); result.Error != nil {
log.Fatal(result.Error)
}
// category
cats := []tables.Category{
{Model: tables.Model{ID: 1}, Name: "未分类", Status: tables.CategoryStatusPublish},
{Model: tables.Model{ID: 2}, Name: "新闻公告", Status: tables.CategoryStatusPublish},
{Model: tables.Model{ID: 3}, Name: "游戏介绍", Status: tables.CategoryStatusPublish},
{Model: tables.Model{ID: 4}, Name: "游戏特色", Status: tables.CategoryStatusPublish},
{Model: tables.Model{ID: 5}, Name: "特定内容", Status: tables.CategoryStatusPublish},
{Model: tables.Model{ID: 6}, Name: "怪物介绍", Status: tables.CategoryStatusPublish, ParentID: 3},
{Model: tables.Model{ID: 7}, Name: "NPC介绍", Status: tables.CategoryStatusPublish, ParentID: 3},
{Model: tables.Model{ID: 8}, Name: "武功介绍", Status: tables.CategoryStatusPublish, ParentID: 3},
{Model: tables.Model{ID: 9}, Name: "系统介绍", Status: tables.CategoryStatusPublish, ParentID: 3},
{Model: tables.Model{ID: 10}, Name: "装备预览", Status: tables.CategoryStatusPublish, ParentID: 3},
{Model: tables.Model{ID: 11}, Name: "武功", Status: tables.CategoryStatusPublish, ParentID: 4},
{Model: tables.Model{ID: 12}, Name: "离线挂机", Status: tables.CategoryStatusPublish, ParentID: 4},
{Model: tables.Model{ID: 13}, Name: "境界", Status: tables.CategoryStatusPublish, ParentID: 4},
{Model: tables.Model{ID: 14}, Name: "副本", Status: tables.CategoryStatusPublish, ParentID: 4},
}
if result := db.Create(&cats); result.Error != nil {
log.Fatal(result.Error)
}
contents := []tables.Content{
{Subject: "家长监护工程", Content: "<p>家长监护工程(请自行编辑)</p>", CategoryID: 5, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "联系我们", Content: "<p>联系我们(请自行编辑)</p>", CategoryID: 5, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "商务合作", Content: "<p>商务合作(请自行编辑)</p>", CategoryID: 5, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "用户纠纷处理", Content: "<p>用户纠纷处理(请自行编辑)</p>", CategoryID: 5, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "用户协议", Content: "<p>用户协议(请自行编辑)</p>", CategoryID: 5, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "QRCode", Content: "<p>QRCode请自行编辑</p>", Cover: "contents/d45d56cdc40ff9ad6245b6a30b5c10a9.png", CategoryID: 5, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "App Store 下载", Content: "<p>App Store 下载(请自行编辑)</p>", Cover: "contents/6ac066a9023d893deb030d8d04faaf4d.png", CategoryID: 5, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "安卓下载", Content: "<p>安卓下载(请自行编辑)</p>", Cover: "contents/1d6e08823657c551a66bb25693e69678.png", CategoryID: 5, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "Logo", Content: "<p>Logo请自行编辑</p>", Cover: "contents/fbc658d2b9c8209a0eaa9b9721e426c2.png", CategoryID: 5, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "底部文字", Content: "健康游戏忠告:抵制不良游戏,拒绝盗版游戏。注意自我保护,谨防受骗上当。适度游戏益脑,沉迷游戏伤身。合理安排时间,享受健康生活", CategoryID: 5, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "Video Logo", Content: "<p>请自行编辑</p>", Cover: "contents/41af642d078b5cd5049f8eb4ee2472ed.png", CategoryID: 5, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "维护公告", Content: "<p>测试内容1</p>", Cover: "contents/e842edc945d88a4f3fa49a39a22c8920.png", CategoryID: 2, Status: tables.ContentStatusPublish, PublishTime: time.Now(), Promotes: tables.ContentPromoteIndexImage | tables.ContentPromoteLatest},
{Subject: "攻击类", Content: "<p>测试内容2</p>", Cover: "contents/25510c2d7e8c3dcc8c4e17b5787e1765.png", CategoryID: 11, Status: tables.ContentStatusPublish, PublishTime: time.Now(), Promotes: tables.ContentPromoteIndexImage},
{Subject: "步伐类", Content: "<p>测试内容3</p>", Cover: "contents/ca2d7c6650b11683d5af710583c5dbf0.png", CategoryID: 11, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "境界系统", Content: "<p>测试内容4</p>", Cover: "contents/9b17f01564d8a207de42e114aa5c9b2e.png", CategoryID: 13, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
{Subject: "出入境", Content: "<p>测试内容5</p>", Cover: "contents/cac4665a7441c3ceeea3b19bdd3d0ffd.png", CategoryID: 13, Status: tables.ContentStatusPublish, PublishTime: time.Now()},
}
if result := db.Create(&contents); result.Error != nil {
log.Fatal(result.Error)
}
log.Println("Migrated")
}