func_list.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 listData struct {
  10. Id int32 `json:"id"` // ID
  11. HashID string `json:"hashid"` // hashid
  12. Pid int32 `json:"pid"` // 父类ID
  13. Name string `json:"name"` // 菜单名称
  14. Link string `json:"link"` // 链接地址
  15. Icon string `json:"icon"` // 图标
  16. IsUsed int32 `json:"is_used"` // 是否启用 1=启用 -1=禁用
  17. Sort int32 `json:"sort"` // 排序
  18. }
  19. type listResponse struct {
  20. List []listData `json:"list"`
  21. }
  22. // List 菜单列表
  23. // @Summary 菜单列表
  24. // @Description 菜单列表
  25. // @Tags API.menu
  26. // @Accept application/x-www-form-urlencoded
  27. // @Produce json
  28. // @Success 200 {object} listResponse
  29. // @Failure 400 {object} code.Failure
  30. // @Router /api/menu [get]
  31. // @Security LoginToken
  32. func (h *handler) List() core.HandlerFunc {
  33. return func(c core.Context) {
  34. res := new(listResponse)
  35. resListData, err := h.menuService.List(c, new(menu.SearchData))
  36. if err != nil {
  37. c.AbortWithError(core.Error(
  38. http.StatusBadRequest,
  39. code.MenuListError,
  40. code.Text(code.MenuListError)).WithError(err),
  41. )
  42. return
  43. }
  44. res.List = make([]listData, len(resListData))
  45. for k, v := range resListData {
  46. hashId, err := h.hashids.HashidsEncode([]int{cast.ToInt(v.Id)})
  47. if err != nil {
  48. c.AbortWithError(core.Error(
  49. http.StatusBadRequest,
  50. code.HashIdsEncodeError,
  51. code.Text(code.HashIdsEncodeError)).WithError(err),
  52. )
  53. return
  54. }
  55. data := listData{
  56. Id: v.Id,
  57. HashID: hashId,
  58. Pid: v.Pid,
  59. Name: v.Name,
  60. Link: v.Link,
  61. Icon: v.Icon,
  62. IsUsed: v.IsUsed,
  63. Sort: v.Sort,
  64. }
  65. res.List[k] = data
  66. }
  67. c.Payload(res)
  68. }
  69. }