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.

89 lines
2.6 KiB

  1. package v1
  2. import (
  3. "fmt"
  4. "gin-vue-admin/global/response"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/service"
  7. "gin-vue-admin/utils"
  8. "github.com/gin-gonic/gin"
  9. "net/url"
  10. "os"
  11. )
  12. // @Tags SysApi
  13. // @Summary 自动代码模板
  14. // @Security ApiKeyAuth
  15. // @accept application/json
  16. // @Produce application/json
  17. // @Param data body model.AutoCodeStruct true "创建自动代码"
  18. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  19. // @Router /autoCode/createTemp [post]
  20. func CreateTemp(c *gin.Context) {
  21. var a model.AutoCodeStruct
  22. _ = c.ShouldBindJSON(&a)
  23. AutoCodeVerify := utils.Rules{
  24. "Abbreviation": {utils.NotEmpty()},
  25. "StructName": {utils.NotEmpty()},
  26. "PackageName": {utils.NotEmpty()},
  27. "Fields": {utils.NotEmpty()},
  28. }
  29. WKVerifyErr := utils.Verify(a, AutoCodeVerify)
  30. if WKVerifyErr!=nil {
  31. response.FailWithMessage(WKVerifyErr.Error(), c)
  32. return
  33. }
  34. if a.AutoCreateApiToSql {
  35. apiList := [5]model.SysApi{
  36. {
  37. Path: "/" + a.Abbreviation + "/" + "create" + a.StructName,
  38. Description: "新增" + a.StructName,
  39. ApiGroup: a.Abbreviation,
  40. Method: "POST",
  41. },
  42. {
  43. Path: "/" + a.Abbreviation + "/" + "delete" + a.StructName,
  44. Description: "删除" + a.StructName,
  45. ApiGroup: a.Abbreviation,
  46. Method: "DELETE",
  47. },
  48. {
  49. Path: "/" + a.Abbreviation + "/" + "update" + a.StructName,
  50. Description: "更新" + a.StructName,
  51. ApiGroup: a.Abbreviation,
  52. Method: "PUT",
  53. },
  54. {
  55. Path: "/" + a.Abbreviation + "/" + "find" + a.StructName,
  56. Description: "根据ID获取" + a.StructName,
  57. ApiGroup: a.Abbreviation,
  58. Method: "GET",
  59. },
  60. {
  61. Path: "/" + a.Abbreviation + "/" + "get" + a.StructName + "List",
  62. Description: "获取" + a.StructName + "列表",
  63. ApiGroup: a.Abbreviation,
  64. Method: "GET",
  65. },
  66. }
  67. for _, v := range apiList {
  68. errC := service.CreateApi(v)
  69. if errC != nil {
  70. c.Writer.Header().Add("success", "false")
  71. c.Writer.Header().Add("msg", url.QueryEscape(fmt.Sprintf("自动化创建失败,%v,请自行清空垃圾数据", errC)))
  72. return
  73. }
  74. }
  75. }
  76. err := service.CreateTemp(a)
  77. if err != nil {
  78. response.FailWithMessage(fmt.Sprintf("创建失败,%v", err), c)
  79. os.Remove("./ginvueadmin.zip")
  80. } else {
  81. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf("attachment; filename=%s", "ginvueadmin.zip")) //fmt.Sprintf("attachment; filename=%s", filename)对下载的文件重命名
  82. c.Writer.Header().Add("Content-Type", "application/json")
  83. c.Writer.Header().Add("success", "true")
  84. c.File("./ginvueadmin.zip")
  85. os.Remove("./ginvueadmin.zip")
  86. }
  87. }