Files
Artveris/apps/api/src/trace-code/trace-code.controller.ts
T

163 lines
5.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import {
Body,
Controller,
Delete,
Get,
Param,
ParseIntPipe,
Post,
Put,
Query,
Res,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { ConfigService } from '@nestjs/config';
import { Response } from 'express';
import { RoleType } from '@artveris/shared';
import { TraceCodeService } from './trace-code.service';
import { SettingsService } from '../settings/settings.service';
import { currentPreviewToken, PREVIEW_PERIOD_MS } from '../common/utils/preview-token.util';
import {
BatchDeleteDto,
BatchGroupDto,
BatchMoveDto,
BatchStatusDto,
ExportTraceCodeDto,
GenerateBatchDto,
GroupDto,
QueryBatchDto,
QueryTraceCodeDto,
UpdateTraceCodeDto,
} from './dto/trace-code.dto';
import { Roles } from '../common/decorators/roles.decorator';
import { CurrentUser, AuthUser } from '../common/decorators/current-user.decorator';
import { parseBigId } from '../common/utils/bigint.util';
@ApiTags('批量生码')
@ApiBearerAuth()
@Controller()
export class TraceCodeController {
constructor(
private readonly service: TraceCodeService,
private readonly settings: SettingsService,
private readonly config: ConfigService,
) {}
// 预览令牌:类 TOTP,随时间自动轮换(当前+上一窗口有效)。后台点开预览页时随取随用,
// 预览地址外泄后最长 2 个周期即失效,无法长期传播。
@Get('trace-codes/preview-token')
@ApiOperation({ summary: '获取 C 端预览令牌(自动轮换)' })
async previewToken() {
return {
token: currentPreviewToken(this.config.get<string>('jwt.secret') || ''),
clientBase: await this.settings.getClientBaseUrl(),
periodMs: PREVIEW_PERIOD_MS,
};
}
@Post('trace-codes/batch')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '按批次异步生成追溯码' })
generate(@Body() dto: GenerateBatchDto, @CurrentUser() user: AuthUser) {
return this.service.generateBatch(dto, user.id);
}
@Get('batches')
@ApiOperation({ summary: '批次列表' })
batches(@Query() query: QueryBatchDto) {
return this.service.listBatches(query);
}
@Get('trace-code-groups')
@ApiOperation({ summary: '分组列表' })
groups(@Query('artworkId') artworkId?: string) {
const id = artworkId ? Number(artworkId) : undefined;
return this.service.listGroups(id && Number.isFinite(id) ? id : undefined);
}
@Post('trace-code-groups')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '创建分组' })
createGroup(@Body() dto: GroupDto) {
return this.service.createGroup(dto);
}
@Put('trace-code-groups/:id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '更新分组' })
updateGroup(@Param('id', ParseIntPipe) id: number, @Body() dto: GroupDto) {
return this.service.updateGroup(id, dto);
}
@Delete('trace-code-groups/:id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '删除分组' })
removeGroup(@Param('id', ParseIntPipe) id: number) {
return this.service.removeGroup(id);
}
@Put('trace-codes/batch/group')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '批量设置分组' })
batchGroup(@Body() dto: BatchGroupDto) {
return this.service.batchSetGroup(dto);
}
@Put('trace-codes/batch/status')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '批量启用/停用' })
batchStatus(@Body() dto: BatchStatusDto) {
return this.service.batchSetStatus(dto);
}
@Put('trace-codes/batch/move')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '批量修改追溯码批次' })
batchMove(@Body() dto: BatchMoveDto, @CurrentUser() user: AuthUser) {
return this.service.batchMove(dto, user.id);
}
@Post('trace-codes/batch/delete')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '批量删除追溯码' })
batchDelete(@Body() dto: BatchDeleteDto) {
return this.service.batchDelete(dto);
}
@Put('trace-codes/:id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '更新单个追溯码' })
updateOne(@Param('id') id: string, @Body() dto: UpdateTraceCodeDto, @CurrentUser() user: AuthUser) {
return this.service.updateOne(id, dto, user.id);
}
@Delete('trace-codes/:id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '删除单个追溯码' })
removeOne(@Param('id') id: string) {
return this.service.removeOne(id);
}
@Get('trace-codes')
@ApiOperation({ summary: '追溯码列表' })
list(@Query() query: QueryTraceCodeDto) {
return this.service.list(query);
}
@Get('trace-codes/export')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '批量导出追溯码 ZIP(xlsx + 二维码图片)' })
export(@Query() query: ExportTraceCodeDto, @Res() res: Response) {
return this.service.exportCodes(query, res);
}
@Get('trace-codes/:id/qrcode')
@ApiOperation({ summary: '获取追溯码二维码 PNG' })
async qrcode(@Param('id') id: string, @Res() res: Response) {
const buf = await this.service.getQrPng(parseBigId(id, '追溯码 ID'));
res.setHeader('Content-Type', 'image/png');
res.end(buf);
}
}