123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- package system
- import (
- "errors"
- "fmt"
- "net/url"
- "os"
- "strings"
- "server/global"
- "server/model/common/response"
- "server/model/system"
- "server/utils"
- "github.com/gin-gonic/gin"
- "go.uber.org/zap"
- )
- type AutoCodeApi struct{}
- // PreviewTemp
- // @Tags AutoCode
- // @Summary 预览创建后的代码
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body system.AutoCodeStruct true "预览创建代码"
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "预览创建后的代码"
- // @Router /autoCode/preview [post]
- func (autoApi *AutoCodeApi) PreviewTemp(c *gin.Context) {
- var a system.AutoCodeStruct
- _ = c.ShouldBindJSON(&a)
- if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- a.Pretreatment() // 处理go关键字
- a.PackageT = utils.FirstUpper(a.Package)
- autoCode, err := autoCodeService.PreviewTemp(a)
- if err != nil {
- global.GVA_LOG.Error("预览失败!", zap.Error(err))
- response.FailWithMessage("预览失败", c)
- } else {
- response.OkWithDetailed(gin.H{"autoCode": autoCode}, "预览成功", c)
- }
- }
- // CreateTemp
- // @Tags AutoCode
- // @Summary 自动代码模板
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body system.AutoCodeStruct true "创建自动代码"
- // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
- // @Router /autoCode/createTemp [post]
- func (autoApi *AutoCodeApi) CreateTemp(c *gin.Context) {
- var a system.AutoCodeStruct
- _ = c.ShouldBindJSON(&a)
- if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- a.Pretreatment()
- var apiIds []uint
- var menuId uint
- if a.AutoCreateApiToSql {
- if ids, err := autoCodeService.AutoCreateApi(&a); err != nil {
- global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Error(err))
- c.Writer.Header().Add("success", "false")
- c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
- return
- } else {
- apiIds = ids
- }
- }
- if a.AutoCreateMenuToSql {
- if id, err := autoCodeService.AutoCreateMenu(&a); err != nil {
- global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Error(err))
- c.Writer.Header().Add("success", "false")
- c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
- } else {
- menuId = id
- }
- }
- a.PackageT = utils.FirstUpper(a.Package)
- err := autoCodeService.CreateTemp(a, menuId, apiIds...)
- if err != nil {
- if errors.Is(err, system.ErrAutoMove) {
- c.Writer.Header().Add("success", "true")
- c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
- } else {
- c.Writer.Header().Add("success", "false")
- c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
- _ = os.Remove("./ginvueadmin.zip")
- }
- } else {
- c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; deptname=%s", "ginvueadmin.zip")) // fmt.Sprintf("attachment; deptname=%s", deptname)对下载的文件重命名
- c.Writer.Header().Add("Content-Type", "application/json")
- c.Writer.Header().Add("success", "true")
- c.File("./ginvueadmin.zip")
- _ = os.Remove("./ginvueadmin.zip")
- }
- }
- // GetDB
- // @Tags AutoCode
- // @Summary 获取当前所有数据库
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前所有数据库"
- // @Router /autoCode/getDatabase [get]
- func (autoApi *AutoCodeApi) GetDB(c *gin.Context) {
- businessDB := c.Query("businessDB")
- dbs, err := autoCodeService.Database(businessDB).GetDB(businessDB)
- var dbList []map[string]interface{}
- for _, db := range global.GVA_CONFIG.DBList {
- var item = make(map[string]interface{})
- item["aliasName"] = db.AliasName
- item["dbName"] = db.Dbname
- item["disable"] = db.Disable
- item["dbtype"] = db.Type
- dbList = append(dbList, item)
- }
- if err != nil {
- global.GVA_LOG.Error("获取失败!", zap.Error(err))
- response.FailWithMessage("获取失败", c)
- } else {
- response.OkWithDetailed(gin.H{"dbs": dbs, "dbList": dbList}, "获取成功", c)
- }
- }
- // GetTables
- // @Tags AutoCode
- // @Summary 获取当前数据库所有表
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前数据库所有表"
- // @Router /autoCode/getTables [get]
- func (autoApi *AutoCodeApi) GetTables(c *gin.Context) {
- dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
- businessDB := c.Query("businessDB")
- tables, err := autoCodeService.Database(businessDB).GetTables(businessDB, dbName)
- if err != nil {
- global.GVA_LOG.Error("查询table失败!", zap.Error(err))
- response.FailWithMessage("查询table失败", c)
- } else {
- response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", c)
- }
- }
- // GetColumn
- // @Tags AutoCode
- // @Summary 获取当前表所有字段
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "获取当前表所有字段"
- // @Router /autoCode/getColumn [get]
- func (autoApi *AutoCodeApi) GetColumn(c *gin.Context) {
- businessDB := c.Query("businessDB")
- dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
- tableName := c.Query("tableName")
- columns, err := autoCodeService.Database(businessDB).GetColumn(businessDB, tableName, dbName)
- if err != nil {
- global.GVA_LOG.Error("获取失败!", zap.Error(err))
- response.FailWithMessage("获取失败", c)
- } else {
- response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
- }
- }
- // CreatePackage
- // @Tags AutoCode
- // @Summary 创建package
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body system.SysAutoCode true "创建package"
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "创建package成功"
- // @Router /autoCode/createPackage [post]
- func (autoApi *AutoCodeApi) CreatePackage(c *gin.Context) {
- var a system.SysAutoCode
- _ = c.ShouldBindJSON(&a)
- if err := utils.Verify(a, utils.AutoPackageVerify); err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- // PackageName可能导致路径穿越的问题 / 和 \ 都要防止
- if strings.Contains(a.PackageName, "\\") || strings.Contains(a.PackageName, "/") || strings.Contains(a.PackageName, "..") {
- response.FailWithMessage("包名不合法", c)
- return
- }
- err := autoCodeService.CreateAutoCode(&a)
- if err != nil {
- global.GVA_LOG.Error("创建失败!", zap.Error(err))
- response.FailWithMessage("创建失败", c)
- } else {
- response.OkWithMessage("创建成功", c)
- }
- }
- // GetPackage
- // @Tags AutoCode
- // @Summary 获取package
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "创建package成功"
- // @Router /autoCode/getPackage [post]
- func (autoApi *AutoCodeApi) GetPackage(c *gin.Context) {
- pkgs, err := autoCodeService.GetPackage()
- if err != nil {
- global.GVA_LOG.Error("获取失败!", zap.Error(err))
- response.FailWithMessage("获取失败", c)
- } else {
- response.OkWithDetailed(gin.H{"pkgs": pkgs}, "获取成功", c)
- }
- }
- // DelPackage
- // @Tags AutoCode
- // @Summary 删除package
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body system.SysAutoCode true "创建package"
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "删除package成功"
- // @Router /autoCode/delPackage [post]
- func (autoApi *AutoCodeApi) DelPackage(c *gin.Context) {
- var a system.SysAutoCode
- _ = c.ShouldBindJSON(&a)
- err := autoCodeService.DelPackage(a)
- if err != nil {
- global.GVA_LOG.Error("删除失败!", zap.Error(err))
- response.FailWithMessage("删除失败", c)
- } else {
- response.OkWithMessage("删除成功", c)
- }
- }
- // AutoPlug
- // @Tags AutoCode
- // @Summary 创建插件模板
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body system.SysAutoCode true "创建插件模板"
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "创建插件模板成功"
- // @Router /autoCode/createPlug [post]
- func (autoApi *AutoCodeApi) AutoPlug(c *gin.Context) {
- var a system.AutoPlugReq
- err := c.ShouldBindJSON(&a)
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- if strings.Contains(a.PlugName, "\\") || strings.Contains(a.PlugName, "/") || strings.Contains(a.PlugName, "..") {
- response.FailWithMessage("插件名称不合法", c)
- return
- }
- a.Snake = strings.ToLower(a.PlugName)
- a.NeedModel = a.HasRequest || a.HasResponse
- err = autoCodeService.CreatePlug(a)
- if err != nil {
- global.GVA_LOG.Error("预览失败!", zap.Error(err))
- response.FailWithMessage("预览失败", c)
- return
- }
- response.Ok(c)
- }
- // InstallPlugin
- // @Tags AutoCode
- // @Summary 安装插件
- // @Security ApiKeyAuth
- // @accept multipart/form-data
- // @Produce application/json
- // @Param plug formData dept true "this is a test dept"
- // @Success 200 {object} response.Response{data=[]interface{},msg=string} "安装插件成功"
- // @Router /autoCode/installPlugin [post]
- func (autoApi *AutoCodeApi) InstallPlugin(c *gin.Context) {
- header, err := c.FormFile("plug")
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- web, server, err := autoCodeService.InstallPlugin(header)
- webStr := "web插件安装成功"
- serverStr := "server插件安装成功"
- if web == -1 {
- webStr = "web端插件未成功安装,请按照文档自行解压安装,如果为纯后端插件请忽略此条提示"
- }
- if server == -1 {
- serverStr = "server端插件未成功安装,请按照文档自行解压安装,如果为纯前端插件请忽略此条提示"
- }
- if err != nil {
- response.FailWithMessage(err.Error(), c)
- return
- }
- response.OkWithData([]interface{}{
- gin.H{
- "code": web,
- "msg": webStr,
- },
- gin.H{
- "code": server,
- "msg": serverStr,
- }}, c)
- }
- // PubPlug
- // @Tags AutoCode
- // @Summary 打包插件
- // @Security ApiKeyAuth
- // @accept application/json
- // @Produce application/json
- // @Param data body system.SysAutoCode true "打包插件"
- // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "打包插件成功"
- // @Router /autoCode/pubPlug [get]
- func (autoApi *AutoCodeApi) PubPlug(c *gin.Context) {
- plugName := c.Query("plugName")
- zipPath, err := autoCodeService.PubPlug(plugName)
- if err != nil {
- global.GVA_LOG.Error("打包失败!", zap.Error(err))
- response.FailWithMessage("打包失败"+err.Error(), c)
- return
- }
- response.OkWithMessage(fmt.Sprintf("打包成功,文件路径为:%s", zipPath), c)
- }
|