api.go.template 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package apis
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/gin-gonic/gin/binding"
  5. "go-admin/models"
  6. "github.com/go-admin-team/go-admin-core/sdk/pkg"
  7. "go-admin/utils"
  8. "net/http"
  9. )
  10. // @Summary 配置列表数据
  11. // @Description 获取JSON
  12. // @Tags 配置
  13. // @Param configKey query string false "configKey"
  14. // @Param configName query string false "configName"
  15. // @Param configType query string false "configType"
  16. // @Param pageSize query int false "页条数"
  17. // @Param pageIndex query int false "页码"
  18. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  19. // @Router /api/v1/configList [get]
  20. // @Security Bearer
  21. func Get{{.ClassName}}List(c *gin.Context) {
  22. var data models.{{.ClassName}}
  23. var err error
  24. var pageSize = 10
  25. var pageIndex = 1
  26. if size := c.Request.FormValue("pageSize"); size != "" {
  27. pageSize = pkg.StrToInt(err, size)
  28. }
  29. if index := c.Request.FormValue("pageIndex"); index != "" {
  30. pageIndex = pkg.StrToInt(err, index)
  31. }
  32. {{ range .Columns -}}
  33. {{$z := .IsQuery}}
  34. {{- if ($z) -}}
  35. data.{{.GoField}} = c.Request.FormValue("{{.JsonField}}")
  36. {{ end }}
  37. {{- end -}}
  38. data.DataScope = utils.GetUserIdStr(c)
  39. result, count, err := data.GetPage(pageSize, pageIndex)
  40. pkg.HasError(err, "", -1)
  41. var mp = make(map[string]interface{}, 3)
  42. mp["list"] = result
  43. mp["count"] = count
  44. mp["pageIndex"] = pageIndex
  45. mp["pageIndex"] = pageSize
  46. var res app.Response
  47. res.Data = mp
  48. c.JSON(http.StatusOK, res.ReturnOK())
  49. }
  50. // @Summary 获取配置
  51. // @Description 获取JSON
  52. // @Tags 配置
  53. // @Param configId path int true "配置编码"
  54. // @Success 200 {object} app.Response "{"code": 200, "data": [...]}"
  55. // @Router /api/v1/config/{configId} [get]
  56. // @Security Bearer
  57. func Get{{.ClassName}}(c *gin.Context) {
  58. var data models.{{.ClassName}}
  59. data.{{.PkGoField}}, _ = utils.StringToInt(c.Param("{{.PkJsonField}}"))
  60. result, err := data.Get()
  61. pkg.HasError(err, "抱歉未找到相关信息", -1)
  62. var res app.Response
  63. res.Data = result
  64. c.JSON(http.StatusOK, res.ReturnOK())
  65. }
  66. // @Summary 添加配置
  67. // @Description 获取JSON
  68. // @Tags 配置
  69. // @Accept application/json
  70. // @Product application/json
  71. // @Param data body models.{{.ClassName}} true "data"
  72. // @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
  73. // @Success 200 {string} string "{"code": -1, "message": "添加失败"}"
  74. // @Router /api/v1/dict/data [post]
  75. // @Security Bearer
  76. func Insert{{.ClassName}}(c *gin.Context) {
  77. var data models.{{.ClassName}}
  78. err := c.BindWith(&data, binding.JSON)
  79. data.CreateBy = utils.GetUserIdStr(c)
  80. pkg.HasError(err, "", 500)
  81. result, err := data.Create()
  82. pkg.HasError(err, "", -1)
  83. var res app.Response
  84. res.Data = result
  85. c.JSON(http.StatusOK, res.ReturnOK())
  86. }
  87. func Update{{.ClassName}}(c *gin.Context) {
  88. var data models.{{.ClassName}}
  89. err := c.BindWith(&data, binding.JSON)
  90. pkg.HasError(err, "数据解析失败", -1)
  91. data.UpdateBy = utils.GetUserIdStr(c)
  92. result, err := data.Update(data.{{.PkGoField}})
  93. pkg.HasError(err, "", -1)
  94. var res app.Response
  95. res.Data = result
  96. c.JSON(http.StatusOK, res.ReturnOK())
  97. }
  98. func Delete{{.ClassName}}(c *gin.Context) {
  99. var data models.{{.ClassName}}
  100. id, err := utils.StringToInt(c.Param("{{.PkJsonField}}"))
  101. data.UpdateBy = utils.GetUserIdStr(c)
  102. _, err = data.Delete(id)
  103. pkg.HasError(err, "修改失败", 500)
  104. var res app.Response
  105. res.Msg = "删除成功"
  106. c.JSON(http.StatusOK, res.ReturnOK())
  107. }