e-scooter-rental-system/server/initStore.js

43 lines
1.4 KiB
JavaScript
Raw Permalink 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.

/**
* 初始化默认 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); });