Files
QLinker/frontend/src/components/account/AccountSwitcher.vue
T

397 lines
9.6 KiB
Vue
Raw Normal View History

<script setup>
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
import { RouterLink } from 'vue-router'
import { getToken } from '../../api.js'
import AvatarBadge from '../AvatarBadge.vue'
import {
accountState,
currentAccount,
loadAccounts,
setCurrentAccount,
statusText
} from '../../account-context.js'
const open = ref(false)
const root = ref(null)
const search = ref('')
const current = computed(() => currentAccount())
const filteredItems = computed(() => {
const keyword = search.value.trim().toLowerCase()
if (!keyword) return accountState.items
return accountState.items.filter((item) => {
const name = String(item.displayName || item.nickname || '').toLowerCase()
return name.includes(keyword) || String(item.qqAccountId).toLowerCase().includes(keyword)
})
})
const triggerTitle = computed(() => {
if (current.value) {
return `${current.value.displayName || current.value.qqAccountId}(${current.value.qqAccountId})`
}
return accountState.items.length ? '正在确定当前账号' : '未绑定可用账号'
})
/** 只有一个账号时没有可切换项,入口降级为纯展示。 */
const canSwitch = computed(() => accountState.items.length > 1)
function toggle() {
if (!canSwitch.value) return
if (!open.value && !accountState.loaded && !accountState.loading) loadAccounts()
open.value = !open.value
if (!open.value) search.value = ''
}
function choose(qqAccountId) {
if (!qqAccountId) return
setCurrentAccount(qqAccountId)
open.value = false
search.value = ''
}
function onDocumentClick(event) {
if (open.value && root.value && !root.value.contains(event.target)) open.value = false
}
function onKeydown(event) {
if (event.key === 'Escape') open.value = false
}
function retry() {
loadAccounts()
}
onMounted(() => {
document.addEventListener('click', onDocumentClick)
document.addEventListener('keydown', onKeydown)
if (getToken() && !accountState.loaded) loadAccounts()
})
onBeforeUnmount(() => {
document.removeEventListener('click', onDocumentClick)
document.removeEventListener('keydown', onKeydown)
})
</script>
<template>
<div ref="root" class="account-switcher">
<button
type="button"
class="account-trigger"
:class="{ 'is-static': !canSwitch }"
:aria-expanded="open"
:aria-haspopup="canSwitch ? 'listbox' : undefined"
v-tooltip="triggerTitle"
@click="toggle"
>
<AvatarBadge
v-if="current"
:media-uid="current.avatarMediaUid"
:name="current.displayName || current.qqAccountId"
type="ACCOUNT"
size="sm"
/>
<AvatarBadge v-else name="账号" type="ACCOUNT" size="sm" />
<span class="account-meta">
<span class="account-name">{{ current ? (current.displayName || current.qqAccountId) : '未绑定可用账号' }}</span>
<span class="account-qq">
{{ current ? `${current.qqAccountId} · ${statusText(current.connectionStatus)}` : (accountState.loading ? '正在加载账号目录…' : '请先在接入管理绑定账号') }}
</span>
</span>
<span v-if="canSwitch" class="account-switch-text">切换</span>
</button>
<div v-if="open && canSwitch" class="account-popover" role="listbox" aria-label="选择 QQ 账号" @click.stop>
<div class="account-popover-title">切换 QQ 账号</div>
<input
v-if="accountState.items.length > 1"
v-model="search"
class="account-search"
type="search"
placeholder="搜索昵称或 QQ 号"
aria-label="搜索账号"
/>
<template v-if="accountState.items.length">
<button
v-for="item in filteredItems"
:key="item.qqAccountId"
type="button"
class="account-option"
:class="{ active: accountState.current === item.qqAccountId }"
role="option"
:aria-selected="accountState.current === item.qqAccountId"
@click="choose(item.qqAccountId)"
>
<AvatarBadge
:media-uid="item.avatarMediaUid"
:name="item.displayName || item.qqAccountId"
type="ACCOUNT"
size="sm"
/>
<span class="account-option-meta">
<span class="account-option-name">{{ item.displayName || item.qqAccountId }}</span>
<span class="account-option-sub">
{{ item.qqAccountId }} · {{ statusText(item.connectionStatus) }} · {{ item.bindingCount }} 条绑定
</span>
</span>
<span class="account-status-dot" :class="String(item.connectionStatus).toLowerCase()" />
</button>
<p v-if="search && filteredItems.length === 0" class="account-note">没有匹配的账号</p>
</template>
<p v-if="accountState.loading" class="account-note">正在加载账号目录…</p>
<p v-else-if="accountState.error" class="account-note warn">
账号目录加载失败:{{ accountState.error }}
<button type="button" class="account-retry" @click.stop="retry">重试</button>
</p>
<p v-if="accountState.unattributedBindings > 0" class="account-note">
另有 {{ accountState.unattributedBindings }} 条启用绑定未回填 QQ 号,未计入账号列表。
</p>
<p v-if="accountState.message" class="account-note warn">{{ accountState.message }}</p>
</div>
<div v-if="!accountState.loading && !accountState.items.length" class="account-empty-inline">
<p>{{ accountState.error ? `账号目录加载失败:${accountState.error}` : '当前实例没有可用 QQ 账号' }}</p>
<RouterLink v-if="!accountState.error" to="/connections" class="account-link">去接入管理绑定并上线</RouterLink>
<button v-else type="button" class="account-retry" @click.stop="retry">重试</button>
</div>
</div>
</template>
<style scoped>
.account-switcher {
position: relative;
width: 100%;
}
.account-trigger {
display: flex;
align-items: center;
gap: 8px;
width: 100%;
padding: 8px;
border: 1px solid var(--line);
border-radius: 10px;
background: color-mix(in srgb, var(--card-bg) 80%, transparent);
color: var(--qk-text);
cursor: pointer;
text-align: left;
}
.account-trigger:hover,
.account-trigger:focus-visible {
border-color: var(--qk-green);
outline: none;
}
/* 仅一个账号时没有可切换项,入口按展示样式处理,避免误导为可点。 */
.account-trigger.is-static {
cursor: default;
}
.account-trigger.is-static:hover {
border-color: var(--line);
}
.account-empty-inline {
margin-top: 8px;
padding: 8px 10px;
border: 1px dashed var(--line);
border-radius: 10px;
font-size: 11px;
line-height: 1.6;
color: var(--muted);
}
.account-empty-inline p {
margin: 0;
}
.account-empty-inline .account-link {
display: inline-block;
margin-top: 2px;
}
.account-meta {
display: flex;
flex-direction: column;
gap: 2px;
min-width: 0;
flex: 1;
}
.account-name,
.account-qq {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.account-name {
font-size: 12px;
font-weight: 600;
}
.account-qq {
font-size: 11px;
color: var(--muted);
}
.account-switch-text {
flex: 0 0 auto;
font-size: 11px;
color: var(--qk-green);
}
.account-popover {
position: absolute;
left: 0;
bottom: calc(100% + 8px);
z-index: 40;
width: 264px;
max-height: 60vh;
overflow: auto;
padding: 10px;
border: 1px solid var(--line);
border-radius: 12px;
background: var(--card-bg);
box-shadow: 0 12px 30px rgba(0, 0, 0, 0.18);
}
.account-popover-title {
padding: 2px 6px 8px;
font-size: 11px;
color: var(--muted);
}
.account-search {
width: 100%;
margin-bottom: 8px;
padding: 7px 9px;
border: 1px solid var(--line);
border-radius: 8px;
background: var(--card-bg);
color: var(--qk-text);
font-size: 12px;
}
.account-search:focus {
border-color: var(--qk-green);
outline: none;
}
.account-empty {
padding: 14px 8px 6px;
text-align: center;
}
.account-empty strong {
display: block;
font-size: 13px;
margin-bottom: 6px;
}
.account-empty p {
margin: 0 0 10px;
font-size: 11px;
color: var(--muted);
line-height: 1.6;
}
.account-link {
font-size: 12px;
color: var(--qk-green);
text-decoration: none;
}
.account-link:hover {
text-decoration: underline;
}
.account-option {
display: flex;
align-items: center;
gap: 8px;
width: 100%;
padding: 7px 6px;
border: none;
border-radius: 8px;
background: transparent;
color: var(--qk-text);
cursor: pointer;
text-align: left;
}
.account-option:hover,
.account-option:focus-visible {
background: color-mix(in srgb, var(--qk-green) 10%, transparent);
outline: none;
}
.account-option.active {
background: color-mix(in srgb, var(--qk-green) 16%, transparent);
}
.account-option-meta {
display: flex;
flex-direction: column;
gap: 2px;
min-width: 0;
flex: 1;
}
.account-option-name,
.account-option-sub {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.account-option-name {
font-size: 12px;
font-weight: 600;
}
.account-option-sub {
font-size: 11px;
color: var(--muted);
}
.account-status-dot {
flex: 0 0 7px;
width: 7px;
height: 7px;
border-radius: 50%;
background: var(--muted);
}
.account-status-dot.online {
background: var(--qk-green);
}
.account-status-dot.connecting {
background: var(--qk-amber);
}
.account-status-dot.error {
background: var(--qk-red);
}
.account-note {
margin: 8px 6px 2px;
font-size: 11px;
color: var(--muted);
line-height: 1.5;
}
.account-note.warn {
color: var(--qk-amber);
}
.account-retry {
border: none;
background: transparent;
color: var(--qk-green);
cursor: pointer;
font-size: 11px;
padding: 0 2px;
}
</style>