From 99c196afad4d24299943b6ea6ba5a4c09365ba36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=87=E8=B6=A3=E4=BF=9D=E7=BD=97?= Date: Sat, 16 Apr 2022 23:50:57 +0800 Subject: [PATCH] Feat: CORS Headers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit API 增加 CORS 头 --- src/api/acgm.ts | 4 ++++ src/api/bing.ts | 16 +++++++++++----- src/api/netease.ts | 7 +++++++ src/pages/acgm.tsx | 3 ++- src/pages/bing.tsx | 3 ++- src/pages/netease.tsx | 3 ++- src/pages/wallpaper.tsx | 4 ++-- 7 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/api/acgm.ts b/src/api/acgm.ts index b796333..b24569a 100644 --- a/src/api/acgm.ts +++ b/src/api/acgm.ts @@ -11,6 +11,10 @@ export default Api( async () => { const ctx = useContext(); + // 设置 Header + ctx.set("access-control-allow-origin", "*"); + ctx.set("access-control-allow-headers", "x-requested-with"); + // 增加使用数量 await client.incr("api-next:stat:acgm"); diff --git a/src/api/bing.ts b/src/api/bing.ts index af7fc06..72e01fa 100644 --- a/src/api/bing.ts +++ b/src/api/bing.ts @@ -9,17 +9,23 @@ export default Api( async () => { const ctx = useContext(); + // 设置 Header + ctx.set("access-control-allow-origin", "*"); + ctx.set("access-control-allow-headers", "x-requested-with"); + ctx.set("cache-control", "max-age=604800"); + // 增加使用数量 await client.incr("api-next:stat:bing"); - const cache = await client.lRange("api-next:bing", 0, 2); + // 尝试使用缓存 + const cached = await client.lRange("api-next:bing", 0, 2); let image; - if (cache.length) { + if (cached.length) { image = { - url: cache[0], - copyright: cache[1], + url: cached[0], + copyright: cached[1], }; } else { @@ -60,7 +66,7 @@ export default Api( code: 1, msg: "Success", data: image, - cached: cache.length ? true : false + cached: cached.length ? true : false }; } ); diff --git a/src/api/netease.ts b/src/api/netease.ts index 282d0e3..b966cdd 100644 --- a/src/api/netease.ts +++ b/src/api/netease.ts @@ -10,6 +10,10 @@ export default Api( async () => { const ctx = useContext(); + // 设置 Header + ctx.set("access-control-allow-origin", "*"); + ctx.set("access-control-allow-headers", "x-requested-with"); + // 增加使用数量 await client.incr("api-next:stat:netease"); @@ -30,6 +34,9 @@ export default Api( return ""; } + // 成功获取,设置缓存头 + ctx.set("cache-control", "max-age=604800"); + // 尝试使用缓存 const cached = await client.lRange(`api-next:163:${id}`, 0, 7); diff --git a/src/pages/acgm.tsx b/src/pages/acgm.tsx index e04a657..b5873af 100644 --- a/src/pages/acgm.tsx +++ b/src/pages/acgm.tsx @@ -3,6 +3,7 @@ import React, { useEffect, useRef } from "react"; // UI +import { Link } from "react-router-dom"; import prism from "prismjs"; import ArticleHead from "@/components/Layout/ArticleHead"; @@ -55,7 +56,7 @@ function ACGM() {

使用方法:

调用地址 https://api.paugram.com/api/acgm,即可获得一段歌曲 JSON 信息。

-

如果您已开始使用本 API 服务,则默认视为遵守 本约定

+

如果您已开始使用本 API 服务,则默认视为遵守 本约定。

音乐版权归网易云音乐所有,本站不为滥用本 API 进行违规操作者承担责任。部分歌曲可能因版权问题无法播放,敬请谅解!

为了防止多次出现随机到同一首歌的情况,请允许我们使用你的 Cookie 记录。

diff --git a/src/pages/bing.tsx b/src/pages/bing.tsx index f2151c5..fc793a5 100644 --- a/src/pages/bing.tsx +++ b/src/pages/bing.tsx @@ -3,6 +3,7 @@ import React, { useEffect } from "react"; // UI +import { Link } from "react-router-dom"; import prism from "prismjs"; import ArticleHead from "@/components/Layout/ArticleHead"; @@ -19,7 +20,7 @@ function Bing() {

使用方法:

调用地址 https://api.paugram.com/api/bing 可自动跳转到对应的壁纸

-

如果您已开始使用本 API 服务,则默认视为遵守 本约定

+

如果您已开始使用本 API 服务,则默认视为遵守 本约定。

图片版权归原作者所有,本站不为滥用本 API 进行违规操作者承担责任。

参数:

diff --git a/src/pages/netease.tsx b/src/pages/netease.tsx index 79fdced..e14fd10 100644 --- a/src/pages/netease.tsx +++ b/src/pages/netease.tsx @@ -3,6 +3,7 @@ import React, { useEffect } from "react"; // UI +import { Link } from "react-router-dom"; import prism from "prismjs"; import ArticleHead from "@/components/Layout/ArticleHead"; @@ -19,7 +20,7 @@ function Netease() {

使用方法:

调用地址 https://api.paugram.com/netease 并输入参数 idtitle,即可获得一段歌曲 JSON 信息。

-

如果您已开始使用本 API 服务,则默认视为遵守 本约定

+

如果您已开始使用本 API 服务,则默认视为遵守 本约定。

音乐版权归网易云音乐所有,本站不为滥用本 API 进行违规操作者承担责任。

参数:

diff --git a/src/pages/wallpaper.tsx b/src/pages/wallpaper.tsx index 3aadf0f..4093cdc 100644 --- a/src/pages/wallpaper.tsx +++ b/src/pages/wallpaper.tsx @@ -3,8 +3,8 @@ import React, { useEffect } from "react"; // UI +import { Link } from "react-router-dom"; import prism from "prismjs"; - import ArticleHead from "@/components/Layout/ArticleHead"; import HelpExample from "../images/help/wallpaper-pr-example.jpg"; @@ -42,7 +42,7 @@ function Wallpaper() {

使用方法:

调用地址 https://api.paugram.com/api/wallpaper 可自动跳转到对应的壁纸(由于运营成本限制,目前本 API 借助 图床/CDN 等平台托管图片。欢迎给予资源赞助~)

-

如果您已开始使用本 API 服务,则默认视为遵守 本约定

+

如果您已开始使用本 API 服务,则默认视为遵守 本约定。

图片版权归原作者所有,本站不为滥用本 API 进行违规操作者承担责任。

参数: