Initial commit: upload services folder
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const mysql = require('mysql2/promise')
|
||||
|
||||
const categoriesData = require('../data/categories.json')
|
||||
const goodsData = require('../data/goods.json')
|
||||
const usersData = require('../data/users.json')
|
||||
|
||||
const config = {
|
||||
host: '110.42.255.239',
|
||||
port: 3306,
|
||||
user: 'root',
|
||||
password: 'Wentian9588.',
|
||||
database: 'miniprogram'
|
||||
}
|
||||
|
||||
async function run() {
|
||||
let connection = null
|
||||
try {
|
||||
console.log('Connecting to MySQL...')
|
||||
connection = await mysql.createConnection({
|
||||
host: config.host,
|
||||
port: config.port,
|
||||
user: config.user,
|
||||
password: config.password
|
||||
})
|
||||
|
||||
console.log('Creating database...')
|
||||
await connection.query(`CREATE DATABASE IF NOT EXISTS ${config.database} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`)
|
||||
await connection.query(`USE ${config.database}`)
|
||||
|
||||
console.log('Granting permissions...')
|
||||
await connection.query('GRANT ALL PRIVILEGES ON miniprogram.* TO admin@"%";')
|
||||
await connection.query('FLUSH PRIVILEGES;')
|
||||
|
||||
console.log('Creating tables...')
|
||||
const schema = fs.readFileSync(path.join(__dirname, '../config/schema.sql'), 'utf8')
|
||||
const statements = schema.split(';').filter(s => s.trim())
|
||||
|
||||
for (const statement of statements) {
|
||||
await connection.query(statement)
|
||||
}
|
||||
|
||||
console.log('Inserting categories...')
|
||||
for (const cat of categoriesData) {
|
||||
await connection.query(
|
||||
'INSERT INTO categories (id, name, icon, is_show, sort_order) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name=VALUES(name), icon=VALUES(icon), is_show=VALUES(is_show), sort_order=VALUES(sort_order)',
|
||||
[cat.id, cat.name, cat.icon, cat.is_show, cat.sort_order]
|
||||
)
|
||||
}
|
||||
|
||||
console.log('Inserting goods...')
|
||||
for (const good of goodsData) {
|
||||
await connection.query(
|
||||
'INSERT INTO goods (id, name, price, original_price, unit, category_id, images, stock, sales, is_hot, is_new, pricing_type, description) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name=VALUES(name), price=VALUES(price), original_price=VALUES(original_price)',
|
||||
[good.id, good.name, good.price, good.original_price, good.unit, good.category_id, good.images, good.stock, good.sales, good.is_hot, good.is_new, good.pricing_type, good.description]
|
||||
)
|
||||
}
|
||||
|
||||
console.log('Inserting users...')
|
||||
for (const user of usersData) {
|
||||
await connection.query(
|
||||
'INSERT INTO users (id, phone, password, name, avatar, points) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name=VALUES(name), points=VALUES(points)',
|
||||
[user.id, user.phone, user.password, user.name, user.avatar, user.points]
|
||||
)
|
||||
}
|
||||
|
||||
console.log('Database initialization completed successfully!')
|
||||
process.exit(0)
|
||||
} catch (error) {
|
||||
console.error('Database initialization failed:', error)
|
||||
process.exit(1)
|
||||
} finally {
|
||||
if (connection) {
|
||||
await connection.end()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
run()
|
||||
@@ -0,0 +1,46 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const { initDatabase, query } = require('../config/database')
|
||||
|
||||
const categoriesData = require('../data/categories.json')
|
||||
const goodsData = require('../data/goods.json')
|
||||
const usersData = require('../data/users.json')
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
console.log('Initializing database...')
|
||||
await initDatabase()
|
||||
|
||||
console.log('Inserting categories...')
|
||||
for (const cat of categoriesData) {
|
||||
await query(
|
||||
'INSERT INTO categories (id, name, icon, is_show, sort_order) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name=VALUES(name), icon=VALUES(icon), is_show=VALUES(is_show), sort_order=VALUES(sort_order)',
|
||||
[cat.id, cat.name, cat.icon, cat.is_show, cat.sort_order]
|
||||
)
|
||||
}
|
||||
|
||||
console.log('Inserting goods...')
|
||||
for (const good of goodsData) {
|
||||
await query(
|
||||
'INSERT INTO goods (id, name, price, original_price, unit, category_id, images, stock, sales, is_hot, is_new, pricing_type, description) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name=VALUES(name), price=VALUES(price), original_price=VALUES(original_price)',
|
||||
[good.id, good.name, good.price, good.original_price, good.unit, good.category_id, good.images, good.stock, good.sales, good.is_hot, good.is_new, good.pricing_type, good.description]
|
||||
)
|
||||
}
|
||||
|
||||
console.log('Inserting users...')
|
||||
for (const user of usersData) {
|
||||
await query(
|
||||
'INSERT INTO users (id, phone, password, name, avatar, points) VALUES (?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE name=VALUES(name), points=VALUES(points)',
|
||||
[user.id, user.phone, user.password, user.name, user.avatar, user.points]
|
||||
)
|
||||
}
|
||||
|
||||
console.log('Database initialization completed successfully!')
|
||||
process.exit(0)
|
||||
} catch (error) {
|
||||
console.error('Database initialization failed:', error)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
run()
|
||||
@@ -0,0 +1,141 @@
|
||||
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 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')
|
||||
|
||||
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} 条`)
|
||||
|
||||
process.exit(0)
|
||||
} catch (error) {
|
||||
console.error('Failed to insert mock data:', error)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
run()
|
||||
@@ -0,0 +1,47 @@
|
||||
const crypto = require('crypto')
|
||||
const { query } = require('../config/database')
|
||||
|
||||
function md5(str) {
|
||||
return crypto.createHash('md5').update(str).digest('hex')
|
||||
}
|
||||
|
||||
async function registerStaff() {
|
||||
try {
|
||||
const phone = '12370109282'
|
||||
const name = '董海洋'
|
||||
const password = md5('123456')
|
||||
|
||||
const existing = await query('SELECT * FROM users WHERE phone = ?', [phone])
|
||||
|
||||
if (existing.length > 0) {
|
||||
console.log('⚠️ 该手机号已注册')
|
||||
|
||||
const user = existing[0]
|
||||
if (user.role === 1) {
|
||||
console.log('该账号已经是店员身份')
|
||||
} else {
|
||||
console.log('该账号是普通用户,正在升级为店员...')
|
||||
await query('UPDATE users SET role = 1 WHERE phone = ?', [phone])
|
||||
console.log('✅ 已升级为店员身份')
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const result = await query(
|
||||
'INSERT INTO users (phone, password, name, avatar, points, role, status) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||
[phone, password, name, '', 0, 1, 1]
|
||||
)
|
||||
|
||||
console.log('✅ 店员注册成功!')
|
||||
console.log('姓名: ' + name)
|
||||
console.log('手机号: ' + phone)
|
||||
console.log('密码: 123456')
|
||||
|
||||
process.exit(0)
|
||||
} catch (error) {
|
||||
console.error('注册失败:', error)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
registerStaff()
|
||||
@@ -0,0 +1,50 @@
|
||||
const crypto = require('crypto')
|
||||
const { query } = require('../config/database')
|
||||
|
||||
function md5(str) {
|
||||
return crypto.createHash('md5').update(str).digest('hex')
|
||||
}
|
||||
|
||||
async function registerStaff() {
|
||||
try {
|
||||
const phone = '13070109282'
|
||||
const name = '店员' // 可以改成你想要的名字
|
||||
const password = md5('123456') // 默认密码,需要 md5 加密
|
||||
|
||||
// 检查是否已存在
|
||||
const existing = await query('SELECT * FROM users WHERE phone = ?', [phone])
|
||||
|
||||
if (existing.length > 0) {
|
||||
console.log('⚠️ 该手机号已注册')
|
||||
|
||||
// 如果已存在,检查是否是店员
|
||||
const user = existing[0]
|
||||
if (user.role === 1) {
|
||||
console.log('该账号已经是店员身份,无需重复注册')
|
||||
} else {
|
||||
console.log('该账号是普通用户,正在升级为店员...')
|
||||
await query('UPDATE users SET role = 1 WHERE phone = ?', [phone])
|
||||
console.log('✅ 已升级为店员身份')
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 插入店员账号
|
||||
const result = await query(
|
||||
'INSERT INTO users (phone, password, name, avatar, points, role, status) VALUES (?, ?, ?, ?, ?, ?, ?)',
|
||||
[phone, password, name, '', 0, 1, 1] // role=1 表示店员
|
||||
)
|
||||
|
||||
console.log('✅ 店员注册成功!')
|
||||
console.log('手机号: ' + phone)
|
||||
console.log('姓名: ' + name)
|
||||
console.log('默认密码: 123456')
|
||||
|
||||
process.exit(0)
|
||||
} catch (error) {
|
||||
console.error('注册失败:', error)
|
||||
process.exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
registerStaff()
|
||||
Reference in New Issue
Block a user