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.

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