初始化v1
This commit is contained in:
131
internal/cmd/cmd.go
Normal file
131
internal/cmd/cmd.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gcmd"
|
||||
|
||||
"nl-video-api/internal/controller/admin"
|
||||
"nl-video-api/internal/controller/user"
|
||||
"nl-video-api/api/middleware"
|
||||
"nl-video-api/api/v1"
|
||||
)
|
||||
|
||||
var (
|
||||
Main = gcmd.Command{
|
||||
Name: "main",
|
||||
Usage: "main",
|
||||
Brief: "start http server",
|
||||
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
||||
// 初始化日志配置
|
||||
g.Log().Info(ctx, "开始初始化日志系统...")
|
||||
|
||||
s := g.Server()
|
||||
|
||||
// 设置静态文件服务
|
||||
s.SetServerRoot("resource/public")
|
||||
s.AddStaticPath("/uploads", "resource/public/uploads")
|
||||
|
||||
// 记录服务器启动日志
|
||||
g.Log().Info(ctx, "服务器配置初始化完成")
|
||||
|
||||
// 注册中间件
|
||||
s.Use(middleware.CORS)
|
||||
s.Use(middleware.ErrorHandler) // 错误处理中间件
|
||||
s.Use(middleware.ResponseHandler) // 响应处理中间件
|
||||
|
||||
// 注册路由
|
||||
s.Group("/api/v1", func(group *ghttp.RouterGroup) {
|
||||
// === 用户端路由 ===
|
||||
|
||||
// 用户认证路由(无需认证)
|
||||
group.Group("/auth", func(authGroup *ghttp.RouterGroup) {
|
||||
v1.AuthGroup(authGroup)
|
||||
})
|
||||
|
||||
// 影片管理路由
|
||||
v1.MovieGroup(group)
|
||||
|
||||
// 用户收藏路由
|
||||
v1.UserCollectGroup(group)
|
||||
|
||||
// 用户观看历史路由
|
||||
v1.UserWatchHistoryGroup(group)
|
||||
|
||||
// 轮播图路由
|
||||
group.Group("/banner", func(bannerGroup *ghttp.RouterGroup) {
|
||||
bannerGroup.GET("/list", user.Banner.GetList)
|
||||
})
|
||||
|
||||
// 支付订单路由
|
||||
group.Group("/payment", func(paymentGroup *ghttp.RouterGroup) {
|
||||
paymentGroup.POST("/create", user.PaymentOrder.Create)
|
||||
paymentGroup.GET("/list", user.PaymentOrder.GetList)
|
||||
paymentGroup.GET("/{id}", user.PaymentOrder.GetDetail)
|
||||
})
|
||||
|
||||
// VIP等级路由
|
||||
group.Group("/vip", func(vipGroup *ghttp.RouterGroup) {
|
||||
vipGroup.GET("/levels", user.VipLevel.GetList)
|
||||
vipGroup.GET("/my", user.VipLevel.GetDetail)
|
||||
})
|
||||
|
||||
// 附件管理路由
|
||||
group.Group("/attachment", func(attachmentGroup *ghttp.RouterGroup) {
|
||||
attachmentGroup.POST("/upload", user.NewAttachmentController().Upload)
|
||||
attachmentGroup.GET("/list", user.NewAttachmentController().GetList)
|
||||
})
|
||||
|
||||
// 系统配置路由
|
||||
group.Group("/config", func(configGroup *ghttp.RouterGroup) {
|
||||
configGroup.GET("/public", user.Config.GetList)
|
||||
})
|
||||
|
||||
// 日志管理路由
|
||||
group.Group("/log", func(logGroup *ghttp.RouterGroup) {
|
||||
logGroup.GET("/my", user.Log.GetList)
|
||||
})
|
||||
|
||||
// === 管理员端路由 ===
|
||||
|
||||
// 管理员认证路由(无需认证)
|
||||
group.Group("/admin", func(adminGroup *ghttp.RouterGroup) {
|
||||
// 管理员基础认证接口
|
||||
adminController := &admin.AdminController{}
|
||||
adminGroup.POST("/login", adminController.Login)
|
||||
adminGroup.POST("/register", adminController.Register)
|
||||
adminGroup.GET("/profile", adminController.Profile)
|
||||
adminGroup.POST("/profile", adminController.UpdateProfile)
|
||||
adminGroup.POST("/logout", adminController.Logout)
|
||||
adminGroup.POST("/refresh", adminController.RefreshToken)
|
||||
|
||||
// 管理员业务功能路由
|
||||
v1.AdminGroup(adminGroup)
|
||||
})
|
||||
})
|
||||
|
||||
s.Run()
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// InitDB 数据库初始化命令
|
||||
InitDB = gcmd.Command{
|
||||
Name: "init-db",
|
||||
Usage: "init-db",
|
||||
Brief: "initialize database",
|
||||
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
|
||||
g.Log().Info(ctx, "开始初始化数据库...")
|
||||
|
||||
if err := InitDatabase(ctx); err != nil {
|
||||
g.Log().Errorf(ctx, "数据库初始化失败: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
g.Log().Info(ctx, "数据库初始化完成!")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
)
|
||||
254
internal/cmd/init_db.go
Normal file
254
internal/cmd/init_db.go
Normal file
@@ -0,0 +1,254 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// InitDatabase 初始化数据库
|
||||
func InitDatabase(ctx context.Context) error {
|
||||
db := g.DB()
|
||||
|
||||
// 检查数据库连接
|
||||
if err := db.PingMaster(); err != nil {
|
||||
return fmt.Errorf("数据库连接失败: %v", err)
|
||||
}
|
||||
|
||||
g.Log().Info(ctx, "数据库连接成功,开始初始化数据库...")
|
||||
|
||||
// 读取SQL文件
|
||||
sqlFile := "nl_video_database.sql"
|
||||
if !gfile.Exists(sqlFile) {
|
||||
return fmt.Errorf("SQL文件不存在: %s", sqlFile)
|
||||
}
|
||||
|
||||
sqlContent, err := ioutil.ReadFile(sqlFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("读取SQL文件失败: %v", err)
|
||||
}
|
||||
|
||||
// 分割SQL语句
|
||||
sqlStatements := strings.Split(string(sqlContent), ";")
|
||||
|
||||
// 执行SQL语句
|
||||
for i, statement := range sqlStatements {
|
||||
statement = strings.TrimSpace(statement)
|
||||
if statement == "" || strings.HasPrefix(statement, "--") || strings.HasPrefix(statement, "/*") {
|
||||
continue
|
||||
}
|
||||
|
||||
g.Log().Infof(ctx, "执行SQL语句 %d/%d", i+1, len(sqlStatements))
|
||||
|
||||
if _, err := db.Exec(ctx, statement); err != nil {
|
||||
// 如果是表已存在的错误,跳过
|
||||
if strings.Contains(err.Error(), "already exists") {
|
||||
g.Log().Warning(ctx, "表已存在,跳过创建:", statement[:50])
|
||||
continue
|
||||
}
|
||||
return fmt.Errorf("执行SQL失败: %v, SQL: %s", err, statement[:100])
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化基础数据
|
||||
if err := initBaseData(ctx); err != nil {
|
||||
return fmt.Errorf("初始化基础数据失败: %v", err)
|
||||
}
|
||||
|
||||
g.Log().Info(ctx, "数据库初始化完成!")
|
||||
return nil
|
||||
}
|
||||
|
||||
// initBaseData 初始化基础数据
|
||||
func initBaseData(ctx context.Context) error {
|
||||
db := g.DB()
|
||||
|
||||
// 检查是否已有管理员数据
|
||||
count, err := db.Model("nl_admin").Count()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
g.Log().Info(ctx, "管理员数据已存在,跳过初始化")
|
||||
return nil
|
||||
}
|
||||
|
||||
// 创建默认超级管理员
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte("admin123"), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 插入超级管理员角色
|
||||
roleResult, err := db.Model("nl_role").Data(g.Map{
|
||||
"name": "超级管理员",
|
||||
"code": "super_admin",
|
||||
"level": 1,
|
||||
"is_system": 1,
|
||||
"description": "系统超级管理员,拥有所有权限",
|
||||
"status": 1,
|
||||
"created_at": gtime.Now(),
|
||||
"updated_at": gtime.Now(),
|
||||
}).Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
roleId, err := roleResult.LastInsertId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 插入超级管理员账号
|
||||
_, err = db.Model("nl_admin").Data(g.Map{
|
||||
"username": "admin",
|
||||
"password": string(hashedPassword),
|
||||
"email": "admin@nlvideo.com",
|
||||
"real_name": "系统管理员",
|
||||
"nickname": "超级管理员",
|
||||
"role_id": roleId,
|
||||
"status": 1,
|
||||
"created_at": gtime.Now(),
|
||||
"updated_at": gtime.Now(),
|
||||
}).Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 初始化基础权限
|
||||
if err := initPermissions(ctx, roleId); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 初始化影片分类
|
||||
if err := initCategories(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 初始化VIP等级
|
||||
if err := initVipLevels(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.Log().Info(ctx, "基础数据初始化完成")
|
||||
g.Log().Info(ctx, "默认管理员账号: admin")
|
||||
g.Log().Info(ctx, "默认管理员密码: admin123")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// initPermissions 初始化权限数据
|
||||
func initPermissions(ctx context.Context, roleId int64) error {
|
||||
db := g.DB()
|
||||
|
||||
permissions := []g.Map{
|
||||
{
|
||||
"name": "系统管理", "code": "system", "type": "menu", "parent_id": 0,
|
||||
"path": "/system", "component": "Layout", "icon": "system",
|
||||
"sort": 1, "status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now(),
|
||||
},
|
||||
{
|
||||
"name": "用户管理", "code": "user", "type": "menu", "parent_id": 0,
|
||||
"path": "/user", "component": "Layout", "icon": "user",
|
||||
"sort": 2, "status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now(),
|
||||
},
|
||||
{
|
||||
"name": "影片管理", "code": "movie", "type": "menu", "parent_id": 0,
|
||||
"path": "/movie", "component": "Layout", "icon": "movie",
|
||||
"sort": 3, "status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now(),
|
||||
},
|
||||
{
|
||||
"name": "权限管理", "code": "permission", "type": "menu", "parent_id": 0,
|
||||
"path": "/permission", "component": "Layout", "icon": "permission",
|
||||
"sort": 4, "status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now(),
|
||||
},
|
||||
}
|
||||
|
||||
for _, perm := range permissions {
|
||||
result, err := db.Model("nl_permission").Data(perm).Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
permId, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 给超级管理员分配权限
|
||||
_, err = db.Model("nl_role_permission").Data(g.Map{
|
||||
"role_id": roleId,
|
||||
"permission_id": permId,
|
||||
"created_at": gtime.Now(),
|
||||
}).Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// initCategories 初始化影片分类
|
||||
func initCategories(ctx context.Context) error {
|
||||
db := g.DB()
|
||||
|
||||
categories := []g.Map{
|
||||
{"name": "电影", "code": "movie", "parent_id": 0, "sort": 1, "status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now()},
|
||||
{"name": "电视剧", "code": "tv", "parent_id": 0, "sort": 2, "status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now()},
|
||||
{"name": "综艺", "code": "variety", "parent_id": 0, "sort": 3, "status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now()},
|
||||
{"name": "动漫", "code": "anime", "parent_id": 0, "sort": 4, "status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now()},
|
||||
{"name": "纪录片", "code": "documentary", "parent_id": 0, "sort": 5, "status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now()},
|
||||
}
|
||||
|
||||
for _, category := range categories {
|
||||
_, err := db.Model("nl_category").Data(category).Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// initVipLevels 初始化VIP等级
|
||||
func initVipLevels(ctx context.Context) error {
|
||||
db := g.DB()
|
||||
|
||||
vipLevels := []g.Map{
|
||||
{
|
||||
"name": "普通用户", "level": 0, "price": 0, "duration": 0,
|
||||
"description": "免费用户,可观看部分免费内容",
|
||||
"privileges": `{"free_content": true, "hd_quality": false, "download": false}`,
|
||||
"status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now(),
|
||||
},
|
||||
{
|
||||
"name": "月度VIP", "level": 1, "price": 1500, "duration": 30,
|
||||
"description": "月度会员,享受高清观看和下载权限",
|
||||
"privileges": `{"free_content": true, "hd_quality": true, "download": true, "ad_free": true}`,
|
||||
"status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now(),
|
||||
},
|
||||
{
|
||||
"name": "年度VIP", "level": 2, "price": 15000, "duration": 365,
|
||||
"description": "年度会员,享受所有内容和特权",
|
||||
"privileges": `{"free_content": true, "hd_quality": true, "download": true, "ad_free": true, "exclusive_content": true}`,
|
||||
"status": 1, "created_at": gtime.Now(), "updated_at": gtime.Now(),
|
||||
},
|
||||
}
|
||||
|
||||
for _, vip := range vipLevels {
|
||||
_, err := db.Model("nl_vip_level").Data(vip).Insert()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user