更新完善页面
This commit is contained in:
@@ -1,210 +0,0 @@
|
||||
const { query } = require('../config/database')
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
console.log('Inserting mock orders...')
|
||||
|
||||
const orders = [
|
||||
{
|
||||
id: 'ORD202401010001',
|
||||
user_id: 1,
|
||||
status: 'completed',
|
||||
total_price: 156.80,
|
||||
cart: JSON.stringify([
|
||||
{ goods_id: 1, name: '红富士苹果', price: 12.80, quantity: 5, unit: '斤' },
|
||||
{ goods_id: 5, name: '土鸡蛋', price: 19.90, quantity: 2, unit: '盒' },
|
||||
{ goods_id: 15, name: '可口可乐', price: 3.50, quantity: 6, unit: '瓶' }
|
||||
]),
|
||||
user_info: JSON.stringify({ phone: '13800138000', name: '张三' })
|
||||
},
|
||||
{
|
||||
id: 'ORD202401020002',
|
||||
user_id: 1,
|
||||
status: 'paid',
|
||||
total_price: 89.70,
|
||||
cart: JSON.stringify([
|
||||
{ goods_id: 3, name: '进口车厘子', price: 59.90, quantity: 1, unit: '斤' },
|
||||
{ goods_id: 8, name: '蒙牛纯牛奶', price: 59.90, quantity: 1, unit: '箱' }
|
||||
]),
|
||||
user_info: JSON.stringify({ phone: '13800138000', name: '张三' })
|
||||
},
|
||||
{
|
||||
id: 'ORD202401030003',
|
||||
user_id: 2,
|
||||
status: 'pending',
|
||||
total_price: 45.60,
|
||||
cart: JSON.stringify([
|
||||
{ goods_id: 7, name: '金龙鱼花生油', price: 89.90, quantity: 1, unit: '桶' },
|
||||
{ goods_id: 12, name: '卫龙辣条', price: 5.50, quantity: 3, unit: '包' }
|
||||
]),
|
||||
user_info: JSON.stringify({ phone: '13900139000', name: '李四' })
|
||||
},
|
||||
{
|
||||
id: 'ORD202401040004',
|
||||
user_id: 2,
|
||||
status: 'completed',
|
||||
total_price: 123.40,
|
||||
cart: JSON.stringify([
|
||||
{ goods_id: 10, name: '农夫山泉', price: 2.00, quantity: 12, unit: '瓶' },
|
||||
{ goods_id: 14, name: '青岛啤酒', price: 5.00, quantity: 12, unit: '瓶' },
|
||||
{ goods_id: 6, name: '五花肉', price: 28.80, quantity: 2, unit: '斤' }
|
||||
]),
|
||||
user_info: JSON.stringify({ phone: '13900139000', name: '李四' })
|
||||
},
|
||||
{
|
||||
id: 'ORD202401050005',
|
||||
user_id: 1,
|
||||
status: 'cancelled',
|
||||
total_price: 299.90,
|
||||
cart: JSON.stringify([
|
||||
{ goods_id: 3, name: '进口车厘子', price: 59.90, quantity: 5, unit: '斤' }
|
||||
]),
|
||||
user_info: JSON.stringify({ phone: '13800138000', name: '张三' })
|
||||
}
|
||||
]
|
||||
|
||||
for (const order of orders) {
|
||||
await query(
|
||||
'INSERT INTO orders (id, user_id, status, total_price, cart, user_info) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE status=VALUES(status)',
|
||||
[order.id, order.user_id, order.status, order.total_price, order.cart, order.user_info]
|
||||
)
|
||||
}
|
||||
|
||||
console.log('Inserting mock stock...')
|
||||
|
||||
const stockItems = [
|
||||
{ goods_id: 1, quantity: 100, warehouse: '默认仓库' },
|
||||
{ goods_id: 2, quantity: 200, warehouse: '默认仓库' },
|
||||
{ goods_id: 3, quantity: 50, warehouse: '默认仓库' },
|
||||
{ goods_id: 4, quantity: 80, warehouse: '默认仓库' },
|
||||
{ goods_id: 5, quantity: 120, warehouse: '默认仓库' },
|
||||
{ goods_id: 6, quantity: 60, warehouse: '默认仓库' },
|
||||
{ goods_id: 7, quantity: 40, warehouse: '默认仓库' },
|
||||
{ goods_id: 8, quantity: 30, warehouse: '默认仓库' },
|
||||
{ goods_id: 9, quantity: 150, warehouse: '默认仓库' },
|
||||
{ goods_id: 10, quantity: 300, warehouse: '默认仓库' },
|
||||
{ goods_id: 11, quantity: 200, warehouse: '默认仓库' },
|
||||
{ goods_id: 12, quantity: 500, warehouse: '默认仓库' },
|
||||
{ goods_id: 13, quantity: 200, warehouse: '默认仓库' },
|
||||
{ goods_id: 14, quantity: 150, warehouse: '默认仓库' },
|
||||
{ goods_id: 15, quantity: 500, warehouse: '默认仓库' }
|
||||
]
|
||||
|
||||
for (const stock of stockItems) {
|
||||
await query(
|
||||
'INSERT INTO stock (goods_id, quantity, warehouse) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE quantity=VALUES(quantity)',
|
||||
[stock.goods_id, stock.quantity, stock.warehouse]
|
||||
)
|
||||
}
|
||||
|
||||
console.log('Inserting mock suppliers...')
|
||||
|
||||
const suppliers = [
|
||||
{ name: '鲜果源供应链', contact: '王经理', phone: '13800001001', address: '广州市白云区江南批发市场A区' },
|
||||
{ name: '旺旺食品总代理', contact: '李经理', phone: '13800001002', address: '广州市天河区中山大道88号' },
|
||||
{ name: '百事饮品华南分公司', contact: '陈经理', phone: '13800001003', address: '广州市番禺区南村镇兴业大道' }
|
||||
]
|
||||
|
||||
for (const s of suppliers) {
|
||||
await query(
|
||||
'INSERT INTO suppliers (name, contact, phone, address) VALUES (?, ?, ?, ?) ON DUPLICATE KEY UPDATE name=VALUES(name)',
|
||||
[s.name, s.contact, s.phone, s.address]
|
||||
)
|
||||
}
|
||||
|
||||
console.log('Inserting mock purchases...')
|
||||
|
||||
const purchases = [
|
||||
{ supplier_name: '鲜果源供应链', total: 2560.00, status: 1, remarks: '周常补货' },
|
||||
{ supplier_name: '旺旺食品总代理', total: 1800.50, status: 0, remarks: '' }
|
||||
]
|
||||
|
||||
for (const p of purchases) {
|
||||
const supplier = await query('SELECT id FROM suppliers WHERE name = ?', [p.supplier_name])
|
||||
const supplierId = supplier.length > 0 ? supplier[0].id : null
|
||||
const purchaseResult = await query(
|
||||
'INSERT INTO purchases (supplier_id, supplier_name, total, status, remarks) VALUES (?, ?, ?, ?, ?)',
|
||||
[supplierId, p.supplier_name, p.total, p.status, p.remarks || '']
|
||||
)
|
||||
|
||||
if (p.supplier_name === '鲜果源供应链') {
|
||||
await query(
|
||||
'INSERT INTO purchase_items (purchase_id, goods_id, goods_name, quantity, purchase_price) VALUES (?, ?, ?, ?, ?)',
|
||||
[purchaseResult.insertId, 1, '红富士苹果', 50, 10.00]
|
||||
)
|
||||
await query(
|
||||
'INSERT INTO purchase_items (purchase_id, goods_id, goods_name, quantity, purchase_price) VALUES (?, ?, ?, ?, ?)',
|
||||
[purchaseResult.insertId, 3, '进口车厘子', 20, 45.00]
|
||||
)
|
||||
} else if (p.supplier_name === '旺旺食品总代理') {
|
||||
await query(
|
||||
'INSERT INTO purchase_items (purchase_id, goods_id, goods_name, quantity, purchase_price) VALUES (?, ?, ?, ?, ?)',
|
||||
[purchaseResult.insertId, 12, '卫龙辣条', 100, 3.80]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Inserting mock points goods...')
|
||||
|
||||
const pointsGoods = [
|
||||
{ name: '定制帆布袋', points: 200, stock: 50, image: '', description: '环保帆布袋' },
|
||||
{ name: '玻璃水杯', points: 500, stock: 30, image: '', description: '350ml 双层玻璃杯' },
|
||||
{ name: '50元优惠券', points: 1000, stock: 20, image: '', description: '满100可用' }
|
||||
]
|
||||
|
||||
for (const g of pointsGoods) {
|
||||
await query(
|
||||
'INSERT INTO points_goods (name, points, stock, image, description) VALUES (?, ?, ?, ?, ?)',
|
||||
[g.name, g.points, g.stock, g.image, g.description]
|
||||
)
|
||||
}
|
||||
|
||||
console.log('Inserting mock points logs...')
|
||||
|
||||
const pointsLogs = [
|
||||
{ user_id: 1, type: 'earn', amount: 1000, description: '新用户注册赠送' },
|
||||
{ user_id: 1, type: 'spend', amount: 200, description: '积分兑换商品' },
|
||||
{ user_id: 1, type: 'earn', amount: 50, description: '购物满100元奖励' },
|
||||
{ user_id: 2, type: 'earn', amount: 500, description: '新用户注册赠送' },
|
||||
{ user_id: 2, type: 'earn', amount: 30, description: '购物满50元奖励' }
|
||||
]
|
||||
|
||||
for (const log of pointsLogs) {
|
||||
await query(
|
||||
'INSERT INTO points_logs (user_id, type, amount, description) VALUES (?, ?, ?, ?)',
|
||||
[log.user_id, log.type, log.amount, log.description]
|
||||
)
|
||||
}
|
||||
|
||||
console.log('✅ Mock data inserted successfully!')
|
||||
|
||||
console.log('\n--- Data Summary ---')
|
||||
const categories = await query('SELECT COUNT(*) as count FROM categories')
|
||||
const goods = await query('SELECT COUNT(*) as count FROM goods')
|
||||
const users = await query('SELECT COUNT(*) as count FROM users')
|
||||
const ordersCount = await query('SELECT COUNT(*) as count FROM orders')
|
||||
const stockCount = await query('SELECT COUNT(*) as count FROM stock')
|
||||
const logsCount = await query('SELECT COUNT(*) as count FROM points_logs')
|
||||
|
||||
const suppliersCount = await query('SELECT COUNT(*) as count FROM suppliers')
|
||||
const purchasesCount = await query('SELECT COUNT(*) as count FROM purchases')
|
||||
const pointsGoodsCount = await query('SELECT COUNT(*) as count FROM points_goods')
|
||||
|
||||
console.log(`分类: ${categories[0]?.count || 0} 条`)
|
||||
console.log(`商品: ${goods[0]?.count || 0} 条`)
|
||||
console.log(`用户: ${users[0]?.count || 0} 条`)
|
||||
console.log(`订单: ${ordersCount[0]?.count || 0} 条`)
|
||||
console.log(`库存: ${stockCount[0]?.count || 0} 条`)
|
||||
console.log(`积分记录: ${logsCount[0]?.count || 0} 条`)
|
||||
console.log(`供应商: ${suppliersCount[0]?.count || 0} 条`)
|
||||
console.log(`采购单: ${purchasesCount[0]?.count || 0} 条`)
|
||||
console.log(`积分商品: ${pointsGoodsCount[0]?.count || 0} 条`)
|
||||
|
||||
process.exit(0)
|
||||
} catch (error) {
|
||||
console.error('Failed to insert mock data:', error)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
run()
|
||||
@@ -0,0 +1,71 @@
|
||||
const crypto = require('crypto')
|
||||
const readline = require('readline')
|
||||
const { query } = require('../config/database')
|
||||
require('dotenv').config()
|
||||
|
||||
const DEFAULT_PASSWORD = process.env.DEFAULT_PASSWORD || '123456'
|
||||
const DEFAULT_PHONE = process.env.ADMIN_PHONE || '13800000000'
|
||||
const DEFAULT_NAME = process.env.ADMIN_NAME || '系统管理员'
|
||||
|
||||
function md5(str) {
|
||||
return crypto.createHash('md5').update(str).digest('hex')
|
||||
}
|
||||
|
||||
function ask(question) {
|
||||
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
|
||||
return new Promise(resolve => rl.question(question, ans => { rl.close(); resolve(ans) }))
|
||||
}
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
const phoneArg = process.argv[2]
|
||||
const passwordArg = process.argv[3]
|
||||
|
||||
let phone = phoneArg
|
||||
let password = passwordArg
|
||||
|
||||
if (!phone) {
|
||||
phone = (await ask(`管理员手机号 [${DEFAULT_PHONE}]: `)) || DEFAULT_PHONE
|
||||
}
|
||||
if (!password) {
|
||||
password = (await ask(`管理员密码 [${DEFAULT_PASSWORD}]: `)) || DEFAULT_PASSWORD
|
||||
}
|
||||
|
||||
if (!/^1\d{10}$/.test(phone)) {
|
||||
console.error('手机号格式错误')
|
||||
process.exit(1)
|
||||
}
|
||||
if (password.length < 8) {
|
||||
console.error('密码至少 8 位')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const existing = await query('SELECT id, role FROM users WHERE phone = ?', [phone])
|
||||
if (existing.length > 0) {
|
||||
if (existing[0].role === 2) {
|
||||
console.log(`该手机号已是管理员 (id=${existing[0].id})`)
|
||||
process.exit(0)
|
||||
}
|
||||
await query('UPDATE users SET role = 2, password = ? WHERE id = ?', [md5(password), existing[0].id])
|
||||
console.log(`已将用户 ${phone} 提升为管理员,密码已重置`)
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
const result = await query(
|
||||
'INSERT INTO users (phone, password, name, avatar, points, role, status) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||
[phone, md5(password), DEFAULT_NAME, '', 0, 2, 1]
|
||||
)
|
||||
console.log(`\n✅ 管理员创建成功`)
|
||||
console.log(` id : ${result.insertId}`)
|
||||
console.log(` phone : ${phone}`)
|
||||
console.log(` name : ${DEFAULT_NAME}`)
|
||||
console.log(` role : 2 (管理员)`)
|
||||
console.log(` password: ${password} (首次登录后请尽快修改)\n`)
|
||||
process.exit(0)
|
||||
} catch (error) {
|
||||
console.error('创建管理员失败:', error)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
run()
|
||||
Reference in New Issue
Block a user