const axios = require('@/utils/axios'); const host = 'https://met.red'; const mainPage = 'https://met.red/h/weal/list#'; const cheerio = require('cheerio'); const url = require('url'); async function load(link, ctx) { const cache = await ctx.cache.get(link); if (cache) { return cache; } const response = await axios.get(link); const $ = cheerio.load(response.data); const images = $('img'); for (let k = 0; k < images.length; k++) { $(images[k]).replaceWith(``); } const couponUrl = $('.layui-btn.layui-btn.layui-btn-lg').attr('href'); let eventHtml; if (!couponUrl || couponUrl === undefined) { eventHtml = '

活动链接:无

'; } else { eventHtml = `
点我前往活动
`; } const description = eventHtml + $('.p-detail-html').html(); await ctx.cache.set(link, description); return { description }; } module.exports = async (ctx) => { const response = await axios({ method: 'get', url: mainPage, }); const data = response.data; const $ = cheerio.load(data); const list = $('h4 > a').get(); const process = await Promise.all( list.map(async (item) => { const itemUrl = host + $(item).attr('href'); const single = { title: $(item).text(), link: itemUrl, guid: itemUrl, }; const other = await load(itemUrl, ctx); return Promise.resolve(Object.assign({}, single, other)); }) ); ctx.state.data = { title: '福利资源-met.red', link: mainPage, description: '福利资源更新提醒', item: process, }; };