sys_auto_code.go 4.5 KB

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