From 708d0aa9904367bfc7a9649d1adb5b56c602cc27 Mon Sep 17 00:00:00 2001 From: Wang Junyu Date: Fri, 5 Jul 2019 11:10:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=BE=AE=E5=8D=9A=E5=85=A8=E6=96=87?= =?UTF-8?q?=E5=92=8C=E5=87=86=E7=A1=AE=E7=9A=84=E5=BE=AE=E5=8D=9A=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E6=97=B6=E9=97=B4=20(#2546)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/routes/weibo/user.js | 54 ++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/routes/weibo/user.js b/lib/routes/weibo/user.js index 3f0d54103..ce74a29cb 100644 --- a/lib/routes/weibo/user.js +++ b/lib/routes/weibo/user.js @@ -1,6 +1,5 @@ const got = require('@/utils/got'); const weiboUtils = require('./utils'); -const date = require('@/utils/date'); module.exports = async (ctx) => { const uid = ctx.params.uid; @@ -28,22 +27,51 @@ module.exports = async (ctx) => { }, }); + const resultItem = await Promise.all( + response.data.data.cards + .filter((item) => item.mblog && !item.mblog.isTop) + .map(async (item) => { + let data = {}; + const key = 'weibo:user:' + item.mblog.bid; + const value = await ctx.cache.get(key); + if (value) { + data = JSON.parse(value); + } else { + // 可以判断出是否是长微博,如果不是那么不调下边的接口获取完整全文也行,但是拿不到准确的created_at,而是“几小时前”这种 + // 索性就都再调接口获取详细data了,这个接口只返回json所以性能消耗不大。另外做了缓存处理 + const link = `https://m.weibo.cn/statuses/show?id=${item.mblog.bid}`; + const itemResponse = await got.get(link, { + headers: { + Referer: `https://m.weibo.cn/u/${uid}`, + 'MWeibo-Pwa': 1, + 'X-Requested-With': 'XMLHttpRequest', + 'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1', + }, + }); + data = itemResponse.data.data; + ctx.cache.set(key, JSON.stringify(data)); + } + + item.mblog.text = data.text; + const description = weiboUtils.format(item.mblog); + const title = description.replace(//g, '[图片]').replace(/<.*?>/g, ''); + const pubDate = new Date(data.created_at).toUTCString(); + + const it = { + title: title, + description: description, + link: `https://weibo.com/${uid}/${item.mblog.bid}`, + pubDate: pubDate, + }; + return Promise.resolve(it); + }) + ); + ctx.state.data = { title: `${name}的微博`, link: `http://weibo.com/${uid}/`, description: description, image: profileImageUrl, - item: response.data.data.cards - .filter((item) => item.mblog && !item.mblog.isTop) - .map((item) => { - const description = weiboUtils.format(item.mblog); - const title = description.replace(//g, '[图片]').replace(/<.*?>/g, ''); - return { - title, - description: description, - pubDate: date(item.mblog.created_at, 8), - link: `https://weibo.com/${uid}/${item.mblog.bid}`, - }; - }), + item: resultItem, }; };