From ac741fb34e721b5cf8746feb4fdef975067461b4 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 15 Oct 2026 20:49:50 +0800 Subject: [PATCH] fix(ele.me): include public announcement content --- lib/routes/ele.me/open/announce.ts | 71 ++++++++++++++++++++++++------ 1 file changed, 58 insertions(+), 13 deletions(-) diff --git a/lib/routes/ele.me/open/announce.ts b/lib/routes/ele.me/open/announce.ts index 9ddb57d424..2885cd1ac2 100644 --- a/lib/routes/ele.me/open/announce.ts +++ b/lib/routes/ele.me/open/announce.ts @@ -1,6 +1,12 @@ import { randomUUID } from 'node:crypto'; +import { load } from 'cheerio'; +import { FetchError } from 'ofetch'; +import pMap from 'p-map'; + import type { Route } from '@/types'; +import cache from '@/utils/cache'; +import logger from '@/utils/logger'; import ofetch from '@/utils/ofetch'; import { parseDate } from '@/utils/parse-date'; import timezone from '@/utils/timezone'; @@ -14,8 +20,17 @@ export const route: Route = { handler, }; -async function handler() { - const data = await ofetch('https://app-api.shop.ele.me/arena/invoke/?method=NoticeKeeperService.getBroadcastList', { +class NoticeContentError extends Error {} + +interface Notice { + templateId: string | number; + title: string; + beginDate?: string; + content?: string; +} + +function getNoticeData(method: string, params: Record) { + return ofetch<{ code: number; result: T }>(`https://app-api.shop.ele.me/arena/invoke/?method=NoticeKeeperService.${method}`, { method: 'POST', body: { id: `${randomUUID().replaceAll('-', '').toUpperCase()}|${Date.now()}`, @@ -24,23 +39,53 @@ async function handler() { appVersion: '4.4.0', }, service: 'NoticeKeeperService', - method: 'getBroadcastList', - params: { - offset: 0, - limit: 999, - }, + method, + params, ncp: '2.0.0', }, }); +} + +async function getNoticeContent(templateId: string | number) { + const data = await getNoticeData('getBroadcastByTemplateId', { templateId: String(templateId) }); + const content = data.result?.content; + if (data.code !== 200 || String(data.result?.templateId) !== String(templateId) || typeof content !== 'string' || !content.trim()) { + throw new NoticeContentError(`Ele.me did not return content for notice ${templateId}.`); + } + const $ = load(content); + if (!$('body').text().trim() && !$('img, video, audio, iframe, object, embed').length) { + throw new NoticeContentError(`Ele.me returned an empty body for notice ${templateId}.`); + } + return content; +} + +async function handler() { + const data = await getNoticeData<{ broadcastList: Notice[] }>('getBroadcastList', { offset: 0, limit: 999 }); + const items = await pMap( + data.result.broadcastList, + async (item) => { + let description = ''; + try { + description = await cache.tryGet(`ele.me:notice-content:${item.templateId}`, () => getNoticeContent(item.templateId)); + } catch (error) { + if (!(error instanceof FetchError || error instanceof NoticeContentError)) { + throw error; + } + logger.warn(`Ele.me did not return usable content for notice ${item.templateId}.`); + } + return { + title: item.title, + description, + pubDate: item.beginDate ? timezone(parseDate(item.beginDate), 8) : undefined, + link: `https://open.shop.ele.me/common/publicnotice/${item.templateId}`, + }; + }, + { concurrency: 3 } + ); return { title: '饿了么商家开放平台-公告', link: 'https://open.shop.ele.me/common/publicnotice/0', - item: data.result.broadcastList.map((item) => ({ - title: item.title, - description: '', - pubDate: item.beginDate ? timezone(parseDate(item.beginDate), 8) : undefined, - link: `https://open.shop.ele.me/common/publicnotice/${item.templateId}`, - })), + item: items, }; }