func_create.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package menu
  2. import (
  3. "net/http"
  4. "github.com/xinliangnote/go-gin-api/internal/code"
  5. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  6. "github.com/xinliangnote/go-gin-api/internal/services/menu"
  7. "github.com/spf13/cast"
  8. )
  9. type createRequest struct {
  10. Id string `form:"id"` // ID
  11. Pid int32 `form:"pid"` // 父类ID
  12. Name string `form:"name"` // 菜单名称
  13. Link string `form:"link"` // 链接地址
  14. Icon string `form:"icon"` // 图标
  15. Level int32 `form:"level"` // 菜单类型 1:一级菜单 2:二级菜单
  16. }
  17. type createResponse struct {
  18. Id int32 `json:"id"` // 主键ID
  19. }
  20. // Create 创建/编辑菜单
  21. // @Summary 创建/编辑菜单
  22. // @Description 创建/编辑菜单
  23. // @Tags API.menu
  24. // @Accept application/x-www-form-urlencoded
  25. // @Produce json
  26. // @Param Request body createRequest true "请求信息"
  27. // @Success 200 {object} createResponse
  28. // @Failure 400 {object} code.Failure
  29. // @Router /api/menu [post]
  30. // @Security LoginToken
  31. func (h *handler) Create() core.HandlerFunc {
  32. return func(c core.Context) {
  33. req := new(createRequest)
  34. res := new(createResponse)
  35. if err := c.ShouldBindForm(req); err != nil {
  36. c.AbortWithError(core.Error(
  37. http.StatusBadRequest,
  38. code.ParamBindError,
  39. code.Text(code.ParamBindError)).WithError(err),
  40. )
  41. return
  42. }
  43. if req.Id != "" { // 编辑功能
  44. ids, err := h.hashids.HashidsDecode(req.Id)
  45. if err != nil {
  46. c.AbortWithError(core.Error(
  47. http.StatusBadRequest,
  48. code.HashIdsDecodeError,
  49. code.Text(code.HashIdsDecodeError)).WithError(err),
  50. )
  51. return
  52. }
  53. id := int32(ids[0])
  54. updateData := new(menu.UpdateMenuData)
  55. updateData.Name = req.Name
  56. updateData.Icon = req.Icon
  57. updateData.Link = req.Link
  58. err = h.menuService.Modify(c, id, updateData)
  59. if err != nil {
  60. c.AbortWithError(core.Error(
  61. http.StatusBadRequest,
  62. code.MenuUpdateError,
  63. code.Text(code.MenuUpdateError)).WithError(err),
  64. )
  65. return
  66. }
  67. res.Id = id
  68. c.Payload(res)
  69. } else { // 新增功能
  70. pid := req.Level
  71. level := 2
  72. if req.Level == -1 {
  73. pid = 0
  74. level = 1
  75. }
  76. createData := new(menu.CreateMenuData)
  77. createData.Pid = pid
  78. createData.Name = req.Name
  79. createData.Icon = req.Icon
  80. createData.Link = req.Link
  81. createData.Level = cast.ToInt32(level)
  82. id, err := h.menuService.Create(c, createData)
  83. if err != nil {
  84. c.AbortWithError(core.Error(
  85. http.StatusBadRequest,
  86. code.MenuCreateError,
  87. code.Text(code.MenuCreateError)).WithError(err),
  88. )
  89. return
  90. }
  91. res.Id = id
  92. c.Payload(res)
  93. }
  94. }
  95. }