feat: 第二次优化迭代

- Links 后端分页+前端分页器
- 搜索关键词高亮(mark标签)
- 搜索防抖200ms,支持描述搜索
- 移除不可用的 type-check 脚本
- 更新 README 项目文档
This commit is contained in:
Agent
2026-07-14 21:34:02 +08:00
parent 454e05e82d
commit cf12869614
5 changed files with 105 additions and 61 deletions
+54 -45
View File
@@ -1,62 +1,71 @@
# yx-nav 网址导航 # 言逍导航 (yx-nav)
## 项目简介 精选实用网址导航系统,支持分类管理、搜索、后台管理。
纯 Node.js 全栈实现的网址导航系统,包含前台展示页和后台管理面板。后台可配置前台的分类、网址、站点主题、布局模式等所有内容。
## 技术栈 ## 技术栈
- **后端**: Node.js + Express + MySQL - **后端**: Node.js + Express + MySQL (110.42.189.169:9109)
- **前端**: Vue 3 + TypeScript + Vite + Element Plus - **前端**: Vue 3.5 + TypeScript + Vite 6 + Element Plus
- **部署**: Docker (端口 9110) + Jenkins CI
## 外部服务配置 ## 目录结构
| 服务 | 地址 | 账号 | 密码 |
|------|------|------|------|
| 数据库 (MySQL) | 110.42.189.169:9109 | yx-nav | yx-nav |
| Jenkins (CI/CD) | 110.42.189.169:9101 | hermesagent | hermesagent |
| Gitea (代码仓库) | 110.42.189.169:3000 | hermesagent/yx-nav | — |
## 默认登录
后台管理:`http://localhost:5173/admin/login`
- 账号: `admin`
- 密码: `admin123`
## 项目结构
``` ```
backend/ # 后端 (Express + MySQL) yx-nav/
src/ ├── backend/ # 后端服务
index.js # 入口,启动 Express │ ├── src/
database.js # MySQL 连接池 + 表结构初始化 │ │ ├── index.js # 入口 + Express 配置
middleware/auth.js # JWT 认证中间件 │ │ ├── database.js # MySQL 连接 + 表初始化
routes/api.js # 全部 API 路由 │ │ ├── middleware/auth.js # JWT 认证中间件
frontend/ # 前端 (Vue 3 + TS + Vite) │ │ └── routes/api.js # API 路由(公开 + 后台)
src/ │ └── package.json
views/ ├── frontend/ # 前端应用
Home.vue # 前台:分类导航 + 搜索 │ ├── src/
admin/ │ │ ├── views/ # 页面组件
Login.vue # 后台登录 │ │ │ ├── Home.vue # 前台首页
AdminLayout.vue # 后台布局 │ │ │ ├── NotFound.vue # 404 页面
Dashboard.vue # 仪表盘 │ │ │ └── admin/ # 后台管理
Links.vue # 网址管理 │ │ ├── api/index.ts # API 调用封装
Categories.vue # 分类管理 │ │ ├── router/index.ts # Vue Router 配置
Settings.vue # 站点配置 │ │ └── stores/user.ts # Pinia 用户状态
api/index.ts # API 封装 │ ├── public/favicon.svg # 站点图标
stores/user.ts # 用户状态 (Pinia) │ └── package.json
└── build.sh # Jenkins 构建脚本
``` ```
## 运行 ## 本地开发
```bash ```bash
# 后端 # 后端
cd backend && npm install && npm start # 监听 3000 cd backend
npm install
npm run dev
# 前端 # 前端
cd frontend && npm install && npm run dev # 监听 5173,代理 /api → 后端 cd frontend
npm install
npm run dev
``` ```
## Jenkins 发布 前端运行在 5173 端口(自动代理 API 到 3000)。
推送代码到 `hermesagent/yx-nav` 后,在 Jenkins `http://110.42.189.169:9101/` 手动触发 `yx-nav` job 即可构建部署。 ## 部署
Jenkins 构建触发(需认证):
```bash
curl -u hermesagent:hermesagent \
"http://110.42.189.169:9101/job/yx-nav/build?token=yx-navbuild"
```
## 默认管理员
- 用户名: `admin`
- 密码: `qq755141`
## 服务地址
- 前台首页: http://110.42.189.169:9110/
- 后台管理: http://110.42.189.169:9110/#/admin
- MySQL: 110.42.189.169:9109 (yx-nav/yx-nav)
- Jenkins: http://110.42.189.169:9101 (hermesagent/hermesagent)
+14 -7
View File
@@ -120,14 +120,21 @@ const router = (app) => {
app.get('/api/admin/links', authenticate, async (req, res) => { app.get('/api/admin/links', authenticate, async (req, res) => {
try { try {
const { keyword, categoryId } = req.query; const { keyword, categoryId, page = '1', pageSize = '20' } = req.query;
let sql = 'SELECT l.*, c.name as categoryName FROM links l JOIN categories c ON l.category_id = c.id'; const p = Math.max(1, Number(page));
const ps = Math.min(100, Math.max(1, Number(pageSize)));
const offset = (p - 1) * ps;
let where = '';
const params = []; const params = [];
if (keyword) { sql += ' WHERE l.title LIKE ? OR l.description LIKE ?'; params.push(`%${keyword}%`, `%${keyword}%`); } if (keyword) { where += ' WHERE (l.title LIKE ? OR l.description LIKE ?)'; params.push(`%${keyword}%`, `%${keyword}%`); }
if (categoryId) { sql += (keyword ? ' AND' : ' WHERE') + ' l.category_id = ?'; params.push(Number(categoryId)); } if (categoryId) { where += (where ? ' AND' : ' WHERE') + ' l.category_id = ?'; params.push(Number(categoryId)); }
sql += ' ORDER BY l.sort, l.id';
const [rows] = await pool.query(sql, params); const [countRows] = await pool.query(`SELECT COUNT(*) as total FROM links l${where}`, params);
res.json({ status: 200, result: { data: rows } }); const total = Number(countRows[0].total);
const [rows] = await pool.query(`SELECT l.*, c.name as categoryName FROM links l JOIN categories c ON l.category_id = c.id${where} ORDER BY l.sort, l.id LIMIT ? OFFSET ?`, [...params, ps, offset]);
res.json({ status: 200, result: { data: { list: rows, total, page: p, pageSize: ps } } });
} catch { } catch {
res.json({ status: 500, message: '加载失败' }); res.json({ status: 500, message: '加载失败' });
} }
+1 -2
View File
@@ -6,8 +6,7 @@
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "vite build", "build": "vite build",
"preview": "vite preview", "preview": "vite preview"
"type-check": "vue-tsc --noEmit"
}, },
"dependencies": { "dependencies": {
"vue": "^3.5.13", "vue": "^3.5.13",
+19 -6
View File
@@ -53,8 +53,8 @@
/> />
<div v-else class="link-icon default-icon">{{ link.title[0] }}</div> <div v-else class="link-icon default-icon">{{ link.title[0] }}</div>
<div class="link-info"> <div class="link-info">
<div class="link-title">{{ link.title }}</div> <div class="link-title" v-html="highlight(link.title, search)"></div>
<div class="link-desc">{{ link.description }}</div> <div class="link-desc" v-html="highlight(link.description, search)"></div>
</div> </div>
</a> </a>
</div> </div>
@@ -74,6 +74,7 @@ import { siteApi } from '@/api'
const data = reactive({ categories: [] as any[], settings: {} as any, links: [] as any[] }) const data = reactive({ categories: [] as any[], settings: {} as any, links: [] as any[] })
const search = ref('') const search = ref('')
const filteredLinks = ref<any[]>([]) const filteredLinks = ref<any[]>([])
const searchTimer = ref<ReturnType<typeof setTimeout>>()
const filteredCategories = computed(() => { const filteredCategories = computed(() => {
return data.categories.filter((cat: any) => return data.categories.filter((cat: any) =>
@@ -86,10 +87,20 @@ function linksInCat(catId: number) {
} }
function filterLinks() { function filterLinks() {
const q = search.value.trim() if (searchTimer.value) clearTimeout(searchTimer.value)
filteredLinks.value = q searchTimer.value = setTimeout(() => {
? data.links.filter((l: any) => l.title.toLowerCase().includes(q)) const q = search.value.trim().toLowerCase()
: [...data.links] filteredLinks.value = q
? data.links.filter((l: any) => l.title.toLowerCase().includes(q) || l.description.toLowerCase().includes(q))
: [...data.links]
}, 200)
}
function highlight(text: string, keyword: string) {
if (!keyword.trim()) return text
const idx = text.toLowerCase().indexOf(keyword.toLowerCase())
if (idx === -1) return text
return text.slice(0, idx) + '<mark>' + text.slice(idx, idx + keyword.length) + '</mark>' + text.slice(idx + keyword.length)
} }
async function loadData() { async function loadData() {
@@ -243,7 +254,9 @@ loadData()
.link-info { flex: 1; min-width: 0; } .link-info { flex: 1; min-width: 0; }
.link-title { font-weight: 500; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .link-title { font-weight: 500; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.link-title :deep(mark) { background: #fef08a; color: #333; padding: 0 2px; border-radius: 2px; }
.link-desc { font-size: 12px; color: #888; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } .link-desc { font-size: 12px; color: #888; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.link-desc :deep(mark) { background: #fef08a; color: #333; padding: 0 2px; border-radius: 2px; }
} }
.footer { .footer {
+17 -1
View File
@@ -47,6 +47,15 @@
</template> </template>
</el-table-column> </el-table-column>
</el-table> </el-table>
<div v-if="total > pageSize" class="pagination-wrap">
<el-pagination
v-model:current-page="currentPage"
:page-size="pageSize"
:total="total"
layout="prev, pager, next"
@current-change="loadLinks"
/>
</div>
</div> </div>
</el-card> </el-card>
@@ -95,6 +104,9 @@ const links = ref([])
const categories = ref([]) const categories = ref([])
const keyword = ref('') const keyword = ref('')
const catFilter = ref('') const catFilter = ref('')
const currentPage = ref(1)
const pageSize = ref(20)
const total = ref(0)
const dialogVisible = ref(false) const dialogVisible = ref(false)
const editId = ref(null) const editId = ref(null)
const form = ref({ title: '', url: '', categoryId: '', description: '', iconUrl: '', sort: 0, isShow: true }) const form = ref({ title: '', url: '', categoryId: '', description: '', iconUrl: '', sort: 0, isShow: true })
@@ -103,7 +115,10 @@ const dialogWidth = computed(() => window.innerWidth < 500 ? '92%' : '500px')
async function loadLinks() { async function loadLinks() {
loading.value = true loading.value = true
try { try {
links.value = (await adminApi.getLinks({ keyword: keyword.value || undefined, categoryId: catFilter.value || undefined })).result.data const res = await adminApi.getLinks({ keyword: keyword.value || undefined, categoryId: catFilter.value || undefined, page: currentPage.value, pageSize: pageSize.value })
const d = res.result.data
links.value = d.list
total.value = d.total
} finally { } finally {
loading.value = false loading.value = false
} }
@@ -145,6 +160,7 @@ onMounted(async () => { await loadLinks(); await loadCats() })
.page-header h2 { font-size: 20px; font-weight: 600; } .page-header h2 { font-size: 20px; font-weight: 600; }
.table-wrap { overflow-x: auto; } .table-wrap { overflow-x: auto; }
.loading-wrap { padding: 20px; } .loading-wrap { padding: 20px; }
.pagination-wrap { display: flex; justify-content: center; margin-top: 20px; }
.link-icon-preview { .link-icon-preview {
display: inline-flex; display: inline-flex;
width: 32px; width: 32px;