diff --git a/backend/model/spider.go b/backend/model/spider.go index 3026a66b..475b12e2 100644 --- a/backend/model/spider.go +++ b/backend/model/spider.go @@ -32,6 +32,7 @@ type Spider struct { Envs []Env `json:"envs" bson:"envs"` // 环境变量 Remark string `json:"remark" bson:"remark"` // 备注 Src string `json:"src" bson:"src"` // 源码位置 + ProjectId bson.ObjectId `json:"project_id" bson:"project_id"` // 项目ID // 自定义爬虫 Cmd string `json:"cmd" bson:"cmd"` // 执行命令 @@ -56,6 +57,11 @@ func (spider *Spider) Save() error { spider.UpdateTs = time.Now() + // 兼容没有项目ID的爬虫 + if spider.ProjectId.Hex() == "" { + spider.ProjectId = bson.ObjectIdHex(constants.ObjectIdNull) + } + if err := c.UpdateId(spider.Id, spider); err != nil { debug.PrintStack() return err diff --git a/backend/routes/spider.go b/backend/routes/spider.go index 4adfb707..2b6dfd63 100644 --- a/backend/routes/spider.go +++ b/backend/routes/spider.go @@ -30,6 +30,7 @@ func GetSpiderList(c *gin.Context) { pageNum, _ := c.GetQuery("page_num") pageSize, _ := c.GetQuery("page_size") keyword, _ := c.GetQuery("keyword") + pid, _ := c.GetQuery("project_id") t, _ := c.GetQuery("type") sortKey, _ := c.GetQuery("sort_key") sortDirection, _ := c.GetQuery("sort_direction") @@ -41,6 +42,16 @@ func GetSpiderList(c *gin.Context) { if t != "" && t != "all" { filter["type"] = t } + if pid == "" { + // do nothing + } else if pid == constants.ObjectIdNull { + filter["$or"] = []bson.M{ + {"project_id": bson.ObjectIdHex(pid)}, + {"project_id": bson.M{"$exists": false}}, + } + } else { + filter["project_id"] = bson.ObjectIdHex(pid) + } // 排序 sortStr := "-_id" diff --git a/backend/routes/variable.go b/backend/routes/variable.go index 56f51ed7..c35c16ab 100644 --- a/backend/routes/variable.go +++ b/backend/routes/variable.go @@ -8,7 +8,7 @@ import ( ) // 新增 -func PostVariable(c *gin.Context) { +func PutVariable(c *gin.Context) { var variable model.Variable if err := c.ShouldBindJSON(&variable); err != nil { HandleError(http.StatusBadRequest, c, err) @@ -22,7 +22,7 @@ func PostVariable(c *gin.Context) { } // 修改 -func PutVariable(c *gin.Context) { +func PostVariable(c *gin.Context) { var id = c.Param("id") var variable model.Variable if err := c.ShouldBindJSON(&variable); err != nil { diff --git a/frontend/src/views/spider/SpiderList.vue b/frontend/src/views/spider/SpiderList.vue index f39745bb..bbfbee75 100644 --- a/frontend/src/views/spider/SpiderList.vue +++ b/frontend/src/views/spider/SpiderList.vue @@ -571,8 +571,12 @@ export default { this.getList() }, onAdd () { + let projectId = '000000000000000000000000' + if (this.filter.project_id) { + projectId = this.filter.project_id + } this.$store.commit('spider/SET_SPIDER_FORM', { - project_id: '000000000000000000000000', + project_id: projectId, template: this.templateList[0] }) this.addDialogVisible = true @@ -812,7 +816,6 @@ export default { // fetch template list await this.$store.dispatch('spider/getTemplateList') - }, mounted () { const vm = this