-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_list.go
executable file
·80 lines (69 loc) · 1.86 KB
/
func_list.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package menu_handler
import (
"net/http"
"github.com/skyzhouzj/xhframwork/internal/api/service/menu_service"
"github.com/skyzhouzj/xhframwork/internal/pkg/code"
"github.com/skyzhouzj/xhframwork/internal/pkg/core"
"github.com/skyzhouzj/xhframwork/pkg/errno"
"github.com/spf13/cast"
)
type listData struct {
Id int32 `json:"id"` // ID
HashID string `json:"hashid"` // hashid
Pid int32 `json:"pid"` // 父类ID
Name string `json:"name"` // 菜单名称
Link string `json:"link"` // 链接地址
Icon string `json:"icon"` // 图标
IsUsed int32 `json:"is_used"` // 是否启用 1=启用 -1=禁用
Sort int32 `json:"sort"` // 排序
}
type listResponse struct {
List []listData `json:"list"`
}
// List 菜单列表
// @Summary 菜单列表
// @Description 菜单列表
// @Tags API.menu
// @Accept json
// @Produce json
// @Success 200 {object} listResponse
// @Failure 400 {object} code.Failure
// @Router /api/menu [get]
func (h *handler) List() core.HandlerFunc {
return func(c core.Context) {
res := new(listResponse)
resListData, err := h.menuService.List(c, new(menu_service.SearchData))
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.MenuListError,
code.Text(code.MenuListError)).WithErr(err),
)
return
}
res.List = make([]listData, len(resListData))
for k, v := range resListData {
hashId, err := h.hashids.HashidsEncode([]int{cast.ToInt(v.Id)})
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.HashIdsEncodeError,
code.Text(code.HashIdsEncodeError)).WithErr(err),
)
return
}
data := listData{
Id: v.Id,
HashID: hashId,
Pid: v.Pid,
Name: v.Name,
Link: v.Link,
Icon: v.Icon,
IsUsed: v.IsUsed,
Sort: v.Sort,
}
res.List[k] = data
}
c.Payload(res)
}
}