fix(route): discuz use ofetch (#18543)

* fix(route): discuz use ofetch

* fix(route): discuz use ofetch with charset handling
This commit is contained in:
Yuliang Tan 2025-03-08 12:03:12 +08:00 committed by GitHub
parent b47613a331
commit 385759d7b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 7 additions and 9 deletions

View File

@ -1,6 +1,6 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
import iconv from 'iconv-lite';
import { parseDate } from '@/utils/parse-date';
@ -22,14 +22,13 @@ function fixUrl(itemLink, baseUrl) {
// discuz 7.x 与 discuz x系列 通用文章内容抓取
async function loadContent(itemLink, charset, header) {
// 处理编码问题
const response = await got({
const response = await ofetch.raw(itemLink, {
method: 'get',
url: itemLink,
responseType: 'buffer',
responseType: 'arrayBuffer',
headers: header,
});
const responseData = iconv.decode(response.data, charset ?? 'utf-8');
const responseData = iconv.decode(Buffer.from(response._data), charset ?? 'utf-8');
if (!responseData) {
const description = '获取详细内容失败';
return { description };
@ -77,14 +76,13 @@ async function handler(ctx) {
Cookie: cookie,
};
const response = await got({
const response = await ofetch.raw(link, {
method: 'get',
url: link,
responseType: 'buffer',
responseType: 'arrayBuffer',
headers: header,
});
const responseData = response.data;
const responseData = Buffer.from(response._data);
// 若没有指定编码则默认utf-8
const contentType = response.headers['content-type'] || '';
let $ = load(iconv.decode(responseData, 'utf-8'));