/
func_list.go
executable file
·137 lines (124 loc) · 4 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package site
import (
"github.com/ch3nnn/webstack-go/internal/code"
"github.com/ch3nnn/webstack-go/internal/pkg/core"
siteMd "github.com/ch3nnn/webstack-go/internal/repository/mysql/site"
"github.com/ch3nnn/webstack-go/internal/services/site"
"github.com/spf13/cast"
"net/http"
"strings"
)
type listRequest struct {
Page int `form:"page"` // 第几页
PageSize int `form:"page_size"` // 每页显示条数
BusinessKey string `form:"business_key"` // 调用方key
BusinessSecret string `form:"business_secret"` // 调用方secret
BusinessDeveloper string `form:"business_developer"` // 调用方对接人
Remark string `form:"remark"` // 备注
}
type listData struct {
Id int `json:"id"` // ID
Thumb string `json:"thumb"` // 网站 logo
Title string `json:"title"` // 名称简介
Url string `json:"url"` // 链接
Category string `json:"category"` // 分类
CategoryId int32 `json:"category_id"` // 分类id
Description string `json:"description"` // 描述
IsUsed siteMd.IsUsedStatus `json:"is_used"` // 是否启用
CreatedAt string `json:"created_at"` // 创建时间
UpdatedAt string `json:"updated_at"` // 更新时间
}
type listResponse struct {
List []listData `json:"list"`
Pagination struct {
Total int `json:"total"`
CurrentPage int `json:"current_page"`
PerPageCount int `json:"per_page_count"`
} `json:"pagination"`
}
// List 网站列表
// @Summary 网站列表
// @Description 网站列表
// @Tags API.site
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param Request body listRequest true "请求信息"
// @Success 200 {object} listResponse
// @Failure 400 {object} code.Failure
// @Router /api/site [get]
func (h *handler) List() core.HandlerFunc {
return func(c core.Context) {
req := new(listRequest)
res := new(listResponse)
if err := c.ShouldBindForm(req); err != nil {
c.AbortWithError(core.Error(
http.StatusBadRequest,
code.ParamBindError,
code.Text(code.ParamBindError)).WithError(err),
)
return
}
page := req.Page
if page == 0 {
page = 1
}
pageSize := req.PageSize
if pageSize == 0 {
pageSize = 10
}
searchData := new(site.SearchData)
searchData.Page = page
searchData.PageSize = pageSize
searchData.BusinessKey = req.BusinessKey
searchData.BusinessSecret = req.BusinessSecret
searchData.Remark = req.Remark
searchData.Search = strings.TrimSpace(c.Query("search"))
resListData, err := h.siteService.PageList(c, searchData)
if err != nil {
c.AbortWithError(core.Error(
http.StatusBadRequest,
code.AuthorizedListError,
code.Text(code.AuthorizedListError)).WithError(err),
)
return
}
resCountData, err := h.siteService.PageListCount(c, searchData)
if err != nil {
c.AbortWithError(core.Error(
http.StatusBadRequest,
code.SiteListError,
code.Text(code.SiteListError)).WithError(err),
)
return
}
res.Pagination.Total = cast.ToInt(resCountData)
res.Pagination.PerPageCount = pageSize
res.Pagination.CurrentPage = page
res.List = make([]listData, len(resListData))
for k, v := range resListData {
_, 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 := listData{
Id: cast.ToInt(v.Id),
Thumb: v.Thumb,
Title: v.Title,
Url: v.Url,
Category: v.Category.Title,
CategoryId: v.Category.Id,
Description: v.Description,
IsUsed: v.IsUsed,
CreatedAt: v.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: v.UpdatedAt.Format("2006-01-02 15:04:05"),
}
res.List[k] = data
}
c.Payload(res)
}
}