feat: 追溯码客户端预览(TOTP式令牌保护,不激活不计数,可切换激活状态预览)
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
import { Controller, Get, Param, Req, Res } from '@nestjs/common';
|
||||
import { Controller, Get, Param, Query, Req, Res } from '@nestjs/common';
|
||||
import { Request, Response } from 'express';
|
||||
import { ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { Throttle } from '@nestjs/throttler';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { Public } from '../common/decorators/public.decorator';
|
||||
import { extractIp } from '../common/utils/ua.util';
|
||||
import { isValidPreviewToken } from '../common/utils/preview-token.util';
|
||||
import { PublicTraceService } from './public-trace.service';
|
||||
import { SettingsService } from '../settings/settings.service';
|
||||
|
||||
@@ -13,6 +15,7 @@ export class PublicTraceController {
|
||||
constructor(
|
||||
private readonly service: PublicTraceService,
|
||||
private readonly settings: SettingsService,
|
||||
private readonly config: ConfigService,
|
||||
) {}
|
||||
|
||||
@Public()
|
||||
@@ -26,6 +29,22 @@ export class PublicTraceController {
|
||||
return this.service.trace(shortCode, { ip, userAgent });
|
||||
}
|
||||
|
||||
// 后台预览:凭时间令牌访问,零副作用(不激活/不计数/不写日志)。令牌过期返回 PREVIEW_EXPIRED。
|
||||
@Public()
|
||||
@Get('public/trace/:shortCode/preview')
|
||||
@Throttle({ default: { limit: 60, ttl: 60_000 } })
|
||||
@ApiOperation({ summary: 'C 端溯源预览(令牌保护,不激活不计数)' })
|
||||
preview(
|
||||
@Param('shortCode') shortCode: string,
|
||||
@Query('token') token?: string,
|
||||
@Query('state') state?: string,
|
||||
) {
|
||||
if (!isValidPreviewToken(this.config.get<string>('jwt.secret') || '', token)) {
|
||||
return { status: 'PREVIEW_EXPIRED' as const };
|
||||
}
|
||||
return this.service.preview(shortCode, state === 'active' ? 'active' : 'inactive');
|
||||
}
|
||||
|
||||
// 短链跳转:扫码打开 /t/:shortCode -> 重定向到 C 端溯源页(不在 /api 前缀下)
|
||||
@Public()
|
||||
@Get('t/:shortCode')
|
||||
|
||||
Reference in New Issue
Block a user