feat: 个人资料编辑页+分类折叠展开
- 新增个人资料页(昵称修改+密码修改) - 后端 PUT /api/admin/password 接口 - 侧边栏新增个人资料菜单项 - 前台分类增加折叠/展开交互 - Profile页+路由注册
This commit is contained in:
@@ -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');
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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 },
|
||||
|
||||
@@ -34,8 +34,11 @@
|
||||
<span class="icon">{{ cat.icon }}</span>
|
||||
<span class="name">{{ cat.name }}</span>
|
||||
<span class="count">{{ linksInCat(cat.id).length }}</span>
|
||||
<span class="collapse-btn" @click="toggleCat(cat.id)">
|
||||
{{ collapsedCats.has(cat.id) ? '展开' : '收起' }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="links-grid">
|
||||
<div v-show="!collapsedCats.has(cat.id)" class="links-grid">
|
||||
<a
|
||||
v-for="link in linksInCat(cat.id)"
|
||||
:key="link.id"
|
||||
@@ -79,6 +82,13 @@ const data = reactive({ categories: [] as any[], settings: {} as any, links: []
|
||||
const search = ref('')
|
||||
const filteredLinks = ref<any[]>([])
|
||||
const searchTimer = ref<ReturnType<typeof setTimeout>>()
|
||||
const collapsedCats = ref<Set<number>>(new Set())
|
||||
|
||||
function toggleCat(id: number) {
|
||||
const s = new Set(collapsedCats.value)
|
||||
if (s.has(id)) s.delete(id); else s.add(id)
|
||||
collapsedCats.value = s
|
||||
}
|
||||
|
||||
const filteredCategories = computed(() => {
|
||||
return data.categories.filter((cat: any) =>
|
||||
@@ -213,6 +223,16 @@ loadData()
|
||||
padding: 2px 10px;
|
||||
border-radius: 12px;
|
||||
}
|
||||
.collapse-btn {
|
||||
margin-left: auto;
|
||||
font-size: 12px;
|
||||
color: #6366f1;
|
||||
cursor: pointer;
|
||||
padding: 2px 8px;
|
||||
border-radius: 4px;
|
||||
user-select: none;
|
||||
}
|
||||
.collapse-btn:hover { background: rgba(99,102,241,0.1); }
|
||||
}
|
||||
|
||||
.links-grid {
|
||||
|
||||
@@ -25,6 +25,10 @@
|
||||
<el-icon><Setting /></el-icon>
|
||||
<span>站点配置</span>
|
||||
</el-menu-item>
|
||||
<el-menu-item index="/admin/profile">
|
||||
<el-icon><User /></el-icon>
|
||||
<span>个人资料</span>
|
||||
</el-menu-item>
|
||||
</el-menu>
|
||||
</el-aside>
|
||||
<el-main>
|
||||
@@ -55,7 +59,7 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted, onUnmounted } from 'vue'
|
||||
import { useRoute } from 'vue-router'
|
||||
import { DataBoard, Link, Folder, Setting, Sunny, Moon } from '@element-plus/icons-vue'
|
||||
import { DataBoard, Link, Folder, Setting, Sunny, Moon, User } from '@element-plus/icons-vue'
|
||||
import { useUserStore } from '@/stores/user'
|
||||
import router from '@/router'
|
||||
import { useDarkMode } from '@/composables/useDarkMode'
|
||||
@@ -79,6 +83,7 @@ const titleMap: Record<string, string> = {
|
||||
'/admin/links': '网址管理',
|
||||
'/admin/categories': '分类管理',
|
||||
'/admin/settings': '站点配置',
|
||||
'/admin/profile': '个人资料',
|
||||
}
|
||||
const pageTitle = computed(() => titleMap[route.path] || '管理')
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
<template>
|
||||
<div class="profile-page">
|
||||
<div class="page-header"><h2>个人资料</h2></div>
|
||||
|
||||
<el-card style="margin-bottom:24px">
|
||||
<template #header><span>基本信息</span></template>
|
||||
<el-form :model="profile" label-width="100px">
|
||||
<el-form-item label="用户名">
|
||||
<el-input v-model="profile.username" disabled />
|
||||
</el-form-item>
|
||||
<el-form-item label="昵称">
|
||||
<el-input v-model="profile.nickname" placeholder="管理员昵称" />
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button type="primary" @click="updateProfile">保存</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</el-card>
|
||||
|
||||
<el-card>
|
||||
<template #header><span>修改密码</span></template>
|
||||
<el-form :model="pwForm" label-width="100px" ref="pwFormRef">
|
||||
<el-form-item label="当前密码" prop="oldPassword">
|
||||
<el-input v-model="pwForm.oldPassword" type="password" show-password />
|
||||
</el-form-item>
|
||||
<el-form-item label="新密码" prop="newPassword">
|
||||
<el-input v-model="pwForm.newPassword" type="password" show-password />
|
||||
</el-form-item>
|
||||
<el-form-item label="确认密码" prop="confirmPassword">
|
||||
<el-input v-model="pwForm.confirmPassword" type="password" show-password />
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
<el-button type="primary" @click="changePwd">修改密码</el-button>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
</el-card>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { adminApi } from '@/api'
|
||||
|
||||
const profile = ref({ username: '', nickname: '' })
|
||||
const pwForm = ref({ oldPassword: '', newPassword: '', confirmPassword: '' })
|
||||
|
||||
onMounted(async () => {
|
||||
const res = await adminApi.profile()
|
||||
profile.value = res.result.data
|
||||
})
|
||||
|
||||
async function updateProfile() {
|
||||
await adminApi.updateProfile({ nickname: profile.value.nickname })
|
||||
ElMessage.success('保存成功')
|
||||
}
|
||||
|
||||
async function changePwd() {
|
||||
const { oldPassword, newPassword, confirmPassword } = pwForm.value
|
||||
if (!oldPassword || !newPassword) { ElMessage.warning('请填写完整'); return }
|
||||
if (newPassword !== confirmPassword) { ElMessage.warning('两次密码不一致'); return }
|
||||
if (newPassword.length < 6) { ElMessage.warning('密码至少6位'); return }
|
||||
try {
|
||||
await adminApi.changePassword({ oldPassword, newPassword })
|
||||
ElMessage.success('密码修改成功')
|
||||
pwForm.value = { oldPassword: '', newPassword: '', confirmPassword: '' }
|
||||
} catch {
|
||||
ElMessage.error('当前密码错误')
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.page-header { margin-bottom: 20px; }
|
||||
.page-header h2 { font-size: 20px; font-weight: 600; }
|
||||
</style>
|
||||
Reference in New Issue
Block a user