34 lines
853 B
TypeScript
34 lines
853 B
TypeScript
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
|
|
};
|
|
}
|
|
);
|