✨ Init
This commit is contained in:
176
source/system/api.go
Normal file
176
source/system/api.go
Normal file
@@ -0,0 +1,176 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
sysModel "miniapp/model/system"
|
||||
"miniapp/service/system"
|
||||
)
|
||||
|
||||
type initApi struct{}
|
||||
|
||||
const initOrderApi = system.InitOrderSystem + 1
|
||||
|
||||
// auto run
|
||||
func init() {
|
||||
system.RegisterInit(initOrderApi, &initApi{})
|
||||
}
|
||||
|
||||
func (i initApi) InitializerName() string {
|
||||
return sysModel.SysApi{}.TableName()
|
||||
}
|
||||
|
||||
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(&sysModel.SysApi{})
|
||||
}
|
||||
|
||||
func (i *initApi) TableCreated(ctx context.Context) bool {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return db.Migrator().HasTable(&sysModel.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 := []sysModel.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: "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: "角色", 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: "/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/createPlug", Description: "自动创建插件包"},
|
||||
{ApiGroup: "代码生成器", Method: "POST", Path: "/autoCode/installPlugin", Description: "安装插件"},
|
||||
{ApiGroup: "代码生成器", Method: "POST", Path: "/autoCode/pubPlug", Description: "打包插件"},
|
||||
|
||||
{ApiGroup: "包(pkg)生成器", Method: "POST", Path: "/autoCode/createPackage", Description: "生成包(package)"},
|
||||
{ApiGroup: "包(pkg)生成器", Method: "POST", Path: "/autoCode/getPackage", Description: "获取所有包(package)"},
|
||||
{ApiGroup: "包(pkg)生成器", Method: "POST", Path: "/autoCode/delPackage", Description: "删除包(package)"},
|
||||
|
||||
{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: "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: "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: "/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/emailSend", 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: "/chatGpt/getTable", Description: "通过gpt获取内容"},
|
||||
{ApiGroup: "万用表格", Method: "POST", Path: "/chatGpt/createSK", Description: "录入sk"},
|
||||
{ApiGroup: "万用表格", Method: "GET", Path: "/chatGpt/getSK", Description: "获取sk"},
|
||||
{ApiGroup: "万用表格", Method: "DELETE", Path: "/chatGpt/deleteSK", Description: "删除sk"},
|
||||
}
|
||||
if err := db.Create(&entities).Error; err != nil {
|
||||
return ctx, errors.Wrap(err, sysModel.SysApi{}.TableName()+"表数据初始化失败!")
|
||||
}
|
||||
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(&sysModel.SysApi{}).Error, gorm.ErrRecordNotFound) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
83
source/system/authorities_menus.go
Normal file
83
source/system/authorities_menus.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
sysModel "miniapp/model/system"
|
||||
"miniapp/service/system"
|
||||
)
|
||||
|
||||
const initOrderMenuAuthority = initOrderMenu + initOrderAuthority
|
||||
|
||||
type initMenuAuthority struct{}
|
||||
|
||||
// auto run
|
||||
func init() {
|
||||
system.RegisterInit(initOrderMenuAuthority, &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_menu_authorities"
|
||||
}
|
||||
|
||||
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(initAuthority{}.InitializerName()).([]sysModel.SysAuthority)
|
||||
if !ok {
|
||||
return ctx, errors.Wrap(system.ErrMissingDependentContext, "创建 [菜单-权限] 关联失败, 未找到权限表初始化数据")
|
||||
}
|
||||
menus, ok := ctx.Value(initMenu{}.InitializerName()).([]sysModel.SysBaseMenu)
|
||||
if !ok {
|
||||
return next, errors.Wrap(errors.New(""), "创建 [菜单-权限] 关联失败, 未找到菜单表初始化数据")
|
||||
}
|
||||
next = ctx
|
||||
// 888
|
||||
if err = db.Model(&authorities[0]).Association("SysBaseMenus").Replace(menus); err != nil {
|
||||
return next, err
|
||||
}
|
||||
|
||||
// 8881
|
||||
menu8881 := menus[:2]
|
||||
menu8881 = append(menu8881, menus[7])
|
||||
if err = db.Model(&authorities[1]).Association("SysBaseMenus").Replace(menu8881); err != nil {
|
||||
return next, err
|
||||
}
|
||||
|
||||
// 9528
|
||||
if err = db.Model(&authorities[2]).Association("SysBaseMenus").Replace(menus[:11]); err != nil {
|
||||
return next, err
|
||||
}
|
||||
if err = db.Model(&authorities[2]).Association("SysBaseMenus").Append(menus[12:17]); err != nil {
|
||||
return next, err
|
||||
}
|
||||
return next, nil
|
||||
}
|
||||
|
||||
func (i *initMenuAuthority) DataInserted(ctx context.Context) bool {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
auth := &sysModel.SysAuthority{}
|
||||
if ret := db.Model(auth).
|
||||
Where("authority_id = ?", 9528).Preload("SysBaseMenus").Find(auth); ret != nil {
|
||||
if ret.Error != nil {
|
||||
return false
|
||||
}
|
||||
return len(auth.SysBaseMenus) > 0
|
||||
}
|
||||
return false
|
||||
}
|
88
source/system/authority.go
Normal file
88
source/system/authority.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
sysModel "miniapp/model/system"
|
||||
"miniapp/service/system"
|
||||
"miniapp/utils"
|
||||
)
|
||||
|
||||
const initOrderAuthority = initOrderCasbin + 1
|
||||
|
||||
type initAuthority struct{}
|
||||
|
||||
// auto run
|
||||
func init() {
|
||||
system.RegisterInit(initOrderAuthority, &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(&sysModel.SysAuthority{})
|
||||
}
|
||||
|
||||
func (i *initAuthority) TableCreated(ctx context.Context) bool {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return db.Migrator().HasTable(&sysModel.SysAuthority{})
|
||||
}
|
||||
|
||||
func (i initAuthority) InitializerName() string {
|
||||
return sysModel.SysAuthority{}.TableName()
|
||||
}
|
||||
|
||||
func (i *initAuthority) InitializeData(ctx context.Context) (context.Context, error) {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return ctx, system.ErrMissingDBContext
|
||||
}
|
||||
entities := []sysModel.SysAuthority{
|
||||
{AuthorityId: 888, AuthorityName: "普通用户", ParentId: utils.Pointer[uint](0), DefaultRouter: "dashboard"},
|
||||
{AuthorityId: 9528, AuthorityName: "测试角色", ParentId: utils.Pointer[uint](0), DefaultRouter: "dashboard"},
|
||||
{AuthorityId: 8881, AuthorityName: "普通用户子角色", ParentId: utils.Pointer[uint](888), DefaultRouter: "dashboard"},
|
||||
}
|
||||
|
||||
if err := db.Create(&entities).Error; err != nil {
|
||||
return ctx, errors.Wrapf(err, "%s表数据初始化失败!", sysModel.SysAuthority{}.TableName())
|
||||
}
|
||||
// data authority
|
||||
if err := db.Model(&entities[0]).Association("DataAuthorityId").Replace(
|
||||
[]*sysModel.SysAuthority{
|
||||
{AuthorityId: 888},
|
||||
{AuthorityId: 9528},
|
||||
{AuthorityId: 8881},
|
||||
}); err != nil {
|
||||
return ctx, errors.Wrapf(err, "%s表数据初始化失败!",
|
||||
db.Model(&entities[0]).Association("DataAuthorityId").Relationship.JoinTable.Name)
|
||||
}
|
||||
if err := db.Model(&entities[1]).Association("DataAuthorityId").Replace(
|
||||
[]*sysModel.SysAuthority{
|
||||
{AuthorityId: 9528},
|
||||
{AuthorityId: 8881},
|
||||
}); err != nil {
|
||||
return ctx, errors.Wrapf(err, "%s表数据初始化失败!",
|
||||
db.Model(&entities[1]).Association("DataAuthorityId").Relationship.JoinTable.Name)
|
||||
}
|
||||
|
||||
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(&sysModel.SysAuthority{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
258
source/system/casbin.go
Normal file
258
source/system/casbin.go
Normal file
@@ -0,0 +1,258 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
adapter "github.com/casbin/gorm-adapter/v3"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
"miniapp/service/system"
|
||||
)
|
||||
|
||||
const initOrderCasbin = initOrderApi + 1
|
||||
|
||||
type initCasbin struct{}
|
||||
|
||||
// auto run
|
||||
func init() {
|
||||
system.RegisterInit(initOrderCasbin, &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(&adapter.CasbinRule{})
|
||||
}
|
||||
|
||||
func (i *initCasbin) TableCreated(ctx context.Context) bool {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return db.Migrator().HasTable(&adapter.CasbinRule{})
|
||||
}
|
||||
|
||||
func (i initCasbin) InitializerName() string {
|
||||
var entity adapter.CasbinRule
|
||||
return entity.TableName()
|
||||
}
|
||||
|
||||
func (i *initCasbin) InitializeData(ctx context.Context) (context.Context, error) {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return ctx, system.ErrMissingDBContext
|
||||
}
|
||||
entities := []adapter.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: "/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: "/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: "/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/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: "/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: "/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: "/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: "/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: "/chatGpt/getTable", V2: "POST"},
|
||||
{Ptype: "p", V0: "888", V1: "/chatGpt/createSK", V2: "POST"},
|
||||
{Ptype: "p", V0: "888", V1: "/chatGpt/getSK", V2: "GET"},
|
||||
{Ptype: "p", V0: "888", V1: "/chatGpt/deleteSK", 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: "/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: "/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(adapter.CasbinRule{Ptype: "p", V0: "9528", V1: "/user/getUserInfo", V2: "GET"}).
|
||||
First(&adapter.CasbinRule{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
71
source/system/dictionary.go
Normal file
71
source/system/dictionary.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
sysModel "miniapp/model/system"
|
||||
"miniapp/service/system"
|
||||
)
|
||||
|
||||
const initOrderDict = initOrderCasbin + 1
|
||||
|
||||
type initDict struct{}
|
||||
|
||||
// auto run
|
||||
func init() {
|
||||
system.RegisterInit(initOrderDict, &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(&sysModel.SysDictionary{})
|
||||
}
|
||||
|
||||
func (i *initDict) TableCreated(ctx context.Context) bool {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return db.Migrator().HasTable(&sysModel.SysDictionary{})
|
||||
}
|
||||
|
||||
func (i initDict) InitializerName() string {
|
||||
return sysModel.SysDictionary{}.TableName()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
True := true
|
||||
entities := []sysModel.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, sysModel.SysDictionary{}.TableName()+"表数据初始化失败!")
|
||||
}
|
||||
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(&sysModel.SysDictionary{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
113
source/system/dictionary_detail.go
Normal file
113
source/system/dictionary_detail.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
sysModel "miniapp/model/system"
|
||||
"miniapp/service/system"
|
||||
)
|
||||
|
||||
const initOrderDictDetail = initOrderDict + 1
|
||||
|
||||
type initDictDetail struct{}
|
||||
|
||||
// auto run
|
||||
func init() {
|
||||
system.RegisterInit(initOrderDictDetail, &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(&sysModel.SysDictionaryDetail{})
|
||||
}
|
||||
|
||||
func (i *initDictDetail) TableCreated(ctx context.Context) bool {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return db.Migrator().HasTable(&sysModel.SysDictionaryDetail{})
|
||||
}
|
||||
|
||||
func (i initDictDetail) InitializerName() string {
|
||||
return sysModel.SysDictionaryDetail{}.TableName()
|
||||
}
|
||||
|
||||
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(initDict{}.InitializerName()).([]sysModel.SysDictionary)
|
||||
if !ok {
|
||||
return ctx, errors.Wrap(system.ErrMissingDependentContext,
|
||||
fmt.Sprintf("未找到 %s 表初始化数据", sysModel.SysDictionary{}.TableName()))
|
||||
}
|
||||
True := true
|
||||
dicts[0].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
|
||||
{Label: "男", Value: 1, Status: &True, Sort: 1},
|
||||
{Label: "女", Value: 2, Status: &True, Sort: 2},
|
||||
}
|
||||
|
||||
dicts[1].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
|
||||
{Label: "smallint", Value: 1, Status: &True, Sort: 1},
|
||||
{Label: "mediumint", Value: 2, Status: &True, Sort: 2},
|
||||
{Label: "int", Value: 3, Status: &True, Sort: 3},
|
||||
{Label: "bigint", Value: 4, Status: &True, Sort: 4},
|
||||
}
|
||||
|
||||
dicts[2].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
|
||||
{Label: "date", Status: &True},
|
||||
{Label: "time", Value: 1, Status: &True, Sort: 1},
|
||||
{Label: "year", Value: 2, Status: &True, Sort: 2},
|
||||
{Label: "datetime", Value: 3, Status: &True, Sort: 3},
|
||||
{Label: "timestamp", Value: 5, Status: &True, Sort: 5},
|
||||
}
|
||||
dicts[3].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
|
||||
{Label: "float", Status: &True},
|
||||
{Label: "double", Value: 1, Status: &True, Sort: 1},
|
||||
{Label: "decimal", Value: 2, Status: &True, Sort: 2},
|
||||
}
|
||||
|
||||
dicts[4].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
|
||||
{Label: "char", Status: &True},
|
||||
{Label: "varchar", Value: 1, Status: &True, Sort: 1},
|
||||
{Label: "tinyblob", Value: 2, Status: &True, Sort: 2},
|
||||
{Label: "tinytext", Value: 3, Status: &True, Sort: 3},
|
||||
{Label: "text", Value: 4, Status: &True, Sort: 4},
|
||||
{Label: "blob", Value: 5, Status: &True, Sort: 5},
|
||||
{Label: "mediumblob", Value: 6, Status: &True, Sort: 6},
|
||||
{Label: "mediumtext", Value: 7, Status: &True, Sort: 7},
|
||||
{Label: "longblob", Value: 8, Status: &True, Sort: 8},
|
||||
{Label: "longtext", Value: 9, Status: &True, Sort: 9},
|
||||
}
|
||||
|
||||
dicts[5].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
|
||||
{Label: "tinyint", Status: &True},
|
||||
}
|
||||
for _, dict := range dicts {
|
||||
if err := db.Model(&dict).Association("SysDictionaryDetails").
|
||||
Replace(dict.SysDictionaryDetails); err != nil {
|
||||
return ctx, errors.Wrap(err, sysModel.SysDictionaryDetail{}.TableName()+"表数据初始化失败!")
|
||||
}
|
||||
}
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
func (i *initDictDetail) DataInserted(ctx context.Context) bool {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
var dict sysModel.SysDictionary
|
||||
if err := db.Preload("SysDictionaryDetails").
|
||||
First(&dict, &sysModel.SysDictionary{Name: "数据库bool类型"}).Error; err != nil {
|
||||
return false
|
||||
}
|
||||
return len(dict.SysDictionaryDetails) > 0 && dict.SysDictionaryDetails[0].Label == "tinyint"
|
||||
}
|
101
source/system/menu.go
Normal file
101
source/system/menu.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
. "miniapp/model/system"
|
||||
"miniapp/service/system"
|
||||
)
|
||||
|
||||
const initOrderMenu = initOrderAuthority + 1
|
||||
|
||||
type initMenu struct{}
|
||||
|
||||
// auto run
|
||||
func init() {
|
||||
system.RegisterInit(initOrderMenu, &initMenu{})
|
||||
}
|
||||
|
||||
func (i initMenu) InitializerName() string {
|
||||
return SysBaseMenu{}.TableName()
|
||||
}
|
||||
|
||||
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(
|
||||
&SysBaseMenu{},
|
||||
&SysBaseMenuParameter{},
|
||||
&SysBaseMenuBtn{},
|
||||
)
|
||||
}
|
||||
|
||||
func (i *initMenu) TableCreated(ctx context.Context) bool {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
m := db.Migrator()
|
||||
return m.HasTable(&SysBaseMenu{}) &&
|
||||
m.HasTable(&SysBaseMenuParameter{}) &&
|
||||
m.HasTable(&SysBaseMenuBtn{})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
entities := []SysBaseMenu{
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "0", Path: "dashboard", Name: "dashboard", Component: "view/dashboard/index.vue", Sort: 1, Meta: Meta{Title: "仪表盘", Icon: "odometer"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "0", Path: "about", Name: "about", Component: "view/about/index.vue", Sort: 9, Meta: Meta{Title: "关于我们", Icon: "info-filled"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "0", Path: "admin", Name: "superAdmin", Component: "view/superAdmin/index.vue", Sort: 3, Meta: Meta{Title: "超级管理员", Icon: "user"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "3", Path: "authority", Name: "authority", Component: "view/superAdmin/authority/authority.vue", Sort: 1, Meta: Meta{Title: "角色管理", Icon: "avatar"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "3", Path: "menu", Name: "menu", Component: "view/superAdmin/menu/menu.vue", Sort: 2, Meta: Meta{Title: "菜单管理", Icon: "tickets", KeepAlive: true}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "3", Path: "api", Name: "api", Component: "view/superAdmin/api/api.vue", Sort: 3, Meta: Meta{Title: "api管理", Icon: "platform", KeepAlive: true}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "3", Path: "user", Name: "user", Component: "view/superAdmin/user/user.vue", Sort: 4, Meta: Meta{Title: "用户管理", Icon: "coordinate"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "3", Path: "dictionary", Name: "dictionary", Component: "view/superAdmin/dictionary/sysDictionary.vue", Sort: 5, Meta: Meta{Title: "字典管理", Icon: "notebook"}},
|
||||
{MenuLevel: 0, Hidden: true, ParentId: "3", Path: "dictionaryDetail/:id", Name: "dictionaryDetail", Component: "view/superAdmin/dictionary/sysDictionaryDetail.vue", Sort: 1, Meta: Meta{Title: "字典详情-${id}", Icon: "list", ActiveName: "dictionary"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "3", Path: "operation", Name: "operation", Component: "view/superAdmin/operation/sysOperationRecord.vue", Sort: 6, Meta: Meta{Title: "操作历史", Icon: "pie-chart"}},
|
||||
{MenuLevel: 0, Hidden: true, ParentId: "0", Path: "person", Name: "person", Component: "view/person/person.vue", Sort: 4, Meta: Meta{Title: "个人信息", Icon: "message"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "0", Path: "example", Name: "example", Component: "view/example/index.vue", Sort: 7, Meta: Meta{Title: "示例文件", Icon: "management"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "12", Path: "upload", Name: "upload", Component: "view/example/upload/upload.vue", Sort: 5, Meta: Meta{Title: "媒体库(上传下载)", Icon: "upload"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "12", Path: "breakpoint", Name: "breakpoint", Component: "view/example/breakpoint/breakpoint.vue", Sort: 6, Meta: Meta{Title: "断点续传", Icon: "upload-filled"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "12", Path: "customer", Name: "customer", Component: "view/example/customer/customer.vue", Sort: 7, Meta: Meta{Title: "客户列表(资源示例)", Icon: "avatar"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "0", Path: "systemTools", Name: "systemTools", Component: "view/systemTools/index.vue", Sort: 5, Meta: Meta{Title: "系统工具", Icon: "tools"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "16", Path: "autoCode", Name: "autoCode", Component: "view/systemTools/autoCode/index.vue", Sort: 1, Meta: Meta{Title: "代码生成器", Icon: "cpu", KeepAlive: true}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "16", Path: "formCreate", Name: "formCreate", Component: "view/systemTools/formCreate/index.vue", Sort: 2, Meta: Meta{Title: "表单生成器", Icon: "magic-stick", KeepAlive: true}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "16", Path: "system", Name: "system", Component: "view/systemTools/system/system.vue", Sort: 3, Meta: Meta{Title: "系统配置", Icon: "operation"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "16", Path: "autoCodeAdmin", Name: "autoCodeAdmin", Component: "view/systemTools/autoCodeAdmin/index.vue", Sort: 1, Meta: Meta{Title: "自动化代码管理", Icon: "magic-stick"}},
|
||||
{MenuLevel: 0, Hidden: true, ParentId: "16", Path: "autoCodeEdit/:id", Name: "autoCodeEdit", Component: "view/systemTools/autoCode/index.vue", Sort: 0, Meta: Meta{Title: "自动化代码-${id}", Icon: "magic-stick"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "16", Path: "autoPkg", Name: "autoPkg", Component: "view/systemTools/autoPkg/autoPkg.vue", Sort: 0, Meta: Meta{Title: "自动化package", Icon: "folder"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "0", Path: "https://www.gin-vue-admin.com", Name: "https://www.gin-vue-admin.com", Component: "/", Sort: 0, Meta: Meta{Title: "官方网站", Icon: "home-filled"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "0", Path: "state", Name: "state", Component: "view/system/state.vue", Sort: 8, Meta: Meta{Title: "服务器状态", Icon: "cloudy"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "0", Path: "plugin", Name: "plugin", Component: "view/routerHolder.vue", Sort: 6, Meta: Meta{Title: "插件系统", Icon: "cherry"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "25", Path: "https://plugin.gin-vue-admin.com/", Name: "https://plugin.gin-vue-admin.com/", Component: "https://plugin.gin-vue-admin.com/", Sort: 0, Meta: Meta{Title: "插件市场", Icon: "shop"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "25", Path: "installPlugin", Name: "installPlugin", Component: "view/systemTools/installPlugin/index.vue", Sort: 1, Meta: Meta{Title: "插件安装", Icon: "box"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "25", Path: "autoPlug", Name: "autoPlug", Component: "view/systemTools/autoPlug/autoPlug.vue", Sort: 2, Meta: Meta{Title: "插件模板", Icon: "folder"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "25", Path: "pubPlug", Name: "pubPlug", Component: "view/systemTools/pubPlug/pubPlug.vue", Sort: 3, Meta: Meta{Title: "打包插件", Icon: "files"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "25", Path: "plugin-email", Name: "plugin-email", Component: "plugin/email/view/index.vue", Sort: 4, Meta: Meta{Title: "邮件插件", Icon: "message"}},
|
||||
{MenuLevel: 0, Hidden: false, ParentId: "16", Path: "chatTable", Name: "chatTable", Component: "view/chatgpt/chatTable.vue", Sort: 6, Meta: Meta{Title: "万用表格", Icon: "chat-dot-square"}},
|
||||
}
|
||||
if err = db.Create(&entities).Error; err != nil {
|
||||
return ctx, errors.Wrap(err, SysBaseMenu{}.TableName()+"表数据初始化失败!")
|
||||
}
|
||||
next = context.WithValue(ctx, i.InitializerName(), entities)
|
||||
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(&SysBaseMenu{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
99
source/system/user.go
Normal file
99
source/system/user.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gofrs/uuid/v5"
|
||||
"github.com/pkg/errors"
|
||||
"gorm.io/gorm"
|
||||
sysModel "miniapp/model/system"
|
||||
"miniapp/service/system"
|
||||
"miniapp/utils"
|
||||
)
|
||||
|
||||
const initOrderUser = initOrderAuthority + 1
|
||||
|
||||
type initUser struct{}
|
||||
|
||||
// auto run
|
||||
func init() {
|
||||
system.RegisterInit(initOrderUser, &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(&sysModel.SysUser{}, &sysModel.SysChatGptOption{})
|
||||
}
|
||||
|
||||
func (i *initUser) TableCreated(ctx context.Context) bool {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
return db.Migrator().HasTable(&sysModel.SysUser{})
|
||||
}
|
||||
|
||||
func (i initUser) InitializerName() string {
|
||||
return sysModel.SysUser{}.TableName()
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
password := utils.BcryptHash("6447985")
|
||||
adminPassword := utils.BcryptHash("123456")
|
||||
|
||||
entities := []sysModel.SysUser{
|
||||
{
|
||||
UUID: uuid.Must(uuid.NewV4()),
|
||||
Username: "admin",
|
||||
Password: adminPassword,
|
||||
NickName: "Mr.奇淼",
|
||||
HeaderImg: "https://qmplusimg.henrongyi.top/gva_header.jpg",
|
||||
AuthorityId: 888,
|
||||
Phone: "17611111111",
|
||||
Email: "333333333@qq.com",
|
||||
},
|
||||
{
|
||||
UUID: uuid.Must(uuid.NewV4()),
|
||||
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, sysModel.SysUser{}.TableName()+"表数据初始化失败!")
|
||||
}
|
||||
next = context.WithValue(ctx, i.InitializerName(), entities)
|
||||
authorityEntities, ok := ctx.Value(initAuthority{}.InitializerName()).([]sysModel.SysAuthority)
|
||||
if !ok {
|
||||
return next, errors.Wrap(system.ErrMissingDependentContext, "创建 [用户-权限] 关联失败, 未找到权限表初始化数据")
|
||||
}
|
||||
if err = db.Model(&entities[0]).Association("Authorities").Replace(authorityEntities); err != nil {
|
||||
return next, err
|
||||
}
|
||||
if err = db.Model(&entities[1]).Association("Authorities").Replace(authorityEntities[:1]); err != nil {
|
||||
return next, err
|
||||
}
|
||||
return next, err
|
||||
}
|
||||
|
||||
func (i *initUser) DataInserted(ctx context.Context) bool {
|
||||
db, ok := ctx.Value("db").(*gorm.DB)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
var record sysModel.SysUser
|
||||
if errors.Is(db.Where("username = ?", "a303176530").
|
||||
Preload("Authorities").First(&record).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据
|
||||
return false
|
||||
}
|
||||
return len(record.Authorities) > 0 && record.Authorities[0].AuthorityId == 888
|
||||
}
|
Reference in New Issue
Block a user