const got = require('@/utils/got'); const { parseDate } = require('@/utils/parse-date'); const titles = { topic: '热门话题', news: '科技动态', tech: '技术资讯', technews: '技术资讯', blockchain: '区块链快讯', daily: '每日早报', }; module.exports = async (ctx) => { let category = 'topic'; let overview = false; if (titles[ctx.params.category]) { category = ctx.params.category; overview = ctx.params.overview ? true : false; category = category === 'tech' ? 'technews' : category; } else if (ctx.params.category) { overview = true; } const rootUrl = 'https://readhub.cn'; const apiRootUrl = 'https://api.readhub.cn'; const currentUrl = `${rootUrl}/topic/${category}`; const apiUrl = `${apiRootUrl}/${category === 'daily' ? 'topic/daily' : category}`; const response = await got({ method: 'get', url: apiUrl, }); const list = response.data.data.map((item) => ({ id: item.id, title: item.title, pubDate: parseDate(item.publishDate), description: item.summaryAuto || item.summary || '', link: item.url || item.mobileUrl || `${rootUrl}/topic/${item.id}`, author: item.authorName || item.siteName || '', hasInstantView: item.hasInstantView || false, })); const items = await Promise.all( list.map((item) => ctx.cache.tryGet(item.id + overview, async () => { if (item.hasInstantView || category === 'daily') { const { data } = await got({ method: 'get', url: `${apiRootUrl}/topic/instantview?topicId=${item.id}`, }); item.description = `访问原网址
${!overview ? data.content : ''}`; } if (isNaN(item.id)) { const { data } = await got({ method: 'get', url: `${apiRootUrl}/topic/${item.id}`, }); if (data.summary && overview) { item.description += `${data.summary}
`; } if (data.newsArray) { item.description += '
媒体报道'; } if (data.timeline && data.timeline.topics) { const hasTimeline = data.timeline.commonEntities && data.timeline.commonEntities.length > 0; item.description += `
${hasTimeline ? '事件追踪' : '相关事件'}'; } } delete item.id; delete item.hasInstantView; return item; }) ) ); ctx.state.data = { title: `Readhub - ${titles[category]}`, link: currentUrl, item: items, }; };