31 lines
870 B
JavaScript
31 lines
870 B
JavaScript
/**
|
|
* 初始化默认 admin 账号
|
|
* 用法: node server/initAdmin.js
|
|
*/
|
|
require('dotenv').config();
|
|
const mongoose = require('mongoose');
|
|
const Admin = require('./models/Admin');
|
|
const { hashPassword } = require('./utils/password');
|
|
|
|
async function init() {
|
|
await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/e-scooter-rental');
|
|
|
|
const existing = await Admin.findOne({ username: 'admin' });
|
|
if (existing) {
|
|
console.log('⚠️ Admin already exists:', existing.username);
|
|
} else {
|
|
const hashed = await hashPassword('admin123');
|
|
await Admin.create({
|
|
username: 'admin',
|
|
password: hashed,
|
|
name: '系统管理员',
|
|
role: 'superadmin'
|
|
});
|
|
console.log('✅ Admin created: admin / admin123');
|
|
}
|
|
|
|
await mongoose.disconnect();
|
|
}
|
|
|
|
init().catch(err => { console.error(err); process.exit(1); });
|