2025-10-07 13:31:06 +08:00
|
|
|
<template>
|
|
|
|
|
<div class="user-dropdown" @click="toggleDropdown" v-click-outside="closeDropdown">
|
2025-10-08 14:11:54 +08:00
|
|
|
<!-- 未登录状态 -->
|
|
|
|
|
<div class="login-info" v-if="!isLoggedIn">
|
|
|
|
|
<div class="login-text">
|
|
|
|
|
<i class="login-icon">👤</i>
|
|
|
|
|
<span v-if="!collapsed">登录/注册</span>
|
|
|
|
|
</div>
|
|
|
|
|
<i class="dropdown-icon" :class="{ 'open': dropdownVisible }"></i>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- 已登录状态 -->
|
|
|
|
|
<div class="user-info" v-else>
|
2025-10-07 13:31:06 +08:00
|
|
|
<div class="user-avatar">
|
|
|
|
|
<img :src="userAvatar" :alt="user?.username" v-if="userAvatar">
|
|
|
|
|
<span class="avatar-placeholder" v-else>{{ avatarText }}</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="user-details" v-if="!collapsed">
|
|
|
|
|
<div class="user-name">{{ user?.realName || user?.username }}</div>
|
|
|
|
|
<div class="user-role">{{ primaryRole }}</div>
|
|
|
|
|
</div>
|
|
|
|
|
<i class="dropdown-icon" :class="{ 'open': dropdownVisible }"></i>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<!-- 下拉菜单 -->
|
|
|
|
|
<transition name="dropdown">
|
|
|
|
|
<div class="dropdown-menu" v-if="dropdownVisible">
|
2025-10-08 14:11:54 +08:00
|
|
|
<!-- 未登录时的菜单 -->
|
|
|
|
|
<template v-if="!isLoggedIn">
|
|
|
|
|
<div class="dropdown-item" @click="goToLogin">
|
|
|
|
|
<i class="item-icon icon-login"></i>
|
|
|
|
|
<span>登录</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="dropdown-item" @click="goToRegister">
|
|
|
|
|
<i class="item-icon icon-register"></i>
|
|
|
|
|
<span>注册</span>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<!-- 已登录时的菜单 -->
|
|
|
|
|
<template v-else>
|
|
|
|
|
<div class="dropdown-item" @click="goToProfile">
|
|
|
|
|
<i class="item-icon icon-profile"></i>
|
|
|
|
|
<span>个人资料</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="dropdown-item" @click="goToSettings">
|
|
|
|
|
<i class="item-icon icon-settings"></i>
|
|
|
|
|
<span>账户设置</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div class="dropdown-divider"></div>
|
|
|
|
|
<div class="dropdown-item danger" @click="handleLogout">
|
|
|
|
|
<i class="item-icon icon-logout"></i>
|
|
|
|
|
<span>退出登录</span>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
2025-10-07 13:31:06 +08:00
|
|
|
</div>
|
|
|
|
|
</transition>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
import { ref, computed } from 'vue';
|
|
|
|
|
import { useRouter } from 'vue-router';
|
2025-10-08 14:11:54 +08:00
|
|
|
import { useStore } from 'vuex';
|
2025-10-07 13:31:06 +08:00
|
|
|
import type { UserVO } from '@/types';
|
|
|
|
|
|
|
|
|
|
// Props
|
|
|
|
|
interface Props {
|
|
|
|
|
user?: UserVO | null;
|
|
|
|
|
collapsed?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const props = defineProps<Props>();
|
|
|
|
|
|
|
|
|
|
// Emits
|
|
|
|
|
const emit = defineEmits<{
|
|
|
|
|
logout: [];
|
|
|
|
|
}>();
|
|
|
|
|
|
|
|
|
|
// 状态
|
|
|
|
|
const dropdownVisible = ref(false);
|
|
|
|
|
|
|
|
|
|
// Composition API
|
|
|
|
|
const router = useRouter();
|
2025-10-08 14:11:54 +08:00
|
|
|
const store = useStore();
|
2025-10-07 13:31:06 +08:00
|
|
|
|
|
|
|
|
// 计算属性
|
2025-10-08 14:11:54 +08:00
|
|
|
const isLoggedIn = computed(() => {
|
|
|
|
|
return store.getters['auth/isAuthenticated'];
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-07 13:31:06 +08:00
|
|
|
const userAvatar = computed(() => {
|
|
|
|
|
return props.user?.avatar || '';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const avatarText = computed(() => {
|
|
|
|
|
const name = props.user?.realName || props.user?.username || '';
|
|
|
|
|
return name.charAt(0).toUpperCase();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const primaryRole = computed(() => {
|
|
|
|
|
// 这里可以从store中获取用户角色信息
|
|
|
|
|
return '管理员'; // 暂时硬编码
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 方法 - 使用 function 声明
|
|
|
|
|
function toggleDropdown() {
|
|
|
|
|
dropdownVisible.value = !dropdownVisible.value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function closeDropdown() {
|
|
|
|
|
dropdownVisible.value = false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-08 14:11:54 +08:00
|
|
|
// 未登录时的操作
|
|
|
|
|
function goToLogin() {
|
|
|
|
|
closeDropdown();
|
|
|
|
|
router.push('/login');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function goToRegister() {
|
|
|
|
|
closeDropdown();
|
|
|
|
|
router.push('/register');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 已登录时的操作
|
2025-10-07 13:31:06 +08:00
|
|
|
function goToProfile() {
|
|
|
|
|
closeDropdown();
|
|
|
|
|
router.push('/profile');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function goToSettings() {
|
|
|
|
|
closeDropdown();
|
|
|
|
|
router.push('/profile/settings');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleLogout() {
|
|
|
|
|
closeDropdown();
|
|
|
|
|
emit('logout');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 点击外部关闭指令
|
|
|
|
|
const vClickOutside = {
|
|
|
|
|
mounted(el: any, binding: any) {
|
|
|
|
|
el._clickOutside = (event: Event) => {
|
|
|
|
|
if (!(el === event.target || el.contains(event.target as Node))) {
|
|
|
|
|
binding.value();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
document.addEventListener('click', el._clickOutside);
|
|
|
|
|
},
|
|
|
|
|
unmounted(el: any) {
|
|
|
|
|
document.removeEventListener('click', el._clickOutside);
|
|
|
|
|
delete el._clickOutside;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
.user-dropdown {
|
2025-10-08 14:11:54 +08:00
|
|
|
color: #ffffff;
|
2025-10-07 13:31:06 +08:00
|
|
|
position: relative;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
user-select: none;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-08 14:11:54 +08:00
|
|
|
.login-info,
|
2025-10-07 13:31:06 +08:00
|
|
|
.user-info {
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
padding: 8px 12px;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
transition: background-color 0.2s ease;
|
|
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
|
background-color: #f5f5f5;
|
2025-10-08 14:11:54 +08:00
|
|
|
color: #000000;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.login-text {
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
|
|
|
|
|
.login-icon {
|
|
|
|
|
margin-right: 8px;
|
|
|
|
|
font-size: 16px;
|
2025-10-07 13:31:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.user-avatar {
|
|
|
|
|
width: 32px;
|
|
|
|
|
height: 32px;
|
|
|
|
|
border-radius: 50%;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
margin-right: 12px;
|
|
|
|
|
|
|
|
|
|
img {
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
object-fit: cover;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.avatar-placeholder {
|
|
|
|
|
width: 100%;
|
|
|
|
|
height: 100%;
|
|
|
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
|
|
|
color: white;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.user-details {
|
|
|
|
|
flex: 1;
|
|
|
|
|
min-width: 0;
|
|
|
|
|
|
|
|
|
|
.user-name {
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
font-weight: 500;
|
|
|
|
|
color: #333;
|
|
|
|
|
line-height: 1.2;
|
|
|
|
|
white-space: nowrap;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
text-overflow: ellipsis;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.user-role {
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
color: #666;
|
|
|
|
|
margin-top: 2px;
|
|
|
|
|
line-height: 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.dropdown-icon {
|
|
|
|
|
margin-left: 8px;
|
|
|
|
|
font-size: 12px;
|
|
|
|
|
color: #666;
|
|
|
|
|
transition: transform 0.2s ease;
|
|
|
|
|
|
|
|
|
|
&::before {
|
|
|
|
|
content: "▼";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
&.open {
|
|
|
|
|
transform: rotate(180deg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.dropdown-menu {
|
|
|
|
|
position: absolute;
|
|
|
|
|
top: 100%;
|
|
|
|
|
right: 0;
|
|
|
|
|
min-width: 160px;
|
|
|
|
|
background: white;
|
|
|
|
|
border-radius: 4px;
|
|
|
|
|
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
|
|
|
|
|
border: 1px solid #e8e8e8;
|
|
|
|
|
z-index: 1000;
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.dropdown-item {
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
padding: 12px 16px;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
transition: background-color 0.2s ease;
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
color: #333;
|
|
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
|
background-color: #f5f5f5;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
&.danger {
|
|
|
|
|
color: #ff4d4f;
|
|
|
|
|
|
|
|
|
|
&:hover {
|
|
|
|
|
background-color: #fff2f0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.item-icon {
|
|
|
|
|
width: 16px;
|
|
|
|
|
margin-right: 12px;
|
|
|
|
|
text-align: center;
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.dropdown-divider {
|
|
|
|
|
height: 1px;
|
|
|
|
|
background-color: #e8e8e8;
|
|
|
|
|
margin: 4px 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 动画效果 */
|
|
|
|
|
.dropdown-enter-active,
|
|
|
|
|
.dropdown-leave-active {
|
|
|
|
|
transition: all 0.2s ease;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.dropdown-enter-from,
|
|
|
|
|
.dropdown-leave-to {
|
|
|
|
|
opacity: 0;
|
|
|
|
|
transform: translateY(-4px);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 图标字体类 */
|
2025-10-08 14:11:54 +08:00
|
|
|
.icon-login::before { content: "🔑"; }
|
|
|
|
|
.icon-register::before { content: "📝"; }
|
2025-10-07 13:31:06 +08:00
|
|
|
.icon-profile::before { content: "👤"; }
|
|
|
|
|
.icon-settings::before { content: "⚙️"; }
|
|
|
|
|
.icon-logout::before { content: "🚪"; }
|
|
|
|
|
</style>
|