import { BadRequestException, Body, Controller, Delete, Get, Param, ParseIntPipe, Patch, Post, UploadedFile, UseGuards, UseInterceptors, } from '@nestjs/common'; import { FileInterceptor } from '@nestjs/platform-express'; import { memoryStorage } from 'multer'; import { IdentifyService } from './identify.service'; import { CurrentUser, JwtAuthGuard, type JwtPayload } from '../auth/roles'; const ALLOWED = /\.(jpe?g|png|webp|gif)$/i; @Controller('api') @UseGuards(JwtAuthGuard) export class IdentifyController { constructor(private readonly svc: IdentifyService) {} /** AI 识车:上传一张照片,调用通义千问视觉模型识别并持久化(命中哈希缓存则复用)。*/ @Post('identify') @UseInterceptors( FileInterceptor('file', { storage: memoryStorage(), limits: { fileSize: 8 * 1024 * 1024 }, fileFilter: (_req, file, cb) => ALLOWED.test(file.originalname) || /^image\//.test(file.mimetype) ? cb(null, true) : cb(new BadRequestException('仅支持图片文件'), false), }), ) async identify( @UploadedFile() file: Express.Multer.File, @CurrentUser() user: JwtPayload, ) { if (!file) throw new BadRequestException('未收到图片'); return this.svc.identifyAndSave(user.sub, file.buffer, file.mimetype); } /** 当前用户的识别历史。*/ @Get('identifications') list(@CurrentUser() user: JwtPayload) { return this.svc.list(user.sub); } /** 修改备注(人工纠正/标注)。*/ @Patch('identifications/:id') update( @Param('id', ParseIntPipe) id: number, @Body('note') note: string, @CurrentUser() user: JwtPayload, ) { return this.svc.updateNote(user.sub, id, note ?? ''); } /** 删除一条识别历史。*/ @Delete('identifications/:id') remove(@Param('id', ParseIntPipe) id: number, @CurrentUser() user: JwtPayload) { return this.svc.remove(user.sub, id); } }