sys_auto_code.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // TODO @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. apiList := [6]model.SysApi{
  32. {
  33. Path: "/" + a.Abbreviation + "/" + "create" + a.StructName,
  34. Description: "新增" + a.Description,
  35. ApiGroup: a.Abbreviation,
  36. Method: "POST",
  37. },
  38. {
  39. Path: "/" + a.Abbreviation + "/" + "delete" + a.StructName,
  40. Description: "删除" + a.Description,
  41. ApiGroup: a.Abbreviation,
  42. Method: "DELETE",
  43. },
  44. {
  45. Path: "/" + a.Abbreviation + "/" + "delete" + a.StructName + "ByIds",
  46. Description: "批量删除" + a.Description,
  47. ApiGroup: a.Abbreviation,
  48. Method: "DELETE",
  49. },
  50. {
  51. Path: "/" + a.Abbreviation + "/" + "update" + a.StructName,
  52. Description: "更新" + a.Description,
  53. ApiGroup: a.Abbreviation,
  54. Method: "PUT",
  55. },
  56. {
  57. Path: "/" + a.Abbreviation + "/" + "find" + a.StructName,
  58. Description: "根据ID获取" + a.Description,
  59. ApiGroup: a.Abbreviation,
  60. Method: "GET",
  61. },
  62. {
  63. Path: "/" + a.Abbreviation + "/" + "get" + a.StructName + "List",
  64. Description: "获取" + a.Description + "列表",
  65. ApiGroup: a.Abbreviation,
  66. Method: "GET",
  67. },
  68. }
  69. for _, v := range apiList {
  70. if err := service.AutoCreateApi(v); err != nil {
  71. global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
  72. c.Writer.Header().Add("success", "false")
  73. c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
  74. return
  75. }
  76. }
  77. }
  78. err := service.CreateTemp(a)
  79. if err != nil {
  80. if errors.Is(err, model.AutoMoveErr) {
  81. response.Ok(c)
  82. } else {
  83. c.Writer.Header().Add("success", "false")
  84. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  85. _ = os.Remove("./ginvueadmin.zip")
  86. }
  87. } else {
  88. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) // fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
  89. c.Writer.Header().Add("Content-Type", "application/json")
  90. c.Writer.Header().Add("success", "true")
  91. c.File("./ginvueadmin.zip")
  92. _ = os.Remove("./ginvueadmin.zip")
  93. }
  94. }
  95. // @Tags AutoCode
  96. // @Summary 获取当前数据库所有表
  97. // @Security ApiKeyAuth
  98. // @accept application/json
  99. // @Produce application/json
  100. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  101. // @Router /autoCode/getTables [get]
  102. func GetTables(c *gin.Context) {
  103. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  104. err, tables := service.GetTables(dbName)
  105. if err != nil {
  106. global.GVA_LOG.Error("查询table失败!", zap.Any("err", err))
  107. response.FailWithMessage("查询table失败", c)
  108. } else {
  109. response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", 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/getDatabase [get]
  119. func GetDB(c *gin.Context) {
  120. if err, dbs := service.GetDB(); err != nil {
  121. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  122. response.FailWithMessage("获取失败", c)
  123. } else {
  124. response.OkWithDetailed(gin.H{"dbs": dbs}, "获取成功", c)
  125. }
  126. }
  127. // @Tags AutoCode
  128. // @Summary 获取当前表所有字段
  129. // @Security ApiKeyAuth
  130. // @accept application/json
  131. // @Produce application/json
  132. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  133. // @Router /autoCode/getColumn [get]
  134. func GetColumn(c *gin.Context) {
  135. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  136. tableName := c.Query("tableName")
  137. if err, columns := service.GetColumn(tableName, dbName); err != nil {
  138. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  139. response.FailWithMessage("获取失败", c)
  140. } else {
  141. response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
  142. }
  143. }