diff --git a/src/components/Detail.vue b/src/components/Detail.vue index a30605e..5793316 100644 --- a/src/components/Detail.vue +++ b/src/components/Detail.vue @@ -343,11 +343,9 @@ export default { if (recommendations) { this.info.recommendations = [] recommendations.forEach(element => { - zy.search(this.detail.key, element).then(res => { - if (res) { - zy.detail(this.detail.key, res[0].id).then(detailRes => { - this.info.recommendations.push(detailRes) - }) + zy.searchFirstDetail(this.detail.key, element).then(detailRes => { + if (detailRes) { + this.info.recommendations.push(detailRes) } }) }) diff --git a/src/components/Recommendation.vue b/src/components/Recommendation.vue index 9a89188..f4fd4c2 100644 --- a/src/components/Recommendation.vue +++ b/src/components/Recommendation.vue @@ -264,20 +264,18 @@ export default { axios.get(doubleUrl).then(res => { if (res.data) { res.data.subjects.forEach(element => { - zy.search(this.sites[0].key, element.title).then(res => { - if (res && res[0] && res[0].name === element.title) { - zy.detail(this.sites[0].key, res[0].id).then(detailRes => { - const doc = { - key: this.sites[0].key, - ids: res[0].id, - site: this.sites[0], - name: res[0].name, - detail: detailRes, - rate: element.rate - } - this.recommendationsDoubanMovie.push(doc) - this.recommendations.push(doc) - }) + zy.searchFirstDetail(this.sites[0].key, element.title).then(detailRes => { + if (detailRes) { + const doc = { + key: this.sites[0].key, + ids: detailRes.id, + site: this.sites[0], + name: detailRes.name, + detail: detailRes, + rate: element.rate + } + this.recommendationsDoubanMovie.push(doc) + this.recommendations.push(doc) } }) }) @@ -294,20 +292,20 @@ export default { axios.get(doubleUrl).then(res => { if (res.data) { res.data.subjects.forEach(element => { - zy.search(this.sites[0].key, element.title).then(res => { - if (res && res[0] && res[0].name === element.title) { - zy.detail(this.sites[0].key, res[0].id).then(detailRes => { - const doc = { - key: this.sites[0].key, - ids: res[0].id, - site: this.sites[0], - name: res[0].name, - detail: detailRes, - rate: element.rate - } - this.recommendationsDoubanTV.push(doc) - this.recommendations.push(doc) - }) + console.log('searching ' + element.title) + zy.searchFirstDetail(this.sites[0].key, element.title).then(detailRes => { + console.log(detailRes) + if (detailRes) { + const doc = { + key: this.sites[0].key, + ids: detailRes.id, + site: this.sites[0], + name: detailRes.name, + detail: detailRes, + rate: element.rate + } + this.recommendationsDoubanTV.push(doc) + this.recommendations.push(doc) } }) }) diff --git a/src/lib/site/tools.js b/src/lib/site/tools.js index a90133b..6a84ad6 100644 --- a/src/lib/site/tools.js +++ b/src/lib/site/tools.js @@ -240,6 +240,43 @@ const zy = { }) }) }, + /** + * 搜索资源详情 + * @param {*} key 资源网 key + * @param {*} wd 搜索关键字 + * @returns + */ + searchFirstDetail (key, wd) { + return new Promise((resolve, reject) => { + this.getSite(key).then(res => { + const site = res + const url = `${site.api}?wd=${encodeURI(wd)}` + axios.get(url, { timeout: 3000 }).then(res => { + const data = res.data + const json = parser.parse(data, this.xmlConfig) + const jsondata = json?.rss === undefined ? json : json.rss + if (json && jsondata && jsondata.list) { + let videoList = jsondata.list.video + if (Object.prototype.toString.call(videoList) === '[object Object]') videoList = [].concat(videoList) + videoList = videoList?.filter(e => e.name.toLowerCase().includes(wd.toLowerCase())) + if (videoList?.length) { + this.detail(key, videoList[0].id).then(detailRes => { + resolve(detailRes) + }) + } else { + resolve() + } + } else { + resolve() + } + }).catch(err => { + reject(err) + }) + }).catch(err => { + reject(err) + }) + }) + }, /** * 获取资源详情 * @param {*} key 资源网 key