diff --git a/backend/src/routes/api.js b/backend/src/routes/api.js index 737b71b..98b6c1e 100644 --- a/backend/src/routes/api.js +++ b/backend/src/routes/api.js @@ -80,6 +80,22 @@ const router = (app) => { } }); + // 修改密码 + app.put('/api/admin/password', authenticate, async (req, res) => { + try { + const { oldPassword, newPassword } = req.body; + const [users] = await pool.query('SELECT password FROM users WHERE id = ?', [req.user.id]); + if (!users.length || !bcrypt.compareSync(oldPassword, users[0].password)) { + return res.json({ status: 401, message: '当前密码错误' }); + } + const hash = bcrypt.hashSync(newPassword, 10); + await pool.query('UPDATE users SET password = ? WHERE id = ?', [hash, req.user.id]); + res.json({ status: 200, message: '密码修改成功' }); + } catch { + res.json({ status: 500, message: '修改失败' }); + } + }); + app.get('/api/admin/categories', authenticate, async (req, res) => { try { const [rows] = await pool.query('SELECT * FROM categories ORDER BY sort, id'); diff --git a/frontend/src/api/index.ts b/frontend/src/api/index.ts index af91583..18a05c0 100644 --- a/frontend/src/api/index.ts +++ b/frontend/src/api/index.ts @@ -37,6 +37,7 @@ export const siteApi = { export const adminApi = { profile: () => request.get('/admin/profile'), updateProfile: (data: any) => request.put('/admin/profile', data), + changePassword: (data: any) => request.put('/admin/password', data), getCategories: () => request.get('/admin/categories'), addCategory: (data: any) => request.post('/admin/categories', data), updateCategory: (id: number, data: any) => request.put(`/admin/categories/${id}`, data), diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index f0fdbbb..2c2132f 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -7,6 +7,7 @@ import AdminLinks from '@/views/admin/Links.vue' import AdminCategories from '@/views/admin/Categories.vue' import AdminSettings from '@/views/admin/Settings.vue' import AdminLogin from '@/views/admin/Login.vue' +import AdminProfile from '@/views/admin/Profile.vue' const router = createRouter({ history: createWebHashHistory(), @@ -23,6 +24,7 @@ const router = createRouter({ { path: 'links', name: 'links', component: AdminLinks, meta: { transition: 'slide' } }, { path: 'categories', name: 'categories', component: AdminCategories, meta: { transition: 'slide' } }, { path: 'settings', name: 'settings', component: AdminSettings, meta: { transition: 'slide' } }, + { path: 'profile', name: 'profile', component: AdminProfile, meta: { transition: 'slide' } }, ], }, { path: '/:pathMatch(.*)*', name: 'not-found', component: NotFound }, diff --git a/frontend/src/views/Home.vue b/frontend/src/views/Home.vue index 9c5fc10..c47d087 100644 --- a/frontend/src/views/Home.vue +++ b/frontend/src/views/Home.vue @@ -34,8 +34,11 @@ {{ cat.icon }} {{ cat.name }} {{ linksInCat(cat.id).length }} + + {{ collapsedCats.has(cat.id) ? '展开' : '收起' }} + -