feat: 实现模板、艺术品建档、溯源节点与证书水印

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Nixevol
2026-06-28 16:05:13 +08:00
co-authored by Cursor
parent 6e5c767a53
commit b4108d7434
20 changed files with 1087 additions and 1 deletions
@@ -0,0 +1,65 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import {
IsArray,
IsDateString,
IsEnum,
IsInt,
IsNotEmpty,
IsOptional,
IsString,
MaxLength,
ValidateNested,
} from 'class-validator';
import { NodeType } from '@artveris/shared';
import { MediaItemDto } from '../../artwork/dto/artwork.dto';
export class CreateNodeDto {
@ApiProperty({ description: '节点类型', enum: NodeType })
@IsEnum(NodeType)
nodeType!: NodeType;
@ApiProperty({ description: '节点标题' })
@IsString()
@IsNotEmpty()
@MaxLength(80)
title!: string;
@ApiPropertyOptional({ description: '发生时间' })
@IsOptional()
@IsDateString()
occurredAt?: string;
@ApiPropertyOptional({ description: '地点' })
@IsOptional()
@IsString()
@MaxLength(120)
location?: string;
@ApiPropertyOptional({ description: '说明' })
@IsOptional()
@IsString()
@MaxLength(2000)
description?: string;
@ApiPropertyOptional({ description: '图片/视频', type: [MediaItemDto] })
@IsOptional()
@IsArray()
@ValidateNested({ each: true })
@Type(() => MediaItemDto)
mediaFiles?: MediaItemDto[];
@ApiPropertyOptional({ description: '排序' })
@IsOptional()
@IsInt()
sortOrder?: number;
}
export class UpdateNodeDto extends CreateNodeDto {}
export class ReorderNodeDto {
@ApiProperty({ description: '按目标顺序排列的节点 ID 列表', type: [Number] })
@IsArray()
@IsInt({ each: true })
ids!: number[];
}
@@ -0,0 +1,47 @@
import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { RoleType } from '@artveris/shared';
import { TraceNodeService } from './trace-node.service';
import { CreateNodeDto, ReorderNodeDto, UpdateNodeDto } from './dto/trace-node.dto';
import { Roles } from '../common/decorators/roles.decorator';
@ApiTags('溯源节点')
@ApiBearerAuth()
@Controller()
export class TraceNodeController {
constructor(private readonly nodeService: TraceNodeService) {}
@Get('artworks/:artworkId/nodes')
@ApiOperation({ summary: '作品的溯源节点列表' })
list(@Param('artworkId', ParseIntPipe) artworkId: number) {
return this.nodeService.listByArtwork(artworkId);
}
@Post('artworks/:artworkId/nodes')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '新增溯源节点' })
create(@Param('artworkId', ParseIntPipe) artworkId: number, @Body() dto: CreateNodeDto) {
return this.nodeService.create(artworkId, dto);
}
@Put('artworks/:artworkId/nodes/reorder')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '调整节点顺序' })
reorder(@Param('artworkId', ParseIntPipe) artworkId: number, @Body() dto: ReorderNodeDto) {
return this.nodeService.reorder(artworkId, dto);
}
@Put('nodes/:id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '更新溯源节点' })
update(@Param('id', ParseIntPipe) id: number, @Body() dto: UpdateNodeDto) {
return this.nodeService.update(id, dto);
}
@Delete('nodes/:id')
@Roles(RoleType.OPERATOR)
@ApiOperation({ summary: '删除溯源节点' })
remove(@Param('id', ParseIntPipe) id: number) {
return this.nodeService.remove(id);
}
}
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { TraceNodeService } from './trace-node.service';
import { TraceNodeController } from './trace-node.controller';
@Module({
controllers: [TraceNodeController],
providers: [TraceNodeService],
exports: [TraceNodeService],
})
export class TraceNodeModule {}
@@ -0,0 +1,95 @@
import { Injectable } from '@nestjs/common';
import { Prisma } from '@prisma/client';
import { ErrorCode } from '@artveris/shared';
import { PrismaService } from '../prisma/prisma.service';
import { StorageService } from '../storage/storage.service';
import { BusinessException } from '../common/exceptions/business.exception';
import { CreateNodeDto, ReorderNodeDto, UpdateNodeDto } from './dto/trace-node.dto';
@Injectable()
export class TraceNodeService {
constructor(
private readonly prisma: PrismaService,
private readonly storage: StorageService,
) {}
private mapMedia(items: unknown) {
if (!Array.isArray(items)) return [];
return (items as Array<{ key: string }>)
.filter((i) => i && i.key)
.map((i) => ({ ...i, url: this.storage.getUrl(i.key) }));
}
private async ensureArtwork(artworkId: number) {
const a = await this.prisma.artwork.findFirst({ where: { id: artworkId, deletedAt: null } });
if (!a) throw new BusinessException(ErrorCode.ARTWORK_NOT_FOUND, '艺术品不存在', 404);
}
async listByArtwork(artworkId: number) {
const nodes = await this.prisma.traceNode.findMany({
where: { artworkId },
orderBy: { sortOrder: 'asc' },
});
return nodes.map((n) => ({ ...n, mediaFiles: this.mapMedia(n.mediaFiles) }));
}
async create(artworkId: number, dto: CreateNodeDto) {
await this.ensureArtwork(artworkId);
const count = await this.prisma.traceNode.count({ where: { artworkId } });
const node = await this.prisma.traceNode.create({
data: {
artworkId,
nodeType: dto.nodeType,
title: dto.title,
occurredAt: dto.occurredAt ? new Date(dto.occurredAt) : null,
location: dto.location,
description: dto.description,
mediaFiles: (dto.mediaFiles ?? []) as unknown as Prisma.InputJsonValue,
sortOrder: dto.sortOrder ?? count,
},
});
return { ...node, mediaFiles: this.mapMedia(node.mediaFiles) };
}
async update(id: number, dto: UpdateNodeDto) {
await this.ensureNode(id);
const node = await this.prisma.traceNode.update({
where: { id },
data: {
nodeType: dto.nodeType,
title: dto.title,
occurredAt: dto.occurredAt ? new Date(dto.occurredAt) : null,
location: dto.location,
description: dto.description,
mediaFiles: (dto.mediaFiles ?? []) as unknown as Prisma.InputJsonValue,
sortOrder: dto.sortOrder,
},
});
return { ...node, mediaFiles: this.mapMedia(node.mediaFiles) };
}
async remove(id: number) {
await this.ensureNode(id);
await this.prisma.traceNode.delete({ where: { id } });
return { success: true };
}
async reorder(artworkId: number, dto: ReorderNodeDto) {
await this.ensureArtwork(artworkId);
await this.prisma.$transaction(
dto.ids.map((id, index) =>
this.prisma.traceNode.updateMany({
where: { id, artworkId },
data: { sortOrder: index },
}),
),
);
return { success: true };
}
private async ensureNode(id: number) {
const n = await this.prisma.traceNode.findUnique({ where: { id } });
if (!n) throw new BusinessException(ErrorCode.NOT_FOUND, '节点不存在', 404);
return n;
}
}