后端重构

This commit is contained in:
Yvan
2026-01-14 14:34:49 +08:00
parent f5f77b32fe
commit 30842b42e5
274 changed files with 39754 additions and 1005 deletions
+10
View File
@@ -0,0 +1,10 @@
package example
import (
"kra/internal/biz/system"
)
// RegisterExampleInitializers 注册所有示例数据源初始化器
func RegisterExampleInitializers(uc *system.InitDBUsecase) {
uc.RegisterInit(initOrderExaFile, NewInitExaFile())
}
@@ -0,0 +1,66 @@
package example
import (
"context"
"kra/internal/biz/system"
"kra/internal/data/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)
const initOrderExaFile = system.InitOrderInternal + 1
type initExaFile struct{}
func NewInitExaFile() system.SubInitializer {
return &initExaFile{}
}
func (i *initExaFile) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&model.ExaFileUploadAndDownload{})
}
func (i *initExaFile) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&model.ExaFileUploadAndDownload{})
}
func (i *initExaFile) InitializerName() string {
return model.TableNameExaFileUploadAndDownload
}
func (i *initExaFile) InitializeData(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
entities := []model.ExaFileUploadAndDownload{
{Name: "10.png", URL: "https://qmplusimg.henrongyi.top/1576554439myAvatar.png", Tag: "png", Key: "158787308910.png"},
{Name: "logo.png", URL: "https://qmplusimg.henrongyi.top/1576554439myAvatar.png", Tag: "png", Key: "1587973709logo.png"},
}
if err := db.Create(&entities).Error; err != nil {
return ctx, errors.Wrap(err, model.TableNameExaFileUploadAndDownload+"表数据初始化失败!")
}
return ctx, nil
}
func (i *initExaFile) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
var count int64
if err := db.Model(&model.ExaFileUploadAndDownload{}).Where("name = ? AND key = ?", "logo.png", "1587973709logo.png").Count(&count).Error; err != nil {
return false
}
return count > 0
}
+238
View File
@@ -0,0 +1,238 @@
package system
import (
"context"
"kra/internal/biz/system"
"kra/internal/data/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)
type initApi struct{}
const initOrderApi = system.InitOrderSystem + 1
func NewInitApi() system.SubInitializer {
return &initApi{}
}
func (i *initApi) InitializerName() string {
return model.TableNameSysAPI
}
func (i *initApi) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&model.SysAPI{})
}
func (i *initApi) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&model.SysAPI{})
}
func (i *initApi) InitializeData(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
entities := []model.SysAPI{
{APIGroup: "jwt", Method: "POST", Path: "/jwt/jsonInBlacklist", Description: "jwt加入黑名单(退出,必选)"},
{APIGroup: "系统用户", Method: "DELETE", Path: "/user/deleteUser", Description: "删除用户"},
{APIGroup: "系统用户", Method: "POST", Path: "/user/admin_register", Description: "用户注册"},
{APIGroup: "系统用户", Method: "POST", Path: "/user/getUserList", Description: "获取用户列表"},
{APIGroup: "系统用户", Method: "PUT", Path: "/user/setUserInfo", Description: "设置用户信息"},
{APIGroup: "系统用户", Method: "PUT", Path: "/user/setSelfInfo", Description: "设置自身信息(必选)"},
{APIGroup: "系统用户", Method: "GET", Path: "/user/getUserInfo", Description: "获取自身信息(必选)"},
{APIGroup: "系统用户", Method: "POST", Path: "/user/setUserAuthorities", Description: "设置权限组"},
{APIGroup: "系统用户", Method: "POST", Path: "/user/changePassword", Description: "修改密码(建议选择)"},
{APIGroup: "系统用户", Method: "POST", Path: "/user/setUserAuthority", Description: "修改用户角色(必选)"},
{APIGroup: "系统用户", Method: "POST", Path: "/user/resetPassword", Description: "重置用户密码"},
{APIGroup: "系统用户", Method: "PUT", Path: "/user/setSelfSetting", Description: "用户界面配置"},
{APIGroup: "api", Method: "POST", Path: "/api/createApi", Description: "创建api"},
{APIGroup: "api", Method: "POST", Path: "/api/deleteApi", Description: "删除Api"},
{APIGroup: "api", Method: "POST", Path: "/api/updateApi", Description: "更新Api"},
{APIGroup: "api", Method: "POST", Path: "/api/getApiList", Description: "获取api列表"},
{APIGroup: "api", Method: "POST", Path: "/api/getAllApis", Description: "获取所有api"},
{APIGroup: "api", Method: "POST", Path: "/api/getApiById", Description: "获取api详细信息"},
{APIGroup: "api", Method: "DELETE", Path: "/api/deleteApisByIds", Description: "批量删除api"},
{APIGroup: "api", Method: "GET", Path: "/api/syncApi", Description: "获取待同步API"},
{APIGroup: "api", Method: "GET", Path: "/api/getApiGroups", Description: "获取路由组"},
{APIGroup: "api", Method: "POST", Path: "/api/enterSyncApi", Description: "确认同步API"},
{APIGroup: "api", Method: "POST", Path: "/api/ignoreApi", Description: "忽略API"},
{APIGroup: "角色", Method: "POST", Path: "/authority/copyAuthority", Description: "拷贝角色"},
{APIGroup: "角色", Method: "POST", Path: "/authority/createAuthority", Description: "创建角色"},
{APIGroup: "角色", Method: "POST", Path: "/authority/deleteAuthority", Description: "删除角色"},
{APIGroup: "角色", Method: "PUT", Path: "/authority/updateAuthority", Description: "更新角色信息"},
{APIGroup: "角色", Method: "POST", Path: "/authority/getAuthorityList", Description: "获取角色列表"},
{APIGroup: "角色", Method: "POST", Path: "/authority/setDataAuthority", Description: "设置角色资源权限"},
{APIGroup: "casbin", Method: "POST", Path: "/casbin/updateCasbin", Description: "更改角色api权限"},
{APIGroup: "casbin", Method: "POST", Path: "/casbin/getPolicyPathByAuthorityId", Description: "获取权限列表"},
{APIGroup: "菜单", Method: "POST", Path: "/menu/addBaseMenu", Description: "新增菜单"},
{APIGroup: "菜单", Method: "POST", Path: "/menu/getMenu", Description: "获取菜单树(必选)"},
{APIGroup: "菜单", Method: "POST", Path: "/menu/deleteBaseMenu", Description: "删除菜单"},
{APIGroup: "菜单", Method: "POST", Path: "/menu/updateBaseMenu", Description: "更新菜单"},
{APIGroup: "菜单", Method: "POST", Path: "/menu/getBaseMenuById", Description: "根据id获取菜单"},
{APIGroup: "菜单", Method: "POST", Path: "/menu/getMenuList", Description: "分页获取基础menu列表"},
{APIGroup: "菜单", Method: "POST", Path: "/menu/getBaseMenuTree", Description: "获取用户动态路由"},
{APIGroup: "菜单", Method: "POST", Path: "/menu/getMenuAuthority", Description: "获取指定角色menu"},
{APIGroup: "菜单", Method: "POST", Path: "/menu/addMenuAuthority", Description: "增加menu和角色关联关系"},
{APIGroup: "分片上传", Method: "GET", Path: "/fileUploadAndDownload/findFile", Description: "寻找目标文件(秒传)"},
{APIGroup: "分片上传", Method: "POST", Path: "/fileUploadAndDownload/breakpointContinue", Description: "断点续传"},
{APIGroup: "分片上传", Method: "POST", Path: "/fileUploadAndDownload/breakpointContinueFinish", Description: "断点续传完成"},
{APIGroup: "分片上传", Method: "POST", Path: "/fileUploadAndDownload/removeChunk", Description: "上传完成移除文件"},
{APIGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/upload", Description: "文件上传(建议选择)"},
{APIGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/deleteFile", Description: "删除文件"},
{APIGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/editFileName", Description: "文件名或者备注编辑"},
{APIGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/getFileList", Description: "获取上传文件列表"},
{APIGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/importURL", Description: "导入URL"},
{APIGroup: "系统服务", Method: "POST", Path: "/system/getServerInfo", Description: "获取服务器信息"},
{APIGroup: "系统服务", Method: "POST", Path: "/system/getSystemConfig", Description: "获取配置文件内容"},
{APIGroup: "系统服务", Method: "POST", Path: "/system/setSystemConfig", Description: "设置配置文件内容"},
{APIGroup: "客户", Method: "PUT", Path: "/customer/customer", Description: "更新客户"},
{APIGroup: "客户", Method: "POST", Path: "/customer/customer", Description: "创建客户"},
{APIGroup: "客户", Method: "DELETE", Path: "/customer/customer", Description: "删除客户"},
{APIGroup: "客户", Method: "GET", Path: "/customer/customer", Description: "获取单一客户"},
{APIGroup: "客户", Method: "GET", Path: "/customer/customerList", Description: "获取客户列表"},
{APIGroup: "代码生成器", Method: "GET", Path: "/autoCode/getDB", Description: "获取所有数据库"},
{APIGroup: "代码生成器", Method: "GET", Path: "/autoCode/getTables", Description: "获取数据库表"},
{APIGroup: "代码生成器", Method: "POST", Path: "/autoCode/createTemp", Description: "自动化代码"},
{APIGroup: "代码生成器", Method: "POST", Path: "/autoCode/preview", Description: "预览自动化代码"},
{APIGroup: "代码生成器", Method: "GET", Path: "/autoCode/getColumn", Description: "获取所选table的所有字段"},
{APIGroup: "代码生成器", Method: "POST", Path: "/autoCode/installPlugin", Description: "安装插件"},
{APIGroup: "代码生成器", Method: "POST", Path: "/autoCode/pubPlug", Description: "打包插件"},
{APIGroup: "代码生成器", Method: "POST", Path: "/autoCode/mcp", Description: "自动生成 MCP Tool 模板"},
{APIGroup: "代码生成器", Method: "POST", Path: "/autoCode/mcpTest", Description: "MCP Tool 测试"},
{APIGroup: "代码生成器", Method: "POST", Path: "/autoCode/mcpList", Description: "获取 MCP ToolList"},
{APIGroup: "模板配置", Method: "POST", Path: "/autoCode/createPackage", Description: "配置模板"},
{APIGroup: "模板配置", Method: "GET", Path: "/autoCode/getTemplates", Description: "获取模板文件"},
{APIGroup: "模板配置", Method: "POST", Path: "/autoCode/getPackage", Description: "获取所有模板"},
{APIGroup: "模板配置", Method: "POST", Path: "/autoCode/delPackage", Description: "删除模板"},
{APIGroup: "代码生成器历史", Method: "POST", Path: "/autoCode/getMeta", Description: "获取meta信息"},
{APIGroup: "代码生成器历史", Method: "POST", Path: "/autoCode/rollback", Description: "回滚自动生成代码"},
{APIGroup: "代码生成器历史", Method: "POST", Path: "/autoCode/getSysHistory", Description: "查询回滚记录"},
{APIGroup: "代码生成器历史", Method: "POST", Path: "/autoCode/delSysHistory", Description: "删除回滚记录"},
{APIGroup: "代码生成器历史", Method: "POST", Path: "/autoCode/addFunc", Description: "增加模板方法"},
{APIGroup: "系统字典详情", Method: "PUT", Path: "/sysDictionaryDetail/updateSysDictionaryDetail", Description: "更新字典内容"},
{APIGroup: "系统字典详情", Method: "POST", Path: "/sysDictionaryDetail/createSysDictionaryDetail", Description: "新增字典内容"},
{APIGroup: "系统字典详情", Method: "DELETE", Path: "/sysDictionaryDetail/deleteSysDictionaryDetail", Description: "删除字典内容"},
{APIGroup: "系统字典详情", Method: "GET", Path: "/sysDictionaryDetail/findSysDictionaryDetail", Description: "根据ID获取字典内容"},
{APIGroup: "系统字典详情", Method: "GET", Path: "/sysDictionaryDetail/getSysDictionaryDetailList", Description: "获取字典内容列表"},
{APIGroup: "系统字典详情", Method: "GET", Path: "/sysDictionaryDetail/getDictionaryTreeList", Description: "获取字典数列表"},
{APIGroup: "系统字典详情", Method: "GET", Path: "/sysDictionaryDetail/getDictionaryTreeListByType", Description: "根据分类获取字典数列表"},
{APIGroup: "系统字典详情", Method: "GET", Path: "/sysDictionaryDetail/getDictionaryDetailsByParent", Description: "根据父级ID获取字典详情"},
{APIGroup: "系统字典详情", Method: "GET", Path: "/sysDictionaryDetail/getDictionaryPath", Description: "获取字典详情的完整路径"},
{APIGroup: "系统字典", Method: "POST", Path: "/sysDictionary/createSysDictionary", Description: "新增字典"},
{APIGroup: "系统字典", Method: "DELETE", Path: "/sysDictionary/deleteSysDictionary", Description: "删除字典"},
{APIGroup: "系统字典", Method: "PUT", Path: "/sysDictionary/updateSysDictionary", Description: "更新字典"},
{APIGroup: "系统字典", Method: "GET", Path: "/sysDictionary/findSysDictionary", Description: "根据ID获取字典(建议选择)"},
{APIGroup: "系统字典", Method: "GET", Path: "/sysDictionary/getSysDictionaryList", Description: "获取字典列表"},
{APIGroup: "系统字典", Method: "POST", Path: "/sysDictionary/importSysDictionary", Description: "导入字典JSON"},
{APIGroup: "系统字典", Method: "GET", Path: "/sysDictionary/exportSysDictionary", Description: "导出字典JSON"},
{APIGroup: "操作记录", Method: "POST", Path: "/sysOperationRecord/createSysOperationRecord", Description: "新增操作记录"},
{APIGroup: "操作记录", Method: "GET", Path: "/sysOperationRecord/findSysOperationRecord", Description: "根据ID获取操作记录"},
{APIGroup: "操作记录", Method: "GET", Path: "/sysOperationRecord/getSysOperationRecordList", Description: "获取操作记录列表"},
{APIGroup: "操作记录", Method: "DELETE", Path: "/sysOperationRecord/deleteSysOperationRecord", Description: "删除操作记录"},
{APIGroup: "操作记录", Method: "DELETE", Path: "/sysOperationRecord/deleteSysOperationRecordByIds", Description: "批量删除操作历史"},
{APIGroup: "断点续传(插件版)", Method: "POST", Path: "/simpleUploader/upload", Description: "插件版分片上传"},
{APIGroup: "断点续传(插件版)", Method: "GET", Path: "/simpleUploader/checkFileMd5", Description: "文件完整度验证"},
{APIGroup: "断点续传(插件版)", Method: "GET", Path: "/simpleUploader/mergeFileMd5", Description: "上传完成合并文件"},
{APIGroup: "email", Method: "POST", Path: "/email/emailTest", Description: "发送测试邮件"},
{APIGroup: "email", Method: "POST", Path: "/email/sendEmail", Description: "发送邮件"},
{APIGroup: "按钮权限", Method: "POST", Path: "/authorityBtn/setAuthorityBtn", Description: "设置按钮权限"},
{APIGroup: "按钮权限", Method: "POST", Path: "/authorityBtn/getAuthorityBtn", Description: "获取已有按钮权限"},
{APIGroup: "按钮权限", Method: "POST", Path: "/authorityBtn/canRemoveAuthorityBtn", Description: "删除按钮"},
{APIGroup: "导出模板", Method: "POST", Path: "/sysExportTemplate/createSysExportTemplate", Description: "新增导出模板"},
{APIGroup: "导出模板", Method: "DELETE", Path: "/sysExportTemplate/deleteSysExportTemplate", Description: "删除导出模板"},
{APIGroup: "导出模板", Method: "DELETE", Path: "/sysExportTemplate/deleteSysExportTemplateByIds", Description: "批量删除导出模板"},
{APIGroup: "导出模板", Method: "PUT", Path: "/sysExportTemplate/updateSysExportTemplate", Description: "更新导出模板"},
{APIGroup: "导出模板", Method: "GET", Path: "/sysExportTemplate/findSysExportTemplate", Description: "根据ID获取导出模板"},
{APIGroup: "导出模板", Method: "GET", Path: "/sysExportTemplate/getSysExportTemplateList", Description: "获取导出模板列表"},
{APIGroup: "导出模板", Method: "GET", Path: "/sysExportTemplate/exportExcel", Description: "导出Excel"},
{APIGroup: "导出模板", Method: "GET", Path: "/sysExportTemplate/exportTemplate", Description: "下载模板"},
{APIGroup: "导出模板", Method: "GET", Path: "/sysExportTemplate/previewSQL", Description: "预览SQL"},
{APIGroup: "导出模板", Method: "POST", Path: "/sysExportTemplate/importExcel", Description: "导入Excel"},
{APIGroup: "错误日志", Method: "POST", Path: "/sysError/createSysError", Description: "新建错误日志"},
{APIGroup: "错误日志", Method: "DELETE", Path: "/sysError/deleteSysError", Description: "删除错误日志"},
{APIGroup: "错误日志", Method: "DELETE", Path: "/sysError/deleteSysErrorByIds", Description: "批量删除错误日志"},
{APIGroup: "错误日志", Method: "PUT", Path: "/sysError/updateSysError", Description: "更新错误日志"},
{APIGroup: "错误日志", Method: "GET", Path: "/sysError/findSysError", Description: "根据ID获取错误日志"},
{APIGroup: "错误日志", Method: "GET", Path: "/sysError/getSysErrorList", Description: "获取错误日志列表"},
{APIGroup: "错误日志", Method: "GET", Path: "/sysError/getSysErrorSolution", Description: "触发错误处理(异步)"},
{APIGroup: "公告", Method: "POST", Path: "/info/createInfo", Description: "新建公告"},
{APIGroup: "公告", Method: "DELETE", Path: "/info/deleteInfo", Description: "删除公告"},
{APIGroup: "公告", Method: "DELETE", Path: "/info/deleteInfoByIds", Description: "批量删除公告"},
{APIGroup: "公告", Method: "PUT", Path: "/info/updateInfo", Description: "更新公告"},
{APIGroup: "公告", Method: "GET", Path: "/info/findInfo", Description: "根据ID获取公告"},
{APIGroup: "公告", Method: "GET", Path: "/info/getInfoList", Description: "获取公告列表"},
{APIGroup: "参数管理", Method: "POST", Path: "/sysParams/createSysParams", Description: "新建参数"},
{APIGroup: "参数管理", Method: "DELETE", Path: "/sysParams/deleteSysParams", Description: "删除参数"},
{APIGroup: "参数管理", Method: "DELETE", Path: "/sysParams/deleteSysParamsByIds", Description: "批量删除参数"},
{APIGroup: "参数管理", Method: "PUT", Path: "/sysParams/updateSysParams", Description: "更新参数"},
{APIGroup: "参数管理", Method: "GET", Path: "/sysParams/findSysParams", Description: "根据ID获取参数"},
{APIGroup: "参数管理", Method: "GET", Path: "/sysParams/getSysParamsList", Description: "获取参数列表"},
{APIGroup: "参数管理", Method: "GET", Path: "/sysParams/getSysParam", Description: "获取参数列表"},
{APIGroup: "媒体库分类", Method: "GET", Path: "/attachmentCategory/getCategoryList", Description: "分类列表"},
{APIGroup: "媒体库分类", Method: "POST", Path: "/attachmentCategory/addCategory", Description: "添加/编辑分类"},
{APIGroup: "媒体库分类", Method: "POST", Path: "/attachmentCategory/deleteCategory", Description: "删除分类"},
{APIGroup: "版本控制", Method: "GET", Path: "/sysVersion/findSysVersion", Description: "获取单一版本"},
{APIGroup: "版本控制", Method: "GET", Path: "/sysVersion/getSysVersionList", Description: "获取版本列表"},
{APIGroup: "版本控制", Method: "GET", Path: "/sysVersion/downloadVersionJson", Description: "下载版本json"},
{APIGroup: "版本控制", Method: "POST", Path: "/sysVersion/exportVersion", Description: "创建版本"},
{APIGroup: "版本控制", Method: "POST", Path: "/sysVersion/importVersion", Description: "同步版本"},
{APIGroup: "版本控制", Method: "DELETE", Path: "/sysVersion/deleteSysVersion", Description: "删除版本"},
{APIGroup: "版本控制", Method: "DELETE", Path: "/sysVersion/deleteSysVersionByIds", Description: "批量删除版本"},
}
if err := db.Create(&entities).Error; err != nil {
return ctx, errors.Wrap(err, model.TableNameSysAPI+"表数据初始化失败!")
}
next := context.WithValue(ctx, i.InitializerName(), entities)
return next, nil
}
func (i *initApi) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
if errors.Is(db.Where("path = ? AND method = ?", "/authorityBtn/canRemoveAuthorityBtn", "POST").
First(&model.SysAPI{}).Error, gorm.ErrRecordNotFound) {
return false
}
return true
}
// GetInitOrder returns the initialization order
func (i *initApi) GetInitOrder() int {
return initOrderApi
}
+83
View File
@@ -0,0 +1,83 @@
package system
import (
"context"
"kra/internal/biz/system"
"kra/internal/data/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)
type initApiIgnore struct{}
const initOrderApiIgnore = initOrderApi + 1
func NewInitApiIgnore() system.SubInitializer {
return &initApiIgnore{}
}
func (i *initApiIgnore) InitializerName() string {
return model.TableNameSysIgnoreAPI
}
func (i *initApiIgnore) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&model.SysIgnoreAPI{})
}
func (i *initApiIgnore) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&model.SysIgnoreAPI{})
}
func (i *initApiIgnore) InitializeData(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
entities := []model.SysIgnoreAPI{
{Method: "GET", Path: "/swagger/*any"},
{Method: "GET", Path: "/api/freshCasbin"},
{Method: "GET", Path: "/uploads/file/*filepath"},
{Method: "GET", Path: "/health"},
{Method: "HEAD", Path: "/uploads/file/*filepath"},
{Method: "POST", Path: "/autoCode/llmAuto"},
{Method: "POST", Path: "/system/reloadSystem"},
{Method: "POST", Path: "/base/login"},
{Method: "POST", Path: "/base/captcha"},
{Method: "POST", Path: "/init/initdb"},
{Method: "POST", Path: "/init/checkdb"},
{Method: "GET", Path: "/info/getInfoDataSource"},
{Method: "GET", Path: "/info/getInfoPublic"},
}
if err := db.Create(&entities).Error; err != nil {
return ctx, errors.Wrap(err, model.TableNameSysIgnoreAPI+"表数据初始化失败!")
}
next := context.WithValue(ctx, i.InitializerName(), entities)
return next, nil
}
func (i *initApiIgnore) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
if errors.Is(db.Where("path = ? AND method = ?", "/swagger/*any", "GET").
First(&model.SysIgnoreAPI{}).Error, gorm.ErrRecordNotFound) {
return false
}
return true
}
// GetInitOrder returns the initialization order
func (i *initApiIgnore) GetInitOrder() int {
return initOrderApiIgnore
}
+120
View File
@@ -0,0 +1,120 @@
package system
import (
"context"
"kra/internal/biz/system"
"kra/internal/data/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)
const initOrderMenuAuthority = initOrderMenu + initOrderAuthority
type initMenuAuthority struct{}
func NewInitMenuAuthority() system.SubInitializer {
return &initMenuAuthority{}
}
func (i *initMenuAuthority) MigrateTable(ctx context.Context) (context.Context, error) {
return ctx, nil // do nothing
}
func (i *initMenuAuthority) TableCreated(ctx context.Context) bool {
return false // always replace
}
func (i *initMenuAuthority) InitializerName() string {
return "sys_authority_menus"
}
func (i *initMenuAuthority) InitializeData(ctx context.Context) (next context.Context, err error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
authorities, ok := ctx.Value(model.TableNameSysAuthority).([]model.SysAuthority)
if !ok {
return ctx, errors.Wrap(system.ErrMissingDependentContext, "创建 [菜单-权限] 关联失败, 未找到权限表初始化数据")
}
allMenus, ok := ctx.Value(model.TableNameSysBaseMenu).([]model.SysBaseMenu)
if !ok {
return ctx, errors.Wrap(system.ErrMissingDependentContext, "创建 [菜单-权限] 关联失败, 未找到菜单表初始化数据")
}
next = ctx
// 构建菜单ID映射
menuMap := make(map[int64]model.SysBaseMenu)
for _, menu := range allMenus {
menuMap[menu.ID] = menu
}
// 为不同角色分配不同权限
// 1. 超级管理员角色(888) - 拥有所有菜单权限
var menuIDs888 []int64
for _, menu := range allMenus {
menuIDs888 = append(menuIDs888, menu.ID)
}
for _, menuID := range menuIDs888 {
if err = db.Exec("INSERT INTO sys_authority_menus (sys_authority_authority_id, sys_base_menu_id) VALUES (?, ?)", authorities[0].AuthorityID, menuID).Error; err != nil {
return next, errors.Wrap(err, "为超级管理员分配菜单失败")
}
}
// 2. 测试角色(9528) - 仅拥有基础功能菜单
var menu9528 []int64
for _, menu := range allMenus {
if menu.ParentID == 0 && (menu.Name == "dashboard" || menu.Name == "about" || menu.Name == "person" || menu.Name == "state") {
menu9528 = append(menu9528, menu.ID)
}
}
for _, menuID := range menu9528 {
if err = db.Exec("INSERT INTO sys_authority_menus (sys_authority_authority_id, sys_base_menu_id) VALUES (?, ?)", authorities[1].AuthorityID, menuID).Error; err != nil {
return next, errors.Wrap(err, "为测试角色分配菜单失败")
}
}
// 3. 普通用户子角色(8881) - 拥有部分菜单权限
var menu8881 []int64
// 添加所有父级菜单
for _, menu := range allMenus {
if menu.ParentID == 0 {
menu8881 = append(menu8881, menu.ID)
}
}
// 添加部分子菜单
for _, menu := range allMenus {
parentName := ""
if menu.ParentID > 0 {
if parent, exists := menuMap[menu.ParentID]; exists {
parentName = parent.Name
}
}
if menu.ParentID > 0 && (parentName == "systemTools" || parentName == "example") {
menu8881 = append(menu8881, menu.ID)
}
}
for _, menuID := range menu8881 {
if err = db.Exec("INSERT INTO sys_authority_menus (sys_authority_authority_id, sys_base_menu_id) VALUES (?, ?)", authorities[2].AuthorityID, menuID).Error; err != nil {
return next, errors.Wrap(err, "为普通用户子角色分配菜单失败")
}
}
return next, nil
}
func (i *initMenuAuthority) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
var count int64
if err := db.Table("sys_authority_menus").Where("sys_authority_authority_id = ?", 8881).Count(&count).Error; err != nil {
return false
}
return count > 0
}
+80
View File
@@ -0,0 +1,80 @@
package system
import (
"context"
"kra/internal/biz/system"
"kra/internal/data/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)
const initOrderAuthority = initOrderCasbin + 1
type initAuthority struct{}
func NewInitAuthority() system.SubInitializer {
return &initAuthority{}
}
func (i *initAuthority) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&model.SysAuthority{})
}
func (i *initAuthority) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&model.SysAuthority{})
}
func (i *initAuthority) InitializerName() string {
return model.TableNameSysAuthority
}
func (i *initAuthority) InitializeData(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
entities := []model.SysAuthority{
{AuthorityID: 888, AuthorityName: "普通用户", ParentID: 0, DefaultRouter: "dashboard"},
{AuthorityID: 9528, AuthorityName: "测试角色", ParentID: 0, DefaultRouter: "dashboard"},
{AuthorityID: 8881, AuthorityName: "普通用户子角色", ParentID: 888, DefaultRouter: "dashboard"},
}
if err := db.Create(&entities).Error; err != nil {
return ctx, errors.Wrapf(err, "%s表数据初始化失败!", model.TableNameSysAuthority)
}
// data authority - 设置数据权限关联
// 888角色可以查看888、9528、8881的数据
if err := db.Exec("INSERT INTO sys_data_authority_id (sys_authority_authority_id, data_authority_id_authority_id) VALUES (888, 888), (888, 9528), (888, 8881)").Error; err != nil {
return ctx, errors.Wrapf(err, "sys_data_authority_id表数据初始化失败!")
}
// 9528角色可以查看9528、8881的数据
if err := db.Exec("INSERT INTO sys_data_authority_id (sys_authority_authority_id, data_authority_id_authority_id) VALUES (9528, 9528), (9528, 8881)").Error; err != nil {
return ctx, errors.Wrapf(err, "sys_data_authority_id表数据初始化失败!")
}
next := context.WithValue(ctx, i.InitializerName(), entities)
return next, nil
}
func (i *initAuthority) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
if errors.Is(db.Where("authority_id = ?", "8881").
First(&model.SysAuthority{}).Error, gorm.ErrRecordNotFound) {
return false
}
return true
}
+317
View File
@@ -0,0 +1,317 @@
package system
import (
"context"
"kra/internal/biz/system"
"kra/internal/data/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)
const initOrderCasbin = initOrderApiIgnore + 1
type initCasbin struct{}
func NewInitCasbin() system.SubInitializer {
return &initCasbin{}
}
func (i *initCasbin) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&model.CasbinRule{})
}
func (i *initCasbin) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&model.CasbinRule{})
}
func (i *initCasbin) InitializerName() string {
return model.TableNameCasbinRule
}
func (i *initCasbin) InitializeData(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
entities := []model.CasbinRule{
{Ptype: "p", V0: "888", V1: "/user/admin_register", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/api/createApi", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/api/getApiList", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/api/getApiById", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/api/deleteApi", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/api/updateApi", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/api/getAllApis", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/api/deleteApisByIds", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/api/syncApi", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/api/getApiGroups", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/api/enterSyncApi", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/api/ignoreApi", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/authority/copyAuthority", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/authority/updateAuthority", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/authority/createAuthority", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/authority/deleteAuthority", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/authority/getAuthorityList", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/authority/setDataAuthority", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/menu/getMenu", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/menu/getMenuList", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/menu/addBaseMenu", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/menu/getBaseMenuTree", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/menu/addMenuAuthority", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/menu/getMenuAuthority", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/menu/deleteBaseMenu", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/menu/updateBaseMenu", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/menu/getBaseMenuById", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/user/getUserInfo", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/user/setUserInfo", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/user/setSelfInfo", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/user/getUserList", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/user/deleteUser", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/user/changePassword", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/user/setUserAuthority", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/user/setUserAuthorities", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/user/resetPassword", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/user/setSelfSetting", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/findFile", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/breakpointContinueFinish", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/breakpointContinue", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/removeChunk", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/upload", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/deleteFile", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/editFileName", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/getFileList", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/fileUploadAndDownload/importURL", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/casbin/updateCasbin", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/jwt/jsonInBlacklist", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/system/getSystemConfig", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/system/setSystemConfig", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/system/getServerInfo", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/customer/customer", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/customer/customer", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/customer/customer", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/customer/customer", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/customer/customerList", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/autoCode/getDB", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/autoCode/getMeta", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/preview", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/getTables", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/autoCode/getColumn", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/autoCode/rollback", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/createTemp", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/delSysHistory", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/getSysHistory", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/createPackage", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/getTemplates", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/autoCode/getPackage", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/delPackage", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/createPlug", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/installPlugin", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/pubPlug", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/addFunc", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/mcp", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/mcpTest", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/autoCode/mcpList", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/findSysDictionaryDetail", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/updateSysDictionaryDetail", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/createSysDictionaryDetail", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/getSysDictionaryDetailList", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/deleteSysDictionaryDetail", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/getDictionaryTreeList", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/getDictionaryTreeListByType", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/getDictionaryDetailsByParent", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysDictionaryDetail/getDictionaryPath", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysDictionary/findSysDictionary", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysDictionary/updateSysDictionary", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/sysDictionary/getSysDictionaryList", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysDictionary/createSysDictionary", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysDictionary/deleteSysDictionary", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/sysDictionary/importSysDictionary", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysDictionary/exportSysDictionary", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysOperationRecord/findSysOperationRecord", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysOperationRecord/updateSysOperationRecord", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/sysOperationRecord/createSysOperationRecord", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysOperationRecord/getSysOperationRecordList", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysOperationRecord/deleteSysOperationRecord", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/sysOperationRecord/deleteSysOperationRecordByIds", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/email/emailTest", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/email/sendEmail", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/simpleUploader/upload", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/simpleUploader/checkFileMd5", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/simpleUploader/mergeFileMd5", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/authorityBtn/setAuthorityBtn", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/authorityBtn/getAuthorityBtn", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/authorityBtn/canRemoveAuthorityBtn", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysExportTemplate/createSysExportTemplate", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysExportTemplate/deleteSysExportTemplate", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/sysExportTemplate/deleteSysExportTemplateByIds", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/sysExportTemplate/updateSysExportTemplate", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/sysExportTemplate/findSysExportTemplate", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysExportTemplate/getSysExportTemplateList", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysExportTemplate/exportExcel", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysExportTemplate/exportTemplate", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysExportTemplate/previewSQL", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysExportTemplate/importExcel", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysError/createSysError", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysError/deleteSysError", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/sysError/deleteSysErrorByIds", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/sysError/updateSysError", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/sysError/findSysError", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysError/getSysErrorList", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysError/getSysErrorSolution", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/info/createInfo", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/info/deleteInfo", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/info/deleteInfoByIds", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/info/updateInfo", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/info/findInfo", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/info/getInfoList", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysParams/createSysParams", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysParams/deleteSysParams", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/sysParams/deleteSysParamsByIds", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/sysParams/updateSysParams", V2: "PUT"},
{Ptype: "p", V0: "888", V1: "/sysParams/findSysParams", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysParams/getSysParamsList", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysParams/getSysParam", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/attachmentCategory/getCategoryList", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/attachmentCategory/addCategory", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/attachmentCategory/deleteCategory", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysVersion/findSysVersion", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysVersion/getSysVersionList", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysVersion/downloadVersionJson", V2: "GET"},
{Ptype: "p", V0: "888", V1: "/sysVersion/exportVersion", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysVersion/importVersion", V2: "POST"},
{Ptype: "p", V0: "888", V1: "/sysVersion/deleteSysVersion", V2: "DELETE"},
{Ptype: "p", V0: "888", V1: "/sysVersion/deleteSysVersionByIds", V2: "DELETE"},
{Ptype: "p", V0: "8881", V1: "/user/admin_register", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/api/createApi", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/api/getApiList", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/api/getApiById", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/api/deleteApi", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/api/updateApi", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/api/getAllApis", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/authority/createAuthority", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/authority/deleteAuthority", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/authority/getAuthorityList", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/authority/setDataAuthority", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/menu/getMenu", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/menu/getMenuList", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/menu/addBaseMenu", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/menu/getBaseMenuTree", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/menu/addMenuAuthority", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/menu/getMenuAuthority", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/menu/deleteBaseMenu", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/menu/updateBaseMenu", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/menu/getBaseMenuById", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/user/changePassword", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/user/getUserList", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/user/setUserAuthority", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/fileUploadAndDownload/upload", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/fileUploadAndDownload/getFileList", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/fileUploadAndDownload/deleteFile", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/fileUploadAndDownload/editFileName", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/fileUploadAndDownload/importURL", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/casbin/updateCasbin", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/jwt/jsonInBlacklist", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/system/getSystemConfig", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/system/setSystemConfig", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "POST"},
{Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "PUT"},
{Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "DELETE"},
{Ptype: "p", V0: "8881", V1: "/customer/customer", V2: "GET"},
{Ptype: "p", V0: "8881", V1: "/customer/customerList", V2: "GET"},
{Ptype: "p", V0: "8881", V1: "/user/getUserInfo", V2: "GET"},
{Ptype: "p", V0: "9528", V1: "/user/admin_register", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/api/createApi", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/api/getApiList", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/api/getApiById", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/api/deleteApi", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/api/updateApi", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/api/getAllApis", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/authority/createAuthority", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/authority/deleteAuthority", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/authority/getAuthorityList", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/authority/setDataAuthority", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/menu/getMenu", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/menu/getMenuList", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/menu/addBaseMenu", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/menu/getBaseMenuTree", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/menu/addMenuAuthority", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/menu/getMenuAuthority", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/menu/deleteBaseMenu", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/menu/updateBaseMenu", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/menu/getBaseMenuById", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/user/changePassword", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/user/getUserList", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/user/setUserAuthority", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/fileUploadAndDownload/upload", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/fileUploadAndDownload/getFileList", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/fileUploadAndDownload/deleteFile", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/fileUploadAndDownload/editFileName", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/fileUploadAndDownload/importURL", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/casbin/updateCasbin", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/casbin/getPolicyPathByAuthorityId", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/jwt/jsonInBlacklist", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/system/getSystemConfig", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/system/setSystemConfig", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "PUT"},
{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "GET"},
{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/customer/customer", V2: "DELETE"},
{Ptype: "p", V0: "9528", V1: "/customer/customerList", V2: "GET"},
{Ptype: "p", V0: "9528", V1: "/autoCode/createTemp", V2: "POST"},
{Ptype: "p", V0: "9528", V1: "/user/getUserInfo", V2: "GET"},
}
if err := db.Create(&entities).Error; err != nil {
return ctx, errors.Wrap(err, "Casbin 表 ("+i.InitializerName()+") 数据初始化失败!")
}
next := context.WithValue(ctx, i.InitializerName(), entities)
return next, nil
}
func (i *initCasbin) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
if errors.Is(db.Where(&model.CasbinRule{Ptype: "p", V0: "9528", V1: "/user/getUserInfo", V2: "GET"}).
First(&model.CasbinRule{}).Error, gorm.ErrRecordNotFound) {
return false
}
return true
}
+71
View File
@@ -0,0 +1,71 @@
package system
import (
"context"
"kra/internal/biz/system"
"kra/internal/data/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)
const initOrderDict = initOrderCasbin + 1
type initDict struct{}
func NewInitDict() system.SubInitializer {
return &initDict{}
}
func (i *initDict) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&model.SysDictionary{})
}
func (i *initDict) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&model.SysDictionary{})
}
func (i *initDict) InitializerName() string {
return model.TableNameSysDictionary
}
func (i *initDict) InitializeData(ctx context.Context) (next context.Context, err error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
entities := []model.SysDictionary{
{Name: "性别", Type: "gender", Status: true, Desc: "性别字典"},
{Name: "数据库int类型", Type: "int", Status: true, Desc: "int类型对应的数据库类型"},
{Name: "数据库时间日期类型", Type: "time.Time", Status: true, Desc: "数据库时间日期类型"},
{Name: "数据库浮点型", Type: "float64", Status: true, Desc: "数据库浮点型"},
{Name: "数据库字符串", Type: "string", Status: true, Desc: "数据库字符串"},
{Name: "数据库bool类型", Type: "bool", Status: true, Desc: "数据库bool类型"},
}
if err = db.Create(&entities).Error; err != nil {
return ctx, errors.Wrap(err, model.TableNameSysDictionary+"表数据初始化失败!")
}
next = context.WithValue(ctx, i.InitializerName(), entities)
return next, nil
}
func (i *initDict) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
if errors.Is(db.Where("type = ?", "bool").First(&model.SysDictionary{}).Error, gorm.ErrRecordNotFound) {
return false
}
return true
}
+134
View File
@@ -0,0 +1,134 @@
package system
import (
"context"
"fmt"
"kra/internal/biz/system"
"kra/internal/data/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)
const initOrderDictDetail = initOrderDict + 1
type initDictDetail struct{}
func NewInitDictDetail() system.SubInitializer {
return &initDictDetail{}
}
func (i *initDictDetail) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&model.SysDictionaryDetail{})
}
func (i *initDictDetail) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&model.SysDictionaryDetail{})
}
func (i *initDictDetail) InitializerName() string {
return model.TableNameSysDictionaryDetail
}
func (i *initDictDetail) InitializeData(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
dicts, ok := ctx.Value(model.TableNameSysDictionary).([]model.SysDictionary)
if !ok {
return ctx, errors.Wrap(system.ErrMissingDependentContext,
fmt.Sprintf("未找到 %s 表初始化数据", model.TableNameSysDictionary))
}
// 性别字典详情
genderDetails := []model.SysDictionaryDetail{
{Label: "男", Value: "1", Status: true, Sort: 1, SysDictionaryID: dicts[0].ID},
{Label: "女", Value: "2", Status: true, Sort: 2, SysDictionaryID: dicts[0].ID},
}
// int类型字典详情
intDetails := []model.SysDictionaryDetail{
{Label: "smallint", Value: "1", Status: true, Extend: "mysql", Sort: 1, SysDictionaryID: dicts[1].ID},
{Label: "mediumint", Value: "2", Status: true, Extend: "mysql", Sort: 2, SysDictionaryID: dicts[1].ID},
{Label: "int", Value: "3", Status: true, Extend: "mysql", Sort: 3, SysDictionaryID: dicts[1].ID},
{Label: "bigint", Value: "4", Status: true, Extend: "mysql", Sort: 4, SysDictionaryID: dicts[1].ID},
{Label: "int2", Value: "5", Status: true, Extend: "pgsql", Sort: 5, SysDictionaryID: dicts[1].ID},
{Label: "int4", Value: "6", Status: true, Extend: "pgsql", Sort: 6, SysDictionaryID: dicts[1].ID},
{Label: "int6", Value: "7", Status: true, Extend: "pgsql", Sort: 7, SysDictionaryID: dicts[1].ID},
{Label: "int8", Value: "8", Status: true, Extend: "pgsql", Sort: 8, SysDictionaryID: dicts[1].ID},
}
// 时间类型字典详情
timeDetails := []model.SysDictionaryDetail{
{Label: "date", Value: "0", Status: true, Extend: "mysql", Sort: 0, SysDictionaryID: dicts[2].ID},
{Label: "time", Value: "1", Status: true, Extend: "mysql", Sort: 1, SysDictionaryID: dicts[2].ID},
{Label: "year", Value: "2", Status: true, Extend: "mysql", Sort: 2, SysDictionaryID: dicts[2].ID},
{Label: "datetime", Value: "3", Status: true, Extend: "mysql", Sort: 3, SysDictionaryID: dicts[2].ID},
{Label: "timestamp", Value: "5", Status: true, Extend: "mysql", Sort: 5, SysDictionaryID: dicts[2].ID},
{Label: "timestamptz", Value: "6", Status: true, Extend: "pgsql", Sort: 5, SysDictionaryID: dicts[2].ID},
}
// 浮点类型字典详情
floatDetails := []model.SysDictionaryDetail{
{Label: "float", Value: "0", Status: true, Extend: "mysql", Sort: 0, SysDictionaryID: dicts[3].ID},
{Label: "double", Value: "1", Status: true, Extend: "mysql", Sort: 1, SysDictionaryID: dicts[3].ID},
{Label: "decimal", Value: "2", Status: true, Extend: "mysql", Sort: 2, SysDictionaryID: dicts[3].ID},
{Label: "numeric", Value: "3", Status: true, Extend: "pgsql", Sort: 3, SysDictionaryID: dicts[3].ID},
{Label: "smallserial", Value: "4", Status: true, Extend: "pgsql", Sort: 4, SysDictionaryID: dicts[3].ID},
}
// 字符串类型字典详情
stringDetails := []model.SysDictionaryDetail{
{Label: "char", Value: "0", Status: true, Extend: "mysql", Sort: 0, SysDictionaryID: dicts[4].ID},
{Label: "varchar", Value: "1", Status: true, Extend: "mysql", Sort: 1, SysDictionaryID: dicts[4].ID},
{Label: "tinyblob", Value: "2", Status: true, Extend: "mysql", Sort: 2, SysDictionaryID: dicts[4].ID},
{Label: "tinytext", Value: "3", Status: true, Extend: "mysql", Sort: 3, SysDictionaryID: dicts[4].ID},
{Label: "text", Value: "4", Status: true, Extend: "mysql", Sort: 4, SysDictionaryID: dicts[4].ID},
{Label: "blob", Value: "5", Status: true, Extend: "mysql", Sort: 5, SysDictionaryID: dicts[4].ID},
{Label: "mediumblob", Value: "6", Status: true, Extend: "mysql", Sort: 6, SysDictionaryID: dicts[4].ID},
{Label: "mediumtext", Value: "7", Status: true, Extend: "mysql", Sort: 7, SysDictionaryID: dicts[4].ID},
{Label: "longblob", Value: "8", Status: true, Extend: "mysql", Sort: 8, SysDictionaryID: dicts[4].ID},
{Label: "longtext", Value: "9", Status: true, Extend: "mysql", Sort: 9, SysDictionaryID: dicts[4].ID},
}
// bool类型字典详情
boolDetails := []model.SysDictionaryDetail{
{Label: "tinyint", Value: "1", Extend: "mysql", Status: true, SysDictionaryID: dicts[5].ID},
{Label: "bool", Value: "2", Extend: "pgsql", Status: true, SysDictionaryID: dicts[5].ID},
}
// 批量创建所有字典详情
allDetails := append(genderDetails, intDetails...)
allDetails = append(allDetails, timeDetails...)
allDetails = append(allDetails, floatDetails...)
allDetails = append(allDetails, stringDetails...)
allDetails = append(allDetails, boolDetails...)
if err := db.Create(&allDetails).Error; err != nil {
return ctx, errors.Wrap(err, model.TableNameSysDictionaryDetail+"表数据初始化失败!")
}
return ctx, nil
}
func (i *initDictDetail) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
var count int64
if err := db.Model(&model.SysDictionaryDetail{}).Where("label = ?", "tinyint").Count(&count).Error; err != nil {
return false
}
return count > 0
}
+19
View File
@@ -0,0 +1,19 @@
package system
import (
"kra/internal/biz/system"
)
// RegisterSystemInitializers 注册所有系统数据源初始化器
func RegisterSystemInitializers(uc *system.InitDBUsecase) {
uc.RegisterInit(initOrderApi, NewInitApi())
uc.RegisterInit(initOrderApiIgnore, NewInitApiIgnore())
uc.RegisterInit(initOrderCasbin, NewInitCasbin())
uc.RegisterInit(initOrderAuthority, NewInitAuthority())
uc.RegisterInit(initOrderMenu, NewInitMenu())
uc.RegisterInit(initOrderMenuAuthority, NewInitMenuAuthority())
uc.RegisterInit(initOrderUser, NewInitUser())
uc.RegisterInit(initOrderDict, NewInitDict())
uc.RegisterInit(initOrderDictDetail, NewInitDictDetail())
uc.RegisterInit(initOrderExcelTemplate, NewInitExcelTemplate())
}
+76
View File
@@ -0,0 +1,76 @@
package system
import (
"context"
"kra/internal/biz/system"
"kra/internal/data/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)
const initOrderExcelTemplate = initOrderDictDetail + 1
type initExcelTemplate struct{}
func NewInitExcelTemplate() system.SubInitializer {
return &initExcelTemplate{}
}
func (i *initExcelTemplate) InitializerName() string {
return model.TableNameSysExportTemplate
}
func (i *initExcelTemplate) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&model.SysExportTemplate{})
}
func (i *initExcelTemplate) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&model.SysExportTemplate{})
}
func (i *initExcelTemplate) InitializeData(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
entities := []model.SysExportTemplate{
{
Name: "api",
TblName: "sys_apis",
TemplateID: "api",
TemplateInfo: `{
"path":"路径",
"method":"方法(大写)",
"description":"方法介绍",
"api_group":"方法分组"
}`,
},
}
if err := db.Create(&entities).Error; err != nil {
return ctx, errors.Wrap(err, model.TableNameSysExportTemplate+"表数据初始化失败!")
}
next := context.WithValue(ctx, i.InitializerName(), entities)
return next, nil
}
func (i *initExcelTemplate) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
if errors.Is(db.First(&model.SysExportTemplate{}).Error, gorm.ErrRecordNotFound) {
return false
}
return true
}
+129
View File
@@ -0,0 +1,129 @@
package system
import (
"context"
"kra/internal/biz/system"
"kra/internal/data/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)
const initOrderMenu = initOrderAuthority + 1
type initMenu struct{}
func NewInitMenu() system.SubInitializer {
return &initMenu{}
}
func (i *initMenu) InitializerName() string {
return model.TableNameSysBaseMenu
}
func (i *initMenu) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&model.SysBaseMenu{})
}
func (i *initMenu) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&model.SysBaseMenu{})
}
func (i *initMenu) InitializeData(ctx context.Context) (next context.Context, err error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
// 定义所有父级菜单
allMenus := []model.SysBaseMenu{
{MenuLevel: 0, Hidden: false, ParentID: 0, Path: "dashboard", Name: "dashboard", Component: "view/dashboard/index.vue", Sort: 1, Title: "仪表盘", Icon: "odometer"},
{MenuLevel: 0, Hidden: false, ParentID: 0, Path: "about", Name: "about", Component: "view/about/index.vue", Sort: 9, Title: "关于我们", Icon: "info-filled"},
{MenuLevel: 0, Hidden: false, ParentID: 0, Path: "admin", Name: "superAdmin", Component: "view/superAdmin/index.vue", Sort: 3, Title: "超级管理员", Icon: "user"},
{MenuLevel: 0, Hidden: true, ParentID: 0, Path: "person", Name: "person", Component: "view/person/person.vue", Sort: 4, Title: "个人信息", Icon: "message"},
{MenuLevel: 0, Hidden: false, ParentID: 0, Path: "example", Name: "example", Component: "view/example/index.vue", Sort: 7, Title: "示例文件", Icon: "management"},
{MenuLevel: 0, Hidden: false, ParentID: 0, Path: "systemTools", Name: "systemTools", Component: "view/systemTools/index.vue", Sort: 5, Title: "系统工具", Icon: "tools"},
{MenuLevel: 0, Hidden: false, ParentID: 0, Path: "https://github.com/tx7do/kratos-admin", Name: "https://github.com/tx7do/kratos-admin", Component: "/", Sort: 0, Title: "官方网站", Icon: "home-filled"},
{MenuLevel: 0, Hidden: false, ParentID: 0, Path: "state", Name: "state", Component: "view/system/state.vue", Sort: 8, Title: "服务器状态", Icon: "cloudy"},
{MenuLevel: 0, Hidden: false, ParentID: 0, Path: "plugin", Name: "plugin", Component: "view/routerHolder.vue", Sort: 6, Title: "插件系统", Icon: "cherry"},
}
// 先创建父级菜单
if err = db.Create(&allMenus).Error; err != nil {
return ctx, errors.Wrap(err, model.TableNameSysBaseMenu+"父级菜单初始化失败!")
}
// 建立菜单映射
menuNameMap := make(map[string]int64)
for _, menu := range allMenus {
menuNameMap[menu.Name] = menu.ID
}
// 定义子菜单
childMenus := []model.SysBaseMenu{
// superAdmin子菜单
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["superAdmin"], Path: "authority", Name: "authority", Component: "view/superAdmin/authority/authority.vue", Sort: 1, Title: "角色管理", Icon: "avatar"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["superAdmin"], Path: "menu", Name: "menu", Component: "view/superAdmin/menu/menu.vue", Sort: 2, Title: "菜单管理", Icon: "tickets", KeepAlive: true},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["superAdmin"], Path: "api", Name: "api", Component: "view/superAdmin/api/api.vue", Sort: 3, Title: "api管理", Icon: "platform", KeepAlive: true},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["superAdmin"], Path: "user", Name: "user", Component: "view/superAdmin/user/user.vue", Sort: 4, Title: "用户管理", Icon: "coordinate"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["superAdmin"], Path: "dictionary", Name: "dictionary", Component: "view/superAdmin/dictionary/sysDictionary.vue", Sort: 5, Title: "字典管理", Icon: "notebook"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["superAdmin"], Path: "operation", Name: "operation", Component: "view/superAdmin/operation/sysOperationRecord.vue", Sort: 6, Title: "操作历史", Icon: "pie-chart"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["superAdmin"], Path: "sysParams", Name: "sysParams", Component: "view/superAdmin/params/sysParams.vue", Sort: 7, Title: "参数管理", Icon: "compass"},
// example子菜单
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["example"], Path: "upload", Name: "upload", Component: "view/example/upload/upload.vue", Sort: 5, Title: "媒体库(上传下载)", Icon: "upload"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["example"], Path: "breakpoint", Name: "breakpoint", Component: "view/example/breakpoint/breakpoint.vue", Sort: 6, Title: "断点续传", Icon: "upload-filled"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["example"], Path: "customer", Name: "customer", Component: "view/example/customer/customer.vue", Sort: 7, Title: "客户列表(资源示例)", Icon: "avatar"},
// systemTools子菜单
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["systemTools"], Path: "autoCode", Name: "autoCode", Component: "view/systemTools/autoCode/index.vue", Sort: 1, Title: "代码生成器", Icon: "cpu", KeepAlive: true},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["systemTools"], Path: "formCreate", Name: "formCreate", Component: "view/systemTools/formCreate/index.vue", Sort: 3, Title: "表单生成器", Icon: "magic-stick", KeepAlive: true},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["systemTools"], Path: "system", Name: "system", Component: "view/systemTools/system/system.vue", Sort: 4, Title: "系统配置", Icon: "operation"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["systemTools"], Path: "autoCodeAdmin", Name: "autoCodeAdmin", Component: "view/systemTools/autoCodeAdmin/index.vue", Sort: 2, Title: "自动化代码管理", Icon: "magic-stick"},
{MenuLevel: 1, Hidden: true, ParentID: menuNameMap["systemTools"], Path: "autoCodeEdit/:id", Name: "autoCodeEdit", Component: "view/systemTools/autoCode/index.vue", Sort: 0, Title: "自动化代码-${id}", Icon: "magic-stick"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["systemTools"], Path: "autoPkg", Name: "autoPkg", Component: "view/systemTools/autoPkg/autoPkg.vue", Sort: 0, Title: "模板配置", Icon: "folder"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["systemTools"], Path: "exportTemplate", Name: "exportTemplate", Component: "view/systemTools/exportTemplate/exportTemplate.vue", Sort: 5, Title: "导出模板", Icon: "reading"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["systemTools"], Path: "picture", Name: "picture", Component: "view/systemTools/autoCode/picture.vue", Sort: 6, Title: "AI页面绘制", Icon: "picture-filled"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["systemTools"], Path: "mcpTool", Name: "mcpTool", Component: "view/systemTools/autoCode/mcp.vue", Sort: 7, Title: "Mcp Tools模板", Icon: "magnet"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["systemTools"], Path: "mcpTest", Name: "mcpTest", Component: "view/systemTools/autoCode/mcpTest.vue", Sort: 7, Title: "Mcp Tools测试", Icon: "partly-cloudy"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["systemTools"], Path: "sysVersion", Name: "sysVersion", Component: "view/systemTools/version/version.vue", Sort: 8, Title: "版本管理", Icon: "server"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["systemTools"], Path: "sysError", Name: "sysError", Component: "view/systemTools/sysError/sysError.vue", Sort: 9, Title: "错误日志", Icon: "warn"},
// plugin子菜单
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["plugin"], Path: "https://github.com/tx7do/kratos-admin", Name: "https://github.com/tx7do/kratos-admin", Component: "https://github.com/tx7do/kratos-admin", Sort: 0, Title: "插件市场", Icon: "shop"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["plugin"], Path: "installPlugin", Name: "installPlugin", Component: "view/systemTools/installPlugin/index.vue", Sort: 1, Title: "插件安装", Icon: "box"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["plugin"], Path: "pubPlug", Name: "pubPlug", Component: "view/systemTools/pubPlug/pubPlug.vue", Sort: 3, Title: "打包插件", Icon: "files"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["plugin"], Path: "plugin-email", Name: "plugin-email", Component: "plugin/email/view/index.vue", Sort: 4, Title: "邮件插件", Icon: "message"},
{MenuLevel: 1, Hidden: false, ParentID: menuNameMap["plugin"], Path: "anInfo", Name: "anInfo", Component: "plugin/announcement/view/info.vue", Sort: 5, Title: "公告管理[示例]", Icon: "scaleToOriginal"},
}
// 创建子菜单
if err = db.Create(&childMenus).Error; err != nil {
return ctx, errors.Wrap(err, model.TableNameSysBaseMenu+"子菜单初始化失败!")
}
// 组合所有菜单作为返回结果
allEntities := append(allMenus, childMenus...)
next = context.WithValue(ctx, i.InitializerName(), allEntities)
return next, nil
}
func (i *initMenu) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
if errors.Is(db.Where("path = ?", "autoPkg").First(&model.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) {
return false
}
return true
}
+122
View File
@@ -0,0 +1,122 @@
package system
import (
"context"
"kra/internal/biz/system"
"kra/internal/data/model"
"github.com/google/uuid"
"github.com/pkg/errors"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
)
const initOrderUser = initOrderAuthority + 1
type initUser struct{}
func NewInitUser() system.SubInitializer {
return &initUser{}
}
func (i *initUser) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
return ctx, db.AutoMigrate(&model.SysUser{})
}
func (i *initUser) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
return db.Migrator().HasTable(&model.SysUser{})
}
func (i *initUser) InitializerName() string {
return model.TableNameSysUser
}
func (i *initUser) InitializeData(ctx context.Context) (next context.Context, err error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
ap := ctx.Value("adminPassword")
apStr, ok := ap.(string)
if !ok {
apStr = "123456"
}
password := bcryptHash(apStr)
adminPassword := bcryptHash(apStr)
entities := []model.SysUser{
{
UUID: uuid.New().String(),
Username: "admin",
Password: adminPassword,
NickName: "超级管理员",
HeaderImg: "https://qmplusimg.henrongyi.top/1576554439myAvatar.png",
AuthorityID: 888,
Phone: "17611111111",
Email: "333333333@qq.com",
},
{
UUID: uuid.New().String(),
Username: "a303176530",
Password: password,
NickName: "用户1",
HeaderImg: "https://qmplusimg.henrongyi.top/1572075907logo.png",
AuthorityID: 9528,
Phone: "17611111111",
Email: "333333333@qq.com",
},
}
if err = db.Create(&entities).Error; err != nil {
return ctx, errors.Wrap(err, model.TableNameSysUser+"表数据初始化失败!")
}
next = context.WithValue(ctx, i.InitializerName(), entities)
// 获取权限数据
authorities, ok := ctx.Value(model.TableNameSysAuthority).([]model.SysAuthority)
if !ok {
return next, errors.Wrap(system.ErrMissingDependentContext, "创建 [用户-权限] 关联失败, 未找到权限表初始化数据")
}
// 为admin用户分配所有权限
for _, auth := range authorities {
if err = db.Exec("INSERT INTO sys_user_authority (sys_user_id, sys_authority_authority_id) VALUES (?, ?)", entities[0].ID, auth.AuthorityID).Error; err != nil {
return next, errors.Wrap(err, "为admin用户分配权限失败")
}
}
// 为普通用户分配第一个权限
if err = db.Exec("INSERT INTO sys_user_authority (sys_user_id, sys_authority_authority_id) VALUES (?, ?)", entities[1].ID, authorities[0].AuthorityID).Error; err != nil {
return next, errors.Wrap(err, "为普通用户分配权限失败")
}
return next, nil
}
func (i *initUser) DataInserted(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
var count int64
if err := db.Model(&model.SysUser{}).Where("username = ?", "a303176530").Count(&count).Error; err != nil {
return false
}
return count > 0
}
// bcryptHash 使用bcrypt对密码进行加密
func bcryptHash(password string) string {
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
return string(bytes)
}