func_createaction.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. )
  8. type createActionRequest struct {
  9. Id string `form:"id"` // HashID
  10. Method string `form:"method"` // 请求方法
  11. API string `form:"api"` // 请求地址
  12. }
  13. type createActionResponse struct {
  14. Id int32 `json:"id"` // 主键ID
  15. }
  16. // CreateAction 创建功能权限
  17. // @Summary 创建功能权限
  18. // @Description 创建功能权限
  19. // @Tags API.menu
  20. // @Accept application/x-www-form-urlencoded
  21. // @Produce json
  22. // @Param id formData string true "HashID"
  23. // @Param method formData string true "请求方法"
  24. // @Param api formData string true "请求地址"
  25. // @Success 200 {object} createActionResponse
  26. // @Failure 400 {object} code.Failure
  27. // @Router /api/menu_action [post]
  28. // @Security LoginToken
  29. func (h *handler) CreateAction() core.HandlerFunc {
  30. return func(c core.Context) {
  31. req := new(createActionRequest)
  32. res := new(createActionResponse)
  33. if err := c.ShouldBindForm(req); err != nil {
  34. c.AbortWithError(core.Error(
  35. http.StatusBadRequest,
  36. code.ParamBindError,
  37. code.Text(code.ParamBindError)).WithError(err),
  38. )
  39. return
  40. }
  41. ids, err := h.hashids.HashidsDecode(req.Id)
  42. if err != nil {
  43. c.AbortWithError(core.Error(
  44. http.StatusBadRequest,
  45. code.HashIdsDecodeError,
  46. code.Text(code.HashIdsDecodeError)).WithError(err),
  47. )
  48. return
  49. }
  50. id := int32(ids[0])
  51. searchOneData := new(menu.SearchOneData)
  52. searchOneData.Id = id
  53. menuInfo, err := h.menuService.Detail(c, searchOneData)
  54. if err != nil {
  55. c.AbortWithError(core.Error(
  56. http.StatusBadRequest,
  57. code.MenuDetailError,
  58. code.Text(code.MenuDetailError)).WithError(err),
  59. )
  60. return
  61. }
  62. createActionData := new(menu.CreateMenuActionData)
  63. createActionData.MenuId = menuInfo.Id
  64. createActionData.Method = req.Method
  65. createActionData.API = req.API
  66. createId, err := h.menuService.CreateAction(c, createActionData)
  67. if err != nil {
  68. c.AbortWithError(core.Error(
  69. http.StatusBadRequest,
  70. code.MenuCreateActionError,
  71. code.Text(code.MenuCreateActionError)).WithError(err),
  72. )
  73. return
  74. }
  75. res.Id = createId
  76. c.Payload(res)
  77. }
  78. }