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.

154 lines
4.5 KiB

  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.CreateApi(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. response.FailWithMessage(fmt.Sprintf("创建失败,%v", err), c)
  86. os.Remove("./ginvueadmin.zip")
  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 SysApi
  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. response.FailWithMessage(fmt.Sprintf("查询table失败,%v", err), c)
  107. } else {
  108. response.OkWithData(gin.H{
  109. "tables": tables,
  110. }, c)
  111. }
  112. }
  113. // @Tags SysApi
  114. // @Summary 获取当前所有数据库
  115. // @Security ApiKeyAuth
  116. // @accept application/json
  117. // @Produce application/json
  118. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  119. // @Router /autoCode/getDatabase [get]
  120. func GetDB(c *gin.Context) {
  121. err, dbs := service.GetDB()
  122. if err != nil {
  123. response.FailWithMessage(fmt.Sprintf("查询table失败,%v", err), c)
  124. } else {
  125. response.OkWithData(gin.H{
  126. "dbs": dbs,
  127. }, c)
  128. }
  129. }
  130. // @Tags SysApi
  131. // @Summary 获取当前表所有字段
  132. // @Security ApiKeyAuth
  133. // @accept application/json
  134. // @Produce application/json
  135. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  136. // @Router /autoCode/getDatabase [get]
  137. func GetColume(c *gin.Context) {
  138. dbName := c.DefaultQuery("dbName", global.GVA_CONFIG.Mysql.Dbname)
  139. tableName := c.Query("tableName")
  140. err, columes := service.GetColume(tableName, dbName)
  141. if err != nil {
  142. response.FailWithMessage(fmt.Sprintf("查询table失败,%v", err), c)
  143. } else {
  144. response.OkWithData(gin.H{
  145. "columes": columes,
  146. }, c)
  147. }
  148. }