func_detail.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 detailRequest struct {
  9. Id string `uri:"id"` // HashID
  10. }
  11. type detailResponse struct {
  12. Id int32 `json:"id"` // 主键ID
  13. Pid int32 `json:"pid"` // 父类ID
  14. Name string `json:"name"` // 菜单名称
  15. Link string `json:"link"` // 链接地址
  16. Icon string `json:"icon"` // 图标
  17. }
  18. // Detail 菜单详情
  19. // @Summary 菜单详情
  20. // @Description 菜单详情
  21. // @Tags API.menu
  22. // @Accept application/x-www-form-urlencoded
  23. // @Produce json
  24. // @Param id path string true "hashId"
  25. // @Success 200 {object} detailResponse
  26. // @Failure 400 {object} code.Failure
  27. // @Router /api/menu/{id} [get]
  28. // @Security LoginToken
  29. func (h *handler) Detail() core.HandlerFunc {
  30. return func(c core.Context) {
  31. req := new(detailRequest)
  32. res := new(detailResponse)
  33. if err := c.ShouldBindURI(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. info, 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. res.Id = info.Id
  63. res.Pid = info.Pid
  64. res.Name = info.Name
  65. res.Link = info.Link
  66. res.Icon = info.Icon
  67. c.Payload(res)
  68. }
  69. }