fix: 生产启动自动创建管理员
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
const { PrismaClient, RoleType, UserStatus } = require('@prisma/client');
|
||||
const bcrypt = require('bcryptjs');
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function ensureAdmin() {
|
||||
const username = process.env.SEED_ADMIN_USERNAME || 'admin';
|
||||
const password = process.env.SEED_ADMIN_PASSWORD || 'Admin@12345';
|
||||
const realName = process.env.SEED_ADMIN_NAME || '超级管理员';
|
||||
|
||||
const existing = await prisma.sysUser.findUnique({ where: { username } });
|
||||
if (existing) return;
|
||||
|
||||
await prisma.sysUser.create({
|
||||
data: {
|
||||
username,
|
||||
passwordHash: await bcrypt.hash(password, 10),
|
||||
realName,
|
||||
role: RoleType.SUPER_ADMIN,
|
||||
status: UserStatus.ACTIVE,
|
||||
},
|
||||
});
|
||||
console.log(`[seed-production] 已创建超级管理员:${username}`);
|
||||
}
|
||||
|
||||
async function ensureSettings() {
|
||||
const defaults = {
|
||||
appName: process.env.APP_NAME || '艺术品溯源平台',
|
||||
appIconUrl: '/app-icon.svg',
|
||||
logRetentionDays: process.env.LOG_RETENTION_DAYS || '365',
|
||||
operationLogRetentionDays: process.env.OPERATION_LOG_RETENTION_DAYS || '365',
|
||||
watermarkText: process.env.WATERMARK_TEXT || '仅供溯源核验使用',
|
||||
filingInfo: '备案信息待补充',
|
||||
backupEnabled: 'false',
|
||||
backupSchedule: 'off',
|
||||
backupRetentionDays: '30',
|
||||
backupRetentionCount: '10',
|
||||
clientCarouselInterval: '5',
|
||||
};
|
||||
|
||||
for (const [key, value] of Object.entries(defaults)) {
|
||||
await prisma.systemSetting.upsert({
|
||||
where: { key },
|
||||
update: {},
|
||||
create: { key, value },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await ensureAdmin();
|
||||
await ensureSettings();
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((error) => {
|
||||
console.error('[seed-production] 初始化失败', error);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user