Files
nxxmdata/bank-backend/scripts/seed-personal-center-test-data.js

162 lines
5.0 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.

/**
* 个人中心测试数据种子脚本
* @file seed-personal-center-test-data.js
* @description 为个人中心功能添加测试数据
*/
const { User, Role } = require('../models');
const bcrypt = require('bcryptjs');
const { sequelize } = require('../config/database');
async function seedPersonalCenterTestData() {
try {
console.log('=== 开始添加个人中心测试数据 ===\n');
// 1. 确保admin角色存在
console.log('1. 检查admin角色...');
let adminRole = await Role.findOne({ where: { name: 'admin' } });
if (!adminRole) {
adminRole = await Role.create({
name: 'admin',
display_name: '系统管理员',
description: '系统管理员角色',
permissions: JSON.stringify(['*'])
});
console.log('✅ 创建admin角色成功');
} else {
console.log('✅ admin角色已存在');
}
// 2. 确保manager角色存在
console.log('2. 检查manager角色...');
let managerRole = await Role.findOne({ where: { name: 'manager' } });
if (!managerRole) {
managerRole = await Role.create({
name: 'manager',
display_name: '经理',
description: '经理角色',
permissions: JSON.stringify(['read', 'write', 'approve'])
});
console.log('✅ 创建manager角色成功');
} else {
console.log('✅ manager角色已存在');
}
// 3. 创建测试用户 - 银行经理
console.log('3. 创建测试用户 - 银行经理...');
const managerPassword = await bcrypt.hash('Manager123456', 10);
const managerUser = await User.findOrCreate({
where: { username: 'manager001' },
defaults: {
username: 'manager001',
password: managerPassword,
real_name: '李经理',
email: 'manager001@bank.com',
phone: '15004901368',
id_card: '110101198001010001',
status: 'active',
role_id: managerRole.id,
position: '部门经理',
department: '风险管理部'
}
});
if (managerUser[1]) {
console.log('✅ 创建银行经理用户成功');
} else {
console.log('✅ 银行经理用户已存在');
}
// 4. 创建测试用户 - 银行员工
console.log('4. 创建测试用户 - 银行员工...');
const employeePassword = await bcrypt.hash('Employee123456', 10);
const employeeUser = await User.findOrCreate({
where: { username: 'employee001' },
defaults: {
username: 'employee001',
password: employeePassword,
real_name: '王员工',
email: 'employee001@bank.com',
phone: '13800138000',
id_card: '110101199001010002',
status: 'active',
role_id: managerRole.id,
position: '业务专员',
department: '客户服务部'
}
});
if (employeeUser[1]) {
console.log('✅ 创建银行员工用户成功');
} else {
console.log('✅ 银行员工用户已存在');
}
// 5. 更新admin用户信息
console.log('5. 更新admin用户信息...');
const adminUser = await User.findOne({ where: { username: 'admin' } });
if (adminUser) {
await adminUser.update({
real_name: '系统管理员',
phone: '15004901368',
position: '系统管理员',
department: '信息技术部'
});
console.log('✅ 更新admin用户信息成功');
} else {
console.log('⚠️ admin用户不存在请先运行create-admin-user.js');
}
// 6. 显示测试用户信息
console.log('\n=== 测试用户信息 ===');
const testUsers = await User.findAll({
where: {
username: ['admin', 'manager001', 'employee001']
},
include: [{
model: Role,
as: 'role'
}]
});
testUsers.forEach(user => {
console.log(`\n用户名: ${user.username}`);
console.log(`姓名: ${user.real_name}`);
console.log(`手机: ${user.phone}`);
console.log(`邮箱: ${user.email}`);
console.log(`角色: ${user.role ? user.role.display_name : '未知'}`);
console.log(`部门: ${user.department || '未设置'}`);
console.log(`职位: ${user.position || '未设置'}`);
console.log(`状态: ${user.status}`);
});
console.log('\n=== 测试登录信息 ===');
console.log('管理员账号: admin / Admin123456');
console.log('经理账号: manager001 / Manager123456');
console.log('员工账号: employee001 / Employee123456');
console.log('\n=== 个人中心测试数据添加完成 ===');
} catch (error) {
console.error('❌ 添加个人中心测试数据失败:', error);
throw error;
} finally {
await sequelize.close();
}
}
// 运行脚本
if (require.main === module) {
seedPersonalCenterTestData()
.then(() => {
console.log('✅ 脚本执行完成');
process.exit(0);
})
.catch((error) => {
console.error('❌ 脚本执行失败:', error);
process.exit(1);
});
}
module.exports = seedPersonalCenterTestData;