Files
wwjcloud-nest-v1/wwjcloud/insert-test-data.js

166 lines
9.1 KiB
JavaScript
Raw Normal View History

// 手动插入测试数据脚本
// 为4个核心模块插入测试数据
const mysql = require('mysql2/promise');
// 数据库配置
const dbConfig = {
host: 'localhost',
port: 3306,
user: 'wwjcloud',
password: 'wwjcloud',
database: 'wwjcloud'
};
async function insertTestData() {
let connection;
try {
console.log('🔌 连接数据库...');
connection = await mysql.createConnection(dbConfig);
console.log('✅ 数据库连接成功!');
console.log('\n📊 开始插入测试数据...');
// 插入Member模块数据
await insertMemberData(connection);
// 插入RBAC模块数据
await insertRbacData(connection);
// 插入Auth模块数据
await insertAuthData(connection);
console.log('\n🎉 测试数据插入完成!');
} catch (error) {
console.error('❌ 插入失败:', error.message);
} finally {
if (connection) {
await connection.end();
console.log('🔌 数据库连接已关闭');
}
}
}
async function insertMemberData(connection) {
console.log('\n👥 插入Member模块数据...');
try {
// 插入会员等级
console.log(' ⭐ 插入会员等级...');
await connection.execute(`
INSERT INTO member_level (level_id, site_id, level_name, level_weight, level_icon, level_bg_color, level_text_color, level_condition, level_discount, level_point_rate, level_description, status, create_time, update_time)
VALUES
(1, 0, '普通会员', 0, '', '#FFFFFF', '#000000', 0, 100, 1, '新注册用户', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(2, 0, 'VIP会员', 1, '', '#FFD700', '#000000', 1000, 95, 1.2, '消费满1000元', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(3, 0, '钻石会员', 2, '', '#C0C0C0', '#000000', 5000, 90, 1.5, '消费满5000元', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())
`);
console.log(' ✅ 会员等级插入成功');
// 插入会员用户
console.log(' 👤 插入会员用户...');
await connection.execute(`
INSERT INTO member (member_no, pid, site_id, username, mobile, password, nickname, headimg, member_level, member_label, wx_openid, weapp_openid, wx_unionid, ali_openid, douyin_openid, register_channel, register_type, login_ip, login_type, login_channel, login_count, login_time, create_time, last_visit_time, last_consum_time, sex, status, birthday, id_card, point, point_get, balance, balance_get, money, money_get, money_cash_outing, growth, growth_get, commission, commission_get, commission_cash_outing, is_member, member_time, is_del, province_id, city_id, district_id, address, location, remark, delete_time, update_time)
VALUES
('M001', 0, 0, 'member', '13800138000', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', '测试会员', '', 1, 'VIP', '', '', '', '', '', 'H5', 'password', '127.0.0.1', 'h5', '', 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, 1, 1, '', '', 100, 100, 1000.00, 1000.00, 500.00, 500.00, 0.00, 50, 50, 0.00, 0.00, 0.00, 1, UNIX_TIMESTAMP(), 0, 0, 0, 0, '', '', '', 0, UNIX_TIMESTAMP()),
('M002', 0, 0, 'testmember', '13800138001', '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', '普通会员', '', 0, '普通', '', '', '', '', '', 'H5', 'password', '127.0.0.1', 'h5', '', 0, UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), UNIX_TIMESTAMP(), 0, 1, 1, '', '', 50, 50, 500.00, 500.00, 200.00, 200.00, 0.00, 20, 20, 0.00, 0.00, 0.00, 0, 0, 0, 0, 0, 0, '', '', '', 0, UNIX_TIMESTAMP())
`);
console.log(' ✅ 会员用户插入成功');
// 插入会员地址
console.log(' 🏠 插入会员地址...');
await connection.execute(`
INSERT INTO member_address (member_id, site_id, name, mobile, province_id, city_id, district_id, address, address_name, full_address, lng, lat, is_default)
VALUES
(1, 0, '张三', '13800138000', 110000, 110100, 110101, '朝阳区建国路88号', '家', '北京市朝阳区建国路88号', '116.4074', '39.9042', 1),
(1, 0, '张三', '13800138000', 110000, 110100, 110102, '西城区西单大街1号', '公司', '北京市西城区西单大街1号', '116.3741', '39.9139', 0)
`);
console.log(' ✅ 会员地址插入成功');
} catch (error) {
console.error(` ❌ Member模块数据插入失败: ${error.message}`);
}
}
async function insertRbacData(connection) {
console.log('\n🔐 插入RBAC模块数据...');
try {
// 插入角色
console.log(' 🎭 插入系统角色...');
await connection.execute(`
INSERT INTO sys_role (role_id, site_id, role_name, rules, status, create_time, update_time)
VALUES
(1, 0, '超级管理员', '1,2,3,4,5,6,7,8,9,10', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(2, 0, '运营管理员', '1,2,3,4,5', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP()),
(3, 0, '内容管理员', '1,2,3', 1, UNIX_TIMESTAMP(), UNIX_TIMESTAMP())
`);
console.log(' ✅ 系统角色插入成功');
// 插入菜单
console.log(' 📋 插入系统菜单...');
await connection.execute(`
INSERT INTO sys_menu (id, app_type, menu_name, menu_short_name, menu_key, parent_key, menu_type, icon, api_url, router_path, view_path, methods, sort, status, is_show, create_time, delete_time, addon, source, menu_attr, parent_select_key)
VALUES
(1, 'admin', '系统管理', '系统', 'system', '', 0, 'setting', '', '/system', 'system/index', '', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', ''),
(2, 'admin', '用户管理', '用户', 'user', 'system', 1, 'user', '/adminapi/admin', '/system/user', 'system/user/index', 'GET,POST,PUT,DELETE', 1, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'system'),
(3, 'admin', '角色管理', '角色', 'role', 'system', 1, 'team', '/adminapi/role', '/system/role', 'system/role/index', 'GET,POST,PUT,DELETE', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'system'),
(4, 'admin', '菜单管理', '菜单', 'menu', 'system', 1, 'menu', '/adminapi/menu', '/system/menu', 'system/menu/index', 'GET,POST,PUT,DELETE', 3, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', 'system'),
(5, 'admin', '会员管理', '会员', 'member', '', 0, 'user', '', '/member', 'member/index', '', 2, 1, 1, UNIX_TIMESTAMP(), 0, '', 'system', 'system', '')
`);
console.log(' ✅ 系统菜单插入成功');
} catch (error) {
console.error(` ❌ RBAC模块数据插入失败: ${error.message}`);
}
}
async function insertAuthData(connection) {
console.log('\n🔑 插入Auth模块数据...');
try {
// 创建auth_token表
console.log(' 🏗️ 创建auth_token表...');
await connection.execute(`
CREATE TABLE IF NOT EXISTS auth_token (
id int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
token varchar(500) NOT NULL COMMENT 'JWT Token',
user_id int(11) NOT NULL COMMENT '用户ID',
user_type varchar(20) NOT NULL COMMENT '用户类型admin/member',
site_id int(11) NOT NULL DEFAULT 0 COMMENT '站点ID0为独立版',
expires_at datetime NOT NULL COMMENT '过期时间',
refresh_token varchar(500) DEFAULT NULL COMMENT '刷新Token',
refresh_expires_at datetime DEFAULT NULL COMMENT '刷新Token过期时间',
ip_address varchar(45) DEFAULT NULL COMMENT 'IP地址',
user_agent varchar(500) DEFAULT NULL COMMENT '用户代理',
device_type varchar(20) DEFAULT NULL COMMENT '设备类型web/mobile/app',
is_revoked tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否已撤销0未撤销1已撤销',
revoked_at datetime DEFAULT NULL COMMENT '撤销时间',
revoked_reason varchar(200) DEFAULT NULL COMMENT '撤销原因',
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (id),
UNIQUE KEY uk_token (token),
KEY idx_user_type (user_id,user_type)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='认证Token表'
`);
console.log(' ✅ auth_token表创建成功');
// 插入测试Token
console.log(' 🎫 插入测试Token...');
await connection.execute(`
INSERT INTO auth_token (token, user_id, user_type, site_id, expires_at, refresh_token, refresh_expires_at, ip_address, user_agent, device_type, is_revoked, revoked_at, revoked_reason)
VALUES
('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test_admin_token', 1, 'admin', 0, DATE_ADD(NOW(), INTERVAL 7 DAY), 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test_admin_refresh', DATE_ADD(NOW(), INTERVAL 30 DAY), '127.0.0.1', 'Mozilla/5.0', 'web', 0, NULL, NULL),
('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test_member_token', 1, 'member', 0, DATE_ADD(NOW(), INTERVAL 7 DAY), 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test_member_refresh', DATE_ADD(NOW(), INTERVAL 30 DAY), '127.0.0.1', 'Mozilla/5.0', 'web', 0, NULL, NULL)
`);
console.log(' ✅ 测试Token插入成功');
} catch (error) {
console.error(` ❌ Auth模块数据插入失败: ${error.message}`);
}
}
// 运行脚本
insertTestData();