docs: 更新项目文档,完善需求和技术细节

This commit is contained in:
ylweng
2025-09-01 02:35:41 +08:00
parent e1647902e2
commit ad20888cd4
57 changed files with 961 additions and 156 deletions

View File

@@ -0,0 +1,260 @@
#!/usr/bin/env node
/**
* SQLite数据库初始化脚本
* 用于创建SQLite数据库表结构
*/
const sqlite3 = require('sqlite3').verbose();
const path = require('path');
const databaseConfig = require('../config/database');
class SQLiteInitializer {
constructor() {
this.db = null;
}
/**
* 创建数据库连接
*/
createConnection() {
try {
this.db = new sqlite3.Database(databaseConfig.storage);
console.log('✅ SQLite数据库连接成功');
return true;
} catch (error) {
console.error('❌ SQLite数据库连接失败:', error.message);
return false;
}
}
/**
* 执行SQL语句
*/
run(sql, params = []) {
return new Promise((resolve, reject) => {
this.db.run(sql, params, function(err) {
if (err) {
reject(err);
} else {
resolve(this);
}
});
});
}
/**
* 创建users表
*/
async createUsersTable() {
const sql = `
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
phone VARCHAR(20) UNIQUE,
email VARCHAR(100) UNIQUE,
user_type VARCHAR(20) DEFAULT 'user',
status INTEGER DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`;
try {
await this.run(sql);
console.log('✅ users表创建成功');
return true;
} catch (error) {
console.error('❌ 创建users表失败:', error.message);
return false;
}
}
/**
* 创建orders表
*/
async createOrdersTable() {
const sql = `
CREATE TABLE IF NOT EXISTS orders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
order_number VARCHAR(50) UNIQUE NOT NULL,
user_id INTEGER NOT NULL,
total_amount DECIMAL(10,2) NOT NULL,
payment_status INTEGER DEFAULT 0,
shipping_status INTEGER DEFAULT 0,
shipping_address TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id)
)
`;
try {
await this.run(sql);
console.log('✅ orders表创建成功');
return true;
} catch (error) {
console.error('❌ 创建orders表失败:', error.message);
return false;
}
}
/**
* 创建identifications表
*/
async createIdentificationsTable() {
const sql = `
CREATE TABLE IF NOT EXISTS identifications (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
image_url VARCHAR(255) NOT NULL,
result TEXT NOT NULL,
confidence DECIMAL(5,4) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id)
)
`;
try {
await this.run(sql);
console.log('✅ identifications表创建成功');
return true;
} catch (error) {
console.error('❌ 创建identifications表失败:', error.message);
return false;
}
}
/**
* 创建products表
*/
async createProductsTable() {
const sql = `
CREATE TABLE IF NOT EXISTS products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(100) NOT NULL,
category_id INTEGER,
price DECIMAL(10,2) NOT NULL,
stock INTEGER DEFAULT 0,
image VARCHAR(255),
description TEXT,
status INTEGER DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`;
try {
await this.run(sql);
console.log('✅ products表创建成功');
return true;
} catch (error) {
console.error('❌ 创建products表失败:', error.message);
return false;
}
}
/**
* 插入初始管理员用户
*/
async insertAdminUser() {
const sql = `
INSERT OR IGNORE INTO users (username, password, phone, email, user_type, status)
VALUES (?, ?, ?, ?, ?, ?)
`;
// 密码: admin123 (bcrypt加密)
const hashedPassword = '$2b$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi';
try {
await this.run(sql, [
'admin',
hashedPassword,
'13800138000',
'admin@example.com',
'admin',
1
]);
console.log('✅ 管理员用户创建成功');
return true;
} catch (error) {
console.error('❌ 创建管理员用户失败:', error.message);
return false;
}
}
/**
* 关闭数据库连接
*/
closeConnection() {
if (this.db) {
this.db.close();
console.log('🔌 数据库连接已关闭');
}
}
/**
* 主初始化方法
*/
async initialize() {
console.log('🚀 开始SQLite数据库初始化...');
console.log(`📋 环境: ${process.env.NODE_ENV || 'development'}`);
console.log(`🗄️ 数据库文件: ${databaseConfig.storage}`);
console.log('─'.repeat(50));
// 创建连接
const connected = this.createConnection();
if (!connected) {
process.exit(1);
}
// 创建表
const tablesCreated = await Promise.all([
this.createUsersTable(),
this.createOrdersTable(),
this.createIdentificationsTable(),
this.createProductsTable()
]);
if (tablesCreated.every(result => result)) {
console.log('✅ 所有表创建成功');
} else {
console.error('❌ 表创建过程中出现错误');
this.closeConnection();
process.exit(1);
}
// 插入初始数据
const adminCreated = await this.insertAdminUser();
if (!adminCreated) {
console.warn('⚠️ 管理员用户创建失败或已存在');
}
console.log('✅ SQLite数据库初始化完成');
this.closeConnection();
}
}
// 执行初始化
const initializer = new SQLiteInitializer();
// 处理命令行参数
const args = process.argv.slice(2);
if (args.includes('--help') || args.includes('-h')) {
console.log(`
使用方法: node scripts/initSQLite.js [选项]
选项:
--help, -h 显示帮助信息
示例:
node scripts/initSQLite.js # 完整初始化
`);
process.exit(0);
}
initializer.initialize().catch(error => {
console.error('❌ 初始化过程中发生错误:', error.message);
process.exit(1);
});