const got = require('@/utils/got'); const cheerio = require('cheerio'); module.exports = async (ctx) => { const id = ctx.params.id ?? '1'; const limit = ctx.query.limit ? parseInt(ctx.query.limit) : 50; const rootUrl = 'http://www.camchina.org.cn'; const currentUrl = `${rootUrl}/categories/${id}`; const response = await got({ method: 'get', url: currentUrl, }); const $ = cheerio.load(response.data); let items = $('.M-main-l p a') .slice(0, limit) .toArray() .map((item) => { item = $(item); return { title: item.text(), link: `${rootUrl}${item.attr('href')}`, }; }); items = await Promise.all( items.map((item) => ctx.cache.tryGet(item.link, async () => { const detailResponse = await got({ method: 'get', url: item.link, }); const content = cheerio.load(detailResponse.data); item.description = content('.content').html(); return item; }) ) ); ctx.state.data = { title: `中国管理现代化研究会 - ${$('.title_red').text()}`, link: currentUrl, item: items, }; };