Sfoglia il codice sorgente

更新应用配置以信任代理,并在二维码控制器和扫描记录控制器中改进IP地址获取逻辑,确保在不同情况下正确记录用户IP。

wuyi 2 settimane fa
parent
commit
f1a09a9108

+ 1 - 0
src/app.ts

@@ -28,6 +28,7 @@ const options: FastifyEnvOptions = {
 export const createApp = async () => {
   const app = fastify({
     disableRequestLogging: true,
+    trustProxy: true,
     logger: {
       level: process.env.NODE_ENV === 'development' ? 'debug' : 'info',
       transport: {

+ 1 - 1
src/controllers/qr-code.controller.ts

@@ -143,7 +143,7 @@ export class QrCodeController {
       }
 
       // 记录扫描
-      const ipAddress = request.ip
+      const ipAddress = request.ip || request.socket.remoteAddress
       const userAgent = request.headers['user-agent']
 
       // 如果通过 qrCode 查询,记录扫描

+ 1 - 2
src/controllers/scan-record.controller.ts

@@ -16,8 +16,7 @@ export class ScanRecordController {
     try {
       const { qrCode, latitude, longitude, address, ipAddress, userAgent } = request.body
 
-      // 如果前端没有提供IP和UserAgent,从请求中获取
-      const finalIpAddress = ipAddress || request.ip
+      const finalIpAddress = ipAddress || request.ip || request.socket.remoteAddress
       const finalUserAgent = userAgent || request.headers['user-agent']
 
       const scanRecord = await this.scanRecordService.create(