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.

136 lines
4.4 KiB

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