|
| 1 | +const got = require('@/utils/got'); |
| 2 | +const cheerio = require('cheerio'); |
| 3 | +const { parseDate } = require('@/utils/parse-date'); |
| 4 | +const timezone = require('@/utils/timezone'); |
| 5 | +const bootstrapHost = 'https://weixin.shmtu.edu.cn/dynamic/shmtuHttps'; |
| 6 | +const host = 'https://portal.shmtu.edu.cn/api'; |
| 7 | + |
| 8 | +const loadDetail = async (link) => { |
| 9 | + const response = await got.post(bootstrapHost, { |
| 10 | + form: { |
| 11 | + interfaceUrl: link, |
| 12 | + }, |
| 13 | + https: { rejectUnauthorized: false }, |
| 14 | + }); |
| 15 | + |
| 16 | + return JSON.parse(response.data); |
| 17 | +}; |
| 18 | + |
| 19 | +const processFeed = (list, caches) => |
| 20 | + Promise.all( |
| 21 | + list.map((item) => |
| 22 | + caches.tryGet(item.link, async () => { |
| 23 | + const detail = await loadDetail(item.link); |
| 24 | + item.description = detail.body.und[0].safe_value; |
| 25 | + item.link = detail.path; |
| 26 | + return item; |
| 27 | + }) |
| 28 | + ) |
| 29 | + ); |
| 30 | + |
| 31 | +module.exports = async (ctx) => { |
| 32 | + const type = ctx.params.type; |
| 33 | + const info = type === 'bmtzgg' ? '部门通知公告' : '未知'; |
| 34 | + |
| 35 | + const response = await got.post(bootstrapHost, { |
| 36 | + form: { |
| 37 | + interfaceUrl: `${host}/${type}.json?page=0`, |
| 38 | + }, |
| 39 | + https: { rejectUnauthorized: false }, |
| 40 | + }); |
| 41 | + |
| 42 | + const list = JSON.parse(response.data).map((item) => ({ |
| 43 | + title: cheerio.load(item.title).text(), |
| 44 | + link: `${host}/node/${item.nid}.json`, |
| 45 | + pubDate: timezone(parseDate(item.created), 8), |
| 46 | + category: item.field_department[0], |
| 47 | + author: item.field_department[0], |
| 48 | + })); |
| 49 | + |
| 50 | + const result = await processFeed(list, ctx.cache); |
| 51 | + |
| 52 | + ctx.state.data = { |
| 53 | + title: `上海海事大学 ${info}`, |
| 54 | + link: 'https://portal.shmtu.edu.cn/bumentongzhigonggao', |
| 55 | + description: '上海海事大学 数字平台', |
| 56 | + item: result, |
| 57 | + }; |
| 58 | +}; |
0 commit comments