sys_auto_code.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 := [5]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 + "/" + "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. errC := service.CreateApi(v)
  70. if errC != nil {
  71. c.Writer.Header().Add("success", "false")
  72. c.Writer.Header().Add("msg", url.QueryEscape(fmt.Sprintf("自动化创建失败,%v,请自行清空垃圾数据", errC)))
  73. return
  74. }
  75. }
  76. }
  77. err := service.CreateTemp(a)
  78. if err != nil {
  79. response.FailWithMessage(fmt.Sprintf("创建失败,%v", err), c)
  80. os.Remove("./ginvueadmin.zip")
  81. } else {
  82. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) // fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
  83. c.Writer.Header().Add("Content-Type", "application/json")
  84. c.Writer.Header().Add("success", "true")
  85. c.File("./ginvueadmin.zip")
  86. os.Remove("./ginvueadmin.zip")
  87. }
  88. }
  89. // @Tags SysApi
  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/getTables [get]
  96. func GetTables(c *gin.Context) {
  97. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  98. err, tables := service.GetTables(dbName)
  99. if err != nil {
  100. response.FailWithMessage(fmt.Sprintf("查询table失败,%v", err), c)
  101. } else {
  102. response.OkWithData(gin.H{
  103. "tables": tables,
  104. }, c)
  105. }
  106. }
  107. // @Tags SysApi
  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. err, dbs := service.GetDB()
  116. if err != nil {
  117. response.FailWithMessage(fmt.Sprintf("查询table失败,%v", err), c)
  118. } else {
  119. response.OkWithData(gin.H{
  120. "dbs": dbs,
  121. }, c)
  122. }
  123. }
  124. // @Tags SysApi
  125. // @Summary 获取当前表所有字段
  126. // @Security ApiKeyAuth
  127. // @accept application/json
  128. // @Produce application/json
  129. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  130. // @Router /autoCode/getDatabase [get]
  131. func GetColume(c *gin.Context) {
  132. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  133. tableName := c.Query("tableName")
  134. err, columes := service.GetColume(tableName, dbName)
  135. if err != nil {
  136. response.FailWithMessage(fmt.Sprintf("查询table失败,%v", err), c)
  137. } else {
  138. response.OkWithData(gin.H{
  139. "columes": columes,
  140. }, c)
  141. }
  142. }