119 lines
2.7 KiB
JavaScript
119 lines
2.7 KiB
JavaScript
const { Sequelize, DataTypes } = require('sequelize');
|
||
const bcrypt = require('bcryptjs');
|
||
require('dotenv').config();
|
||
|
||
// 数据库配置
|
||
const sequelize = new Sequelize(
|
||
process.env.DB_NAME || 'niumall',
|
||
process.env.DB_USERNAME || 'root',
|
||
process.env.DB_PASSWORD || 'aiotAiot123!',
|
||
{
|
||
host: process.env.DB_HOST || '129.211.213.226',
|
||
port: process.env.DB_PORT || 9527,
|
||
dialect: 'mysql',
|
||
logging: false,
|
||
dialectOptions: {
|
||
connectTimeout: 60000
|
||
}
|
||
}
|
||
);
|
||
|
||
// 定义User模型(根据实际表结构)
|
||
const User = sequelize.define('User', {
|
||
id: {
|
||
type: DataTypes.INTEGER,
|
||
primaryKey: true,
|
||
autoIncrement: true
|
||
},
|
||
openid: {
|
||
type: DataTypes.STRING,
|
||
allowNull: false,
|
||
defaultValue: ''
|
||
},
|
||
nickname: {
|
||
type: DataTypes.STRING,
|
||
allowNull: false
|
||
},
|
||
avatar: {
|
||
type: DataTypes.STRING,
|
||
allowNull: true
|
||
},
|
||
gender: {
|
||
type: DataTypes.ENUM('male', 'female', 'other'),
|
||
allowNull: true
|
||
},
|
||
birthday: {
|
||
type: DataTypes.DATE,
|
||
allowNull: true
|
||
},
|
||
phone: {
|
||
type: DataTypes.STRING,
|
||
allowNull: true
|
||
},
|
||
email: {
|
||
type: DataTypes.STRING,
|
||
allowNull: true
|
||
},
|
||
uuid: {
|
||
type: DataTypes.STRING,
|
||
allowNull: true
|
||
},
|
||
created_at: {
|
||
type: DataTypes.DATE,
|
||
allowNull: false
|
||
},
|
||
updated_at: {
|
||
type: DataTypes.DATE,
|
||
allowNull: false
|
||
}
|
||
}, {
|
||
tableName: 'users',
|
||
timestamps: true,
|
||
createdAt: 'created_at',
|
||
updatedAt: 'updated_at'
|
||
});
|
||
|
||
async function createAdminUser() {
|
||
try {
|
||
console.log('Testing database connection...');
|
||
await sequelize.authenticate();
|
||
console.log('Database connection successful!');
|
||
|
||
// 检查是否已存在管理员用户
|
||
const existingAdmin = await User.findOne({
|
||
where: {
|
||
nickname: 'admin'
|
||
}
|
||
});
|
||
|
||
if (existingAdmin) {
|
||
console.log('Admin user already exists:', existingAdmin.id, existingAdmin.nickname);
|
||
console.log('Admin user password cannot be updated with this script because the table structure does not have a password field');
|
||
return;
|
||
}
|
||
|
||
// 创建管理员用户
|
||
console.log('Creating admin user...');
|
||
const adminUser = await User.create({
|
||
openid: 'admin_openid',
|
||
nickname: 'admin',
|
||
email: 'admin@example.com',
|
||
phone: '13800138000',
|
||
uuid: 'admin-uuid-' + Date.now(),
|
||
created_at: new Date(),
|
||
updated_at: new Date()
|
||
});
|
||
|
||
console.log('Admin user created successfully:', adminUser.id, adminUser.nickname);
|
||
|
||
} catch (error) {
|
||
console.error('Error creating admin user:', error.message);
|
||
if (error.original) {
|
||
console.error('Original error:', error.original.message);
|
||
}
|
||
} finally {
|
||
await sequelize.close();
|
||
}
|
||
}
|
||
|
||
createAdminUser(); |