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.

151 lines
4.8 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. 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. c.Writer.Header().Add("success", "false")
  82. c.Writer.Header().Add("msgtype", "success")
  83. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  84. } else {
  85. c.Writer.Header().Add("success", "false")
  86. c.Writer.Header().Add("msg", url.QueryEscape(err.Error()))
  87. _ = os.Remove("./ginvueadmin.zip")
  88. }
  89. } else {
  90. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) // fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
  91. c.Writer.Header().Add("Content-Type", "application/json")
  92. c.Writer.Header().Add("success", "true")
  93. c.File("./ginvueadmin.zip")
  94. _ = os.Remove("./ginvueadmin.zip")
  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/getTables [get]
  104. func GetTables(c *gin.Context) {
  105. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  106. err, tables := service.GetTables(dbName)
  107. if err != nil {
  108. global.GVA_LOG.Error("查询table失败!", zap.Any("err", err))
  109. response.FailWithMessage("查询table失败", c)
  110. } else {
  111. response.OkWithDetailed(gin.H{"tables": tables}, "获取成功", c)
  112. }
  113. }
  114. // @Tags AutoCode
  115. // @Summary 获取当前所有数据库
  116. // @Security ApiKeyAuth
  117. // @accept application/json
  118. // @Produce application/json
  119. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  120. // @Router /autoCode/getDatabase [get]
  121. func GetDB(c *gin.Context) {
  122. if err, dbs := service.GetDB(); err != nil {
  123. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  124. response.FailWithMessage("获取失败", c)
  125. } else {
  126. response.OkWithDetailed(gin.H{"dbs": dbs}, "获取成功", c)
  127. }
  128. }
  129. // @Tags AutoCode
  130. // @Summary 获取当前表所有字段
  131. // @Security ApiKeyAuth
  132. // @accept application/json
  133. // @Produce application/json
  134. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  135. // @Router /autoCode/getColumn [get]
  136. func GetColumn(c *gin.Context) {
  137. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  138. tableName := c.Query("tableName")
  139. if err, columns := service.GetColumn(tableName, dbName); err != nil {
  140. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  141. response.FailWithMessage("获取失败", c)
  142. } else {
  143. response.OkWithDetailed(gin.H{"columns": columns}, "获取成功", c)
  144. }
  145. }