302 lines
6.7 KiB
Vue
302 lines
6.7 KiB
Vue
<template>
|
|
<div class="user-menu" ref="menuRef">
|
|
<!-- 用户按钮 -->
|
|
<div class="user-button" @click="toggleMenu">
|
|
<div class="user-avatar">
|
|
<img v-if="userAvatar" :src="userAvatar" alt="用户头像" />
|
|
<span v-else class="avatar-placeholder">{{ userInitial }}</span>
|
|
</div>
|
|
<div class="user-info">
|
|
<div class="user-name">{{ userName }}</div>
|
|
<div class="user-email">{{ userEmail }}</div>
|
|
</div>
|
|
<t-icon :name="menuVisible ? 'chevron-up' : 'chevron-down'" class="dropdown-icon" />
|
|
</div>
|
|
|
|
<!-- 下拉菜单 -->
|
|
<Transition name="dropdown">
|
|
<div v-if="menuVisible" class="user-dropdown" @click.stop>
|
|
<div class="menu-item" @click="handleQuickNav('models')">
|
|
<t-icon name="component" class="menu-icon" />
|
|
<span>模型配置</span>
|
|
</div>
|
|
<div class="menu-item" @click="handleQuickNav('ollama')">
|
|
<span class="menu-icon emoji-icon">🦙</span>
|
|
<span>Ollama</span>
|
|
</div>
|
|
<div class="menu-item" @click="handleQuickNav('knowledge')">
|
|
<t-icon name="layers" class="menu-icon" />
|
|
<span>知识库</span>
|
|
</div>
|
|
<div class="menu-divider"></div>
|
|
<div class="menu-item" @click="handleSettings">
|
|
<t-icon name="setting" class="menu-icon" />
|
|
<span>全部设置</span>
|
|
</div>
|
|
<div class="menu-divider"></div>
|
|
<div class="menu-item danger" @click="handleLogout">
|
|
<t-icon name="logout" class="menu-icon" />
|
|
<span>注销</span>
|
|
</div>
|
|
</div>
|
|
</Transition>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ref, computed, onMounted, onUnmounted } from 'vue'
|
|
import { useRouter } from 'vue-router'
|
|
import { useUIStore } from '@/stores/ui'
|
|
import { MessagePlugin } from 'tdesign-vue-next'
|
|
import { getCurrentUser } from '@/api/auth'
|
|
|
|
const router = useRouter()
|
|
const uiStore = useUIStore()
|
|
|
|
const menuRef = ref<HTMLElement>()
|
|
const menuVisible = ref(false)
|
|
|
|
// 用户信息
|
|
const userInfo = ref({
|
|
username: '用户',
|
|
email: 'user@example.com',
|
|
avatar: ''
|
|
})
|
|
|
|
const userName = computed(() => userInfo.value.username)
|
|
const userEmail = computed(() => userInfo.value.email)
|
|
const userAvatar = computed(() => userInfo.value.avatar)
|
|
|
|
// 用户名首字母(用于无头像时显示)
|
|
const userInitial = computed(() => {
|
|
return userName.value.charAt(0).toUpperCase()
|
|
})
|
|
|
|
// 切换菜单显示
|
|
const toggleMenu = () => {
|
|
menuVisible.value = !menuVisible.value
|
|
}
|
|
|
|
// 快捷导航到设置的特定部分
|
|
const handleQuickNav = (section: string) => {
|
|
menuVisible.value = false
|
|
uiStore.openSettings()
|
|
router.push('/platform/settings')
|
|
|
|
// 延迟一下,确保设置页面已经渲染
|
|
setTimeout(() => {
|
|
// 触发设置页面切换到对应section
|
|
const event = new CustomEvent('settings-nav', { detail: { section } })
|
|
window.dispatchEvent(event)
|
|
}, 100)
|
|
}
|
|
|
|
// 打开设置
|
|
const handleSettings = () => {
|
|
menuVisible.value = false
|
|
uiStore.openSettings()
|
|
router.push('/platform/settings')
|
|
}
|
|
|
|
// 注销
|
|
const handleLogout = () => {
|
|
menuVisible.value = false
|
|
|
|
// 清理本地存储
|
|
localStorage.removeItem('WeKnora_token')
|
|
|
|
MessagePlugin.success('已退出登录')
|
|
|
|
// 跳转到登录页
|
|
router.push('/login')
|
|
}
|
|
|
|
// 加载用户信息
|
|
const loadUserInfo = async () => {
|
|
try {
|
|
const response = await getCurrentUser()
|
|
if (response.success && response.data && response.data.user) {
|
|
userInfo.value = {
|
|
username: response.data.user.username || '用户',
|
|
email: response.data.user.email || 'user@example.com',
|
|
avatar: response.data.user.avatar || ''
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to load user info:', error)
|
|
}
|
|
}
|
|
|
|
// 点击外部关闭菜单
|
|
const handleClickOutside = (e: MouseEvent) => {
|
|
if (menuRef.value && !menuRef.value.contains(e.target as Node)) {
|
|
menuVisible.value = false
|
|
}
|
|
}
|
|
|
|
onMounted(() => {
|
|
document.addEventListener('click', handleClickOutside)
|
|
loadUserInfo()
|
|
})
|
|
|
|
onUnmounted(() => {
|
|
document.removeEventListener('click', handleClickOutside)
|
|
})
|
|
</script>
|
|
|
|
<style lang="less" scoped>
|
|
.user-menu {
|
|
position: relative;
|
|
width: 100%;
|
|
}
|
|
|
|
.user-button {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 12px;
|
|
padding: 12px 16px;
|
|
border-radius: 8px;
|
|
cursor: pointer;
|
|
transition: all 0.2s;
|
|
background: transparent;
|
|
|
|
&:hover {
|
|
background: var(--td-bg-color-component-hover);
|
|
}
|
|
|
|
&:active {
|
|
transform: scale(0.98);
|
|
}
|
|
}
|
|
|
|
.user-avatar {
|
|
width: 40px;
|
|
height: 40px;
|
|
border-radius: 50%;
|
|
overflow: hidden;
|
|
flex-shrink: 0;
|
|
background: linear-gradient(135deg, var(--td-brand-color) 0%, var(--td-brand-color-active) 100%);
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
|
|
img {
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: cover;
|
|
}
|
|
|
|
.avatar-placeholder {
|
|
color: var(--td-text-color-anti);
|
|
font-size: 16px;
|
|
font-weight: 600;
|
|
}
|
|
}
|
|
|
|
.user-info {
|
|
flex: 1;
|
|
min-width: 0;
|
|
text-align: left;
|
|
|
|
.user-name {
|
|
font-size: 14px;
|
|
font-weight: 500;
|
|
color: var(--td-text-color-primary);
|
|
white-space: nowrap;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
}
|
|
|
|
.user-email {
|
|
font-size: 12px;
|
|
color: var(--td-text-color-secondary);
|
|
white-space: nowrap;
|
|
overflow: hidden;
|
|
text-overflow: ellipsis;
|
|
}
|
|
}
|
|
|
|
.dropdown-icon {
|
|
font-size: 16px;
|
|
color: var(--td-text-color-secondary);
|
|
flex-shrink: 0;
|
|
transition: transform 0.2s;
|
|
}
|
|
|
|
.user-dropdown {
|
|
position: absolute;
|
|
bottom: 100%;
|
|
left: 8px;
|
|
right: 8px;
|
|
margin-bottom: 8px;
|
|
background: var(--td-bg-color-container);
|
|
border-radius: 8px;
|
|
box-shadow: var(--td-shadow-2);
|
|
border: 1px solid var(--td-component-border);
|
|
overflow: hidden;
|
|
z-index: 1000;
|
|
}
|
|
|
|
.menu-item {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 12px;
|
|
padding: 12px 16px;
|
|
cursor: pointer;
|
|
transition: all 0.2s;
|
|
font-size: 14px;
|
|
color: var(--td-text-color-primary);
|
|
|
|
&:hover {
|
|
background: var(--td-bg-color-component-hover);
|
|
}
|
|
|
|
&.danger {
|
|
color: var(--td-error-color);
|
|
|
|
&:hover {
|
|
background: var(--td-error-color-light);
|
|
}
|
|
|
|
.menu-icon {
|
|
color: var(--td-error-color);
|
|
}
|
|
}
|
|
|
|
.menu-icon {
|
|
font-size: 16px;
|
|
color: var(--td-text-color-secondary);
|
|
}
|
|
}
|
|
|
|
.menu-divider {
|
|
height: 1px;
|
|
background: var(--td-component-border);
|
|
margin: 4px 0;
|
|
}
|
|
|
|
.emoji-icon {
|
|
font-size: 16px;
|
|
line-height: 1;
|
|
}
|
|
|
|
// 下拉动画
|
|
.dropdown-enter-active,
|
|
.dropdown-leave-active {
|
|
transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
|
|
}
|
|
|
|
.dropdown-enter-from,
|
|
.dropdown-leave-to {
|
|
opacity: 0;
|
|
transform: translateY(8px);
|
|
}
|
|
|
|
.dropdown-enter-to,
|
|
.dropdown-leave-from {
|
|
opacity: 1;
|
|
transform: translateY(0);
|
|
}
|
|
</style>
|
|
|