You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
4.7 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package system
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/url"
  6. "os"
  7. "github.com/flipped-aurora/gin-vue-admin/server/global"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  9. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  10. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  11. "github.com/gin-gonic/gin"
  12. "go.uber.org/zap"
  13. )
  14. type AutoCodeApi struct{}
  15. // PreviewTemp
  16. // @Tags AutoCode
  17. // @Summary 预览创建后的代码
  18. // @Security ApiKeyAuth
  19. // @accept application/json
  20. // @Produce application/json
  21. // @Param data body system.AutoCodeStruct true "预览创建代码"
  22. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  23. // @Router /autoCode/preview [post]
  24. func (autoApi *AutoCodeApi) PreviewTemp(c *gin.Context) {
  25. var a system.AutoCodeStruct
  26. _ = c.ShouldBindJSON(&a)
  27. if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
  28. response.FailWithMessage(err.Error(), c)
  29. return
  30. }
  31. autoCode, err := autoCodeService.PreviewTemp(a)
  32. if err != nil {
  33. global.GVA_LOG.Error("预览失败!", zap.Error(err))
  34. response.FailWithMessage("预览失败", c)
  35. } else {
  36. response.OkWithDetailed(gin.H{"autoCode": autoCode}, "预览成功", c)
  37. }
  38. }
  39. // CreateTemp
  40. // @Tags AutoCode
  41. // @Summary 自动代码模板
  42. // @Security ApiKeyAuth
  43. // @accept application/json
  44. // @Produce application/json
  45. // @Param data body system.AutoCodeStruct true "创建自动代码"
  46. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  47. // @Router /autoCode/createTemp [post]
  48. func (autoApi *AutoCodeApi) CreateTemp(c *gin.Context) {
  49. var a system.AutoCodeStruct
  50. _ = c.ShouldBindJSON(&a)
  51. if err := utils.Verify(a, utils.AutoCodeVerify); err != nil {
  52. response.FailWithMessage(err.Error(), c)
  53. return
  54. }
  55. var apiIds []uint
  56. if a.AutoCreateApiToSql {
  57. if ids, err := autoCodeService.AutoCreateApi(&a); err != nil {
  58. global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Error(err))
  59. c.Writer.Header().Add("success", "false")
  60. c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
  61. return
  62. } else {
  63. apiIds = ids
  64. }
  65. }
  66. err := autoCodeService.CreateTemp(a, apiIds...)
  67. if err != nil {
  68. if errors.Is(err, system.AutoMoveErr) {
  69. c.Writer.Header().Add("success", "true")
  70. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  71. } else {
  72. c.Writer.Header().Add("success", "false")
  73. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  74. _ = os.Remove("./ginvueadmin.zip")
  75. }
  76. } else {
  77. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) // fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
  78. c.Writer.Header().Add("Content-Type", "application/json")
  79. c.Writer.Header().Add("success", "true")
  80. c.File("./ginvueadmin.zip")
  81. _ = os.Remove("./ginvueadmin.zip")
  82. }
  83. }
  84. // GetDB
  85. // @Tags AutoCode
  86. // @Summary 获取当前所有数据库
  87. // @Security ApiKeyAuth
  88. // @accept application/json
  89. // @Produce application/json
  90. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  91. // @Router /autoCode/getDatabase [get]
  92. func (autoApi *AutoCodeApi) GetDB(c *gin.Context) {
  93. dbs, err := autoCodeService.Database().GetDB()
  94. if err != nil {
  95. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  96. response.FailWithMessage("获取失败", c)
  97. }
  98. response.OkWithDetailed(gin.H{"dbs": dbs}, "获取成功", c)
  99. }
  100. // GetTables
  101. // @Tags AutoCode
  102. // @Summary 获取当前数据库所有表
  103. // @Security ApiKeyAuth
  104. // @accept application/json
  105. // @Produce application/json
  106. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  107. // @Router /autoCode/getTables [get]
  108. func (autoApi *AutoCodeApi) GetTables(c *gin.Context) {
  109. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  110. tables, err := autoCodeService.Database().GetTables(dbName)
  111. if err != nil {
  112. global.GVA_LOG.Error("查询table失败!", zap.Error(err))
  113. response.FailWithMessage("查询table失败", c)
  114. } else {
  115. response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", c)
  116. }
  117. }
  118. // GetColumn
  119. // @Tags AutoCode
  120. // @Summary 获取当前表所有字段
  121. // @Security ApiKeyAuth
  122. // @accept application/json
  123. // @Produce application/json
  124. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  125. // @Router /autoCode/getColumn [get]
  126. func (autoApi *AutoCodeApi) GetColumn(c *gin.Context) {
  127. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  128. tableName := c.Query("tableName")
  129. columns, err := autoCodeService.Database().GetColumn(tableName, dbName)
  130. if err != nil {
  131. global.GVA_LOG.Error("获取失败!", zap.Error(err))
  132. response.FailWithMessage("获取失败", c)
  133. }
  134. response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
  135. }