import { PrismaClient } from '@prisma/client'; import * as bcrypt from 'bcryptjs'; const prisma = new PrismaClient(); async function main() { const email = process.env.SUPER_ADMIN_EMAIL; const password = process.env.SUPER_ADMIN_PASSWORD; if (!email || !password) { console.error('❌ 请设置环境变量 SUPER_ADMIN_EMAIL 和 SUPER_ADMIN_PASSWORD'); process.exit(1); } if (password.length < 8) { console.error('❌ SUPER_ADMIN_PASSWORD 长度不能少于 8 位'); process.exit(1); } const passwordHash = await bcrypt.hash(password, 12); const adminUser = await prisma.adminUser.upsert({ where: { email }, update: { passwordHash, role: 'SUPER_ADMIN', status: 'ACTIVE', displayName: '超级管理员', }, create: { email, passwordHash, displayName: '超级管理员', role: 'SUPER_ADMIN', status: 'ACTIVE', }, }); console.log(`✅ 超级管理员已创建/更新: ${adminUser.email} (id: ${adminUser.id})`); } main() .catch((e) => { console.error('❌ Seed 失败:', e); process.exit(1); }) .finally(() => prisma.$disconnect());