feat: add 新冠疫苗实时动态 (#6754)

* feat: add 新冠疫苗实时动态

* fix: item links
This commit is contained in:
Ethan Shen 2021-01-22 16:36:24 +08:00 committed by GitHub
parent 46bdcb18cc
commit e9db390eaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 126 additions and 0 deletions

View File

@ -872,6 +872,28 @@ area 分区选项
<Route author="LogicJake" example="/imaijia/category/xls" path="/imaijia/category/:category" :paramsDesc="['类别id可在 URL 中找到']" />
## 丁香园
### 新冠疫苗实时动态
<Route author="nczitzk" example="/dxy/vaccine/北京" path="/dxy/vaccine/:province?/:city?/:location?" :paramsDesc="['省', '市', '区']">
查看北京市的新冠疫苗接种点,路由为 `/dxy/vaccine/北京`
查看北京市朝阳区的新冠疫苗接种点,路由为 `/dxy/vaccine/北京/北京/朝阳区`
查看湖北省武汉市的新冠疫苗接种点,路由为 `/dxy/vaccine/湖北/武汉`
查看湖北省武汉市武昌区的新冠疫苗接种点,路由为 `/dxy/vaccine/湖北/武汉/武昌区`
::: tip 提示
若参数为空,则返回全国所有新冠疫苗接种点。
:::
</Route>
## 懂球帝
::: tip 提示

View File

@ -3901,4 +3901,7 @@ router.get('/growincity/news/:id?', require('./routes/growincity/news'));
// Thrillist
router.get('/thrillist/:tag?', require('./routes/thrillist/index'));
// 丁香园
router.get('/dxy/vaccine/:province?/:city?/:location?', require('./routes/dxy/vaccine'));
module.exports = router;

101
lib/routes/dxy/vaccine.js Normal file
View File

@ -0,0 +1,101 @@
const got = require('@/utils/got');
module.exports = async (ctx) => {
const province = ctx.params.province || '';
const city = ctx.params.city || '';
const location = ctx.params.location || '';
const fullLocation = `${province ? `${province}${city ? `/${city}${location ? `/${location}` : ''}` : ''}` : ''}`;
const rootUrl = 'https://mama.dxy.com';
const currentUrl = `${rootUrl}/client/vaccine/new-crown-vaccine`;
const apiUrl = `${rootUrl}/api/vaccine/client/vaccination-point/all`;
const apiResponse = await got({
method: 'get',
url: apiUrl,
});
const response = await got({
method: 'get',
url: apiResponse.data.results.fileUrl,
});
const allPoints = [],
allLocations = {},
allLocationIds = {},
pointDataArray = response.data.results.pointData;
for (const data of pointDataArray) {
allLocations[data.locationName] = data.locationId;
allLocationIds[data.locationId] = data.locationName;
if (data.points) {
allPoints.push(...data.points);
} else {
pointDataArray.push(...data.pointData);
}
}
const list = allPoints.map((item) => {
const locationId = item.locationId;
const province = locationId - (locationId % 10000);
const city = locationId - (locationId % 100);
return {
title: `${allLocationIds[province]}/${allLocationIds.hasOwnProperty(city) ? `${allLocationIds[city]}/` : `${allLocationIds[province]}/`}${allLocationIds[locationId]}`,
link: item.contentUrl,
pubDate: new Date(item.modifyDate).toUTCString(),
};
});
const items = await Promise.all(
list
.filter((item) => {
if (fullLocation !== '') {
const locationSplit = item.title.split('/');
const fullLocationSplit = fullLocation.split('/');
for (let index = 0; index < fullLocationSplit.length; index++) {
if (locationSplit[index] !== fullLocationSplit[index]) {
return false;
}
}
}
return true;
})
.map(
async (item) =>
await ctx.cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const result = detailResponse.data.results;
const description =
`<h1> ${result.point.pointName}</h1>` +
'<ul>' +
`<li>户籍限制:${result.point.registerLimit}</li>` +
`<li>服务限制:${result.point.serviceTag}</li>` +
`<li>服务时间:${result.detail.serviceTime}</li>` +
`<li>地址:${result.point.address}</li>` +
`<li>电话:${result.point.phoneNo}</li>` +
`<li>接种人群:${result.detail.targetPeople}</li>` +
`<li>接种所需材料:${result.detail.materials}</li>` +
`<li>预约步骤:${result.detail.reserveSteps}</li>` +
'</ul>';
item.description = description;
item.title = `${result.point.pointName}${item.title}`;
item.link = `${rootUrl}/client/vaccine/vaccination-point?pointId=${result.point.id}`;
return item;
})
)
);
ctx.state.data = {
title: `新冠疫苗接种点查询${fullLocation ? `${fullLocation}` : ''} - 丁香园`,
link: currentUrl,
item: items,
};
};