fix(route/caus): migrate to wordpress api

This commit is contained in:
pseudoyu 2026-03-28 16:17:17 +08:00
parent 881a9d895f
commit 8ed4034b09
No known key found for this signature in database
1 changed files with 33 additions and 71 deletions

View File

@ -1,32 +1,18 @@
import type { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
// Reason: preserve original category numbers for backward compatibility.
// WordPress category ids from REST API (GET /wp-json/wp/v2/categories).
// Old categories "商业"(2) and "快讯"(3) no longer exist on the new site,
// mapped to closest equivalents: 2→财富(finance), 3→社会(lifestyle).
const categories = {
0: {
title: '全部',
link: 'home?id=0',
},
1: {
title: '要闻',
link: 'focus_news?id=1',
},
2: {
title: '商业',
link: 'business?id=2',
},
3: {
title: '快讯',
link: 'hours?id=3',
},
8: {
title: '财富',
link: 'fortune?id=8',
},
6: {
title: '生活',
link: 'life?id=6',
},
0: { title: '全部', slug: '', id: 0 },
1: { title: '要闻', slug: 'news', id: 101 },
2: { title: '商业', slug: 'finance', id: 108 },
3: { title: '快讯', slug: 'lifestyle', id: 106 },
8: { title: '财富', slug: 'finance', id: 108 },
6: { title: '生活', slug: 'lifestyle', id: 106 },
};
export const route: Route = {
@ -52,59 +38,35 @@ export const route: Route = {
async function handler(ctx) {
const category = ctx.req.param('category') || '0';
const cat = categories[category] || categories[0];
const isHome = category === '0';
const rootUrl = 'https://caus.com';
const apiUrl = `${rootUrl}/wp-json/wp/v2/posts`;
const rootUrl = 'https://www.caus.com';
const apiRootUrl = 'https://api.caus.money';
const query: Record<string, string | number> = {
per_page: 20,
// Reason: using _embed=1 with many posts produces responses too large for ofetch to parse as JSON
_embed: 'author,wp:term',
};
if (cat.id) {
query.categories = cat.id;
}
const currentUrl = `${rootUrl}/${categories[category].link}`;
const searchUrl = `${apiRootUrl}/toronto/display/searchList`;
const listUrl = `${apiRootUrl}/toronto/display/lanmuArticlelistNew`;
const data = await ofetch(apiUrl, { query });
const response = await got({
method: 'post',
url: isHome ? searchUrl : listUrl,
json: isHome
? {
pageQ: {
pageSize: 10,
sortFiled: 'id',
sortType: 'DESC',
},
types: ['ARTICLE', 'VIDEO'],
}
: {
filterIds: [],
lanmuId: Number.parseInt(category),
},
});
const list = (isHome ? response.data.data : response.data.data.articleList).map((item) => ({
title: item.title,
link: `${rootUrl}/detail/${item.contentId}`,
contentId: item.contentId,
pubDate: new Date(item.createTime),
category: [...new Set([...item.lanmus.map((lanmu) => lanmu.name), ...item.tags.map((tag) => tag.name)])],
const items = data.map((item) => ({
title: item.title.rendered,
link: item.link,
description: item.content.rendered,
pubDate: parseDate(item.date_gmt),
author: item._embedded?.author?.[0]?.name,
category: item._embedded?.['wp:term']?.[0]?.map((t) => t.name),
}));
const items = await Promise.all(
list.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: `${apiRootUrl}/toronto/display/contentWithRelate?contentId=${item.contentId}`,
});
item.description = detailResponse.data.data.content.content;
return item;
})
)
);
const currentUrl = cat.slug ? `${rootUrl}/category/${cat.slug}/` : rootUrl;
return {
title: `${categories[category].title} - 加美财经`,
title: `${cat.title} - 加美财经`,
link: currentUrl,
item: items,
};