parent
ba5e92e2be
commit
aa8c9f82a6
|
|
@ -1,19 +1,25 @@
|
||||||
|
// Tool
|
||||||
import { Api, Get, Query, useContext, useInject } from "@midwayjs/hooks";
|
import { Api, Get, Query, useContext, useInject } from "@midwayjs/hooks";
|
||||||
import { RedisService } from "@midwayjs/redis";
|
import { RedisService } from "@midwayjs/redis";
|
||||||
|
|
||||||
import fetch from "isomorphic-unfetch";
|
import fetch from "isomorphic-unfetch";
|
||||||
|
|
||||||
|
|
||||||
|
// Interface
|
||||||
|
import { Context } from "@midwayjs/koa";
|
||||||
|
|
||||||
|
|
||||||
|
// API
|
||||||
export default Api(
|
export default Api(
|
||||||
Get(),
|
Get(),
|
||||||
Query<{ ip?: string }>(),
|
Query<{ ip?: string }>(),
|
||||||
async () => {
|
async () => {
|
||||||
const ctx = useContext();
|
const ctx = useContext<Context>();
|
||||||
const client = await useInject(RedisService);
|
const client = await useInject(RedisService);
|
||||||
|
|
||||||
// 增加使用数量
|
// 增加使用数量
|
||||||
await client.incr("api-next:stat:ip");
|
await client.incr("api-next:stat:ip");
|
||||||
|
|
||||||
const ip = ctx.query.ip || ctx.ip.replace(/::\S+:/, "");
|
const ip = ctx.headers["x-forwarded-for"] || ctx.request.ip.replace(/::\S+:/, "") || "127.0.0.1";
|
||||||
|
|
||||||
const response = await fetch(`http://ip-api.com/json/${ip}?lang=zh-CN`, {
|
const response = await fetch(`http://ip-api.com/json/${ip}?lang=zh-CN`, {
|
||||||
headers: {
|
headers: {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue