对接后端

This commit is contained in:
ovo 2024-12-07 22:36:18 +08:00
parent b72c5feff6
commit 8663aadfb3
2 changed files with 76 additions and 67 deletions

View File

@ -6,35 +6,40 @@
<div class="right"> <div class="right">
<template v-if="userStore.userInfo"> <template v-if="userStore.userInfo">
<el-dropdown :hide-on-click="false"> <el-popover
<span class="user-info"> :width="200"
trigger="click"
placement="bottom-end"
>
<template #reference>
<div class="user-info">
<el-avatar <el-avatar
:size="32" :size="32"
:src="userStore.userInfo.avatar || defaultAvatar" :src="userStore.userInfo.avatar || defaultAvatar"
/> />
<span class="nickname">{{ userStore.userInfo.nickname || '用户' }}</span> <span class="nickname">{{ userStore.userInfo.nickname || '用户' }}</span>
<el-icon class="el-icon--right"><arrow-down /></el-icon> <el-icon class="el-icon--right"><arrow-down /></el-icon>
</span> </div>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item @click="handleCommand('profile')">
<el-icon><User /></el-icon>
个人中心
</el-dropdown-item>
<el-dropdown-item @click="handleCommand('settings')">
<el-icon><Setting /></el-icon>
设置
</el-dropdown-item>
<el-dropdown-item divided @click="handleCommand('logout')">
<el-icon><SwitchButton /></el-icon>
退出登录
</el-dropdown-item>
</el-dropdown-menu>
</template> </template>
</el-dropdown>
<div class="menu-items">
<div class="menu-item" @click="handleProfile">
<el-icon><User /></el-icon>
<span>个人中心</span>
</div>
<div class="menu-item" @click="handleSettings">
<el-icon><Setting /></el-icon>
<span>设置</span>
</div>
<div class="divider"></div>
<div class="menu-item" @click="handleLogout">
<el-icon><SwitchButton /></el-icon>
<span>退出登录</span>
</div>
</div>
</el-popover>
</template> </template>
<template v-else> <template v-else>
<!-- 未登录状态 -->
<router-link to="/login" class="login-btn"> <router-link to="/login" class="login-btn">
<el-button type="primary">登录</el-button> <el-button type="primary">登录</el-button>
</router-link> </router-link>
@ -57,7 +62,6 @@ import defaultAvatar from '@/assets/default-avatar.png'
const router = useRouter() const router = useRouter()
const userStore = useUserStore() const userStore = useUserStore()
//
onMounted(async () => { onMounted(async () => {
const token = localStorage.getItem('token') const token = localStorage.getItem('token')
if (token && !userStore.userInfo) { if (token && !userStore.userInfo) {
@ -66,17 +70,15 @@ onMounted(async () => {
} }
}) })
// const handleProfile = () => {
const handleCommand = async (command: string) => {
console.log('Menu clicked:', command) //
switch (command) {
case 'profile':
router.push('/profile') router.push('/profile')
break }
case 'settings':
const handleSettings = () => {
router.push('/settings') router.push('/settings')
break }
case 'logout':
const handleLogout = async () => {
try { try {
await ElMessageBox.confirm('确定要退出登录吗?', '提示', { await ElMessageBox.confirm('确定要退出登录吗?', '提示', {
confirmButtonText: '确定', confirmButtonText: '确定',
@ -89,8 +91,6 @@ const handleCommand = async (command: string) => {
} catch { } catch {
// //
} }
break
}
} }
</script> </script>
@ -126,7 +126,6 @@ const handleCommand = async (command: string) => {
padding: 2px 8px; padding: 2px 8px;
border-radius: 4px; border-radius: 4px;
transition: background-color 0.3s; transition: background-color 0.3s;
user-select: none; /* 防止文本被选中 */
} }
.user-info:hover { .user-info:hover {
@ -143,24 +142,40 @@ const handleCommand = async (command: string) => {
text-decoration: none; text-decoration: none;
} }
.el-dropdown-menu__item { .menu-items {
padding: 4px 0;
}
.menu-item {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 8px; gap: 8px;
padding: 8px 16px;
cursor: pointer;
transition: background-color 0.3s;
}
.menu-item:hover {
background-color: #f5f7fa;
}
.divider {
height: 1px;
background-color: #ebeef5;
margin: 4px 0;
} }
.el-icon { .el-icon {
margin-right: 4px; font-size: 16px;
} }
/* 添加下拉箭头样式 */
.el-icon--right { .el-icon--right {
margin-left: 4px; margin-left: 4px;
font-size: 12px; font-size: 12px;
transition: transform 0.3s; transition: transform 0.3s;
} }
.el-dropdown:hover .el-icon--right { .user-info:hover .el-icon--right {
transform: rotate(180deg); transform: rotate(180deg);
} }
</style> </style>

View File

@ -1,22 +1,16 @@
import './assets/styles/global.css' import './assets/styles/global.css'
import { createApp, type Component } from 'vue' import { createApp } from 'vue'
import { createPinia } from 'pinia'
import ElementPlus from 'element-plus' import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import 'element-plus/dist/index.css' import 'element-plus/dist/index.css'
import App from './App.vue' import App from './App.vue'
import router from './router' import router from './router'
import { createPinia } from 'pinia'
const app = createApp(App) const app = createApp(App)
const pinia = createPinia()
// 注册 Element Plus 图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component as Component)
}
app.use(createPinia())
app.use(router)
app.use(ElementPlus) app.use(ElementPlus)
app.use(pinia)
app.use(router)
app.mount('#app') app.mount('#app')