import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; /** 递归将 Date 转为 Unix 毫秒 number(API 时间契约:统一毫秒时间戳)。 */ function toUnixMs(value: unknown): unknown { if (value instanceof Date) return value.getTime(); if (Array.isArray(value)) return value.map(toUnixMs); if (value && typeof value === 'object') { const out: Record = {}; for (const [k, v] of Object.entries(value as Record)) out[k] = toUnixMs(v); return out; } return value; } /** * 统一响应封装:{ code, message, data },成功 code = 0;时间统一 Unix 毫秒。 */ @Injectable() export class TransformInterceptor implements NestInterceptor { intercept(_context: ExecutionContext, next: CallHandler): Observable { return next.handle().pipe(map((data) => ({ code: 0, message: 'ok', data: toUnixMs(data ?? null) }))); } }