From b03c9339bdf714dc3b0b2adcee23ad28a0afff64 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, 21 May 2022 22:46:13 +0800 Subject: [PATCH] Feat: IP API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 查询 IP 的接口,外部服务反代 --- src/components/Admin/RemoveMusicCache.tsx | 6 +---- src/server/api/ip.ts | 33 +++++++++++++++++++++++ src/server/utils/netease.ts | 2 +- 3 files changed, 35 insertions(+), 6 deletions(-) create mode 100644 src/server/api/ip.ts diff --git a/src/components/Admin/RemoveMusicCache.tsx b/src/components/Admin/RemoveMusicCache.tsx index 82e29b2..1eaaf7b 100644 --- a/src/components/Admin/RemoveMusicCache.tsx +++ b/src/components/Admin/RemoveMusicCache.tsx @@ -15,7 +15,6 @@ import cleanCacheRequest from "@/server/api/admin/netease/clean"; // Interface import { MouseEvent, ChangeEvent } from "react"; - // Components function RemoveMusicCache() { const { loading, run } = useRequest( @@ -23,10 +22,7 @@ function RemoveMusicCache() { query: params }), { - manual: true, - onSuccess: (data) => { - console.log(data); - } + manual: true } ); diff --git a/src/server/api/ip.ts b/src/server/api/ip.ts new file mode 100644 index 0000000..70a5270 --- /dev/null +++ b/src/server/api/ip.ts @@ -0,0 +1,33 @@ +import { Api, Get, Query, useContext, useInject } from "@midwayjs/hooks"; +import { RedisService } from "@midwayjs/redis"; + +import fetch from "isomorphic-unfetch"; + +export default Api( + Get(), + Query<{ ip?: string }>(), + async () => { + const ctx = useContext(); + const client = await useInject(RedisService); + + // 增加使用数量 + await client.incr("api-next:stat:ip"); + + const ip = ctx.query.ip || ctx.ip.replace(/::\S+:/, ""); + + const response = await fetch(`http://ip-api.com/json/${ip}?lang=zh-CN`, { + headers: { + "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36" + } + }); + const json = await response.json(); + + delete json.status; + + return { + code: 1, + msg: "Success", + data: json + }; + } +); diff --git a/src/server/utils/netease.ts b/src/server/utils/netease.ts index 74d85a9..d086103 100644 --- a/src/server/utils/netease.ts +++ b/src/server/utils/netease.ts @@ -45,7 +45,7 @@ export const parseSongData = async (item: any) => { return { id: item.id, title: item.name, - artist: item.artists ?. [0].name || "", + artist: item.artists ? item.artists.map((artist: any) => artist.name).join(" / ") : "", alias: item.transName || (Array.isArray(item.alias) ? (item.alias[0] || "") : ""), album: item.album.name || "", cover: `${item.album.picUrl.replace("http://", "https://")}?param=250y250"`,