-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_listapi.go
executable file
·116 lines (98 loc) · 2.89 KB
/
func_listapi.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
package authorized_handler
import (
"net/http"
"github.com/skyzhouzj/xhframwork/internal/api/service/authorized_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 listAPIRequest struct {
Id string `form:"id"` // hashID
}
type listAPIData struct {
HashId string `json:"hash_id"` // hashID
BusinessKey string `json:"business_key"` // 调用方key
Method string `json:"method"` // 调用方secret
API string `json:"api"` // 调用方对接人
}
type listAPIResponse struct {
BusinessKey string `json:"business_key"` // 调用方key
List []listAPIData `json:"list"`
}
// ListAPI 调用方接口地址列表
// @Summary 调用方接口地址列表
// @Description 调用方接口地址列表
// @Tags API.authorized
// @Accept multipart/form-data
// @Produce json
// @Param id query string true "hashID"
// @Success 200 {object} listAPIResponse
// @Failure 400 {object} code.Failure
// @Router /api/authorized_api [get]
func (h *handler) ListAPI() core.HandlerFunc {
return func(c core.Context) {
req := new(listAPIRequest)
res := new(listAPIResponse)
if err := c.ShouldBindForm(req); err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.ParamBindError,
code.Text(code.ParamBindError)).WithErr(err),
)
return
}
ids, err := h.hashids.HashidsDecode(req.Id)
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.HashIdsDecodeError,
code.Text(code.HashIdsDecodeError)).WithErr(err),
)
return
}
id := int32(ids[0])
// 通过 id 查询出 business_key
authorizedInfo, err := h.authorizedService.Detail(c, id)
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.AuthorizedDetailError,
code.Text(code.AuthorizedDetailError)).WithErr(err),
)
return
}
res.BusinessKey = authorizedInfo.BusinessKey
searchAPIData := new(authorized_service.SearchAPIData)
searchAPIData.BusinessKey = authorizedInfo.BusinessKey
resListData, err := h.authorizedService.ListAPI(c, searchAPIData)
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.AuthorizedListAPIError,
code.Text(code.AuthorizedListAPIError)).WithErr(err),
)
return
}
res.List = make([]listAPIData, 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 := listAPIData{
HashId: hashId,
BusinessKey: v.BusinessKey,
Method: v.Method,
API: v.Api,
}
res.List[k] = data
}
c.Payload(res)
}
}