From 04905f38b50c4efa7094da479ebfe033135de637 Mon Sep 17 00:00:00 2001 From: marvzhang Date: Mon, 6 Jan 2020 13:18:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E7=88=AC=E8=99=AB=E5=88=97?= =?UTF-8?q?=E8=A1=A8=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/constants/common.go | 6 ++ backend/model/spider.go | 4 +- backend/routes/spider.go | 24 ++++- backend/services/spider.go | 2 +- frontend/src/views/spider/SpiderList.vue | 112 ++++++++++++++--------- 5 files changed, 100 insertions(+), 48 deletions(-) create mode 100644 backend/constants/common.go diff --git a/backend/constants/common.go b/backend/constants/common.go new file mode 100644 index 00000000..9ac6cdbc --- /dev/null +++ b/backend/constants/common.go @@ -0,0 +1,6 @@ +package constants + +const ( + ASCENDING = "ascending" + DESCENDING = "descending" +) diff --git a/backend/model/spider.go b/backend/model/spider.go index e08352e4..3026a66b 100644 --- a/backend/model/spider.go +++ b/backend/model/spider.go @@ -107,13 +107,13 @@ func (spider *Spider) Delete() error { } // 获取爬虫列表 -func GetSpiderList(filter interface{}, skip int, limit int) ([]Spider, int, error) { +func GetSpiderList(filter interface{}, skip int, limit int, sortStr string) ([]Spider, int, error) { s, c := database.GetCol("spiders") defer s.Close() // 获取爬虫列表 var spiders []Spider - if err := c.Find(filter).Skip(skip).Limit(limit).Sort("+name").All(&spiders); err != nil { + if err := c.Find(filter).Skip(skip).Limit(limit).Sort(sortStr).All(&spiders); err != nil { debug.PrintStack() return spiders, 0, err } diff --git a/backend/routes/spider.go b/backend/routes/spider.go index ca9f74d9..91bab47a 100644 --- a/backend/routes/spider.go +++ b/backend/routes/spider.go @@ -27,22 +27,38 @@ import ( ) func GetSpiderList(c *gin.Context) { - pageNum, _ := c.GetQuery("pageNum") - pageSize, _ := c.GetQuery("pageSize") + pageNum, _ := c.GetQuery("page_num") + pageSize, _ := c.GetQuery("page_size") keyword, _ := c.GetQuery("keyword") t, _ := c.GetQuery("type") + sortKey, _ := c.GetQuery("sort_key") + sortDirection, _ := c.GetQuery("sort_direction") + // 筛选 filter := bson.M{ "name": bson.M{"$regex": bson.RegEx{Pattern: keyword, Options: "im"}}, } - if t != "" && t != "all" { filter["type"] = t } + // 排序 + sortStr := "-_id" + if sortKey != "" && sortDirection != "" { + if sortDirection == constants.DESCENDING { + sortStr = "-" + sortKey + } else if sortDirection == constants.ASCENDING { + sortStr = "+" + sortKey + } else { + HandleErrorF(http.StatusBadRequest, c, "invalid sort_direction") + } + } + + // 分页 page := &entity.Page{} page.GetPage(pageNum, pageSize) - results, count, err := model.GetSpiderList(filter, page.Skip, page.Limit) + + results, count, err := model.GetSpiderList(filter, page.Skip, page.Limit, sortStr) if err != nil { HandleError(http.StatusInternalServerError, c, err) return diff --git a/backend/services/spider.go b/backend/services/spider.go index 3515afa9..e97c7992 100644 --- a/backend/services/spider.go +++ b/backend/services/spider.go @@ -143,7 +143,7 @@ func ReadFileByStep(filePath string, handle func([]byte, *mgo.GridFile), fileCre // 发布所有爬虫 func PublishAllSpiders() { // 获取爬虫列表 - spiders, _, _ := model.GetSpiderList(nil, 0, constants.Infinite) + spiders, _, _ := model.GetSpiderList(nil, 0, constants.Infinite, "-_id") if len(spiders) == 0 { return } diff --git a/frontend/src/views/spider/SpiderList.vue b/frontend/src/views/spider/SpiderList.vue index d31a906f..5e6ea3b9 100644 --- a/frontend/src/views/spider/SpiderList.vue +++ b/frontend/src/views/spider/SpiderList.vue @@ -169,67 +169,86 @@ - @@ -300,10 +319,14 @@ export default { keyword: '', type: 'all' }, + sort: { + sortKey: '', + sortDirection: null + }, types: [], columns: [ - { name: 'display_name', label: 'Name', width: '160', align: 'left' }, - { name: 'type', label: 'Spider Type', width: '120' }, + { name: 'display_name', label: 'Name', width: '160', align: 'left', sortable: true }, + { name: 'type', label: 'Spider Type', width: '120', sortable: true }, { name: 'last_status', label: 'Last Status', width: '120' }, { name: 'last_run_ts', label: 'Last Run', width: '140' }, // { name: 'update_ts', label: 'Update Time', width: '140' }, @@ -543,14 +566,21 @@ export default { onRowClick (row, column, event) { this.onView(row, event) }, + onSortChange ({ column, prop, order }) { + this.sort.sortKey = order ? prop : '' + this.sort.sortDirection = order + this.getList() + }, onClickTab (tab) { this.filter.type = tab.name this.getList() }, getList () { let params = { - pageNum: this.pagination.pageNum, - pageSize: this.pagination.pageSize, + page_num: this.pagination.pageNum, + page_size: this.pagination.pageSize, + sort_key: this.sort.sortKey, + sort_direction: this.sort.sortDirection, keyword: this.filter.keyword, type: this.filter.type }