From 81dcbf0cdeadaf2af47aaf5d545d5a28ea757c7d Mon Sep 17 00:00:00 2001 From: tone <3341154833@qq.com> Date: Sun, 8 Jun 2025 22:10:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=8D=9A=E5=AE=A2=E8=AF=84?= =?UTF-8?q?=E8=AE=BA=E6=B7=BB=E5=8A=A0ip=E7=9A=84=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tone-page-server/src/blog/blog.controller.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tone-page-server/src/blog/blog.controller.ts b/tone-page-server/src/blog/blog.controller.ts index f7a0c95..25ebdf8 100644 --- a/tone-page-server/src/blog/blog.controller.ts +++ b/tone-page-server/src/blog/blog.controller.ts @@ -60,10 +60,30 @@ export class BlogController { let user = userId ? await this.userService.findOne({ userId }) : null; + // 获取IP归属地 + const ip = req.ip || req.socket.remoteAddress || req.headers['x-forwarded-for'] || '未知'; + let address = '未知'; + if (!['::1'].includes(ip)) { + const addressRes = await (await fetch(`https://mesh.if.iqiyi.com/aid/ip/info?version=1.1.1&ip=${ip}`)).json(); + if (addressRes?.code == 0) { + const country: string = addressRes?.data?.countryCN || '未知'; + const province: string = addressRes?.data?.provinceCN || '中国'; + if (country !== '中国') { + // 非中国,显示国家 + address = country; + } else { + // 中国,显示省份 + address = province; + } + } + } + const comment = { ...commentData, blogId: id, user: user, + ip: ip, + address: address, }; return await this.blogService.createComment(comment);