/** * 初始化默认 store(门店)账号 * 用法: node server/initStore.js * 注意:需要先有 Store 记录才能创建门店账号 */ require('dotenv').config(); const mongoose = require('mongoose'); const Store = require('./models/Store'); const StoreAuth = require('./models/StoreAuth'); const { hashPassword } = require('./utils/password'); async function init() { await mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost:27017/e-scooter-rental'); // 查找第一个已审批的门店 const store = await Store.findOne({ approvalStatus: '已通过' }); if (!store) { console.log('⚠️ 没有找到已审批的门店,请先创建门店记录'); await mongoose.disconnect(); return; } const existing = await StoreAuth.findOne({ storeId: store.storeId }); if (existing) { console.log('⚠️ StoreAuth already exists for store:', store.name, '/ storeId:', store.storeId); } else { const hashed = await hashPassword('store123'); await StoreAuth.create({ storeId: store.storeId, username: store.storeId, // 默认用户名 = 门店编号 password: hashed, name: store.manager || '门店管理员', status: 'active' }); console.log('✅ StoreAuth created for:', store.name); console.log(' username:', store.storeId, '/ password: store123'); } await mongoose.disconnect(); } init().catch(err => { console.error(err); process.exit(1); });