Ai config
This commit is contained in:
@@ -1,101 +0,0 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
const config = require('../config/database');
|
||||
const { DOMAIN_CONFIG } = require('../config/domain');
|
||||
const { toRelativeUrl } = require('../utils/image-url');
|
||||
|
||||
async function convertToRelativeUrls() {
|
||||
try {
|
||||
const connection = await mysql.createConnection(config);
|
||||
console.log('✅ 数据库连接成功\n');
|
||||
|
||||
console.log('🔍 检查数据库中的图片URL...');
|
||||
const [goods] = await connection.execute('SELECT id, images FROM goods');
|
||||
|
||||
console.log(`找到 ${goods.length} 条商品记录\n`);
|
||||
|
||||
let updatedCount = 0;
|
||||
|
||||
// 更新图片URL
|
||||
for (const item of goods) {
|
||||
if (item.images) {
|
||||
let oldImages = item.images;
|
||||
let newImages = oldImages;
|
||||
|
||||
try {
|
||||
// 尝试解析JSON
|
||||
let imagesArray = JSON.parse(oldImages);
|
||||
|
||||
// 转换每个URL
|
||||
let convertedArray = imagesArray.map(url => toRelativeUrl(url));
|
||||
|
||||
newImages = JSON.stringify(convertedArray);
|
||||
|
||||
if (oldImages !== newImages) {
|
||||
await connection.execute(
|
||||
'UPDATE goods SET images = ? WHERE id = ?',
|
||||
[newImages, item.id]
|
||||
);
|
||||
|
||||
console.log(`商品ID ${item.id}:`);
|
||||
console.log(` 旧: ${oldImages}`);
|
||||
console.log(` 新: ${newImages}`);
|
||||
console.log('---');
|
||||
updatedCount++;
|
||||
}
|
||||
} catch (e) {
|
||||
// 如果不是有效JSON,直接尝试转换
|
||||
let converted = toRelativeUrl(oldImages);
|
||||
if (converted !== oldImages) {
|
||||
newImages = converted;
|
||||
await connection.execute(
|
||||
'UPDATE goods SET images = ? WHERE id = ?',
|
||||
[newImages, item.id]
|
||||
);
|
||||
console.log(`商品ID ${item.id} (非JSON格式):`);
|
||||
console.log(` 旧: ${oldImages}`);
|
||||
console.log(` 新: ${newImages}`);
|
||||
console.log('---');
|
||||
updatedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 检查积分商品
|
||||
const [pointsGoods] = await connection.execute('SELECT id, images FROM points_goods');
|
||||
if (pointsGoods.length > 0) {
|
||||
console.log(`\n检查 ${pointsGoods.length} 条积分商品记录...`);
|
||||
for (const item of pointsGoods) {
|
||||
if (item.images) {
|
||||
let oldImages = item.images;
|
||||
let newImages = oldImages;
|
||||
|
||||
try {
|
||||
let imagesArray = JSON.parse(oldImages);
|
||||
let convertedArray = imagesArray.map(url => toRelativeUrl(url));
|
||||
newImages = JSON.stringify(convertedArray);
|
||||
} catch (e) {
|
||||
newImages = toRelativeUrl(oldImages);
|
||||
}
|
||||
|
||||
if (oldImages !== newImages) {
|
||||
await connection.execute(
|
||||
'UPDATE points_goods SET images = ? WHERE id = ?',
|
||||
[newImages, item.id]
|
||||
);
|
||||
console.log(`积分商品ID ${item.id}: 已更新`);
|
||||
updatedCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await connection.end();
|
||||
console.log(`\n✅ 完成!共更新了 ${updatedCount} 条记录`);
|
||||
} catch (error) {
|
||||
console.error('❌ 更新失败:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
convertToRelativeUrls();
|
||||
@@ -1,70 +0,0 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
const config = require('../config/database');
|
||||
const { DOMAIN_CONFIG } = require('../config/domain');
|
||||
|
||||
async function fixImageUrls() {
|
||||
try {
|
||||
const connection = await mysql.createConnection(config);
|
||||
console.log('✅ 数据库连接成功\n');
|
||||
|
||||
// 获取当前配置的域名
|
||||
const targetDomain = DOMAIN_CONFIG.BASE_URL;
|
||||
console.log(`🎯 目标域名: ${targetDomain}\n`);
|
||||
|
||||
// 查找包含旧地址的记录
|
||||
console.log('🔍 检查数据库中的图片URL...');
|
||||
const [goods] = await connection.execute('SELECT id, images FROM goods WHERE images LIKE "%localhost%"');
|
||||
|
||||
console.log(`找到 ${goods.length} 条包含 localhost 的记录\n`);
|
||||
|
||||
// 更新图片URL
|
||||
for (const item of goods) {
|
||||
if (item.images) {
|
||||
const oldImages = item.images;
|
||||
// 替换所有 localhost:3000 和 localhost:3006
|
||||
let newImages = oldImages
|
||||
.replace(/http:\/\/localhost:3000/g, targetDomain)
|
||||
.replace(/http:\/\/localhost:3006/g, targetDomain);
|
||||
|
||||
await connection.execute(
|
||||
'UPDATE goods SET images = ? WHERE id = ?',
|
||||
[newImages, item.id]
|
||||
);
|
||||
|
||||
console.log(`商品ID ${item.id}:`);
|
||||
console.log(` 旧: ${oldImages}`);
|
||||
console.log(` 新: ${newImages}`);
|
||||
console.log('---');
|
||||
}
|
||||
}
|
||||
|
||||
// 检查其他表(如有需要)
|
||||
const [pointsGoods] = await connection.execute('SELECT id, images FROM points_goods WHERE images LIKE "%localhost%"');
|
||||
if (pointsGoods.length > 0) {
|
||||
console.log(`\n找到 ${pointsGoods.length} 条积分商品记录...`);
|
||||
for (const item of pointsGoods) {
|
||||
if (item.images) {
|
||||
const oldImages = item.images;
|
||||
let newImages = oldImages
|
||||
.replace(/http:\/\/localhost:3000/g, targetDomain)
|
||||
.replace(/http:\/\/localhost:3006/g, targetDomain);
|
||||
|
||||
await connection.execute(
|
||||
'UPDATE points_goods SET images = ? WHERE id = ?',
|
||||
[newImages, item.id]
|
||||
);
|
||||
|
||||
console.log(`积分商品ID ${item.id}: 已更新`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await connection.end();
|
||||
console.log('\n✅ 所有图片URL更新完成!');
|
||||
} catch (error) {
|
||||
console.error('❌ 更新失败:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
fixImageUrls();
|
||||
@@ -1,89 +0,0 @@
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const mysql = require('mysql2/promise')
|
||||
require('dotenv').config({ path: path.join(__dirname, '../.env') })
|
||||
|
||||
const categoriesData = require('../data/categories.json')
|
||||
const goodsData = require('../data/goods.json')
|
||||
const usersData = require('../data/users.json')
|
||||
|
||||
function requireEnv(name, fallback) {
|
||||
const value = process.env[name] || fallback
|
||||
if (!value && !fallback) {
|
||||
throw new Error(`Missing ${name} in .env`)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
const config = {
|
||||
host: requireEnv('DB_HOST'),
|
||||
port: parseInt(requireEnv('DB_PORT', '3306')),
|
||||
user: 'root',
|
||||
password: requireEnv('DB_ROOT_PASSWORD'),
|
||||
database: requireEnv('DB_NAME', '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()
|
||||
@@ -1,47 +0,0 @@
|
||||
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()
|
||||
@@ -1,50 +0,0 @@
|
||||
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()
|
||||
@@ -1,36 +0,0 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
const path = require('path');
|
||||
require('dotenv').config({ path: path.join(__dirname, '../.env') });
|
||||
|
||||
const config = {
|
||||
host: process.env.DB_HOST || 'localhost',
|
||||
port: parseInt(process.env.DB_PORT || '3306'),
|
||||
user: process.env.DB_USER || 'root',
|
||||
password: process.env.DB_PASSWORD || '',
|
||||
database: process.env.DB_NAME || 'miniprogram'
|
||||
};
|
||||
|
||||
async function testConnection() {
|
||||
try {
|
||||
console.log('尝试连接到:', config.host, config.port);
|
||||
const connection = await mysql.createConnection(config);
|
||||
console.log('✅ 数据库连接成功\n');
|
||||
|
||||
// 检查所有商品的图片URL
|
||||
const [goods] = await connection.execute('SELECT id, name, images FROM goods LIMIT 10');
|
||||
console.log(`找到 ${goods.length} 条商品记录\n`);
|
||||
|
||||
for (const item of goods) {
|
||||
console.log(`商品ID ${item.id}: ${item.name}`);
|
||||
console.log(` 图片: ${item.images}`);
|
||||
}
|
||||
|
||||
await connection.end();
|
||||
console.log('\n✅ 检查完成');
|
||||
} catch (error) {
|
||||
console.error('❌ 连接失败:', error.message);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
testConnection();
|
||||
Reference in New Issue
Block a user