forked from xinliangnote/go-gin-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_listaction.go
executable file
·118 lines (99 loc) · 2.74 KB
/
func_listaction.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package menu
import (
"net/http"
"github.com/xinliangnote/go-gin-api/internal/code"
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
"github.com/xinliangnote/go-gin-api/internal/services/menu"
"github.com/spf13/cast"
)
type listActionRequest struct {
Id string `form:"id"` // hashID
}
type listActionData struct {
HashId string `json:"hash_id"` // hashID
MenuId int32 `json:"menu_id"` // 菜单栏ID
Method string `json:"method"` // 调用方secret
API string `json:"api"` // 调用方对接人
}
type listActionResponse struct {
MenuName string `json:"menu_name"`
List []listActionData `json:"list"`
}
// ListAction 功能权限列表
// @Summary 功能权限列表
// @Description 功能权限列表
// @Tags API.menu
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param id query string true "hashID"
// @Success 200 {object} listActionResponse
// @Failure 400 {object} code.Failure
// @Router /api/menu_action [get]
// @Security LoginToken
func (h *handler) ListAction() core.HandlerFunc {
return func(c core.Context) {
req := new(listActionRequest)
res := new(listActionResponse)
if err := c.ShouldBindForm(req); err != nil {
c.AbortWithError(core.Error(
http.StatusBadRequest,
code.ParamBindError,
code.Text(code.ParamBindError)).WithError(err),
)
return
}
ids, err := h.hashids.HashidsDecode(req.Id)
if err != nil {
c.AbortWithError(core.Error(
http.StatusBadRequest,
code.HashIdsDecodeError,
code.Text(code.HashIdsDecodeError)).WithError(err),
)
return
}
id := int32(ids[0])
searchOneData := new(menu.SearchOneData)
searchOneData.Id = id
menuInfo, err := h.menuService.Detail(c, searchOneData)
if err != nil {
c.AbortWithError(core.Error(
http.StatusBadRequest,
code.MenuDetailError,
code.Text(code.MenuDetailError)).WithError(err),
)
return
}
res.MenuName = menuInfo.Name
searchListData := new(menu.SearchListActionData)
searchListData.MenuId = menuInfo.Id
resListData, err := h.menuService.ListAction(c, searchListData)
if err != nil {
c.AbortWithError(core.Error(
http.StatusBadRequest,
code.AuthorizedListAPIError,
code.Text(code.AuthorizedListAPIError)).WithError(err),
)
return
}
res.List = make([]listActionData, len(resListData))
for k, v := range resListData {
hashId, err := h.hashids.HashidsEncode([]int{cast.ToInt(v.Id)})
if err != nil {
c.AbortWithError(core.Error(
http.StatusBadRequest,
code.HashIdsEncodeError,
code.Text(code.HashIdsEncodeError)).WithError(err),
)
return
}
data := listActionData{
HashId: hashId,
MenuId: v.MenuId,
Method: v.Method,
API: v.Api,
}
res.List[k] = data
}
c.Payload(res)
}
}