255 lines
7.3 KiB
JavaScript
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.

/* eslint-disable @typescript-eslint/no-require-imports */
/**
* 创建演示数据脚本
* 用于快速生成测试产品和装配步骤
*/
const DEMO_CONFIRM_TOKEN = 'reset-local-demo'
let prisma = null
let pool = null
function assertLocalDestructiveDemoAllowed() {
if (!process.argv.includes(`--confirm=${DEMO_CONFIRM_TOKEN}`)) {
throw new Error(`拒绝执行 destructive demo seed:必须显式追加 --confirm=${DEMO_CONFIRM_TOKEN}。`)
}
if (process.env.NODE_ENV === 'production') {
throw new Error('拒绝执行 destructive demo seed:NODE_ENV=production。')
}
const databaseUrl = process.env.DATABASE_URL || ''
if (!databaseUrl) {
throw new Error('拒绝执行 destructive demo seed:DATABASE_URL 未配置。')
}
const parsed = new URL(databaseUrl)
if (!['localhost', '127.0.0.1', '::1'].includes(parsed.hostname)) {
throw new Error('拒绝执行 destructive demo seed:只允许本机数据库。')
}
}
function createPrismaClient() {
const { PrismaClient } = require('@prisma/client')
const { PrismaPg } = require('@prisma/adapter-pg')
const { Pool } = require('pg')
pool = new Pool({ connectionString: process.env.DATABASE_URL })
prisma = new PrismaClient({ adapter: new PrismaPg(pool) })
}
async function createDemoData() {
assertLocalDestructiveDemoAllowed()
createPrismaClient()
console.log('🚀 开始创建演示数据...')
try {
// 清理现有数据(可选)
console.log('🧹 清理现有数据...')
await prisma.shareLink.deleteMany()
await prisma.assemblyStep.deleteMany()
await prisma.panel.deleteMany()
await prisma.product.deleteMany()
// 创建示例产品
console.log('📦 创建示例产品...')
const product = await prisma.product.create({
data: {
name: '简约办公椅',
sku: 'CHAIR-001',
category: '办公家具',
modelUrl: 'https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf',
thumbnailUrl: 'https://threejs.org/examples/screenshots/misc_controls_orbit.jpg'
}
})
console.log(`✅ 产品创建成功: ${product.name} (ID: ${product.id})`)
// 创建装配步骤
console.log('🔧 创建装配步骤...')
const steps = [
{
order: 1,
title: '第一步:准备椅腿',
description: '取出四个椅腿,检查配件是否齐全',
partIds: ['leg1', 'leg2', 'leg3', 'leg4'],
transform: {
position: { x: 0, y: 0, z: 0 },
rotation: { x: 0, y: 0, z: 0 },
scale: { x: 1, y: 1, z: 1 }
},
duration: 2000,
easing: 'power2.inOut'
},
{
order: 2,
title: '第二步:安装椅腿',
description: '将椅腿固定到座椅底部,确保螺丝拧紧',
partIds: ['seat', 'leg1', 'leg2', 'leg3', 'leg4'],
transform: {
position: { x: 0, y: -0.5, z: 0 },
rotation: { x: 0, y: 0, z: 0 },
scale: { x: 1, y: 1, z: 1 }
},
duration: 3000,
easing: 'power2.inOut'
},
{
order: 3,
title: '第三步:安装靠背',
description: '将靠背与座椅连接,调整到合适角度',
partIds: ['backrest', 'seat'],
transform: {
position: { x: 0, y: 0.3, z: -0.2 },
rotation: { x: -0.1, y: 0, z: 0 },
scale: { x: 1, y: 1, z: 1 }
},
duration: 2500,
easing: 'power2.inOut'
},
{
order: 4,
title: '第四步:最终检查',
description: '检查所有连接点,确保椅子稳固',
partIds: ['complete'],
transform: {
position: { x: 0, y: 0, z: 0 },
rotation: { x: 0, y: Math.PI * 2, z: 0 },
scale: { x: 1, y: 1, z: 1 }
},
duration: 4000,
easing: 'power2.inOut'
}
]
for (const stepData of steps) {
const step = await prisma.assemblyStep.create({
data: {
...stepData,
productId: product.id
}
})
console.log(`✅ 步骤 ${step.order}: ${step.title}`)
}
// 创建板件信息
console.log('🪵 创建板件信息...')
const panels = [
{
code: 'A01',
name: '座椅面板',
gltfNodeName: 'seat',
length: 45.0,
width: 40.0,
thickness: 3.0
},
{
code: 'A02',
name: '靠背面板',
gltfNodeName: 'backrest',
length: 40.0,
width: 50.0,
thickness: 2.5
},
{
code: 'B01',
name: '椅腿(前左)',
gltfNodeName: 'leg1',
length: 5.0,
width: 5.0,
thickness: 45.0
},
{
code: 'B02',
name: '椅腿(前右)',
gltfNodeName: 'leg2',
length: 5.0,
width: 5.0,
thickness: 45.0
},
{
code: 'B03',
name: '椅腿(后左)',
gltfNodeName: 'leg3',
length: 5.0,
width: 5.0,
thickness: 45.0
},
{
code: 'B04',
name: '椅腿(后右)',
gltfNodeName: 'leg4',
length: 5.0,
width: 5.0,
thickness: 45.0
}
]
for (const panelData of panels) {
const panel = await prisma.panel.create({
data: {
...panelData,
productId: product.id
}
})
console.log(`✅ 板件: ${panel.code} - ${panel.name}`)
}
// 创建分享链接
console.log('🔗 创建分享链接...')
const shareLink = await prisma.shareLink.create({
data: {
productId: product.id,
token: 'demo123',
expiresAt: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000) // 30天后过期
}
})
console.log(`✅ 分享链接创建成功: /share/${shareLink.token}`)
// 创建管理员用户
console.log('👤 创建管理员用户...')
const admin = await prisma.user.create({
data: {
email: 'admin@furniture3d.com',
name: '系统管理员',
password: 'admin123', // 注意:生产环境需要加密
role: 'admin'
}
})
console.log(`✅ 管理员用户创建成功: ${admin.email}`)
console.log('\n🎉 演示数据创建完成!')
console.log('\n📋 快速访问链接:')
console.log(` 主页: http://localhost:3000`)
console.log(` 演示分享: http://localhost:3000/share/demo123`)
console.log(` 产品管理: http://localhost:3000/products`)
console.log(` 数据库管理: http://localhost:5555`)
console.log(` 健康检查: http://localhost:3000/api/health`)
console.log('\n📊 创建的数据:')
console.log(` 产品: 1个 (${product.name})`)
console.log(` 装配步骤: ${steps.length}个`)
console.log(` 板件: ${panels.length}个`)
console.log(` 分享链接: 1个`)
console.log(` 用户: 1个`)
} catch (error) {
console.error('❌ 创建演示数据失败:', error)
throw error
} finally {
await prisma?.$disconnect()
await pool?.end()
}
}
// 运行脚本
if (require.main === module) {
createDemoData()
.then(() => {
console.log('\n✨ 脚本执行完成,可以开始测试系统了!')
process.exit(0)
})
.catch((error) => {
console.error('脚本执行失败:', error)
process.exit(1)
})
}
module.exports = { createDemoData }