mirror of
https://github.com/go-admin-team/go-admin.git
synced 2026-09-21 10:13:01 +00:00
feat: Database type support
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
|||||||
"go-admin/models/tools"
|
"go-admin/models/tools"
|
||||||
tools2 "go-admin/tools"
|
tools2 "go-admin/tools"
|
||||||
"go-admin/tools/app"
|
"go-admin/tools/app"
|
||||||
config2 "go-admin/tools/config"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,11 +22,11 @@ func GetDBTableList(c *gin.Context) {
|
|||||||
var err error
|
var err error
|
||||||
var pageSize = 10
|
var pageSize = 10
|
||||||
var pageIndex = 1
|
var pageIndex = 1
|
||||||
if config2.DatabaseConfig.Dbtype=="sqlite3"{
|
//if config2.DatabaseConfig.DbType=="sqlite3"{
|
||||||
res.Msg="对不起,sqlite3 暂不支持代码生成!"
|
// res.Msg="对不起,sqlite3 暂不支持代码生成!"
|
||||||
c.JSON(http.StatusOK, res.ReturnError(500))
|
// c.JSON(http.StatusOK, res.ReturnError(500))
|
||||||
return
|
// return
|
||||||
}
|
//}
|
||||||
|
|
||||||
if size := c.Request.FormValue("pageSize"); size != "" {
|
if size := c.Request.FormValue("pageSize"); size != "" {
|
||||||
pageSize = tools2.StrToInt(err, size)
|
pageSize = tools2.StrToInt(err, size)
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ func run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func migrateModel() error {
|
func migrateModel() error {
|
||||||
if config2.DatabaseConfig.Dbtype == "mysql" {
|
if config2.DatabaseConfig.DbType == "mysql" {
|
||||||
orm.Eloquent = orm.Eloquent.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4")
|
orm.Eloquent = orm.Eloquent.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4")
|
||||||
}
|
}
|
||||||
return gorm.AutoMigrate(orm.Eloquent)
|
return gorm.AutoMigrate(orm.Eloquent)
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
-- 开始初始化数据 ;
|
-- 开始初始化数据 ;
|
||||||
SET NAMES utf8mb4;
|
|
||||||
SET FOREIGN_KEY_CHECKS = 0;
|
|
||||||
|
|
||||||
BEGIN;
|
BEGIN;
|
||||||
INSERT INTO `casbin_rule` VALUES ('p', 'Tester', '/api/v1/calendar', 'GET', NULL, NULL, NULL);
|
INSERT INTO `casbin_rule` VALUES ('p', 'Tester', '/api/v1/calendar', 'GET', NULL, NULL, NULL);
|
||||||
@@ -661,5 +659,4 @@ INSERT INTO `sys_user` VALUES (3, '李四', '13838385438', 2, '', '', '0', 'qq@q
|
|||||||
INSERT INTO `sys_user` VALUES (4, '王五', '13535353535', 3, '', '', '2', 'qq@qq.com', 8, 2, '1', '1', '', 0, '2020-04-12 14:06:49', '2020-04-12 14:07:09', NULL, 'wangwu', '$2a$10$3.RT6rpXANXvvlibX6PzU.FGA2CvfDxd1UmJ2H5zTzF4sYocbvsTO');
|
INSERT INTO `sys_user` VALUES (4, '王五', '13535353535', 3, '', '', '2', 'qq@qq.com', 8, 2, '1', '1', '', 0, '2020-04-12 14:06:49', '2020-04-12 14:07:09', NULL, 'wangwu', '$2a$10$3.RT6rpXANXvvlibX6PzU.FGA2CvfDxd1UmJ2H5zTzF4sYocbvsTO');
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|
||||||
SET FOREIGN_KEY_CHECKS = 1;
|
|
||||||
-- 数据完成 ;
|
-- 数据完成 ;
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
[request_definition]
|
|
||||||
r = sub, obj, act
|
|
||||||
|
|
||||||
[policy_definition]
|
|
||||||
p = sub, obj, act
|
|
||||||
|
|
||||||
[policy_effect]
|
|
||||||
e = some(where (p.eft == allow))
|
|
||||||
|
|
||||||
[matchers]
|
|
||||||
m = r.sub == p.sub && (keyMatch2(r.obj, p.obj) || keyMatch(r.obj, p.obj)) && (r.act == p.act || p.act == "*")
|
|
||||||
+6
-5
@@ -18,9 +18,10 @@ settings:
|
|||||||
secret: go-admin
|
secret: go-admin
|
||||||
timeout: 3600
|
timeout: 3600
|
||||||
database:
|
database:
|
||||||
name: goadmindb
|
|
||||||
dbtype: mysql
|
dbtype: mysql
|
||||||
host: 127.0.0.1
|
sqlite:
|
||||||
password: 123456
|
masterconn: ./sqlite3.db
|
||||||
port: 3306
|
mysql:
|
||||||
username: root
|
masterconn: user:password@/tcp(127.0.0.1:3306)/dbname?charset=utf8&parseTime=True&loc=Local
|
||||||
|
pgsql:
|
||||||
|
masterconn: host=myhost port=myport user=gorm dbname=gorm password=mypassword
|
||||||
|
|||||||
@@ -4,14 +4,19 @@ import "go-admin/tools/config"
|
|||||||
|
|
||||||
|
|
||||||
func Setup() {
|
func Setup() {
|
||||||
dbType := config.DatabaseConfig.Dbtype
|
dbType := config.DatabaseConfig.DbType
|
||||||
if dbType == "mysql" {
|
if dbType == "mysql" {
|
||||||
var db = new(Mysql)
|
var db = new(Mysql)
|
||||||
db.Setup()
|
db.Setup()
|
||||||
}
|
}
|
||||||
|
|
||||||
if dbType == "sqlite" {
|
if dbType == "sqlite3" {
|
||||||
var db = new(SqLite)
|
var db = new(SqLite)
|
||||||
db.Setup()
|
db.Setup()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dbType == "pgsql" {
|
||||||
|
var db = new(PgSql)
|
||||||
|
db.Setup()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-35
@@ -1,22 +1,11 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
_ "github.com/go-sql-driver/mysql" //加载mysql
|
_ "github.com/go-sql-driver/mysql" //加载mysql
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"go-admin/global/orm"
|
"go-admin/global/orm"
|
||||||
"go-admin/tools/config"
|
"go-admin/tools/config"
|
||||||
"strconv"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
DbType string
|
|
||||||
Host string
|
|
||||||
Port int
|
|
||||||
Name string
|
|
||||||
Username string
|
|
||||||
Password string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (e *Mysql) Setup() {
|
func (e *Mysql) Setup() {
|
||||||
@@ -27,12 +16,12 @@ func (e *Mysql) Setup() {
|
|||||||
db = new(Mysql)
|
db = new(Mysql)
|
||||||
orm.MysqlConn = db.GetConnect()
|
orm.MysqlConn = db.GetConnect()
|
||||||
log.Info(orm.MysqlConn)
|
log.Info(orm.MysqlConn)
|
||||||
orm.Eloquent, err = db.Open(DbType, orm.MysqlConn)
|
orm.Eloquent, err = db.Open(config.DatabaseConfig.DbType, orm.MysqlConn)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("%s connect error %v", DbType, err)
|
log.Fatalf("%s connect error %v", config.DatabaseConfig.DbType, err)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("%s connect success!", DbType)
|
log.Printf("%s connect success!", config.DatabaseConfig.DbType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if orm.Eloquent.Error != nil {
|
if orm.Eloquent.Error != nil {
|
||||||
@@ -50,25 +39,5 @@ func (e *Mysql) Open(dbType string, conn string) (db *gorm.DB, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Mysql) GetConnect() string {
|
func (e *Mysql) GetConnect() string {
|
||||||
|
return config.DatabaseConfig.Mysql.MasterConn
|
||||||
DbType = config.DatabaseConfig.Dbtype
|
|
||||||
Host = config.DatabaseConfig.Host
|
|
||||||
Port = config.DatabaseConfig.Port
|
|
||||||
Name = config.DatabaseConfig.Name
|
|
||||||
Username = config.DatabaseConfig.Username
|
|
||||||
Password = config.DatabaseConfig.Password
|
|
||||||
|
|
||||||
var conn bytes.Buffer
|
|
||||||
conn.WriteString(Username)
|
|
||||||
conn.WriteString(":")
|
|
||||||
conn.WriteString(Password)
|
|
||||||
conn.WriteString("@tcp(")
|
|
||||||
conn.WriteString(Host)
|
|
||||||
conn.WriteString(":")
|
|
||||||
conn.WriteString(strconv.Itoa(Port))
|
|
||||||
conn.WriteString(")")
|
|
||||||
conn.WriteString("/")
|
|
||||||
conn.WriteString(Name)
|
|
||||||
conn.WriteString("?charset=utf8&parseTime=True&loc=Local&timeout=1000ms")
|
|
||||||
return conn.String()
|
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-5
@@ -5,6 +5,7 @@ import (
|
|||||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"go-admin/global/orm"
|
"go-admin/global/orm"
|
||||||
|
"go-admin/tools/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PgSql struct {
|
type PgSql struct {
|
||||||
@@ -17,7 +18,7 @@ func (*PgSql) Open(dbType string, conn string) (db *gorm.DB, err error) {
|
|||||||
|
|
||||||
func (e *PgSql) GetConnect() string {
|
func (e *PgSql) GetConnect() string {
|
||||||
|
|
||||||
return ""
|
return config.DatabaseConfig.PgSql.MasterConn
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *PgSql) Setup() {
|
func (e *PgSql) Setup() {
|
||||||
@@ -28,12 +29,12 @@ func (e *PgSql) Setup() {
|
|||||||
db = new(PgSql)
|
db = new(PgSql)
|
||||||
orm.PgSqlConn = db.GetConnect()
|
orm.PgSqlConn = db.GetConnect()
|
||||||
log.Info(orm.PgSqlConn)
|
log.Info(orm.PgSqlConn)
|
||||||
orm.Eloquent, err = db.Open(DbType, orm.PgSqlConn)
|
orm.Eloquent, err = db.Open(config.DatabaseConfig.DbType, orm.PgSqlConn)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("%s connect error %v", DbType, err)
|
log.Fatalf("%s connect error %v", config.DatabaseConfig.DbType, err)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("%s connect success!", DbType)
|
log.Printf("%s connect success!", config.DatabaseConfig.DbType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if orm.Eloquent.Error != nil {
|
if orm.Eloquent.Error != nil {
|
||||||
@@ -41,4 +42,4 @@ func (e *PgSql) Setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
orm.Eloquent.LogMode(true)
|
orm.Eloquent.LogMode(true)
|
||||||
}
|
}
|
||||||
|
|||||||
+5
-5
@@ -5,6 +5,7 @@ import (
|
|||||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"go-admin/global/orm"
|
"go-admin/global/orm"
|
||||||
|
"go-admin/tools/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SqLite struct {
|
type SqLite struct {
|
||||||
@@ -16,8 +17,7 @@ func (*SqLite) Open(dbType string, conn string) (db *gorm.DB, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *SqLite) GetConnect() string {
|
func (e *SqLite) GetConnect() string {
|
||||||
|
return config.DatabaseConfig.SqLite.MasterConn
|
||||||
return ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *SqLite) Setup() {
|
func (e *SqLite) Setup() {
|
||||||
@@ -28,12 +28,12 @@ func (e *SqLite) Setup() {
|
|||||||
db = new(SqLite)
|
db = new(SqLite)
|
||||||
orm.SqLiteConn = db.GetConnect()
|
orm.SqLiteConn = db.GetConnect()
|
||||||
log.Info(orm.SqLiteConn)
|
log.Info(orm.SqLiteConn)
|
||||||
orm.Eloquent, err = db.Open(DbType, orm.SqLiteConn)
|
orm.Eloquent, err = db.Open(config.DatabaseConfig.DbType, orm.SqLiteConn)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("%s connect error %v", DbType, err)
|
log.Fatalf("%s connect error %v", config.DatabaseConfig.DbType, err)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("%s connect success!", DbType)
|
log.Printf("%s connect success!", config.DatabaseConfig.DbType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if orm.Eloquent.Error != nil {
|
if orm.Eloquent.Error != nil {
|
||||||
|
|||||||
@@ -8,3 +8,4 @@ var Eloquent *gorm.DB
|
|||||||
var MysqlConn string
|
var MysqlConn string
|
||||||
var SqLiteConn string
|
var SqLiteConn string
|
||||||
var PgSqlConn string
|
var PgSqlConn string
|
||||||
|
var DbType string
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ require (
|
|||||||
github.com/casbin/gorm-adapter/v2 v2.0.3
|
github.com/casbin/gorm-adapter/v2 v2.0.3
|
||||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
github.com/dgrijalva/jwt-go v3.2.0+incompatible
|
||||||
github.com/gin-gonic/gin v1.4.0
|
github.com/gin-gonic/gin v1.4.0
|
||||||
github.com/go-kit/kit v0.8.0
|
|
||||||
github.com/go-ole/go-ole v1.2.4 // indirect
|
github.com/go-ole/go-ole v1.2.4 // indirect
|
||||||
github.com/go-openapi/spec v0.19.7 // indirect
|
github.com/go-openapi/spec v0.19.7 // indirect
|
||||||
github.com/go-openapi/swag v0.19.8 // indirect
|
github.com/go-openapi/swag v0.19.8 // indirect
|
||||||
|
|||||||
+4
-5
@@ -3,17 +3,16 @@ package models
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go-admin/global/orm"
|
"go-admin/global/orm"
|
||||||
config2 "go-admin/tools/config"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitDb() error {
|
func InitDb() error {
|
||||||
filePath := "config/db.sql"
|
filePath := "config/db.sql"
|
||||||
if config2.DatabaseConfig.Dbtype == "sqlite3" {
|
//if config2.DatabaseConfig.DbType == "sqlite" {
|
||||||
fmt.Println("sqlite3数据库无需初始化!")
|
// fmt.Println("sqlite3数据库无需初始化!")
|
||||||
return nil
|
// return nil
|
||||||
}
|
//}
|
||||||
sql, err := Ioutil(filePath)
|
sql, err := Ioutil(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("数据库基础数据初始化脚本读取失败!原因:", err.Error())
|
fmt.Println("数据库基础数据初始化脚本读取失败!原因:", err.Error())
|
||||||
|
|||||||
+21
-14
@@ -2,6 +2,7 @@ package tools
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
"go-admin/global/orm"
|
"go-admin/global/orm"
|
||||||
config2 "go-admin/tools/config"
|
config2 "go-admin/tools/config"
|
||||||
)
|
)
|
||||||
@@ -23,37 +24,43 @@ type DBColumns struct {
|
|||||||
|
|
||||||
func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int, error) {
|
func (e *DBColumns) GetPage(pageSize int, pageIndex int) ([]DBColumns, int, error) {
|
||||||
var doc []DBColumns
|
var doc []DBColumns
|
||||||
|
var count int
|
||||||
|
table := new(gorm.DB)
|
||||||
|
|
||||||
table := orm.Eloquent.Select("*").Table("information_schema.`COLUMNS`")
|
if config2.DatabaseConfig.DbType == "mysql" {
|
||||||
table = table.Where("table_schema= ? ", config2.DatabaseConfig.Name)
|
table = orm.Eloquent.Select("*").Table("information_schema.`COLUMNS`")
|
||||||
|
table = table.Where("table_schema= ? ", config2.DatabaseConfig.Mysql.DBName)
|
||||||
|
|
||||||
if e.TableName != "" {
|
if e.TableName != "" {
|
||||||
return nil, 0, errors.New("table name cannot be empty!")
|
return nil, 0, errors.New("table name cannot be empty!")
|
||||||
|
}
|
||||||
|
|
||||||
|
table = table.Where("TABLE_NAME = ?", e.TableName)
|
||||||
}
|
}
|
||||||
|
|
||||||
table = table.Where("TABLE_NAME = ?", e.TableName)
|
|
||||||
|
|
||||||
var count int
|
|
||||||
|
|
||||||
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
table.Count(&count)
|
table.Count(&count)
|
||||||
return doc, count, nil
|
return doc, count, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *DBColumns) GetList() ([]DBColumns, error) {
|
func (e *DBColumns) GetList() ([]DBColumns, error) {
|
||||||
var doc []DBColumns
|
var doc []DBColumns
|
||||||
|
table := new(gorm.DB)
|
||||||
|
|
||||||
table := orm.Eloquent.Select("*").Table("information_schema.columns")
|
if config2.DatabaseConfig.DbType == "mysql" {
|
||||||
table = table.Where("table_schema= ? ", config2.DatabaseConfig.Name)
|
table = orm.Eloquent.Select("*").Table("information_schema.columns")
|
||||||
|
table = table.Where("table_schema= ? ", config2.DatabaseConfig.Mysql.DBName)
|
||||||
|
|
||||||
if e.TableName == "" {
|
if e.TableName == "" {
|
||||||
return nil, errors.New("table name cannot be empty!")
|
return nil, errors.New("table name cannot be empty!")
|
||||||
|
}
|
||||||
|
|
||||||
|
table = table.Where("TABLE_NAME = ?", e.TableName)
|
||||||
}
|
}
|
||||||
|
|
||||||
table = table.Where("TABLE_NAME = ?", e.TableName)
|
|
||||||
|
|
||||||
if err := table.Find(&doc).Error; err != nil {
|
if err := table.Find(&doc).Error; err != nil {
|
||||||
return doc, err
|
return doc, err
|
||||||
}
|
}
|
||||||
|
|||||||
+38
-17
@@ -2,6 +2,7 @@ package tools
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"github.com/jinzhu/gorm"
|
||||||
"go-admin/global/orm"
|
"go-admin/global/orm"
|
||||||
config2 "go-admin/tools/config"
|
config2 "go-admin/tools/config"
|
||||||
)
|
)
|
||||||
@@ -18,34 +19,54 @@ type DBTables struct {
|
|||||||
|
|
||||||
func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int, error) {
|
func (e *DBTables) GetPage(pageSize int, pageIndex int) ([]DBTables, int, error) {
|
||||||
var doc []DBTables
|
var doc []DBTables
|
||||||
|
table := new(gorm.DB)
|
||||||
table := orm.Eloquent.Select("*").Table("information_schema.tables")
|
|
||||||
table = table.Where("TABLE_NAME not in (select table_name from "+config2.DatabaseConfig.Name+".sys_tables) ")
|
|
||||||
table = table.Where("table_schema= ? ", config2.DatabaseConfig.Name)
|
|
||||||
|
|
||||||
if e.TableName != "" {
|
|
||||||
table = table.Where("TABLE_NAME = ?", e.TableName)
|
|
||||||
}
|
|
||||||
|
|
||||||
var count int
|
var count int
|
||||||
|
|
||||||
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
if config2.DatabaseConfig.DbType == "mysql" {
|
||||||
return nil, 0, err
|
table = orm.Eloquent.Select("*").Table("information_schema.tables")
|
||||||
|
table = table.Where("TABLE_NAME not in (select table_name from " + config2.DatabaseConfig.Mysql.DBName + ".sys_tables) ")
|
||||||
|
table = table.Where("table_schema= ? ", config2.DatabaseConfig.Mysql.DBName)
|
||||||
|
|
||||||
|
if e.TableName != "" {
|
||||||
|
table = table.Where("TABLE_NAME = ?", e.TableName)
|
||||||
|
}
|
||||||
|
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
} else if config2.DatabaseConfig.DbType == "sqlite3" {
|
||||||
|
table = orm.Eloquent.Select("name as TABLE_NAME,name as ENGINE,name as TABLE_ROWS,name as TABLE_COLLATION,name as CREATE_TIME,name as UPDATE_TIME,name as TABLE_COMMENT ").Table("sqlite_master")
|
||||||
|
table = table.Where("type = 'table' ")
|
||||||
|
|
||||||
|
if e.TableName != "" {
|
||||||
|
table = table.Where("name = ?", e.TableName)
|
||||||
|
}
|
||||||
|
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
table.Count(&count)
|
table.Count(&count)
|
||||||
return doc, count, nil
|
return doc, count, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *DBTables) Get() (DBTables, error) {
|
func (e *DBTables) Get() (DBTables, error) {
|
||||||
var doc DBTables
|
var doc DBTables
|
||||||
|
table := new(gorm.DB)
|
||||||
|
if config2.DatabaseConfig.DbType == "mysql" {
|
||||||
|
table = orm.Eloquent.Select("*").Table("information_schema.tables")
|
||||||
|
table = table.Where("table_schema= ? ", config2.DatabaseConfig.Mysql.DBName)
|
||||||
|
if e.TableName == "" {
|
||||||
|
return doc, errors.New("table name cannot be empty!")
|
||||||
|
}
|
||||||
|
table = table.Where("TABLE_NAME = ?", e.TableName)
|
||||||
|
} else if config2.DatabaseConfig.DbType == "sqlite3" {
|
||||||
|
table = orm.Eloquent.Select("name as TABLE_NAME,name as ENGINE,name as TABLE_ROWS,name as TABLE_COLLATION,name as CREATE_TIME,name as UPDATE_TIME,name as TABLE_COMMENT ").Table("sqlite_master")
|
||||||
|
table = table.Where("type = 'table' ")
|
||||||
|
|
||||||
table := orm.Eloquent.Select("*").Table("information_schema.tables")
|
if e.TableName != "" {
|
||||||
table = table.Where("table_schema= ? ", config2.DatabaseConfig.Name)
|
table = table.Where("name = ?", e.TableName)
|
||||||
if e.TableName == "" {
|
}
|
||||||
return doc, errors.New("table name cannot be empty!")
|
|
||||||
}
|
}
|
||||||
table = table.Where("TABLE_NAME = ?", e.TableName)
|
|
||||||
|
|
||||||
if err := table.First(&doc).Error; err != nil {
|
if err := table.First(&doc).Error; err != nil {
|
||||||
return doc, err
|
return doc, err
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-7
@@ -2,23 +2,38 @@ package mycasbin
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/casbin/casbin/v2"
|
"github.com/casbin/casbin/v2"
|
||||||
gormadapter "github.com/casbin/gorm-adapter/v2"
|
"github.com/casbin/casbin/v2/model"
|
||||||
"github.com/go-kit/kit/endpoint"
|
gormAdapter "github.com/casbin/gorm-adapter/v2"
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"go-admin/global/orm"
|
"go-admin/global/orm"
|
||||||
"go-admin/tools/config"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var Em endpoint.Middleware
|
// Initialize the model from a string.
|
||||||
|
var text = `
|
||||||
|
[request_definition]
|
||||||
|
r = sub, obj, act
|
||||||
|
|
||||||
|
[policy_definition]
|
||||||
|
p = sub, obj, act
|
||||||
|
|
||||||
|
[policy_effect]
|
||||||
|
e = some(where (p.eft == allow))
|
||||||
|
|
||||||
|
[matchers]
|
||||||
|
m = r.sub == p.sub && (keyMatch2(r.obj, p.obj) || keyMatch(r.obj, p.obj)) && (r.act == p.act || p.act == "*")
|
||||||
|
`
|
||||||
|
|
||||||
func Casbin() (*casbin.Enforcer, error) {
|
func Casbin() (*casbin.Enforcer, error) {
|
||||||
conn := orm.MysqlConn
|
Apter, err := gormAdapter.NewAdapterByDB(orm.Eloquent)
|
||||||
Apter, err := gormadapter.NewAdapter(config.DatabaseConfig.Dbtype, conn, true)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
e, err := casbin.NewEnforcer("config/rbac_model.conf", Apter)
|
m, err := model.NewModelFromString(text)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
e, err := casbin.NewEnforcer(m, Apter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,170 +0,0 @@
|
|||||||
package service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
"go-admin/global/orm"
|
|
||||||
. "go-admin/models"
|
|
||||||
"go-admin/tools"
|
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
// 获取用户数据
|
|
||||||
func (e *SysUser) Get() (SysUserView SysUserView, err error) {
|
|
||||||
|
|
||||||
table := orm.Eloquent.Table(e.TableName()).Select([]string{"sys_user.*", "sys_role.role_name"})
|
|
||||||
table = table.Joins("left join sys_role on sys_user.role_id=sys_role.role_id")
|
|
||||||
if e.UserId != 0 {
|
|
||||||
table = table.Where("user_id = ?", e.UserId)
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.Username != "" {
|
|
||||||
table = table.Where("username = ?", e.Username)
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.Password != "" {
|
|
||||||
table = table.Where("password = ?", e.Password)
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.RoleId != 0 {
|
|
||||||
table = table.Where("role_id = ?", e.RoleId)
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.DeptId != 0 {
|
|
||||||
table = table.Where("dept_id = ?", e.DeptId)
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.PostId != 0 {
|
|
||||||
table = table.Where("post_id = ?", e.PostId)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = table.First(&SysUserView).Error; err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
SysUserView.Password = ""
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *SysUser) GetPage(pageSize int, pageIndex int) ([]SysUserPage, int, error) {
|
|
||||||
var doc []SysUserPage
|
|
||||||
table := orm.Eloquent.Select("sys_user.*,sys_dept.dept_name").Table(e.TableName())
|
|
||||||
table = table.Joins("left join sys_dept on sys_dept.dept_id = sys_user.dept_id")
|
|
||||||
|
|
||||||
if e.Username != "" {
|
|
||||||
table = table.Where("username = ?", e.Username)
|
|
||||||
}
|
|
||||||
if e.Status != "" {
|
|
||||||
table = table.Where("sys_user.status = ?", e.Status)
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.Phone != "" {
|
|
||||||
table = table.Where("sys_user.phone = ?", e.Phone)
|
|
||||||
}
|
|
||||||
|
|
||||||
if e.DeptId != 0 {
|
|
||||||
table = table.Where("sys_user.dept_id in (select dept_id from sys_dept where dept_path like ? )", "%"+tools.IntToString(e.DeptId)+"%")
|
|
||||||
}
|
|
||||||
|
|
||||||
// 数据权限控制
|
|
||||||
dataPermission := new(DataPermission)
|
|
||||||
dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
|
|
||||||
table, err := dataPermission.GetDataScope("sys_user", table)
|
|
||||||
if err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
var count int
|
|
||||||
|
|
||||||
if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
|
|
||||||
return nil, 0, err
|
|
||||||
}
|
|
||||||
table.Where("sys_user.deleted_at IS NULL").Count(&count)
|
|
||||||
return doc, count, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
//加密
|
|
||||||
func (e *SysUser) Encrypt() (err error) {
|
|
||||||
if e.Password == "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var hash []byte
|
|
||||||
if hash, err = bcrypt.GenerateFromPassword([]byte(e.Password), bcrypt.DefaultCost); err != nil {
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
e.Password = string(hash)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//添加
|
|
||||||
func (e SysUser) Insert() (id int, err error) {
|
|
||||||
if err = e.Encrypt(); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// check 用户名
|
|
||||||
var count int
|
|
||||||
orm.Eloquent.Table(e.TableName()).Where("username = ?", e.Username).Count(&count)
|
|
||||||
if count > 0 {
|
|
||||||
err = errors.New("账户已存在!")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//添加数据
|
|
||||||
if err = orm.Eloquent.Table(e.TableName()).Create(&e).Error; err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
id = e.UserId
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//修改
|
|
||||||
func (e *SysUser) Update(id int) (update SysUser, err error) {
|
|
||||||
if e.Password!="" {
|
|
||||||
if err = e.Encrypt(); err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if err = orm.Eloquent.Table(e.TableName()).First(&update, id).Error; err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if e.RoleId == 0 {
|
|
||||||
e.RoleId = update.RoleId
|
|
||||||
}
|
|
||||||
|
|
||||||
//参数1:是要修改的数据
|
|
||||||
//参数2:是修改的数据
|
|
||||||
if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *SysUser) BatchDelete(id []int) (Result bool, err error) {
|
|
||||||
if err = orm.Eloquent.Table(e.TableName()).Where("user_id in (?)", id).Delete(&SysUser{}).Error; err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
Result = true
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *SysUser) SetPwd(pwd SysUserPwd) (Result bool, err error) {
|
|
||||||
user, err := e.Get()
|
|
||||||
if err != nil {
|
|
||||||
tools.HasError(err, "获取用户数据失败(代码202)", 500)
|
|
||||||
}
|
|
||||||
_, err = tools.CompareHashAndPassword(user.Password, pwd.OldPassword)
|
|
||||||
if err != nil {
|
|
||||||
if strings.Contains(err.Error(), "hashedPassword is not the hash of the given password") {
|
|
||||||
tools.HasError(err, "密码错误(代码202)", 500)
|
|
||||||
}
|
|
||||||
log.Print(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
e.Password = pwd.NewPassword
|
|
||||||
_, err = e.Update(e.UserId)
|
|
||||||
tools.HasError(err, "更新密码失败(代码202)", 500)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
BIN
Binary file not shown.
+69
-13
@@ -3,23 +3,79 @@ package config
|
|||||||
import "github.com/spf13/viper"
|
import "github.com/spf13/viper"
|
||||||
|
|
||||||
type Database struct {
|
type Database struct {
|
||||||
Dbtype string
|
DbType string
|
||||||
Host string
|
SqLite *SqLite
|
||||||
Port int
|
Mysql *Mysql
|
||||||
Name string
|
PgSql *PgSql
|
||||||
Username string
|
}
|
||||||
Password string
|
|
||||||
|
type SqLite struct {
|
||||||
|
MasterConn string
|
||||||
|
DBName string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Mysql struct {
|
||||||
|
MasterConn string
|
||||||
|
DBName string
|
||||||
|
}
|
||||||
|
|
||||||
|
type PgSql struct {
|
||||||
|
MasterConn string
|
||||||
|
DBName string
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitDatabase(cfg *viper.Viper) *Database {
|
func InitDatabase(cfg *viper.Viper) *Database {
|
||||||
return &Database{
|
|
||||||
Port: cfg.GetInt("port"),
|
dbType := cfg.GetString("dbType")
|
||||||
Dbtype: cfg.GetString("dbType"),
|
|
||||||
Host: cfg.GetString("host"),
|
db := &Database{
|
||||||
Name: cfg.GetString("name"),
|
DbType: cfg.GetString("dbType"),
|
||||||
Username: cfg.GetString("username"),
|
|
||||||
Password: cfg.GetString("password"),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dbType == "sqlite3" {
|
||||||
|
sqlit := cfg.Sub("sqlite")
|
||||||
|
if sqlit == nil {
|
||||||
|
panic("config not found settings.database.sqlite")
|
||||||
|
}
|
||||||
|
db.SqLite = InitSqlite(sqlit)
|
||||||
|
} else if dbType == "mysql" {
|
||||||
|
mysql := cfg.Sub("mysql")
|
||||||
|
if mysql == nil {
|
||||||
|
panic("config not found settings.database.mysql")
|
||||||
|
}
|
||||||
|
db.Mysql = InitMysql(mysql)
|
||||||
|
} else if dbType == "pgsql" {
|
||||||
|
pgsql := cfg.Sub("pgsql")
|
||||||
|
if pgsql == nil {
|
||||||
|
panic("config not found settings.database.pgsql")
|
||||||
|
}
|
||||||
|
db.PgSql = InitPgsql(pgsql)
|
||||||
|
} else {
|
||||||
|
panic("unknown dbtype")
|
||||||
|
}
|
||||||
|
|
||||||
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
var DatabaseConfig = new(Database)
|
var DatabaseConfig = new(Database)
|
||||||
|
|
||||||
|
func InitSqlite(cfg *viper.Viper) *SqLite {
|
||||||
|
return &SqLite{
|
||||||
|
MasterConn: cfg.GetString("masterconn"),
|
||||||
|
DBName: cfg.GetString("dbname"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitMysql(cfg *viper.Viper) *Mysql {
|
||||||
|
return &Mysql{
|
||||||
|
MasterConn: cfg.GetString("masterconn"),
|
||||||
|
DBName: cfg.GetString("dbname"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func InitPgsql(cfg *viper.Viper) *PgSql {
|
||||||
|
return &PgSql{
|
||||||
|
MasterConn: cfg.GetString("masterconn"),
|
||||||
|
DBName: cfg.GetString("dbname"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+1
-1
@@ -9,5 +9,5 @@ const (
|
|||||||
ModeTest Mode = "test" //测试模式
|
ModeTest Mode = "test" //测试模式
|
||||||
ModeProd Mode = "prod" //生产模式
|
ModeProd Mode = "prod" //生产模式
|
||||||
Mysql = "mysql" //mysql数据库标识
|
Mysql = "mysql" //mysql数据库标识
|
||||||
Sqlite = "sqlite" //sqlite
|
Sqlite = "sqlite3" //sqlite
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user