auto_code_template.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package system
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  6. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  7. "github.com/gin-gonic/gin"
  8. "go.uber.org/zap"
  9. )
  10. type AutoCodeTemplateApi struct{}
  11. // Preview
  12. // @Tags AutoCodeTemplate
  13. // @Summary 预览创建后的代码
  14. // @Security ApiKeyAuth
  15. // @accept application/json
  16. // @Produce application/json
  17. // @Param data body request.AutoCode true "预览创建代码"
  18. // @Success 200 {object} response.Response{data=map[string]interface{},msg=string} "预览创建后的代码"
  19. // @Router /autoCode/preview [post]
  20. func (a *AutoCodeTemplateApi) Preview(c *gin.Context) {
  21. var info request.AutoCode
  22. err := c.ShouldBindJSON(&info)
  23. if err != nil {
  24. response.FailWithMessage(err.Error(), c)
  25. return
  26. }
  27. err = utils.Verify(info, utils.AutoCodeVerify)
  28. if err != nil {
  29. response.FailWithMessage(err.Error(), c)
  30. return
  31. }
  32. err = info.Pretreatment()
  33. if err != nil {
  34. response.FailWithMessage(err.Error(), c)
  35. return
  36. }
  37. info.PackageT = utils.FirstUpper(info.Package)
  38. autoCode, err := autoCodeTemplateService.Preview(c.Request.Context(), info)
  39. if err != nil {
  40. global.GVA_LOG.Error(err.Error(), zap.Error(err))
  41. response.FailWithMessage("预览失败:"+err.Error(), c)
  42. } else {
  43. response.OkWithDetailed(gin.H{"autoCode": autoCode}, "预览成功", c)
  44. }
  45. }
  46. // Create
  47. // @Tags AutoCodeTemplate
  48. // @Summary 自动代码模板
  49. // @Security ApiKeyAuth
  50. // @accept application/json
  51. // @Produce application/json
  52. // @Param data body request.AutoCode true "创建自动代码"
  53. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  54. // @Router /autoCode/createTemp [post]
  55. func (a *AutoCodeTemplateApi) Create(c *gin.Context) {
  56. var info request.AutoCode
  57. err := c.ShouldBindJSON(&info)
  58. if err != nil {
  59. response.FailWithMessage(err.Error(), c)
  60. return
  61. }
  62. err = utils.Verify(info, utils.AutoCodeVerify)
  63. if err != nil {
  64. response.FailWithMessage(err.Error(), c)
  65. return
  66. }
  67. err = info.Pretreatment()
  68. if err != nil {
  69. response.FailWithMessage(err.Error(), c)
  70. return
  71. }
  72. err = autoCodeTemplateService.Create(c.Request.Context(), info)
  73. if err != nil {
  74. global.GVA_LOG.Error("创建失败!", zap.Error(err))
  75. response.FailWithMessage(err.Error(), c)
  76. } else {
  77. response.OkWithMessage("创建成功", c)
  78. }
  79. }
  80. // AddFunc
  81. // @Tags AddFunc
  82. // @Summary 增加方法
  83. // @Security ApiKeyAuth
  84. // @accept application/json
  85. // @Produce application/json
  86. // @Param data body request.AutoCode true "增加方法"
  87. // @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
  88. // @Router /autoCode/addFunc [post]
  89. func (a *AutoCodeTemplateApi) AddFunc(c *gin.Context) {
  90. var info request.AutoFunc
  91. err := c.ShouldBindJSON(&info)
  92. if err != nil {
  93. response.FailWithMessage(err.Error(), c)
  94. return
  95. }
  96. var tempMap map[string]string
  97. if info.IsPreview {
  98. info.Router = "填充router"
  99. info.FuncName = "填充funcName"
  100. info.Method = "填充method"
  101. info.Description = "填充description"
  102. tempMap, err = autoCodeTemplateService.GetApiAndServer(info)
  103. } else {
  104. err = autoCodeTemplateService.AddFunc(info)
  105. }
  106. if err != nil {
  107. global.GVA_LOG.Error("注入失败!", zap.Error(err))
  108. response.FailWithMessage("注入失败", c)
  109. } else {
  110. if info.IsPreview {
  111. response.OkWithDetailed(tempMap, "注入成功", c)
  112. return
  113. }
  114. response.OkWithMessage("注入成功", c)
  115. }
  116. }