fix: 36kr search error (#2472)

* fix: 36kr search

* fix: 36kr search error
This commit is contained in:
Cloud
2019-06-24 22:32:22 +08:00
committed by DIYgod
parent 9f1a24b2f7
commit 72a149b03a
2 changed files with 23 additions and 4 deletions
+1 -1
View File
@@ -14,7 +14,7 @@ pageClass: routes
### 搜索文章
<Route author="xyqfer" example="/36kr/search/article/8%E7%82%B91%E6%B0%AA" path="/36kr/search/article/:keyword" :paramsDesc="['关键字']" />
<Route author="xyqfer kt286" example="/36kr/search/article/ofo" path="/36kr/search/article/:keyword" :paramsDesc="['关键字']" />
## 99% Invisible
+22 -3
View File
@@ -2,9 +2,27 @@ const got = require('@/utils/got');
module.exports = async (ctx) => {
const { keyword = '' } = ctx.params;
const csrfresponse = await got.get('https://36kr.com/pp/api/csrf');
const cookies = csrfresponse.headers['set-cookie'].toString();
const token = cookies.match(/M-XSRF-TOKEN=.{64}/)[0];
const acw_tc = cookies.match(/acw_tc=.{61}/)[0];
const krnewsfrontss = cookies.match(/krnewsfrontss=.{32}/)[0];
const response = await got({
method: 'get',
url: `https://36kr.com/pp/api/search/entity-search?page=1&per_page=20&sort=date&entity_type=post&keyword=${encodeURIComponent(keyword)}`,
method: 'post',
url: `https://36kr.com/pp/api/search/entity-search`,
headers: {
'Content-Type': 'application/json',
cookie: `${token};${acw_tc};${krnewsfrontss};`,
'M-X-XSRF-TOKEN': token.replace('M-XSRF-TOKEN=', ''),
},
data: JSON.stringify({
page: 1,
per_page: 20,
sort: 'date',
entity_type: 'post',
keyword: encodeURIComponent(keyword),
}),
});
const load = async (link) => {
@@ -12,8 +30,9 @@ module.exports = async (ctx) => {
const description = response.data.data.content;
return { description };
};
const items = await Promise.all(
response.data.data.items.map(async (item) => {
(response.data.data.items || []).map(async (item) => {
const link = `https://36kr.com/api/post/${item.id}`;
const single = {
title: item.title,