feat: 初始化 yx-nav 网址导航项目

- 技术栈: Vue3+TS+Vite + Express+SQLite
- 前台: 分类导航页、搜索、访问统计
- 后台: 仪表盘、网址CRUD、分类管理、站点配置
- 默认账号: admin / admin123
This commit is contained in:
Agent
2026-07-14 14:26:45 +08:00
commit b37140f16c
28 changed files with 5665 additions and 0 deletions
+104
View File
@@ -0,0 +1,104 @@
import Database from 'better-sqlite3';
import bcrypt from 'bcryptjs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const dbPath = path.join(__dirname, '..', 'data', 'yx-nav.db');
const db = new Database(dbPath);
// 启用 WAL 模式和外键
db.pragma('journal_mode = WAL');
db.pragma('foreign_keys = ON');
// 初始化数据库表
db.exec(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
nickname TEXT DEFAULT '管理员',
role TEXT DEFAULT 'admin',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
icon TEXT DEFAULT '🔗',
sort INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS links (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
url TEXT NOT NULL,
category_id INTEGER NOT NULL,
description TEXT DEFAULT '',
icon_url TEXT DEFAULT '',
sort INTEGER DEFAULT 0,
is_show INTEGER DEFAULT 1,
views INTEGER DEFAULT 0,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
);
CREATE TABLE IF NOT EXISTS settings (
id INTEGER PRIMARY KEY AUTOINCREMENT,
key TEXT UNIQUE NOT NULL,
value TEXT NOT NULL,
description TEXT DEFAULT ''
);
`);
// 初始化默认配置
const defaultSettings = {
site_name: '言逍导航',
site_description: '精心整理的网址导航',
site_logo: '',
theme_color: '#6366F1',
layout_mode: 'grid',
footer_text: '© 2026 言逍导航',
};
for (const [key, value] of Object.entries(defaultSettings)) {
db.prepare('INSERT OR IGNORE INTO settings (key, value) VALUES (?, ?)').run(key, value);
}
// 初始化默认管理员
if (!db.prepare("SELECT 1 FROM users WHERE username = 'admin'").get()) {
const hashed = bcrypt.hashSync('admin123', 10);
db.prepare("INSERT INTO users (username, password, nickname) VALUES (?, ?, ?)").run('admin', hashed, '管理员');
}
// 初始化默认分类
if (!db.prepare("SELECT 1 FROM categories WHERE name = '常用网站'").get()) {
const cats = [
{ name: '常用网站', icon: '🌟' },
{ name: '开发工具', icon: '💻' },
{ name: '新闻资讯', icon: '📰' },
{ name: '娱乐休闲', icon: '🎮' },
{ name: '学习资源', icon: '📚' },
];
const stmt = db.prepare("INSERT INTO categories (name, icon, sort) VALUES (?, ?, ?)");
cats.forEach((c, i) => stmt.run(c.name, c.icon, i));
}
// 初始化示例链接
if (!db.prepare("SELECT 1 FROM links").get()) {
const links = [
{ title: '百度', url: 'https://www.baidu.com', desc: '全球最大的中文搜索引擎', cat: 1 },
{ title: 'GitHub', url: 'https://github.com', desc: '全球最大的代码托管平台', cat: 2 },
{ title: '掘金', url: 'https://juejin.cn', desc: '技术社区', cat: 2 },
{ title: '知乎', url: 'https://www.zhihu.com', desc: '问答社区', cat: 3 },
{ title: 'Bilibili', url: 'https://www.bilibili.com', desc: '视频网站', cat: 4 },
{ title: '慕课网', url: 'https://www.imooc.com', desc: '在线学习平台', cat: 5 },
];
const stmt = db.prepare(
"INSERT INTO links (title, url, description, category_id, sort) VALUES (?, ?, ?, ?, ?)"
);
links.forEach((l, i) => stmt.run(l.title, l.url, l.desc, l.cat, i));
}
export default db;