const User = require('../models/User'); const bcrypt = require('bcryptjs'); // 初始化管理员用户 async function initAdminUser() { try { console.log('正在初始化管理员用户...'); // 检查是否已存在管理员用户 const existingAdmin = await User.findOne({ where: { username: 'admin', role: 'admin' } }); if (existingAdmin) { console.log('管理员用户已存在,更新密码...'); // 加密密码 const hashedPassword = await bcrypt.hash('123456', 10); // 更新用户密码 await existingAdmin.update({ password: hashedPassword, status: 'active' }); console.log('管理员用户密码更新成功'); } else { console.log('创建新的管理员用户...'); // 加密密码 const hashedPassword = await bcrypt.hash('123456', 10); // 创建管理员用户 await User.create({ username: 'admin', password: hashedPassword, role: 'admin', status: 'active' }); console.log('管理员用户创建成功'); } console.log('初始化管理员用户完成'); } catch (error) { console.error('初始化管理员用户失败:', error); } finally { // 关闭数据库连接 process.exit(0); } } // 执行初始化 initAdminUser();