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.

111 lines
3.6 KiB

  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/response"
  7. "gin-vue-admin/service"
  8. "gin-vue-admin/utils"
  9. "github.com/gin-gonic/gin"
  10. "github.com/pkg/errors"
  11. "go.uber.org/zap"
  12. "net/url"
  13. "os"
  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/createTemp [post]
  23. func CreateTemp(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. if a.AutoCreateApiToSql {
  31. if err := service.AutoCreateApi(&a); err != nil {
  32. global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
  33. c.Writer.Header().Add("success", "false")
  34. c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
  35. return
  36. }
  37. }
  38. err := service.CreateTemp(a)
  39. if err != nil {
  40. if errors.Is(err, model.AutoMoveErr) {
  41. c.Writer.Header().Add("success", "false")
  42. c.Writer.Header().Add("msgtype", "success")
  43. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  44. } else {
  45. c.Writer.Header().Add("success", "false")
  46. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  47. _ = os.Remove("./ginvueadmin.zip")
  48. }
  49. } else {
  50. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) // fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
  51. c.Writer.Header().Add("Content-Type", "application/json")
  52. c.Writer.Header().Add("success", "true")
  53. c.File("./ginvueadmin.zip")
  54. _ = os.Remove("./ginvueadmin.zip")
  55. }
  56. }
  57. // @Tags AutoCode
  58. // @Summary 获取当前数据库所有表
  59. // @Security ApiKeyAuth
  60. // @accept application/json
  61. // @Produce application/json
  62. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  63. // @Router /autoCode/getTables [get]
  64. func GetTables(c *gin.Context) {
  65. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  66. err, tables := service.GetTables(dbName)
  67. if err != nil {
  68. global.GVA_LOG.Error("查询table失败!", zap.Any("err", err))
  69. response.FailWithMessage("查询table失败", c)
  70. } else {
  71. response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", c)
  72. }
  73. }
  74. // @Tags AutoCode
  75. // @Summary 获取当前所有数据库
  76. // @Security ApiKeyAuth
  77. // @accept application/json
  78. // @Produce application/json
  79. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  80. // @Router /autoCode/getDatabase [get]
  81. func GetDB(c *gin.Context) {
  82. if err, dbs := service.GetDB(); err != nil {
  83. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  84. response.FailWithMessage("获取失败", c)
  85. } else {
  86. response.OkWithDetailed(gin.H{"dbs": dbs}, "获取成功", c)
  87. }
  88. }
  89. // @Tags AutoCode
  90. // @Summary 获取当前表所有字段
  91. // @Security ApiKeyAuth
  92. // @accept application/json
  93. // @Produce application/json
  94. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  95. // @Router /autoCode/getColumn [get]
  96. func GetColumn(c *gin.Context) {
  97. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  98. tableName := c.Query("tableName")
  99. if err, columns := service.GetColumn(tableName, dbName); err != nil {
  100. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  101. response.FailWithMessage("获取失败", c)
  102. } else {
  103. response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
  104. }
  105. }