From 89556ec416fb759ba547f155d744ef72bcc14c00 Mon Sep 17 00:00:00 2001 From: QM303176530 <303176530@qq.com> Date: Sun, 5 Jul 2020 16:29:45 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E4=BB=8E=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/api/v1/sys_auto_code.go | 59 ++++ server/go.mod | 4 +- server/model/request/sys_autocode.go | 15 + server/router/sys_auto_code.go | 7 +- server/service/sys_auto_code.go | 17 + web/src/api/autoCode.js | 47 +++ .../autoCode/component/fieldDialog.vue | 30 +- web/src/view/systemTools/autoCode/index.vue | 302 ++++++++++++------ 8 files changed, 373 insertions(+), 108 deletions(-) create mode 100644 server/model/request/sys_autocode.go diff --git a/server/api/v1/sys_auto_code.go b/server/api/v1/sys_auto_code.go index 93fadf75..a19aeed7 100644 --- a/server/api/v1/sys_auto_code.go +++ b/server/api/v1/sys_auto_code.go @@ -2,6 +2,7 @@ package v1 import ( "fmt" + "gin-vue-admin/global" "gin-vue-admin/global/response" "gin-vue-admin/model" "gin-vue-admin/service" @@ -87,3 +88,61 @@ func CreateTemp(c *gin.Context) { os.Remove("./ginvueadmin.zip") } } + +// @Tags SysApi +// @Summary 获取当前数据库所有表 +// @Security ApiKeyAuth +// @accept application/json +// @Produce application/json +// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}" +// @Router /autoCode/getTables [get] + +func GetTables(c *gin.Context) { + dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname) + err, tables := service.GetTables(dbName) + if err != nil { + response.FailWithMessage(fmt.Sprintf("查询table失败,%v", err), c) + } else { + response.OkWithData(gin.H{ + "tables": tables, + }, c) + } +} + +// @Tags SysApi +// @Summary 获取当前所有数据库 +// @Security ApiKeyAuth +// @accept application/json +// @Produce application/json +// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}" +// @Router /autoCode/getDatabase [get] +func GetDB(c *gin.Context) { + err, dbs := service.GetDB() + if err != nil { + response.FailWithMessage(fmt.Sprintf("查询table失败,%v", err), c) + } else { + response.OkWithData(gin.H{ + "dbs": dbs, + }, c) + } +} + +// @Tags SysApi +// @Summary 获取当前表所有字段 +// @Security ApiKeyAuth +// @accept application/json +// @Produce application/json +// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}" +// @Router /autoCode/getDatabase [get] +func GetColume(c *gin.Context) { + dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname) + tableName := c.Query("tableName") + err, columes := service.GetColume(tableName, dbName) + if err != nil { + response.FailWithMessage(fmt.Sprintf("查询table失败,%v", err), c) + } else { + response.OkWithData(gin.H{ + "columes": columes, + }, c) + } +} diff --git a/server/go.mod b/server/go.mod index 60fc639a..58893cd8 100644 --- a/server/go.mod +++ b/server/go.mod @@ -14,7 +14,7 @@ require ( github.com/go-openapi/swag v0.19.8 // indirect github.com/go-playground/validator/v10 v10.3.0 // indirect github.com/go-redis/redis v6.15.7+incompatible - github.com/go-sql-driver/mysql v1.5.0 // indirect + github.com/go-sql-driver/mysql v1.5.0 github.com/golang/protobuf v1.4.2 // indirect github.com/jehiah/go-strftime v0.0.0-20171201141054-1d33003b3869 // indirect github.com/jinzhu/gorm v1.9.12 @@ -32,8 +32,8 @@ require ( github.com/pelletier/go-toml v1.6.0 // indirect github.com/piexlmax/gvaplug v0.0.8 github.com/pkg/errors v0.9.1 // indirect - github.com/qiniu/x v1.10.5 github.com/qiniu/api.v7/v7 v7.4.1 + github.com/qiniu/x v1.10.5 github.com/satori/go.uuid v1.2.0 github.com/spf13/afero v1.2.2 // indirect github.com/spf13/cast v1.3.1 // indirect diff --git a/server/model/request/sys_autocode.go b/server/model/request/sys_autocode.go new file mode 100644 index 00000000..e163fef4 --- /dev/null +++ b/server/model/request/sys_autocode.go @@ -0,0 +1,15 @@ +package request + +type DBReq struct { + Database string `json:"database";gorm:"column:database"` +} + +type TableReq struct { + TableName string `json:"tableName"` +} + +type ColumeReq struct { + ColumeName string `json:"columeName";gorm:"column:colume_name"` + DataType string `json:"dataType";gorm:"column:data_type"` + ColumeComment string `json:"columeComment";gorm:"column:colume_comment"` +} diff --git a/server/router/sys_auto_code.go b/server/router/sys_auto_code.go index effbd33d..617d88ae 100644 --- a/server/router/sys_auto_code.go +++ b/server/router/sys_auto_code.go @@ -2,13 +2,16 @@ package router import ( "gin-vue-admin/api/v1" - "gin-vue-admin/middleware" "github.com/gin-gonic/gin" ) func InitAutoCodeRouter(Router *gin.RouterGroup) { - AutoCodeRouter := Router.Group("autoCode").Use(middleware.JWTAuth()).Use(middleware.CasbinHandler()) + AutoCodeRouter := Router.Group("autoCode") { AutoCodeRouter.POST("createTemp", v1.CreateTemp) // 创建自动化代码 + AutoCodeRouter.GET("getTables", v1.GetTables) // 获取对应数据库的表 + AutoCodeRouter.GET("getDB", v1.GetDB) // 获取数据库 + AutoCodeRouter.GET("getColume", v1.GetColume) // 获取指定表所有字段信息 } + } diff --git a/server/service/sys_auto_code.go b/server/service/sys_auto_code.go index 7afda057..8e785541 100644 --- a/server/service/sys_auto_code.go +++ b/server/service/sys_auto_code.go @@ -1,7 +1,9 @@ package service import ( + "gin-vue-admin/global" "gin-vue-admin/model" + "gin-vue-admin/model/request" "gin-vue-admin/utils" "io/ioutil" "os" @@ -115,3 +117,18 @@ func GetAllTplFile(pathName string, fileList []string) ([]string, error) { } return fileList, err } + +func GetTables(dbName string) (err error, TableNames []request.TableReq) { + err = global.GVA_DB.Raw("select table_name from information_schema.tables where table_schema= ? and table_type= ? ", dbName, "base table").Scan(&TableNames).Error + return err, TableNames +} + +func GetDB() (err error, DBNames []request.DBReq) { + err = global.GVA_DB.Raw("SELECT SCHEMA_NAME AS `database` FROM INFORMATION_SCHEMA.SCHEMATA;").Scan(&DBNames).Error + return err, DBNames +} + +func GetColume(tableName string, dbName string) (err error, Columes []request.ColumeReq) { + err = global.GVA_DB.Raw("select COLUMN_NAME as 'colume_name', DATA_TYPE as 'data_type', COLUMN_COMMENT as 'colume_comment' from information_schema.COLUMNS where table_name = ? and table_schema = ?", tableName, dbName).Scan(&Columes).Error + return err, Columes +} diff --git a/web/src/api/autoCode.js b/web/src/api/autoCode.js index 2fd8c7b3..d727adfb 100644 --- a/web/src/api/autoCode.js +++ b/web/src/api/autoCode.js @@ -18,4 +18,51 @@ export const createTemp = (data) => { data, responseType: 'blob' }) +} + + +// @Tags SysApi +// @Summary 获取当前所有数据库 +// @Security ApiKeyAuth +// @accept application/json +// @Produce application/json +// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}" +// @Router /autoCode/getDatabase [get] +export const getDB = () => { + return service({ + url: "/autoCode/getDB", + method: 'get', + }) +} + + + +// @Tags SysApi +// @Summary 获取当前数据库所有表 +// @Security ApiKeyAuth +// @accept application/json +// @Produce application/json +// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}" +// @Router /autoCode/getTables [get] +export const getTable = (params) => { + return service({ + url: "/autoCode/getTables", + method: 'get', + params, + }) +} + +// @Tags SysApi +// @Summary 获取当前数据库所有表 +// @Security ApiKeyAuth +// @accept application/json +// @Produce application/json +// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}" +// @Router /autoCode/getColume [get] +export const getColume = (params) => { + return service({ + url: "/autoCode/getColume", + method: 'get', + params, + }) } \ No newline at end of file diff --git a/web/src/view/systemTools/autoCode/component/fieldDialog.vue b/web/src/view/systemTools/autoCode/component/fieldDialog.vue index da2936f7..66c4011a 100644 --- a/web/src/view/systemTools/autoCode/component/fieldDialog.vue +++ b/web/src/view/systemTools/autoCode/component/fieldDialog.vue @@ -29,7 +29,7 @@ - + + + + + + + + + + - +