diff --git a/README.md b/README.md index bb76c59..98063f0 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,11 @@ Settings > Functions > Advanced Setting > Function Region 切换为 Hong Kong, -> 注意:小幻在填写API的时候需要在API后面加上`/api/v2`,如`http://192.168.1.7:9321/87654321/api/v2` +> 注意: +> +> ~~小幻在填写API的时候需要在API后面加上`/api/v2`,如`http://192.168.1.7:9321/87654321/api/v2`~~ +> +> (已对小幻做兼容,`/api/v2`可加可不加都可以正确处理) > > 有很多人问FW能不能用,FW推荐直接使用插件,如果非要使用,则可以配合 `https://raw.githubusercontent.com/huangxd-/ForwardWidgets/refs/heads/main/widgets.fwd` 里的`danmu_api`插件使用 diff --git a/danmu_api/worker.js b/danmu_api/worker.js index b26536c..e55c4d2 100644 --- a/danmu_api/worker.js +++ b/danmu_api/worker.js @@ -3400,6 +3400,40 @@ async function handleRequest(req, env) { log("log", path); + // 智能处理API路径前缀,确保最终有一个正确的 /api/v2 + if (path !== "/" && path !== "/api/logs") { + log('log', `[Path Check] Starting path normalization for: "${path}"`); + const pathBeforeCleanup = path; // 保存清理前的路径检查是否修改 + + // 1. 清理:应对“用户填写/api/v2”+“客户端添加/api/v2”导致的重复前缀 + while (path.startsWith('/api/v2/api/v2/')) { + log('log', `[Path Check] Found redundant /api/v2 prefix. Cleaning...`); + // 从第二个 /api/v2 的位置开始截取,相当于移除第一个 + path = path.substring('/api/v2'.length); + } + + // 打印日志:只有在发生清理时才显示清理后的路径,否则显示“无需清理” + if (path !== pathBeforeCleanup) { + log('log', `[Path Check] Path after cleanup: "${path}"`); + } else { + log('log', `[Path Check] Path after cleanup: No cleanup needed.`); + } + + // 2. 补全:如果路径缺少前缀(例如请求原始路径为 /search/anime),则补全 + const pathBeforePrefixCheck = path; + if (!path.startsWith('/api/v2') && path !== '/' && !path.startsWith('/api/logs')) { + log('log', `[Path Check] Path is missing /api/v2 prefix. Adding...`); + path = '/api/v2' + path; + } + + // 打印日志:只有在发生添加前缀时才显示添加后的路径,否则显示“无需补全” + if (path === pathBeforePrefixCheck) { + log('log', `[Path Check] Prefix Check: No prefix addition needed.`); + } + + log('log', `[Path Check] Final normalized path: "${path}"`); + } + // GET / if (path === "/" && method === "GET") { return handleHomepage(); @@ -3474,4 +3508,4 @@ export async function vercelHandler(req, res) { // 为了测试导出 handleRequest export { handleRequest, searchAnime, searchEpisodes, matchAnime, getBangumi, getComment, fetchTencentVideo, fetchIqiyi, - fetchMangoTV, fetchBilibili, fetchYouku, fetchOtherServer, httpGet, httpPost }; \ No newline at end of file + fetchMangoTV, fetchBilibili, fetchYouku, fetchOtherServer, httpGet, httpPost };