feat: 甩甩尾巴支持关键词筛选模式 (#2649)

This commit is contained in:
Gao Liang
2019-07-20 12:51:56 +08:00
committed by DIYgod
parent 9b619a03d6
commit a87ffdb8e8
3 changed files with 79 additions and 0 deletions

View File

@@ -124,6 +124,10 @@ pageClass: routes
</Route>
### 关键词
<Route author="gaoliang" example="/dgtle/trade/search/ipad" path="/dgtle/trade/search/:keyword" :paramsDesc="['搜索关键词']"/>
## 淘宝众筹
### 众筹项目

View File

@@ -793,6 +793,7 @@ router.get('/parcel/hermesuk/:tracking', require('./routes/parcel/hermesuk'));
// 甩甩尾巴
router.get('/dgtle/trade/:typeId?', require('./routes/dgtle/trade'));
router.get('/dgtle/trade/search/:keyword', require('./routes/dgtle/keyword'));
// 抽屉新热榜
router.get('/chouti/:subject?', require('./routes/chouti'));

View File

@@ -0,0 +1,74 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const keyword = ctx.params.keyword;
const host = 'http://trade.dgtle.com';
const baseUrl = `${host}/dgtle_module.php?mod=trade&searchsort=1&PName=${keyword}`;
const res = await got({
method: 'get',
url: baseUrl,
});
const $ = cheerio.load(res.data);
const tradeList = $('.tradebox');
const resultItem = await Promise.all(
tradeList
.map(async (_, tradeItem) => {
const $tradeItem = $(tradeItem);
const url = `${host}${$tradeItem.find('.tradetitle a').attr('href')}`;
const item = {
title: $tradeItem.find('.tradetitle').attr('title'),
description: '',
link: url,
author: $tradeItem.find('.tradeuser').text(),
};
const key = `dgtle-trade: ${url}`;
const value = await ctx.cache.get(key);
if (value) {
item.description = value.description;
item.pubDate = value.pubDate;
} else {
const tradeDetail = await got({
method: 'get',
url: url,
});
const $ = cheerio.load(tradeDetail.data);
const pubDate = new Date(
$('.cr_date > em')
.last()
.text()
).toUTCString();
let description = `<img referrerpolicy="no-referrer" src="${$tradeItem
.find('.cover')
.attr('src')
.replace(/\?.+/g, '')}" /><br>`;
description += $tradeItem.find('.tradeprice').text();
description += $('#trade_info').html();
description += $('.pcb').html();
item.description = description;
item.pubDate = pubDate;
ctx.cache.set(key, {
pubDate,
description,
});
}
return Promise.resolve(item);
})
.get()
);
ctx.state.data = {
title: `甩甩尾巴 - ${keyword}`,
description: '纯净,安全的玩家闲置物品交易平台',
link: baseUrl,
item: resultItem,
};
};