You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.0 KiB
71 lines
2.0 KiB
// 查询数据库中的用户和商品信息
|
|
require('dotenv').config();
|
|
const { Sequelize } = require('sequelize');
|
|
|
|
// 创建数据库连接
|
|
const sequelize = new Sequelize(
|
|
process.env.DB_DATABASE || 'wechat_app',
|
|
process.env.DB_USER || 'root',
|
|
process.env.DB_PASSWORD === undefined ? null : process.env.DB_PASSWORD,
|
|
{
|
|
host: process.env.DB_HOST || 'localhost',
|
|
port: process.env.DB_PORT || 3306,
|
|
dialect: 'mysql',
|
|
timezone: '+08:00' // 设置时区为UTC+8
|
|
}
|
|
);
|
|
|
|
// 执行查询
|
|
async function queryDatabase() {
|
|
try {
|
|
// 测试连接
|
|
await sequelize.authenticate();
|
|
console.log('✅ 数据库连接成功');
|
|
|
|
// 查询用户信息
|
|
const users = await sequelize.query('SELECT * FROM users LIMIT 10', { type: sequelize.QueryTypes.SELECT });
|
|
console.log('\n👥 用户列表:');
|
|
console.log(users.map(u => ({
|
|
id: u.id,
|
|
openid: u.openid,
|
|
userId: u.userId,
|
|
type: u.type
|
|
})));
|
|
|
|
// 查询商品信息,特别是拒绝状态的商品
|
|
const products = await sequelize.query(
|
|
'SELECT productId, sellerId, productName, status, rejectReason, created_at FROM products LIMIT 20',
|
|
{ type: sequelize.QueryTypes.SELECT }
|
|
);
|
|
|
|
console.log('\n🛒 商品列表:');
|
|
console.log(products.map(p => ({
|
|
productId: p.productId,
|
|
sellerId: p.sellerId,
|
|
productName: p.productName,
|
|
status: p.status,
|
|
rejectReason: p.rejectReason,
|
|
created_at: p.created_at
|
|
})));
|
|
|
|
// 特别列出拒绝状态的商品
|
|
const rejectedProducts = products.filter(p => p.status === 'rejected');
|
|
console.log('\n❌ 审核拒绝的商品:');
|
|
console.log(rejectedProducts.map(p => ({
|
|
productId: p.productId,
|
|
sellerId: p.sellerId,
|
|
productName: p.productName,
|
|
status: p.status,
|
|
rejectReason: p.rejectReason
|
|
})));
|
|
|
|
} catch (error) {
|
|
console.error('❌ 查询失败:', error.message);
|
|
} finally {
|
|
// 关闭连接
|
|
await sequelize.close();
|
|
}
|
|
}
|
|
|
|
// 运行查询
|
|
queryDatabase();
|