2024-12-05 21:56:49 +08:00
|
|
|
<template>
|
2024-12-07 22:26:09 +08:00
|
|
|
<div class="topbar">
|
2024-12-05 21:56:49 +08:00
|
|
|
<div class="left">
|
2024-12-07 22:26:09 +08:00
|
|
|
<router-link to="/" class="logo">智慧养老平台</router-link>
|
2024-12-05 21:56:49 +08:00
|
|
|
</div>
|
2024-12-07 22:26:09 +08:00
|
|
|
|
2024-12-05 21:56:49 +08:00
|
|
|
<div class="right">
|
2024-12-07 22:26:09 +08:00
|
|
|
<template v-if="userStore.userInfo">
|
2024-12-07 22:42:07 +08:00
|
|
|
<el-dropdown trigger="click">
|
|
|
|
<div class="user-info">
|
|
|
|
<el-avatar
|
|
|
|
:size="32"
|
|
|
|
:src="userStore.userInfo.avatar || defaultAvatar"
|
|
|
|
/>
|
|
|
|
<span class="nickname">{{ userStore.userInfo.nickname || '用户' }}</span>
|
|
|
|
<el-icon class="el-icon--right"><arrow-down /></el-icon>
|
2024-12-07 22:36:18 +08:00
|
|
|
</div>
|
2024-12-07 22:42:07 +08:00
|
|
|
<template #dropdown>
|
|
|
|
<el-dropdown-menu>
|
|
|
|
<el-dropdown-item @click="handleProfile">
|
|
|
|
<el-icon><User /></el-icon>
|
|
|
|
个人中心
|
|
|
|
</el-dropdown-item>
|
|
|
|
<el-dropdown-item @click="handleSettings">
|
|
|
|
<el-icon><Setting /></el-icon>
|
|
|
|
设置
|
|
|
|
</el-dropdown-item>
|
|
|
|
<el-dropdown-item divided @click="handleLogout">
|
|
|
|
<el-icon><SwitchButton /></el-icon>
|
|
|
|
退出登录
|
|
|
|
</el-dropdown-item>
|
|
|
|
</el-dropdown-menu>
|
|
|
|
</template>
|
|
|
|
</el-dropdown>
|
2024-12-07 22:26:09 +08:00
|
|
|
</template>
|
|
|
|
<template v-else>
|
|
|
|
<router-link to="/login" class="login-btn">
|
|
|
|
<el-button type="primary">登录</el-button>
|
|
|
|
</router-link>
|
|
|
|
<router-link to="/register" class="register-btn">
|
|
|
|
<el-button>注册</el-button>
|
|
|
|
</router-link>
|
|
|
|
</template>
|
2024-12-05 21:56:49 +08:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2024-12-07 22:26:09 +08:00
|
|
|
import { onMounted } from 'vue'
|
2024-12-05 21:56:49 +08:00
|
|
|
import { useRouter } from 'vue-router'
|
2024-12-07 22:26:09 +08:00
|
|
|
import { useUserStore } from '@/stores/user'
|
2024-12-07 22:28:46 +08:00
|
|
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
|
|
|
import { User, Setting, SwitchButton, ArrowDown } from '@element-plus/icons-vue'
|
2024-12-07 22:26:09 +08:00
|
|
|
import defaultAvatar from '@/assets/default-avatar.png'
|
2024-12-05 21:56:49 +08:00
|
|
|
|
|
|
|
const router = useRouter()
|
2024-12-07 22:26:09 +08:00
|
|
|
const userStore = useUserStore()
|
|
|
|
|
|
|
|
onMounted(async () => {
|
|
|
|
const token = localStorage.getItem('token')
|
|
|
|
if (token && !userStore.userInfo) {
|
|
|
|
userStore.setToken(token)
|
|
|
|
await userStore.getUserInfo()
|
|
|
|
}
|
2024-12-05 21:56:49 +08:00
|
|
|
})
|
|
|
|
|
2024-12-07 22:36:18 +08:00
|
|
|
const handleProfile = () => {
|
|
|
|
router.push('/profile')
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleSettings = () => {
|
|
|
|
router.push('/settings')
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleLogout = async () => {
|
|
|
|
try {
|
|
|
|
await ElMessageBox.confirm('确定要退出登录吗?', '提示', {
|
|
|
|
confirmButtonText: '确定',
|
|
|
|
cancelButtonText: '取消',
|
|
|
|
type: 'warning'
|
|
|
|
})
|
|
|
|
userStore.clearUserInfo()
|
|
|
|
ElMessage.success('退出登录成功')
|
|
|
|
router.push('/login')
|
|
|
|
} catch {
|
|
|
|
// 用户取消操作
|
2024-12-05 21:56:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped>
|
2024-12-07 22:26:09 +08:00
|
|
|
.topbar {
|
2024-12-05 21:56:49 +08:00
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
align-items: center;
|
|
|
|
padding: 0 20px;
|
2024-12-07 22:26:09 +08:00
|
|
|
height: 60px;
|
|
|
|
background-color: #fff;
|
|
|
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
2024-12-05 21:56:49 +08:00
|
|
|
}
|
|
|
|
|
2024-12-07 22:26:09 +08:00
|
|
|
.logo {
|
2024-12-05 21:56:49 +08:00
|
|
|
font-size: 20px;
|
2024-12-07 22:26:09 +08:00
|
|
|
font-weight: bold;
|
|
|
|
color: #333;
|
|
|
|
text-decoration: none;
|
|
|
|
}
|
|
|
|
|
|
|
|
.right {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
gap: 16px;
|
2024-12-05 21:56:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
.user-info {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
2024-12-07 22:26:09 +08:00
|
|
|
gap: 8px;
|
2024-12-05 21:56:49 +08:00
|
|
|
cursor: pointer;
|
2024-12-07 22:26:09 +08:00
|
|
|
padding: 2px 8px;
|
|
|
|
border-radius: 4px;
|
|
|
|
transition: background-color 0.3s;
|
2024-12-05 21:56:49 +08:00
|
|
|
}
|
|
|
|
|
2024-12-07 22:26:09 +08:00
|
|
|
.user-info:hover {
|
|
|
|
background-color: #f5f7fa;
|
|
|
|
}
|
|
|
|
|
|
|
|
.nickname {
|
2024-12-05 21:56:49 +08:00
|
|
|
font-size: 14px;
|
2024-12-07 22:26:09 +08:00
|
|
|
color: #333;
|
2024-12-05 21:56:49 +08:00
|
|
|
}
|
|
|
|
|
2024-12-07 22:26:09 +08:00
|
|
|
.login-btn,
|
|
|
|
.register-btn {
|
|
|
|
text-decoration: none;
|
|
|
|
}
|
|
|
|
|
2024-12-07 22:42:07 +08:00
|
|
|
.el-dropdown-menu__item {
|
|
|
|
display: flex !important;
|
2024-12-05 21:56:49 +08:00
|
|
|
align-items: center;
|
2024-12-07 22:26:09 +08:00
|
|
|
gap: 8px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.el-icon {
|
2024-12-07 22:42:07 +08:00
|
|
|
margin-right: 4px;
|
2024-12-05 21:56:49 +08:00
|
|
|
}
|
2024-12-07 22:28:46 +08:00
|
|
|
|
|
|
|
.el-icon--right {
|
|
|
|
margin-left: 4px;
|
|
|
|
font-size: 12px;
|
|
|
|
transition: transform 0.3s;
|
|
|
|
}
|
|
|
|
|
2024-12-07 22:36:18 +08:00
|
|
|
.user-info:hover .el-icon--right {
|
2024-12-07 22:28:46 +08:00
|
|
|
transform: rotate(180deg);
|
|
|
|
}
|
2024-12-07 22:22:29 +08:00
|
|
|
</style>
|