From 48cc4d0c6f8da4bdd4b018d104e1a8c0c5b118e5 Mon Sep 17 00:00:00 2001 From: SliverHorn <503551462@qq.com> Date: Tue, 16 Nov 2021 02:02:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20-=20source=20=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=E6=95=B0=E6=8D=AE=20new=20=E5=AE=9E=E7=8E=B0=20-=20se?= =?UTF-8?q?rvice=20mysql=20=E4=B8=8E=20pgsql=20=E5=88=86=E5=BC=80=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=20-=20=E4=BB=A3=E7=A0=81=E5=A4=87=E6=B3=A8=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/go.mod | 2 + server/initialize/gorm_mysql.go | 7 +- server/initialize/gorm_pgsql.go | 4 +- server/initialize/internal/gorm.go | 2 + server/initialize/internal/logger.go | 4 +- server/model/system/request/sys_init.go | 55 +++++ server/model/system/sys_initdb.go | 74 +++++++ server/service/system/sys_initdb.go | 170 ++------------ server/service/system/sys_initdb_mysql.go | 116 ++++++++++ server/service/system/sys_initdb_pgsql.go | 105 +++++++++ server/source/example/file.go | 34 +++ server/source/system/api.go | 126 +++++++++++ server/source/system/authorities_menus.go | 95 ++++++++ server/source/system/authority.go | 35 +++ server/source/system/casbin.go | 208 ++++++++++++++++++ server/source/system/data_authorities.go | 56 +++++ server/source/system/dictionary.go | 40 ++++ server/source/system/dictionary_detail.go | 57 +++++ server/source/system/menu.go | 57 +++++ server/source/system/user.go | 35 +++ server/source/system/user_authority.go | 37 ++++ .../system/view_authority_menu_mysql.go | 39 ++++ .../system/view_authority_menu_postgres.go | 58 +++++ 23 files changed, 1256 insertions(+), 160 deletions(-) create mode 100644 server/service/system/sys_initdb_mysql.go create mode 100644 server/service/system/sys_initdb_pgsql.go create mode 100644 server/source/example/file.go create mode 100644 server/source/system/api.go create mode 100644 server/source/system/authorities_menus.go create mode 100644 server/source/system/authority.go create mode 100644 server/source/system/casbin.go create mode 100644 server/source/system/data_authorities.go create mode 100644 server/source/system/dictionary.go create mode 100644 server/source/system/dictionary_detail.go create mode 100644 server/source/system/menu.go create mode 100644 server/source/system/user.go create mode 100644 server/source/system/user_authority.go create mode 100644 server/source/system/view_authority_menu_mysql.go create mode 100644 server/source/system/view_authority_menu_postgres.go diff --git a/server/go.mod b/server/go.mod index 33c647e2..365a8f2b 100644 --- a/server/go.mod +++ b/server/go.mod @@ -16,9 +16,11 @@ require ( github.com/go-redis/redis/v8 v8.11.0 github.com/go-sql-driver/mysql v1.5.0 github.com/gookit/color v1.3.1 + github.com/jackc/pgx/v4 v4.7.1 github.com/jordan-wright/email v0.0.0-20200824153738-3f5bafa1cd84 github.com/mojocn/base64Captcha v1.3.1 github.com/natefinch/lumberjack v2.0.0+incompatible + github.com/pkg/errors v0.9.1 github.com/qiniu/api.v7/v7 v7.4.1 github.com/robfig/cron/v3 v3.0.1 github.com/satori/go.uuid v1.2.0 diff --git a/server/initialize/gorm_mysql.go b/server/initialize/gorm_mysql.go index c0baaf8a..851fa8de 100644 --- a/server/initialize/gorm_mysql.go +++ b/server/initialize/gorm_mysql.go @@ -8,8 +8,8 @@ import ( ) // GormMysql 初始化Mysql数据库 -// Author piexlmax -// Author SliverHorn +// Author [piexlmax](https://github.com/piexlmax) +// Author [SliverHorn](https://github.com/SliverHorn) func GormMysql() *gorm.DB { m := global.GVA_CONFIG.Mysql if m.Dbname == "" { @@ -18,9 +18,6 @@ func GormMysql() *gorm.DB { mysqlConfig := mysql.Config{ DSN: m.Dsn(), // DSN data source name DefaultStringSize: 191, // string 类型字段的默认长度 - DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持 - DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引 - DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列 SkipInitializeWithVersion: false, // 根据版本自动配置 } if db, err := gorm.Open(mysql.New(mysqlConfig), internal.Gorm.Config()); err != nil { diff --git a/server/initialize/gorm_pgsql.go b/server/initialize/gorm_pgsql.go index b9a3b30b..e178a277 100644 --- a/server/initialize/gorm_pgsql.go +++ b/server/initialize/gorm_pgsql.go @@ -8,8 +8,8 @@ import ( ) // GormPgSql 初始化 Postgresql 数据库 -// Author piexlmax -// Author SliverHorn +// Author [piexlmax](https://github.com/piexlmax) +// Author [SliverHorn](https://github.com/SliverHorn) func GormPgSql() *gorm.DB { p := global.GVA_CONFIG.Pgsql if p.Dbname == "" { diff --git a/server/initialize/internal/gorm.go b/server/initialize/internal/gorm.go index 3b34a78e..0502857a 100644 --- a/server/initialize/internal/gorm.go +++ b/server/initialize/internal/gorm.go @@ -13,6 +13,8 @@ var Gorm = new(_gorm) type _gorm struct{} +// Config gorm 自定义配置 +// Author [SliverHorn](https://github.com/SliverHorn) func (g *_gorm) Config() *gorm.Config { config := &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true} _default := logger.New(NewWriter(log.New(os.Stdout, "\r\n", log.LstdFlags)), logger.Config{ diff --git a/server/initialize/internal/logger.go b/server/initialize/internal/logger.go index e04a5a2d..89bd14a3 100644 --- a/server/initialize/internal/logger.go +++ b/server/initialize/internal/logger.go @@ -11,13 +11,13 @@ type writer struct { } // NewWriter writer 构造函数 -// Author SliverHorn +// Author [SliverHorn](https://github.com/SliverHorn) func NewWriter(w logger.Writer) *writer { return &writer{Writer: w} } // Printf 格式化打印日志 -// Author SliverHorn +// Author [SliverHorn](https://github.com/SliverHorn) func (w *writer) Printf(message string, data ...interface{}) { var logZap bool switch global.GVA_CONFIG.System.DbType { diff --git a/server/model/system/request/sys_init.go b/server/model/system/request/sys_init.go index 90b19abb..da12652d 100644 --- a/server/model/system/request/sys_init.go +++ b/server/model/system/request/sys_init.go @@ -1,5 +1,10 @@ package request +import ( + "fmt" + "github.com/flipped-aurora/gin-vue-admin/server/config" +) + type InitDB struct { DBType string `json:"DBType"` // 数据库类型 Host string `json:"host"` // 服务器地址 @@ -8,3 +13,53 @@ type InitDB struct { Password string `json:"password"` // 数据库密码 DBName string `json:"dbName" binding:"required"` // 数据库名 } + +// MysqlEmptyDsn msyql 空数据库 建库链接 +// Author SliverHorn +func (i *InitDB) MysqlEmptyDsn() string { + if i.Host == "" { + i.Host = "127.0.0.1" + } + if i.Port == "" { + i.Port = "3306" + } + return fmt.Sprintf("%s:%s@tcp(%s:%s)/", i.UserName, i.Password, i.Host, i.Port) +} + +// PgsqlEmptyDsn pgsql 空数据库 建库链接 +// Author SliverHorn +func (i *InitDB) PgsqlEmptyDsn() string { + if i.Host == "" { + i.Host = "127.0.0.1" + } + if i.Port == "" { + i.Port = "3306" + } + return "host=" + i.Host + " user=" + i.UserName + " password=" + i.Password + " port=" + i.Port + " " + "sslmode=disable TimeZone=Asia/Shanghai" +} + +// ToMysqlConfig 转换 config.Mysql +// Author [SliverHorn](https://github.com/SliverHorn) +func (i *InitDB) ToMysqlConfig() config.Mysql { + return config.Mysql{ + Path: i.Host, + Port: i.Port, + Dbname: i.DBName, + Username: i.UserName, + Password: i.Password, + Config: "charset=utf8mb4&parseTime=True&loc=Local", + } +} + +// ToPgsqlConfig 转换 config.Pgsql +// Author [SliverHorn](https://github.com/SliverHorn) +func (i *InitDB) ToPgsqlConfig() config.Pgsql { + return config.Pgsql{ + Path: i.Host, + Port: i.Port, + Dbname: i.DBName, + Username: i.UserName, + Password: i.Password, + Config: "sslmode=disable TimeZone=Asia/Shanghai", + } +} diff --git a/server/model/system/sys_initdb.go b/server/model/system/sys_initdb.go index ae05a748..46901b85 100644 --- a/server/model/system/sys_initdb.go +++ b/server/model/system/sys_initdb.go @@ -1,5 +1,79 @@ package system +import "github.com/gookit/color" + type InitDBFunc interface { Init() (err error) } + +const ( + Mysql = "mysql" + Pgsql = "pgsql" + InitSuccess = "\n[%v] --> 初始数据成功!\n" + AuthorityMenu = "\n[%v] --> %v 视图已存在!\n" + InitDataExist = "\n[%v] --> %v 表的初始数据已存在!\n" + InitDataFailed = "\n[%v] --> %v 表初始数据失败! \nerr: %+v\n" + InitDataSuccess = "\n[%v] --> %v 表初始数据成功!\n" +) + +type InitData interface { + TableName() string + Initialize() error + CheckDataExist() bool +} + +// MysqlDataInitialize Mysql 初始化接口使用封装 +// Author [SliverHorn](https://github.com/SliverHorn) +func MysqlDataInitialize(inits ...InitData) error { + var entity SysMenu + for i := 0; i < len(inits); i++ { + if inits[i].TableName() == entity.TableName() { + if k := inits[i].CheckDataExist(); k { + color.Info.Printf(AuthorityMenu, Mysql, inits[i].TableName()) + continue + } + } else { + if inits[i].CheckDataExist() { + color.Info.Printf(InitDataExist, Mysql, inits[i].TableName()) + continue + } + } + + if err := inits[i].Initialize(); err != nil { + color.Info.Printf(InitDataFailed, Mysql, err) + continue + } else { + color.Info.Printf(InitDataSuccess, Mysql, inits[i].TableName()) + } + } + color.Info.Printf(InitSuccess, Mysql) + return nil +} + +// PgsqlDataInitialize Pgsql 初始化接口使用封装 +// Author [SliverHorn](https://github.com/SliverHorn) +func PgsqlDataInitialize(inits ...InitData) error { + var entity SysMenu + for i := 0; i < len(inits); i++ { + if inits[i].TableName() == entity.TableName() { + if k := inits[i].CheckDataExist(); k { + color.Info.Printf(AuthorityMenu, Pgsql, inits[i].TableName()) + continue + } + } else { + if inits[i].CheckDataExist() { + color.Info.Printf(InitDataExist, Pgsql, inits[i].TableName()) + continue + } + } + + if err := inits[i].Initialize(); err != nil { + color.Info.Printf(InitDataFailed, Pgsql, err) + continue + } else { + color.Info.Printf(InitDataSuccess, Pgsql, inits[i].TableName()) + } + } + color.Info.Printf(InitSuccess, Pgsql) + return nil +} diff --git a/server/service/system/sys_initdb.go b/server/service/system/sys_initdb.go index afd15e5e..ca676739 100644 --- a/server/service/system/sys_initdb.go +++ b/server/service/system/sys_initdb.go @@ -1,181 +1,49 @@ package system import ( - "database/sql" - "fmt" - "path/filepath" - - uuid "github.com/satori/go.uuid" - - "github.com/flipped-aurora/gin-vue-admin/server/config" "github.com/flipped-aurora/gin-vue-admin/server/global" "github.com/flipped-aurora/gin-vue-admin/server/model/example" "github.com/flipped-aurora/gin-vue-admin/server/model/system" "github.com/flipped-aurora/gin-vue-admin/server/model/system/request" - "github.com/flipped-aurora/gin-vue-admin/server/source" - "github.com/flipped-aurora/gin-vue-admin/server/utils" - - "github.com/spf13/viper" - "gorm.io/driver/mysql" - "gorm.io/gorm" + _ "github.com/jackc/pgx/v4" ) -//@author: [songzhibin97](https://github.com/songzhibin97) -//@function: writeConfig -//@description: 回写配置 -//@param: viper *viper.Viper, mysql config.Mysql -//@return: error - -type InitDBService struct { -} - -func (initDBService *InitDBService) writeConfig(viper *viper.Viper, mysql config.Mysql) error { - global.GVA_CONFIG.Mysql = mysql - cs := utils.StructToMap(global.GVA_CONFIG) - for k, v := range cs { - viper.Set(k, v) - } - viper.Set("jwt.signing-key", uuid.NewV4()) - return viper.WriteConfig() -} - -//@author: [songzhibin97](https://github.com/songzhibin97) -//@function: createTable -//@description: 创建数据库(mysql) -//@param: dsn string, driver string, createSql -//@return: error - -func (initDBService *InitDBService) createTable(dsn string, driver string, createSql string) error { - db, err := sql.Open(driver, dsn) - if err != nil { - return err - } - defer func(db *sql.DB) { - err := db.Close() - if err != nil { - fmt.Println(err) - } - }(db) - if err = db.Ping(); err != nil { - return err - } - _, err = db.Exec(createSql) - return err -} - -func (initDBService *InitDBService) initDB(InitDBFunctions ...system.InitDBFunc) (err error) { - for _, v := range InitDBFunctions { - err = v.Init() - if err != nil { - return err - } - } - return nil -} - -//@author: [songzhibin97](https://github.com/songzhibin97) -//@function: InitDB -//@description: 创建数据库并初始化 -//@param: conf request.InitDB -//@return: error +type InitDBService struct{} +// InitDB 创建数据库并初始化 总入口 +// Author [piexlmax](https://github.com/piexlmax) +// Author [SliverHorn](https://github.com/SliverHorn) +// Author [songzhibin97](https://github.com/songzhibin97) func (initDBService *InitDBService) InitDB(conf request.InitDB) error { switch conf.DBType { case "mysql": return initDBService.initMsqlDB(conf) case "pgsql": - return nil + return initDBService.initPgsqlDB(conf) default: return initDBService.initMsqlDB(conf) } } -func (initDBService *InitDBService) initMsqlDB(conf request.InitDB) error { - if conf.Host == "" { - conf.Host = "127.0.0.1" - } - - if conf.Port == "" { - conf.Port = "3306" - } - dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/", conf.UserName, conf.Password, conf.Host, conf.Port) - createSql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s` DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;", conf.DBName) - if err := initDBService.createTable(dsn, "mysql", createSql); err != nil { - return err - } - - MysqlConfig := config.Mysql{ - Path: fmt.Sprintf("%s:%s", conf.Host, conf.Port), - Dbname: conf.DBName, - Username: conf.UserName, - Password: conf.Password, - Config: "charset=utf8mb4&parseTime=True&loc=Local", - } - - if MysqlConfig.Dbname == "" { - return nil - } - - linkDns := MysqlConfig.Username + ":" + MysqlConfig.Password + "@tcp(" + MysqlConfig.Path + ")/" + MysqlConfig.Dbname + "?" + MysqlConfig.Config - mysqlConfig := mysql.Config{ - DSN: linkDns, // DSN data source name - DefaultStringSize: 191, // string 类型字段的默认长度 - DisableDatetimePrecision: true, // 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持 - DontSupportRenameIndex: true, // 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引 - DontSupportRenameColumn: true, // 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列 - SkipInitializeWithVersion: false, // 根据版本自动配置 - } - if db, err := gorm.Open(mysql.New(mysqlConfig), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil { - return nil - } else { - sqlDB, _ := db.DB() - sqlDB.SetMaxIdleConns(MysqlConfig.MaxIdleConns) - sqlDB.SetMaxOpenConns(MysqlConfig.MaxOpenConns) - global.GVA_DB = db - } - - err := global.GVA_DB.AutoMigrate( - system.SysUser{}, - system.SysAuthority{}, +// initTables 初始化表 +// Author SliverHorn +func (initDBService *InitDBService) initTables() error { + return global.GVA_DB.AutoMigrate( system.SysApi{}, + system.SysUser{}, system.SysBaseMenu{}, - system.SysBaseMenuParameter{}, + system.SysAuthority{}, system.JwtBlacklist{}, system.SysDictionary{}, + system.SysAutoCodeHistory{}, + system.SysOperationRecord{}, system.SysDictionaryDetail{}, - example.ExaFileUploadAndDownload{}, + system.SysBaseMenuParameter{}, + example.ExaFile{}, - example.ExaFileChunk{}, example.ExaCustomer{}, - system.SysOperationRecord{}, - system.SysAutoCodeHistory{}, - ) - if err != nil { - global.GVA_DB = nil - return err - } - err = initDBService.initDB( - source.Admin, - source.Api, - source.AuthorityMenu, - source.Authority, - source.AuthoritiesMenus, - source.Casbin, - source.DataAuthorities, - source.Dictionary, - source.DictionaryDetail, - source.File, - source.BaseMenu, - source.UserAuthority, + example.ExaFileChunk{}, + example.ExaFileUploadAndDownload{}, ) - if err != nil { - global.GVA_DB = nil - return err - } - if err = initDBService.writeConfig(global.GVA_VP, MysqlConfig); err != nil { - return err - } - global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..") - return nil } diff --git a/server/service/system/sys_initdb_mysql.go b/server/service/system/sys_initdb_mysql.go new file mode 100644 index 00000000..c26b8da1 --- /dev/null +++ b/server/service/system/sys_initdb_mysql.go @@ -0,0 +1,116 @@ +package system + +import ( + "database/sql" + "fmt" + "github.com/flipped-aurora/gin-vue-admin/server/config" + "github.com/flipped-aurora/gin-vue-admin/server/global" + model "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/flipped-aurora/gin-vue-admin/server/model/system/request" + "github.com/flipped-aurora/gin-vue-admin/server/source/example" + "github.com/flipped-aurora/gin-vue-admin/server/source/system" + "github.com/flipped-aurora/gin-vue-admin/server/utils" + uuid "github.com/satori/go.uuid" + "gorm.io/driver/mysql" + "gorm.io/gorm" + "path/filepath" +) + +// writeMysqlConfig mysql回写配置 +// Author [SliverHorn](https://github.com/SliverHorn) +// Author [songzhibin97](https://github.com/songzhibin97) +func (initDBService *InitDBService) writeMysqlConfig(mysql config.Mysql) error { + global.GVA_CONFIG.Mysql = mysql + cs := utils.StructToMap(global.GVA_CONFIG) + for k, v := range cs { + global.GVA_VP.Set(k, v) + } + global.GVA_VP.Set("jwt.signing-key", uuid.NewV4()) + return global.GVA_VP.WriteConfig() +} + +// createDatabase 创建数据库(mysql) +// Author [SliverHorn](https://github.com/SliverHorn) +// Author: [songzhibin97](https://github.com/songzhibin97) +func (initDBService *InitDBService) createDatabase(dsn string, driver string, createSql string) error { + db, err := sql.Open(driver, dsn) + if err != nil { + return err + } + defer func(db *sql.DB) { + err = db.Close() + if err != nil { + fmt.Println(err) + } + }(db) + if err = db.Ping(); err != nil { + return err + } + _, err = db.Exec(createSql) + return err +} + +// initMsqlDB 创建数据库并初始化 mysql +// Author [piexlmax](https://github.com/piexlmax) +// Author [SliverHorn](https://github.com/SliverHorn) +// Author: [songzhibin97](https://github.com/songzhibin97) +func (initDBService *InitDBService) initMsqlDB(conf request.InitDB) error { + dsn := conf.MysqlEmptyDsn() + createSql := fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s` DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_general_ci;", conf.DBName) + if err := initDBService.createDatabase(dsn, "mysql", createSql); err != nil { + return err + } // 创建数据库 + + mysqlConfig := conf.ToMysqlConfig() + if mysqlConfig.Dbname == "" { + return nil + } // 如果没有数据库名, 则跳出初始化数据 + + if db, err := gorm.Open(mysql.New(mysql.Config{ + DSN: mysqlConfig.Dsn(), // DSN data source name + DefaultStringSize: 191, // string 类型字段的默认长度 + SkipInitializeWithVersion: true, // 根据版本自动配置 + }), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil { + return nil + } else { + global.GVA_DB = db + } + + if err := initDBService.initTables(); err != nil { + global.GVA_DB = nil + return err + } + + if err := initDBService.initMysqlData(); err != nil { + global.GVA_DB = nil + return err + } + + if err := initDBService.writeMysqlConfig(mysqlConfig); err != nil { + return err + } + + global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..") + return nil +} + +// initData mysql 初始化数据 +// Author [SliverHorn](https://github.com/SliverHorn) +// Author: [songzhibin97](https://github.com/songzhibin97) +func (initDBService *InitDBService) initMysqlData() error { + return model.MysqlDataInitialize( + system.Api, + system.User, + system.Casbin, + system.BaseMenu, + system.Authority, + system.Dictionary, + system.UserAuthority, + system.DataAuthorities, + system.AuthoritiesMenus, + system.DictionaryDetail, + system.ViewAuthorityMenuMysql, + + example.File, + ) +} diff --git a/server/service/system/sys_initdb_pgsql.go b/server/service/system/sys_initdb_pgsql.go new file mode 100644 index 00000000..8f2d4bc9 --- /dev/null +++ b/server/service/system/sys_initdb_pgsql.go @@ -0,0 +1,105 @@ +package system + +import ( + "context" + "github.com/flipped-aurora/gin-vue-admin/server/config" + "github.com/flipped-aurora/gin-vue-admin/server/global" + model "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/flipped-aurora/gin-vue-admin/server/model/system/request" + "github.com/flipped-aurora/gin-vue-admin/server/source/example" + "github.com/flipped-aurora/gin-vue-admin/server/source/system" + "github.com/flipped-aurora/gin-vue-admin/server/utils" + "github.com/jackc/pgx/v4" + uuid "github.com/satori/go.uuid" + "gorm.io/driver/postgres" + "gorm.io/gorm" + "path/filepath" +) + +// writePgsqlConfig pgsql 回写配置 +// Author [SliverHorn](https://github.com/SliverHorn) +func (initDBService *InitDBService) writePgsqlConfig(pgsql config.Pgsql) error { + global.GVA_CONFIG.Pgsql = pgsql + cs := utils.StructToMap(global.GVA_CONFIG) + for k, v := range cs { + global.GVA_VP.Set(k, v) + } + global.GVA_VP.Set("jwt.signing-key", uuid.NewV4()) + return global.GVA_VP.WriteConfig() +} + +// createPgsqlDatabase 根据页面传递的数据库名 创建数据库 +// Author [SliverHorn](https://github.com/SliverHorn) +func (initDBService *InitDBService) createPgsqlDatabase(dsn string, dbName string) error { + ctx := context.Background() + _sql := "CREATE DATABASE " + dbName + db, err := pgx.Connect(ctx, dsn) + if err != nil { + return err + } + defer func() { + _ = db.Close(ctx) + }() + if err = db.Ping(ctx); err != nil { + return err + } + _, err = db.Exec(ctx, _sql) + return err +} +func (initDBService *InitDBService) initPgsqlDB(conf request.InitDB) error { + dsn := conf.PgsqlEmptyDsn() + if err := initDBService.createPgsqlDatabase(dsn, conf.DBName); err != nil { + return err + } // 创建数据库 + + pgsqlConfig := conf.ToPgsqlConfig() + if pgsqlConfig.Dbname == "" { + return nil + } // 如果没有数据库名, 则跳出初始化数据 + + if db, err := gorm.Open(postgres.New(postgres.Config{ + DSN: pgsqlConfig.Dsn(), // DSN data source name + PreferSimpleProtocol: false, + }), &gorm.Config{DisableForeignKeyConstraintWhenMigrating: true}); err != nil { + return nil + } else { + global.GVA_DB = db + } + + if err := initDBService.initTables(); err != nil { + global.GVA_DB = nil + return err + } + + if err := initDBService.initPgsqlData(); err != nil { + global.GVA_DB = nil + return err + } + + if err := initDBService.writePgsqlConfig(pgsqlConfig); err != nil { + return err + } + + global.GVA_CONFIG.AutoCode.Root, _ = filepath.Abs("..") + return nil +} + +// initPgsqlData pgsql 初始化数据 +// Author [SliverHorn](https://github.com/SliverHorn) +func (initDBService *InitDBService) initPgsqlData() error { + return model.MysqlDataInitialize( + system.Api, + system.User, + system.Casbin, + system.BaseMenu, + system.Authority, + system.Dictionary, + system.UserAuthority, + system.DataAuthorities, + system.AuthoritiesMenus, + system.DictionaryDetail, + system.ViewAuthorityMenuPostgres, + + example.File, + ) +} diff --git a/server/source/example/file.go b/server/source/example/file.go new file mode 100644 index 00000000..e5240215 --- /dev/null +++ b/server/source/example/file.go @@ -0,0 +1,34 @@ +package example + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/example" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +var File = new(file) + +type file struct{} + +func (f *file) TableName() string { + return "exa_file_upload_and_downloads" +} + +func (f *file) Initialize() error { + entities := []example.ExaFileUploadAndDownload{ + {Name: "10.png", Url: "https://qmplusimg.henrongyi.top/gvalogo.png", Tag: "png", Key: "158787308910.png"}, + {Name: "logo.png", Url: "https://qmplusimg.henrongyi.top/1576554439myAvatar.png", Tag: "png", Key: "1587973709logo.png"}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { + return errors.Wrap(err, f.TableName()+"表数据初始化失败!") + } + return nil +} + +func (f *file) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where("name = ? AND key = ?", "logo.png", "1587973709logo.png").First(&example.ExaFileUploadAndDownload{}).Error, gorm.ErrRecordNotFound) { + return false + } + return true +} diff --git a/server/source/system/api.go b/server/source/system/api.go new file mode 100644 index 00000000..e45e33cf --- /dev/null +++ b/server/source/system/api.go @@ -0,0 +1,126 @@ +package system + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +var Api = new(api) + +type api struct{} + +func (a *api) TableName() string { + return "sys_apis" +} + +func (a *api) Initialize() error { + entities := []system.SysApi{{ApiGroup: "base", Method: "POST", Path: "/base/login", Description: "用户登录(必选)"}, + + {ApiGroup: "jwt", Method: "POST", Path: "/jwt/jsonInBlacklist", Description: "jwt加入黑名单(退出,必选)"}, + + {ApiGroup: "系统用户", Method: "DELETE", Path: "/user/deleteUser", Description: "删除用户"}, + {ApiGroup: "系统用户", Method: "POST", Path: "/user/register", Description: "用户注册(必选)"}, + {ApiGroup: "系统用户", Method: "user", Path: "/user/getUserList", Description: "获取用户列表"}, + {ApiGroup: "系统用户", Method: "PUT", Path: "/user/setUserInfo", 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: "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: "POST", Path: "/fileUploadAndDownload/upload", Description: "文件上传示例"}, + {ApiGroup: "文件上传与下载", Method: "POST", Path: "/fileUploadAndDownload/deleteFile", 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/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: "删除操作记录", Path: "/sysOperationRecord/deleteSysOperationRecord", Description: "DELETE"}, + {ApiGroup: "操作记录", Method: "GET", Path: "/sysOperationRecord/findSysOperationRecord", Description: "根据ID获取操作记录"}, + {ApiGroup: "操作记录", Method: "GET", Path: "/sysOperationRecord/getSysOperationRecordList", 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: "excel", Method: "POST", Path: "/excel/importExcel", Description: "导入excel"}, + {ApiGroup: "excel", Method: "GET", Path: "/excel/loadExcel", Description: "下载excel"}, + {ApiGroup: "excel", Method: "POST", Path: "/excel/exportExcel", Description: "导出excel"}, + {ApiGroup: "excel", Method: "GET", Path: "/excel/downloadTemplate", Description: "下载excel模板"}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { + return errors.Wrap(err, a.TableName()+"表数据初始化失败!") + } + return nil +} + +func (a *api) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where("path = ? AND method = ?", "/excel/downloadTemplate", "GET").First(&system.SysApi{}).Error, gorm.ErrRecordNotFound) { + return false + } + return true +} diff --git a/server/source/system/authorities_menus.go b/server/source/system/authorities_menus.go new file mode 100644 index 00000000..46d98666 --- /dev/null +++ b/server/source/system/authorities_menus.go @@ -0,0 +1,95 @@ +package system + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/pkg/errors" + "gorm.io/gorm" + "gorm.io/gorm/schema" + "reflect" +) + +var AuthoritiesMenus = new(authoritiesMenus) + +type authoritiesMenus struct{} + +func (a *authoritiesMenus) TableName() string { + var entity AuthorityMenus + return entity.TableName() +} + +func (a *authoritiesMenus) Initialize() error { + entities := []AuthorityMenus{ + {BaseMenuId: 1, AuthorityId: "888"}, + {BaseMenuId: 2, AuthorityId: "888"}, + {BaseMenuId: 3, AuthorityId: "888"}, + {BaseMenuId: 4, AuthorityId: "888"}, + {BaseMenuId: 5, AuthorityId: "888"}, + {BaseMenuId: 6, AuthorityId: "888"}, + {BaseMenuId: 7, AuthorityId: "888"}, + {BaseMenuId: 8, AuthorityId: "888"}, + {BaseMenuId: 9, AuthorityId: "888"}, + {BaseMenuId: 10, AuthorityId: "888"}, + {BaseMenuId: 11, AuthorityId: "888"}, + {BaseMenuId: 12, AuthorityId: "888"}, + {BaseMenuId: 13, AuthorityId: "888"}, + {BaseMenuId: 14, AuthorityId: "888"}, + {BaseMenuId: 15, AuthorityId: "888"}, + {BaseMenuId: 16, AuthorityId: "888"}, + {BaseMenuId: 17, AuthorityId: "888"}, + {BaseMenuId: 18, AuthorityId: "888"}, + {BaseMenuId: 19, AuthorityId: "888"}, + {BaseMenuId: 20, AuthorityId: "888"}, + {BaseMenuId: 22, AuthorityId: "888"}, + {BaseMenuId: 23, AuthorityId: "888"}, + {BaseMenuId: 24, AuthorityId: "888"}, + {BaseMenuId: 25, AuthorityId: "888"}, + + {BaseMenuId: 1, AuthorityId: "8881"}, + {BaseMenuId: 2, AuthorityId: "8881"}, + {BaseMenuId: 8, AuthorityId: "8881"}, + + {BaseMenuId: 1, AuthorityId: "9528"}, + {BaseMenuId: 2, AuthorityId: "9528"}, + {BaseMenuId: 3, AuthorityId: "9528"}, + {BaseMenuId: 4, AuthorityId: "9528"}, + {BaseMenuId: 5, AuthorityId: "9528"}, + {BaseMenuId: 6, AuthorityId: "9528"}, + {BaseMenuId: 7, AuthorityId: "9528"}, + {BaseMenuId: 8, AuthorityId: "9528"}, + {BaseMenuId: 9, AuthorityId: "9528"}, + {BaseMenuId: 10, AuthorityId: "9528"}, + {BaseMenuId: 11, AuthorityId: "9528"}, + {BaseMenuId: 12, AuthorityId: "9528"}, + {BaseMenuId: 14, AuthorityId: "9528"}, + {BaseMenuId: 15, AuthorityId: "9528"}, + {BaseMenuId: 16, AuthorityId: "9528"}, + {BaseMenuId: 17, AuthorityId: "9528"}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { + return errors.Wrap(err, a.TableName()+"表数据初始化失败!") + } + return nil +} + +func (a *authoritiesMenus) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where("menu_id = ? AND authority_id = ?", 17, "9528").First(&AuthorityMenus{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据 + return false + } + return true +} + +type AuthorityMenus struct { + AuthorityId string `gorm:"column:sys_authority_authority_id"` + BaseMenuId uint `gorm:"column:sys_base_menu_id"` +} + +func (a *AuthorityMenus) TableName() string { + var entity system.SysAuthority + types := reflect.TypeOf(entity) + if s, o := types.FieldByName("SysBaseMenus"); o { + m1 := schema.ParseTagSetting(s.Tag.Get("gorm"), ";") + return m1["MANY2MANY"] + } + return "" +} diff --git a/server/source/system/authority.go b/server/source/system/authority.go new file mode 100644 index 00000000..c92ff221 --- /dev/null +++ b/server/source/system/authority.go @@ -0,0 +1,35 @@ +package system + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +var Authority = new(authority) + +type authority struct{} + +func (a *authority) TableName() string { + return "sys_authorities" +} + +func (a *authority) Initialize() error { + entities := []system.SysAuthority{ + {AuthorityId: "888", AuthorityName: "普通用户", ParentId: "0", DefaultRouter: "dashboard"}, + {AuthorityId: "9528", AuthorityName: "测试角色", ParentId: "0", DefaultRouter: "dashboard"}, + {AuthorityId: "8881", AuthorityName: "普通用户子角色", ParentId: "888", DefaultRouter: "dashboard"}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { + return errors.Wrapf(err, "%s表数据初始化失败!", a.TableName()) + } + return nil +} + +func (a *authority) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where("authority_id = ?", "8881").First(&system.SysAuthority{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据 + return false + } + return true +} diff --git a/server/source/system/casbin.go b/server/source/system/casbin.go new file mode 100644 index 00000000..b7965a42 --- /dev/null +++ b/server/source/system/casbin.go @@ -0,0 +1,208 @@ +package system + +import ( + adapter "github.com/casbin/gorm-adapter/v3" + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +var Casbin = new(casbin) + +type casbin struct{} + +func (c *casbin) TableName() string { + var entity adapter.CasbinRule + return entity.TableName() +} + +func (c *casbin) Initialize() error { + entities := []adapter.CasbinRule{ + {PType: "p", V0: "888", V1: "POST", V2: "/base/login"}, + {PType: "p", V0: "888", V1: "POST", V2: "/user/register"}, + + {PType: "p", V0: "888", V1: "POST", V2: "/api/createApi"}, + {PType: "p", V0: "888", V1: "POST", V2: "/api/getApiList"}, + {PType: "p", V0: "888", V1: "POST", V2: "/api/getApiById"}, + {PType: "p", V0: "888", V1: "POST", V2: "/api/deleteApi"}, + {PType: "p", V0: "888", V1: "POST", V2: "/api/updateApi"}, + {PType: "p", V0: "888", V1: "POST", V2: "/api/getAllApis"}, + {PType: "p", V0: "888", V1: "DELETE", V2: "/api/deleteApisByIds"}, + + {PType: "p", V0: "888", V1: "POST", V2: "/authority/copyAuthority"}, + {PType: "p", V0: "888", V1: "PUT", V2: "/authority/updateAuthority"}, + {PType: "p", V0: "888", V1: "POST", V2: "/authority/createAuthority"}, + {PType: "p", V0: "888", V1: "POST", V2: "/authority/deleteAuthority"}, + {PType: "p", V0: "888", V1: "POST", V2: "/authority/getAuthorityList"}, + {PType: "p", V0: "888", V1: "POST", V2: "/authority/setDataAuthority"}, + + {PType: "p", V0: "888", V1: "POST", V2: "/menu/getMenu"}, + {PType: "p", V0: "888", V1: "POST", V2: "/menu/getMenuList"}, + {PType: "p", V0: "888", V1: "POST", V2: "/menu/addBaseMenu"}, + {PType: "p", V0: "888", V1: "POST", V2: "/menu/getBaseMenuTree"}, + {PType: "p", V0: "888", V1: "POST", V2: "/menu/addMenuAuthority"}, + {PType: "p", V0: "888", V1: "POST", V2: "/menu/getMenuAuthority"}, + {PType: "p", V0: "888", V1: "POST", V2: "/menu/deleteBaseMenu"}, + {PType: "p", V0: "888", V1: "POST", V2: "/menu/updateBaseMenu"}, + {PType: "p", V0: "888", V1: "POST", V2: "/menu/getBaseMenuById"}, + + {PType: "p", V0: "888", V1: "GET", V2: "/user/getUserInfo"}, + {PType: "p", V0: "888", V1: "PUT", V2: "/user/setUserInfo"}, + {PType: "p", V0: "888", V1: "POST", V2: "/user/getUserList"}, + {PType: "p", V0: "888", V1: "DELETE", V2: "/user/deleteUser"}, + {PType: "p", V0: "888", V1: "POST", V2: "/user/changePassword"}, + {PType: "p", V0: "888", V1: "POST", V2: "/user/setUserAuthority"}, + {PType: "p", V0: "888", V1: "POST", V2: "/user/setUserAuthorities"}, + + {PType: "p", V0: "888", V1: "POST", V2: "/fileUploadAndDownload/upload"}, + {PType: "p", V0: "888", V1: "POST", V2: "/fileUploadAndDownload/deleteFile"}, + {PType: "p", V0: "888", V1: "POST", V2: "/fileUploadAndDownload/getFileList"}, + + {PType: "p", V0: "888", V1: "POST", V2: "/casbin/updateCasbin"}, + {PType: "p", V0: "888", V1: "POST", V2: "/casbin/getPolicyPathByAuthorityId"}, + + {PType: "p", V0: "888", V1: "POST", V2: "/jwt/jsonInBlacklist"}, + + {PType: "p", V0: "888", V1: "POST", V2: "/system/getSystemConfig"}, + {PType: "p", V0: "888", V1: "POST", V2: "/system/setSystemConfig"}, + {PType: "p", V0: "888", V1: "POST", V2: "/system/getServerInfo"}, + + {PType: "p", V0: "888", V1: "GET", V2: "/customer/customer"}, + {PType: "p", V0: "888", V1: "PUT", V2: "/customer/customer"}, + {PType: "p", V0: "888", V1: "POST", V2: "/customer/customer"}, + {PType: "p", V0: "888", V1: "DELETE", V2: "/customer/customer"}, + {PType: "p", V0: "888", V1: "GET", V2: "/customer/customerList"}, + + {PType: "p", V0: "888", V1: "GET", V2: "/autoCode/getDB"}, + {PType: "p", V0: "888", V1: "POST", V2: "/autoCode/getMeta"}, + {PType: "p", V0: "888", V1: "POST", V2: "/autoCode/preview"}, + {PType: "p", V0: "888", V1: "GET", V2: "/autoCode/getTables"}, + {PType: "p", V0: "888", V1: "GET", V2: "/autoCode/getColumn"}, + {PType: "p", V0: "888", V1: "POST", V2: "/autoCode/rollback"}, + {PType: "p", V0: "888", V1: "POST", V2: "/autoCode/createTemp"}, + {PType: "p", V0: "888", V1: "POST", V2: "/autoCode/delSysHistory"}, + {PType: "p", V0: "888", V1: "POST", V2: "/autoCode/getSysHistory"}, + + {PType: "p", V0: "888", V1: "GET", V2: "/sysDictionaryDetail/findSysDictionaryDetail"}, + {PType: "p", V0: "888", V1: "PUT", V2: "/sysDictionaryDetail/updateSysDictionaryDetail"}, + {PType: "p", V0: "888", V1: "POST", V2: "/sysDictionaryDetail/createSysDictionaryDetail"}, + {PType: "p", V0: "888", V1: "GET", V2: "/sysDictionaryDetail/getSysDictionaryDetailList"}, + {PType: "p", V0: "888", V1: "DELETE", V2: "/sysDictionaryDetail/deleteSysDictionaryDetail"}, + + {PType: "p", V0: "888", V1: "GET", V2: "/sysDictionary/findSysDictionary"}, + {PType: "p", V0: "888", V1: "PUT", V2: "/sysDictionary/updateSysDictionary"}, + {PType: "p", V0: "888", V1: "GET", V2: "/sysDictionary/getSysDictionaryList"}, + {PType: "p", V0: "888", V1: "POST", V2: "/sysDictionary/createSysDictionary"}, + {PType: "p", V0: "888", V1: "DELETE", V2: "/sysDictionary/deleteSysDictionary"}, + + {PType: "p", V0: "888", V1: "GET", V2: "/sysOperationRecord/findSysOperationRecord"}, + {PType: "p", V0: "888", V1: "PUT", V2: "/sysOperationRecord/updateSysOperationRecord"}, + {PType: "p", V0: "888", V1: "POST", V2: "/sysOperationRecord/createSysOperationRecord"}, + {PType: "p", V0: "888", V1: "GET", V2: "/sysOperationRecord/getSysOperationRecordList"}, + {PType: "p", V0: "888", V1: "DELETE", V2: "/sysOperationRecord/deleteSysOperationRecord"}, + {PType: "p", V0: "888", V1: "DELETE", V2: "/sysOperationRecord/deleteSysOperationRecordByIds"}, + + {PType: "p", V0: "888", V1: "POST", V2: "/email/emailTest"}, + + {PType: "p", V0: "888", V1: "POST", V2: "/simpleUploader/upload"}, + {PType: "p", V0: "888", V1: "GET", V2: "/simpleUploader/checkFileMd5"}, + {PType: "p", V0: "888", V1: "GET", V2: "/simpleUploader/mergeFileMd5"}, + + {PType: "p", V0: "888", V1: "POST", V2: "/excel/importExcel"}, + {PType: "p", V0: "888", V1: "GET", V2: "/excel/loadExcel"}, + {PType: "p", V0: "888", V1: "POST", V2: "/excel/exportExcel"}, + {PType: "p", V0: "888", V1: "GET", V2: "/excel/downloadTemplate"}, + + {PType: "p", V0: "8881", V1: "POST", V2: "/base/login"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/user/register"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/api/createApi"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/api/getApiList"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/api/getApiById"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/api/deleteApi"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/api/updateApi"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/api/getAllApis"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/authority/createAuthority"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/authority/deleteAuthority"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/authority/getAuthorityList"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/authority/setDataAuthority"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/menu/getMenu"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/menu/getMenuList"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/menu/addBaseMenu"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/menu/getBaseMenuTree"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/menu/addMenuAuthority"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/menu/getMenuAuthority"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/menu/deleteBaseMenu"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/menu/updateBaseMenu"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/menu/getBaseMenuById"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/user/changePassword"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/user/getUserList"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/user/setUserAuthority"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/fileUploadAndDownload/upload"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/fileUploadAndDownload/getFileList"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/fileUploadAndDownload/deleteFile"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/casbin/updateCasbin"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/casbin/getPolicyPathByAuthorityId"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/jwt/jsonInBlacklist"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/system/getSystemConfig"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/system/setSystemConfig"}, + {PType: "p", V0: "8881", V1: "POST", V2: "/customer/customer"}, + {PType: "p", V0: "8881", V1: "PUT", V2: "/customer/customer"}, + {PType: "p", V0: "8881", V1: "DELETE", V2: "/customer/customer"}, + {PType: "p", V0: "8881", V1: "GET", V2: "/customer/customer"}, + {PType: "p", V0: "8881", V1: "GET", V2: "/customer/customerList"}, + {PType: "p", V0: "8881", V1: "GET", V2: "/user/getUserInfo"}, + + {PType: "p", V0: "9528", V1: "POST", V2: "/base/login"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/user/register"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/api/createApi"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/api/getApiList"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/api/getApiById"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/api/deleteApi"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/api/updateApi"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/api/getAllApis"}, + + {PType: "p", V0: "9528", V1: "POST", V2: "/authority/createAuthority"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/authority/deleteAuthority"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/authority/getAuthorityList"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/authority/setDataAuthority"}, + + {PType: "p", V0: "9528", V1: "POST", V2: "/menu/getMenu"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/menu/getMenuList"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/menu/addBaseMenu"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/menu/getBaseMenuTree"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/menu/addMenuAuthority"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/menu/getMenuAuthority"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/menu/deleteBaseMenu"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/menu/updateBaseMenu"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/menu/getBaseMenuById"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/user/changePassword"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/user/getUserList"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/user/setUserAuthority"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/fileUploadAndDownload/upload"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/fileUploadAndDownload/getFileList"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/fileUploadAndDownload/deleteFile"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/casbin/updateCasbin"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/casbin/getPolicyPathByAuthorityId"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/jwt/jsonInBlacklist"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/system/getSystemConfig"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/system/setSystemConfig"}, + {PType: "p", V0: "9528", V1: "PUT", V2: "/customer/customer"}, + {PType: "p", V0: "9528", V1: "GET", V2: "/customer/customer"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/customer/customer"}, + {PType: "p", V0: "9528", V1: "DELETE", V2: "/customer/customer"}, + {PType: "p", V0: "9528", V1: "GET", V2: "/customer/customerList"}, + {PType: "p", V0: "9528", V1: "POST", V2: "/autoCode/createTemp"}, + {PType: "p", V0: "9528", V1: "GET", V2: "/user/getUserInfo"}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { + return errors.Wrap(err, c.TableName()+"表数据初始化失败!") + } + return nil +} + +func (c *casbin) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where(adapter.CasbinRule{PType: "p", V0: "9528", V1: "GET", V2: "/user/getUserInfo"}).First(&adapter.CasbinRule{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据 + return false + } + return true +} diff --git a/server/source/system/data_authorities.go b/server/source/system/data_authorities.go new file mode 100644 index 00000000..f43c1ef8 --- /dev/null +++ b/server/source/system/data_authorities.go @@ -0,0 +1,56 @@ +package system + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/pkg/errors" + "gorm.io/gorm" + "gorm.io/gorm/schema" + "reflect" +) + +var DataAuthorities = new(dataAuthorities) + +type dataAuthorities struct{} + +func (a *dataAuthorities) TableName() string { + var entity AuthoritiesResources + return entity.TableName() +} + +func (a *dataAuthorities) Initialize() error { + entities := []AuthoritiesResources{ + {AuthorityId: "888", ResourcesId: "888"}, + {AuthorityId: "888", ResourcesId: "8881"}, + {AuthorityId: "888", ResourcesId: "9528"}, + {AuthorityId: "9528", ResourcesId: "8881"}, + {AuthorityId: "9528", ResourcesId: "9528"}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { + return errors.Wrap(err, a.TableName()+"表数据初始化失败!") + } + return nil +} + +func (a *dataAuthorities) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where("authority_id = ? AND resources_id = ?", "9528", "9528").First(&AuthoritiesResources{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据 + return false + } + return true +} + +// AuthoritiesResources 角色资源表 +type AuthoritiesResources struct { + AuthorityId string `gorm:"column:authority_id"` + ResourcesId string `gorm:"column:resources_id"` +} + +func (a *AuthoritiesResources) TableName() string { + var entity system.SysAuthority + types := reflect.TypeOf(entity) + if s, o := types.FieldByName("DataAuthorityId"); o { + m1 := schema.ParseTagSetting(s.Tag.Get("gorm"), ";") + return m1["MANY2MANY"] + } + return "" +} diff --git a/server/source/system/dictionary.go b/server/source/system/dictionary.go new file mode 100644 index 00000000..bdc49985 --- /dev/null +++ b/server/source/system/dictionary.go @@ -0,0 +1,40 @@ +package system + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +var Dictionary = new(dictionary) + +type dictionary struct{} + +func (d *dictionary) TableName() string { + return "sys_dictionaries" +} + +func (d *dictionary) Initialize() error { + status := new(bool) + *status = true + entities := []system.SysDictionary{ + {Name: "性别", Type: "gender", Status: status, Desc: "性别字典"}, + {Name: "数据库int类型", Type: "int", Status: status, Desc: "int类型对应的数据库类型"}, + {Name: "数据库时间日期类型", Type: "time.Time", Status: status, Desc: "数据库时间日期类型"}, + {Name: "数据库浮点型", Type: "float64", Status: status, Desc: "数据库浮点型"}, + {Name: "数据库字符串", Type: "string", Status: status, Desc: "数据库字符串"}, + {Name: "数据库bool类型", Type: "bool", Status: status, Desc: "数据库bool类型"}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { + return errors.Wrap(err, d.TableName()+"表数据初始化失败!") + } + return nil +} + +func (d *dictionary) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where("type = ?", "bool").First(&system.SysDictionary{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据 + return false + } + return true +} diff --git a/server/source/system/dictionary_detail.go b/server/source/system/dictionary_detail.go new file mode 100644 index 00000000..fdb83ed3 --- /dev/null +++ b/server/source/system/dictionary_detail.go @@ -0,0 +1,57 @@ +package system + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +var DictionaryDetail = new(dictionaryDetail) + +type dictionaryDetail struct{} + +func (d *dictionaryDetail) TableName() string { + return "sys_dictionary_details" +} + +func (d *dictionaryDetail) Initialize() error { + status := new(bool) + *status = true + entities := []system.SysDictionaryDetail{ + {Label: "smallint", Value: 1, Status: status, Sort: 1, SysDictionaryID: 2}, + {Label: "mediumint", Value: 2, Status: status, Sort: 2, SysDictionaryID: 2}, + {Label: "int", Value: 3, Status: status, Sort: 3, SysDictionaryID: 2}, + {Label: "bigint", Value: 4, Status: status, Sort: 4, SysDictionaryID: 2}, + {Label: "date", Status: status, SysDictionaryID: 3}, + {Label: "time", Value: 1, Status: status, Sort: 1, SysDictionaryID: 3}, + {Label: "year", Value: 2, Status: status, Sort: 2, SysDictionaryID: 3}, + {Label: "datetime", Value: 3, Status: status, Sort: 3, SysDictionaryID: 3}, + {Label: "timestamp", Value: 5, Status: status, Sort: 5, SysDictionaryID: 3}, + {Label: "float", Status: status, SysDictionaryID: 4}, + {Label: "double", Value: 1, Status: status, Sort: 1, SysDictionaryID: 4}, + {Label: "decimal", Value: 2, Status: status, Sort: 2, SysDictionaryID: 4}, + {Label: "char", Status: status, SysDictionaryID: 5}, + {Label: "varchar", Value: 1, Status: status, Sort: 1, SysDictionaryID: 5}, + {Label: "tinyblob", Value: 2, Status: status, Sort: 2, SysDictionaryID: 5}, + {Label: "tinytext", Value: 3, Status: status, Sort: 3, SysDictionaryID: 5}, + {Label: "text", Value: 4, Status: status, Sort: 4, SysDictionaryID: 5}, + {Label: "blob", Value: 5, Status: status, Sort: 5, SysDictionaryID: 5}, + {Label: "mediumblob", Value: 6, Status: status, Sort: 6, SysDictionaryID: 5}, + {Label: "mediumtext", Value: 7, Status: status, Sort: 7, SysDictionaryID: 5}, + {Label: "longblob", Value: 8, Status: status, Sort: 8, SysDictionaryID: 5}, + {Label: "longtext", Value: 9, Status: status, Sort: 9, SysDictionaryID: 5}, + {Label: "tinyint", Status: status, SysDictionaryID: 6}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { + return errors.Wrap(err, d.TableName()+"表数据初始化失败!") + } + return nil +} + +func (d *dictionaryDetail) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where("id = ?", 23).First(&system.SysDictionaryDetail{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据 + return false + } + return true +} diff --git a/server/source/system/menu.go b/server/source/system/menu.go new file mode 100644 index 00000000..25f0cc90 --- /dev/null +++ b/server/source/system/menu.go @@ -0,0 +1,57 @@ +package system + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +var BaseMenu = new(menu) + +type menu struct{} + +func (m *menu) TableName() string { + return "sys_base_menus" +} + +func (m *menu) Initialize() error { + entities := []system.SysBaseMenu{ + {MenuLevel: 0, Hidden: false, ParentId: "0", Path: "dashboard", Name: "dashboard", Component: "view/dashboard/index.vue", Sort: 1, Meta: system.Meta{Title: "仪表盘", Icon: "setting"}}, + {MenuLevel: 0, Hidden: false, ParentId: "0", Path: "about", Name: "about", Component: "view/about/index.vue", Sort: 7, Meta: system.Meta{Title: "关于我们", Icon: "info"}}, + {MenuLevel: 0, Hidden: false, ParentId: "0", Path: "admin", Name: "superAdmin", Component: "view/superAdmin/index.vue", Sort: 3, Meta: system.Meta{Title: "超级管理员", Icon: "user-solid"}}, + {MenuLevel: 0, Hidden: false, ParentId: "3", Path: "authority", Name: "authority", Component: "view/superAdmin/authority/authority.vue", Sort: 1, Meta: system.Meta{Title: "角色管理", Icon: "s-custom"}}, + {MenuLevel: 0, Hidden: false, ParentId: "3", Path: "menu", Name: "menu", Component: "view/superAdmin/menu/menu.vue", Sort: 2, Meta: system.Meta{Title: "菜单管理", Icon: "s-order", KeepAlive: true}}, + {MenuLevel: 0, Hidden: false, ParentId: "3", Path: "api", Name: "api", Component: "view/superAdmin/api/api.vue", Sort: 3, Meta: system.Meta{Title: "api管理", Icon: "s-platform", KeepAlive: true}}, + {MenuLevel: 0, Hidden: false, ParentId: "3", Path: "user", Name: "user", Component: "view/superAdmin/user/user.vue", Sort: 4, Meta: system.Meta{Title: "用户管理", Icon: "coordinate"}}, + {MenuLevel: 0, Hidden: true, ParentId: "0", Path: "person", Name: "person", Component: "view/person/person.vue", Sort: 4, Meta: system.Meta{Title: "个人信息", Icon: "message-solid"}}, + {MenuLevel: 0, Hidden: false, ParentId: "0", Path: "example", Name: "example", Component: "view/example/index.vue", Sort: 6, Meta: system.Meta{Title: "示例文件", Icon: "s-management"}}, + {MenuLevel: 0, Hidden: false, ParentId: "9", Path: "excel", Name: "excel", Component: "view/example/excel/excel.vue", Sort: 4, Meta: system.Meta{Title: "excel导入导出", Icon: "s-marketing"}}, + {MenuLevel: 0, Hidden: false, ParentId: "9", Path: "upload", Name: "upload", Component: "view/example/upload/upload.vue", Sort: 5, Meta: system.Meta{Title: "媒体库(上传下载)", Icon: "upload"}}, + {MenuLevel: 0, Hidden: false, ParentId: "9", Path: "breakpoint", Name: "breakpoint", Component: "view/example/breakpoint/breakpoint.vue", Sort: 6, Meta: system.Meta{Title: "断点续传", Icon: "upload"}}, + {MenuLevel: 0, Hidden: false, ParentId: "9", Path: "customer", Name: "customer", Component: "view/example/customer/customer.vue", Sort: 7, Meta: system.Meta{Title: "客户列表(资源示例)", Icon: "s-custom"}}, + {MenuLevel: 0, Hidden: false, ParentId: "0", Path: "systemTools", Name: "systemTools", Component: "view/systemTools/index.vue", Sort: 5, Meta: system.Meta{Title: "系统工具", Icon: "s-cooperation"}}, + {MenuLevel: 0, Hidden: false, ParentId: "14", Path: "autoCode", Name: "autoCode", Component: "view/systemTools/autoCode/index.vue", Sort: 1, Meta: system.Meta{Title: "代码生成器", Icon: "cpu", KeepAlive: true}}, + {MenuLevel: 0, Hidden: false, ParentId: "14", Path: "formCreate", Name: "formCreate", Component: "view/systemTools/formCreate/index.vue", Sort: 2, Meta: system.Meta{Title: "表单生成器", Icon: "magic-stick", KeepAlive: true}}, + {MenuLevel: 0, Hidden: false, ParentId: "14", Path: "system", Name: "system", Component: "view/systemTools/system/system.vue", Sort: 3, Meta: system.Meta{Title: "系统配置", Icon: "s-operation"}}, + {MenuLevel: 0, Hidden: false, ParentId: "3", Path: "dictionary", Name: "dictionary", Component: "view/superAdmin/dictionary/sysDictionary.vue", Sort: 5, Meta: system.Meta{Title: "字典管理", Icon: "notebook-2"}}, + {MenuLevel: 0, Hidden: true, ParentId: "3", Path: "dictionaryDetail/:id", Name: "dictionaryDetail", Component: "view/superAdmin/dictionary/sysDictionaryDetail.vue", Sort: 1, Meta: system.Meta{Title: "字典详情", Icon: "s-order"}}, + {MenuLevel: 0, Hidden: false, ParentId: "3", Path: "operation", Name: "operation", Component: "view/superAdmin/operation/sysOperationRecord.vue", Sort: 6, Meta: system.Meta{Title: "操作历史", Icon: "time"}}, + {MenuLevel: 0, Hidden: false, ParentId: "9", Path: "simpleUploader", Name: "simpleUploader", Component: "view/example/simpleUploader/simpleUploader", Sort: 6, Meta: system.Meta{Title: "断点续传(插件版)", Icon: "upload"}}, + {MenuLevel: 0, Hidden: false, ParentId: "0", Path: "https://www.gin-vue-admin.com", Name: "https://www.gin-vue-admin.com", Component: "/", Sort: 0, Meta: system.Meta{Title: "官方网站", Icon: "s-home"}}, + {MenuLevel: 0, Hidden: false, ParentId: "0", Path: "state", Name: "state", Component: "view/system/state.vue", Sort: 6, Meta: system.Meta{Title: "服务器状态", Icon: "cloudy"}}, + {MenuLevel: 0, Hidden: false, ParentId: "14", Path: "autoCodeAdmin", Name: "autoCodeAdmin", Component: "view/systemTools/autoCodeAdmin/index.vue", Sort: 1, Meta: system.Meta{Title: "自动化代码管理", Icon: "s-finance"}}, + {MenuLevel: 0, Hidden: true, ParentId: "14", Path: "autoCodeEdit/:id", Name: "autoCodeEdit", Component: "view/systemTools/autoCode/index.vue", Sort: 0, Meta: system.Meta{Title: "自动化代码(复用)", Icon: "s-finance"}}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { // 创建 model.User 初始化数据 + return errors.Wrap(err, m.TableName()+"表数据初始化失败!") + } + return nil +} + +func (m *menu) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where("path = ?", "autoCodeEdit/:id").First(&system.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据 + return false + } + return true +} diff --git a/server/source/system/user.go b/server/source/system/user.go new file mode 100644 index 00000000..b1a375b8 --- /dev/null +++ b/server/source/system/user.go @@ -0,0 +1,35 @@ +package system + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/pkg/errors" + uuid "github.com/satori/go.uuid" + "gorm.io/gorm" +) + +var User = new(user) + +type user struct{} + +func (u *user) TableName() string { + return "sys_users" +} + +func (u *user) Initialize() error { + entities := []system.SysUser{ + {UUID: uuid.NewV4(), Username: "admin", Password: "e10adc3949ba59abbe56e057f20f883e", NickName: "超级管理员", HeaderImg: "https://qmplusimg.henrongyi.top/gva_header.jpg", AuthorityId: "888"}, + {UUID: uuid.NewV4(), Username: "a303176530", Password: "3ec063004a6f31642261936a379fde3d", NickName: "QMPlusUser", HeaderImg: "https:///qmplusimg.henrongyi.top/1572075907logo.png", AuthorityId: "9528"}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { + return errors.Wrap(err, u.TableName()+"表数据初始化失败!") + } + return nil +} + +func (u *user) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where("username = ?", "a303176530").First(&system.SysUser{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据 + return false + } + return true +} diff --git a/server/source/system/user_authority.go b/server/source/system/user_authority.go new file mode 100644 index 00000000..89a5e528 --- /dev/null +++ b/server/source/system/user_authority.go @@ -0,0 +1,37 @@ +package system + +import ( + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/pkg/errors" + "gorm.io/gorm" +) + +var UserAuthority = new(userAuthority) + +type userAuthority struct{} + +func (a *userAuthority) TableName() string { + var entity system.SysUseAuthority + return entity.TableName() +} + +func (a *userAuthority) Initialize() error { + entities := []system.SysUseAuthority{ + {SysUserId: 1, SysAuthorityAuthorityId: "888"}, + {SysUserId: 1, SysAuthorityAuthorityId: "8881"}, + {SysUserId: 1, SysAuthorityAuthorityId: "9528"}, + {SysUserId: 2, SysAuthorityAuthorityId: "888"}, + } + if err := global.GVA_DB.Create(&entities).Error; err != nil { + return errors.Wrap(err, a.TableName()+"表数据初始化失败!") + } + return nil +} + +func (a *userAuthority) CheckDataExist() bool { + if errors.Is(global.GVA_DB.Where("sys_user_id = ? AND sys_authority_authority_id = ?", 2, "888").First(&system.SysUseAuthority{}).Error, gorm.ErrRecordNotFound) { // 判断是否存在数据 + return false + } + return true +} diff --git a/server/source/system/view_authority_menu_mysql.go b/server/source/system/view_authority_menu_mysql.go new file mode 100644 index 00000000..b0c500e8 --- /dev/null +++ b/server/source/system/view_authority_menu_mysql.go @@ -0,0 +1,39 @@ +package system + +import ( + "fmt" + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/pkg/errors" + "strings" +) + +var ViewAuthorityMenuMysql = new(viewAuthorityMenuMysql) + +type viewAuthorityMenuMysql struct{} + +func (v *viewAuthorityMenuMysql) TableName() string { + var entity system.SysMenu + return entity.TableName() +} + +func (v *viewAuthorityMenuMysql) Initialize() error { + var entity AuthorityMenus + sql := "CREATE ALGORITHM = UNDEFINED SQL SECURITY DEFINER VIEW `@table_name` AS select `@menus`.id AS id, `@menus`.path AS path, `@menus`.icon AS icon, `@menus`.name AS name, `@menus`.sort AS sort, `@menus`.title AS title, `@menus`.hidden AS hidden, `@menus`.component AS component, `@menus`.parent_id AS parent_id, `@menus`.created_at AS created_at, `@menus`.updated_at AS updated_at, `@menus`.deleted_at AS deleted_at, `@menus`.keep_alive AS keep_alive, `@menus`.menu_level AS menu_level, `@menus`.default_menu AS default_menu, `@authorities_menus`.menu_id AS menu_id, `@authorities_menus`.authority_id AS authority_id from (`@authorities_menus` join `@menus` on ((`@authorities_menus`.menu_id = `@menus`.id)));" + sql = strings.ReplaceAll(sql, "@table_name", v.TableName()) + sql = strings.ReplaceAll(sql, "@menus", "sys_base_menus") + sql = strings.ReplaceAll(sql, "@authorities_menus", entity.TableName()) + if err := global.GVA_DB.Exec(sql).Error; err != nil { + return errors.Wrap(err, v.TableName()+"视图创建失败!") + } + return nil +} + +func (v *viewAuthorityMenuMysql) CheckDataExist() bool { + err1 := global.GVA_DB.Find(&[]system.SysMenu{}).Error + err2 := errors.New(fmt.Sprintf("Error 1146: Table '%v.%v' doesn't exist", global.GVA_CONFIG.Mysql.Dbname, v.TableName())) + if errors.As(err1, &err2) { + return false + } + return true +} diff --git a/server/source/system/view_authority_menu_postgres.go b/server/source/system/view_authority_menu_postgres.go new file mode 100644 index 00000000..43b47ca3 --- /dev/null +++ b/server/source/system/view_authority_menu_postgres.go @@ -0,0 +1,58 @@ +package system + +import ( + "fmt" + "github.com/flipped-aurora/gin-vue-admin/server/global" + "github.com/flipped-aurora/gin-vue-admin/server/model/system" + "github.com/pkg/errors" + "strings" +) + +var ViewAuthorityMenuPostgres = new(viewAuthorityMenuPostgres) + +type viewAuthorityMenuPostgres struct{} + +func (a *viewAuthorityMenuPostgres) TableName() string { + var entity system.SysMenu + return entity.TableName() +} + +func (a *viewAuthorityMenuPostgres) Initialize() error { + var entity AuthorityMenus + sql := ` + CREATE VIEW @table_name as + select @menus.id as id, + @menus.path as path, + @menus.name as name, + @menus.icon as icon, + @menus.sort as sort, + @menus.title as title, + @menus.hidden as hidden, + @menus.parent_id as parent_id, + @menus.component as component, + @menus.keep_alive as keep_alive, + @menus.created_at as created_at, + @menus.updated_at as updated_at, + @menus.deleted_at as deleted_at, + @menus.menu_level as menu_level, + @menus.default_menu as default_menu, + @authorities_menus.menu_id as menu_id, + @authorities_menus.authority_id as authority_id + from (@authorities_menus join @menus on ((@authorities_menus.menu_id = @menus.id)));` + sql = strings.ReplaceAll(sql, "@table_name", a.TableName()) + sql = strings.ReplaceAll(sql, "@menus", "sys_base_menus") + sql = strings.ReplaceAll(sql, "@authorities_menus", entity.TableName()) + if err := global.GVA_DB.Exec(sql).Error; err != nil { + return errors.Wrap(err, a.TableName()+"视图创建失败!") + } + return nil +} + +func (a *viewAuthorityMenuPostgres) CheckDataExist() bool { + err1 := global.GVA_DB.Find(&[]system.SysMenu{}).Error + err2 := errors.New(fmt.Sprintf("Error 1146: Table '%v.%v' doesn't exist", global.GVA_CONFIG.Pgsql.Dbname, a.TableName())) + if errors.As(err1, &err2) { + return false + } + return true +}