mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-18 21:10:57 +08:00
312 lines
6.8 KiB
Vue
312 lines
6.8 KiB
Vue
<template>
|
|
<header class="header">
|
|
<div class="header-content">
|
|
<div class="header-content-left">
|
|
<div v-if="showMenuBtn" class="menu-btn-wrapper">
|
|
<button class="menu-btn" ref="menuBtn" @click="$emit('toggle-menu')">
|
|
<i class="fas fa-bars"></i>
|
|
</button>
|
|
<span v-if="isMobile && unreadCount > 0" class="menu-unread-dot"></span>
|
|
</div>
|
|
<NuxtLink class="logo-container" to="/" replace @click="handleLogoClick">
|
|
<img
|
|
alt="OpenIsle"
|
|
src="https://openisle-1307107697.cos.ap-guangzhou.myqcloud.com/assert/image.png"
|
|
width="60"
|
|
height="60"
|
|
/>
|
|
<div class="logo-text">OpenIsle</div>
|
|
</NuxtLink>
|
|
</div>
|
|
|
|
<ClientOnly>
|
|
<div v-if="isLogin" class="header-content-right">
|
|
<div v-if="isMobile" class="search-icon" @click="search">
|
|
<i class="fas fa-search"></i>
|
|
</div>
|
|
<DropdownMenu ref="userMenu" :items="headerMenuItems">
|
|
<template #trigger>
|
|
<div class="avatar-container">
|
|
<img class="avatar-img" :src="avatar" alt="avatar" />
|
|
<i class="fas fa-caret-down dropdown-icon"></i>
|
|
</div>
|
|
</template>
|
|
</DropdownMenu>
|
|
</div>
|
|
|
|
<div v-else class="header-content-right">
|
|
<div v-if="isMobile" class="search-icon" @click="search">
|
|
<i class="fas fa-search"></i>
|
|
</div>
|
|
<div class="header-content-item-main" @click="goToLogin">登录</div>
|
|
<div class="header-content-item-secondary" @click="goToSignup">注册</div>
|
|
</div>
|
|
</ClientOnly>
|
|
|
|
<SearchDropdown ref="searchDropdown" v-if="isMobile && showSearch" @close="closeSearch" />
|
|
</div>
|
|
</header>
|
|
</template>
|
|
|
|
<script setup>
|
|
import { ClientOnly } from '#components'
|
|
import { computed, nextTick, ref, watch } from 'vue'
|
|
import { useRouter } from 'vue-router'
|
|
import DropdownMenu from '~/components/DropdownMenu.vue'
|
|
import SearchDropdown from '~/components/SearchDropdown.vue'
|
|
import { authState, clearToken, loadCurrentUser } from '~/utils/auth'
|
|
import { fetchUnreadCount, notificationState } from '~/utils/notification'
|
|
import { useIsMobile } from '~/utils/screen'
|
|
const props = defineProps({
|
|
showMenuBtn: {
|
|
type: Boolean,
|
|
default: true,
|
|
},
|
|
})
|
|
|
|
const isLogin = computed(() => authState.loggedIn)
|
|
const isMobile = useIsMobile()
|
|
const unreadCount = computed(() => notificationState.unreadCount)
|
|
const router = useRouter()
|
|
const avatar = ref('')
|
|
const showSearch = ref(false)
|
|
const searchDropdown = ref(null)
|
|
const userMenu = ref(null)
|
|
const menuBtn = ref(null)
|
|
|
|
const handleLogoClick = (event) => {
|
|
if (router.currentRoute.value.fullPath === '/') {
|
|
event.preventDefault()
|
|
window.dispatchEvent(new Event('refresh-home'))
|
|
}
|
|
}
|
|
const search = () => {
|
|
showSearch.value = true
|
|
nextTick(() => {
|
|
searchDropdown.value.toggle()
|
|
})
|
|
}
|
|
const closeSearch = () => {
|
|
nextTick(() => {
|
|
showSearch.value = false
|
|
})
|
|
}
|
|
const goToLogin = () => {
|
|
navigateTo('/login', { replace: true })
|
|
}
|
|
const goToSettings = () => {
|
|
navigateTo('/settings', { replace: true })
|
|
}
|
|
const goToProfile = async () => {
|
|
if (!authState.loggedIn) {
|
|
navigateTo('/login', { replace: true })
|
|
return
|
|
}
|
|
let id = authState.username || authState.userId
|
|
if (!id) {
|
|
const user = await loadCurrentUser()
|
|
if (user) {
|
|
id = user.username || user.id
|
|
}
|
|
}
|
|
if (id) {
|
|
navigateTo(`/users/${id}`, { replace: true })
|
|
}
|
|
}
|
|
const goToSignup = () => {
|
|
navigateTo('/signup', { replace: true })
|
|
}
|
|
const goToLogout = () => {
|
|
clearToken()
|
|
navigateTo('/login', { replace: true })
|
|
}
|
|
|
|
const headerMenuItems = computed(() => [
|
|
{ text: '设置', onClick: goToSettings },
|
|
{ text: '个人主页', onClick: goToProfile },
|
|
{ text: '退出', onClick: goToLogout },
|
|
])
|
|
|
|
onMounted(async () => {
|
|
const updateAvatar = async () => {
|
|
if (authState.loggedIn) {
|
|
const user = await loadCurrentUser()
|
|
if (user && user.avatar) {
|
|
avatar.value = user.avatar
|
|
}
|
|
}
|
|
}
|
|
const updateUnread = async () => {
|
|
if (authState.loggedIn) {
|
|
await fetchUnreadCount()
|
|
} else {
|
|
notificationState.unreadCount = 0
|
|
}
|
|
}
|
|
|
|
await updateAvatar()
|
|
await updateUnread()
|
|
|
|
watch(
|
|
() => authState.loggedIn,
|
|
async () => {
|
|
await updateAvatar()
|
|
await updateUnread()
|
|
},
|
|
)
|
|
|
|
watch(
|
|
() => router.currentRoute.value.fullPath,
|
|
() => {
|
|
if (userMenu.value) userMenu.value.close()
|
|
showSearch.value = false
|
|
},
|
|
)
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
.header {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
height: var(--header-height);
|
|
background-color: var(--background-color-blur);
|
|
backdrop-filter: blur(10px);
|
|
color: var(--header-text-color);
|
|
border-bottom: 1px solid var(--header-border-color);
|
|
}
|
|
|
|
.logo-container {
|
|
display: flex;
|
|
align-items: center;
|
|
font-size: 20px;
|
|
font-weight: bold;
|
|
cursor: pointer;
|
|
text-decoration: none;
|
|
color: inherit;
|
|
}
|
|
|
|
.header-content {
|
|
display: flex;
|
|
flex-direction: row;
|
|
align-items: center;
|
|
justify-content: space-between;
|
|
width: 100%;
|
|
height: 100%;
|
|
margin: 0 auto;
|
|
max-width: var(--page-max-width);
|
|
}
|
|
|
|
.header-content-left {
|
|
display: flex;
|
|
flex-direction: row;
|
|
align-items: center;
|
|
}
|
|
|
|
.header-content-right {
|
|
display: flex;
|
|
flex-direction: row;
|
|
align-items: center;
|
|
gap: 20px;
|
|
}
|
|
|
|
.menu-btn {
|
|
font-size: 24px;
|
|
background: none;
|
|
border: none;
|
|
color: inherit;
|
|
cursor: pointer;
|
|
opacity: 0.4;
|
|
margin-right: 10px;
|
|
}
|
|
|
|
.menu-btn-wrapper {
|
|
position: relative;
|
|
display: inline-block;
|
|
}
|
|
|
|
.menu-unread-dot {
|
|
position: absolute;
|
|
top: 0;
|
|
right: 10px;
|
|
width: 8px;
|
|
height: 8px;
|
|
border-radius: 50%;
|
|
background-color: #ff4d4f;
|
|
}
|
|
|
|
.menu-btn:hover {
|
|
opacity: 0.8;
|
|
}
|
|
|
|
.header-content-item-main {
|
|
background-color: var(--primary-color);
|
|
color: white;
|
|
padding: 8px 16px;
|
|
border-radius: 10px;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.header-content-item-main:hover {
|
|
background-color: var(--primary-color-hover);
|
|
}
|
|
|
|
.header-content-item-secondary {
|
|
color: var(--primary-color);
|
|
text-decoration: underline;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.avatar-container {
|
|
position: relative;
|
|
display: flex;
|
|
align-items: center;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.avatar-img {
|
|
width: 32px;
|
|
height: 32px;
|
|
border-radius: 50%;
|
|
background-color: lightgray;
|
|
object-fit: cover;
|
|
}
|
|
|
|
.dropdown-icon {
|
|
margin-left: 5px;
|
|
}
|
|
|
|
.dropdown-item {
|
|
padding: 8px 16px;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.dropdown-item:hover {
|
|
background-color: var(--menu-selected-background-color);
|
|
}
|
|
|
|
.search-icon {
|
|
font-size: 18px;
|
|
cursor: pointer;
|
|
}
|
|
|
|
@media (max-width: 1200px) {
|
|
.header-content {
|
|
padding-left: 15px;
|
|
padding-right: 15px;
|
|
width: calc(100% - 30px);
|
|
}
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.header-content-item-secondary {
|
|
display: none;
|
|
}
|
|
|
|
.logo-text {
|
|
display: none;
|
|
}
|
|
}
|
|
</style>
|