feat: 微信开放平台相关路由代码简化 (#3804)

This commit is contained in:
Henry Wang
2020-01-31 09:37:58 +00:00
committed by GitHub
parent a86d033ad1
commit f87d381609
8 changed files with 35 additions and 117 deletions

View File

@@ -0,0 +1,33 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
const link = 'https://developers.weixin.qq.com/miniprogram/dev/framework/release/';
const response = await got({
method: 'get',
url: link,
});
const data = response.data;
const $ = cheerio.load(data);
const name = $('#docContent .content h1')
.text()
.replace(/[\s|#]/g, '');
const titles = $('#docContent .content h3').map((i, ele) =>
$(ele)
.text()
.replace(/[\s|#]/g, '')
);
const list = $('#docContent > .content ol')
.map((i, ele) => ({
title: titles[i],
description: $(ele).html(),
}))
.get();
ctx.state.data = {
title: `${name}最新动态`,
link: link,
item: list,
description: '基础库更新日志RSS',
};
};

View File

@@ -0,0 +1,43 @@
const got = require('@/utils/got');
const cheerio = require('cheerio');
module.exports = async (ctx) => {
let { type } = ctx.params;
let title = '微信开放社区 - ';
if (type.startsWith('pay')) {
type = 'pay';
title += '微信支付公告';
} else if (type.startsWith('xcx')) {
type = 'develop';
title += '小程序公告';
} else if (type.startsWith('xyx')) {
type = 'minigame';
title += '小游戏公告';
}
const link = `https://developers.weixin.qq.com/community/${type}/list/2`;
const { data: htmlString } = await got({
method: 'get',
url: link,
});
const $ = cheerio.load(htmlString);
const announceList = [];
$('#article_frame > div > ul > li').each(function() {
const $item = $(this);
const $link = $item.find('a').attr('href');
const time = $item.find('div > em').text();
const title = $item.find('h2 > meta').attr('content');
announceList.push({
title: title,
link: `https://developers.weixin.qq.com${$link}`,
description: title,
pubDate: time,
});
});
ctx.state.data = {
title,
link,
item: announceList,
};
};

View File

@@ -0,0 +1,37 @@
const got = require('@/utils/got');
module.exports = async (ctx) => {
const { type, category } = ctx.params;
let url, title;
if (type.startsWith('xcx')) {
url = `https://developers.weixin.qq.com/community/ngi/question/list?page=1&tag=${category}`;
title = `微信开放社区的小程序问题 - ${category}`;
} else if (type.startsWith('xyx')) {
url = `https://developers.weixin.qq.com/community/ngi/timeline/2/1/${category}?page=1&limit=10`;
title = `微信开放社区的小游戏问题 - ${category}`;
}
const response = await got.get(url);
const data = response.data.data;
ctx.state.data = {
// 源标题
title,
// 源链接
link: url,
// 源说明
description: title,
// 遍历此前获取的数据
item: data.rows.map((item) => ({
// 文章标题
title: item.Title,
// 文章正文
description: `${item.Content}`,
// 文章发布时间
pubDate: new Date(item.CreateTime * 1000).toUTCString(),
// 文章链接
link: `https://developers.weixin.qq.com/community/develop/doc/${item.DocId}`,
})),
};
};

View File

@@ -0,0 +1,32 @@
const got = require('@/utils/got');
module.exports = async (ctx) => {
const data = (
await got({
method: 'get',
url:
'https://pay.weixin.qq.com/index.php/public/cms/get_contents?id=6200&cmstype=1&url=https%253A%252F%252Fpay.weixin.qq.com%252Fpublic%252Fcms%252Fcontent_list%253Flang%253Dzh%2526id%253D6200&states=2&publishtimeend=1565537507&expiretimebeg=1565537507&propertyinclude=1&ordertype=4&field=contentId%2CcontentTitle%2CcontentPublishTime&g_ty=ajax',
})
).data.data.contentlist;
const data2 = (
await got({
method: 'get',
url:
'https://pay.weixin.qq.com/index.php/public/cms/get_contents?pagenum=1&id=6200&cmstype=1&url=https%253A%252F%252Fpay.weixin.qq.com%252Fpublic%252Fcms%252Fcontent_list%253Flang%253Dzh%2526id%253D6200&states=2&publishtimeend=1565537507&expiretimebeg=1565537507&propertyexclude=1&ordertype=4&field=contentId%2CcontentTitle%2CcontentPublishTime&g_ty=ajax',
})
).data.data.contentlist;
data.push(...data2);
ctx.state.data = {
title: '微信支付-商户平台公告',
link: 'https://pay.weixin.qq.com/public/cms/content_list?lang=zh&id=6200',
item: data.map((item) => ({
title: item.contentTitle,
description: item.contentTitle,
pubDate: new Date(item.contentPublishTime * 1000).toUTCString(),
link: `https://pay.weixin.qq.com/index.php/public/cms/content_detail?platformType=0&lang=zh&id=${item.contentId}`,
})),
};
};