228 lines
8.9 KiB
Vue
228 lines
8.9 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onMounted, ref } from 'vue'
|
|
import {
|
|
ArrowLeftRight,
|
|
Bell,
|
|
ChevronLeft,
|
|
ChevronRight,
|
|
CircleHelp,
|
|
LogOut,
|
|
Menu,
|
|
Search,
|
|
Settings,
|
|
ShieldCheck,
|
|
X,
|
|
} from 'lucide-vue-next'
|
|
import { adminNavigation, userNavigation } from './data'
|
|
import AuthView from './views/AuthView.vue'
|
|
import AdminConsole from './views/AdminConsole.vue'
|
|
import UserConsole from './views/UserConsole.vue'
|
|
import AccountSettings from './components/AccountSettings.vue'
|
|
import type { Portal } from './types'
|
|
import { authClient, type AuthUser } from './services/auth'
|
|
|
|
const portal = ref<Portal>('user')
|
|
const activeView = ref('overview')
|
|
const sidebarCollapsed = ref(false)
|
|
const mobileNavigationOpen = ref(false)
|
|
const toastMessage = ref('')
|
|
const authenticated = ref(false)
|
|
const authUser = ref<AuthUser>()
|
|
const accountMenuOpen = ref(false)
|
|
const userName = ref('')
|
|
const availableCredit = ref<number>()
|
|
const accountSettingsOpen = ref(false)
|
|
|
|
onMounted(async () => {
|
|
try {
|
|
const user = await authClient.currentUser()
|
|
if (user) {
|
|
authUser.value = user
|
|
userName.value = user.displayName
|
|
authenticated.value = true
|
|
}
|
|
} catch {
|
|
notify('认证服务暂不可用,请稍后重试')
|
|
}
|
|
})
|
|
|
|
const navigation = computed(() => portal.value === 'user' ? userNavigation : adminNavigation)
|
|
const activeLabel = computed(() => navigation.value.flatMap((group) => group.items).find((item) => item.id === activeView.value)?.label ?? '总览')
|
|
|
|
function navigate(view: string) {
|
|
activeView.value = view
|
|
mobileNavigationOpen.value = false
|
|
accountMenuOpen.value = false
|
|
window.scrollTo({ top: 0, behavior: 'smooth' })
|
|
}
|
|
|
|
function switchPortal() {
|
|
if (!['admin', 'super_admin'].includes(authUser.value?.role || '')) {
|
|
notify('当前账号没有管理端权限')
|
|
return
|
|
}
|
|
portal.value = portal.value === 'user' ? 'admin' : 'user'
|
|
activeView.value = portal.value === 'user' ? 'overview' : 'admin-overview'
|
|
mobileNavigationOpen.value = false
|
|
notify(portal.value === 'admin' ? '已进入管理端' : '已返回用户端')
|
|
}
|
|
|
|
function notify(message: string) {
|
|
toastMessage.value = message
|
|
window.setTimeout(() => {
|
|
if (toastMessage.value === message) toastMessage.value = ''
|
|
}, 2800)
|
|
}
|
|
|
|
function signIn(user: AuthUser) {
|
|
userName.value = user.displayName
|
|
authUser.value = user
|
|
authenticated.value = true
|
|
activeView.value = 'overview'
|
|
notify('登录成功,欢迎进入模亦灵枢')
|
|
}
|
|
|
|
function logout() {
|
|
availableCredit.value = undefined
|
|
accountMenuOpen.value = false
|
|
portal.value = 'user'
|
|
activeView.value = 'overview'
|
|
authenticated.value = false
|
|
authUser.value = undefined
|
|
void authClient.logout().catch(() => undefined)
|
|
notify('已安全退出登录')
|
|
}
|
|
|
|
function openAccountSettings() {
|
|
accountMenuOpen.value = false
|
|
accountSettingsOpen.value = true
|
|
}
|
|
|
|
function accountSaved(user: AuthUser) {
|
|
authUser.value = user
|
|
userName.value = user.displayName
|
|
accountSettingsOpen.value = false
|
|
}
|
|
|
|
function passwordChanged() {
|
|
accountSettingsOpen.value = false
|
|
accountMenuOpen.value = false
|
|
availableCredit.value = undefined
|
|
authUser.value = undefined
|
|
authenticated.value = false
|
|
userName.value = ''
|
|
portal.value = 'user'
|
|
activeView.value = 'overview'
|
|
}
|
|
|
|
function openHelp() {
|
|
accountMenuOpen.value = false
|
|
if (portal.value === 'user') {
|
|
navigate('docs')
|
|
notify('已打开接入文档')
|
|
return
|
|
}
|
|
navigate('admin-settings')
|
|
notify('平台设置页包含服务器配置说明')
|
|
}
|
|
|
|
function openGlobalSearch() {
|
|
const query = window.prompt('搜索模型、用户或功能')?.trim()
|
|
if (!query) return
|
|
const match = navigation.value.flatMap((group) => group.items).find((item) => `${item.label} ${item.id}`.toLowerCase().includes(query.toLowerCase()))
|
|
if (match) {
|
|
navigate(match.id)
|
|
notify(`已打开:${match.label}`)
|
|
} else {
|
|
notify('没有找到匹配的页面,请尝试模型名或功能名')
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<AuthView v-if="!authenticated" @success="signIn" @notify="notify" />
|
|
|
|
<div v-else class="app" :class="{ 'sidebar-collapsed': sidebarCollapsed, 'admin-mode': portal === 'admin' }">
|
|
<aside class="sidebar" :class="{ 'mobile-open': mobileNavigationOpen }">
|
|
<div class="sidebar-brand">
|
|
<span class="brand-mark"><img src="/assets/moyi-logo-mark.png" alt="模亦灵枢 Logo" /></span>
|
|
<div class="brand-name">
|
|
<strong>{{ portal === 'admin' ? '模亦灵枢管理端' : '模亦灵枢工作区' }}</strong>
|
|
<span>{{ portal === 'admin' ? 'ADMIN CONSOLE' : 'MODEL INTELLIGENCE HUB' }}</span>
|
|
</div>
|
|
<button class="icon-button mobile-only" aria-label="关闭菜单" @click="mobileNavigationOpen = false"><X :size="19" /></button>
|
|
</div>
|
|
|
|
<nav class="navigation">
|
|
<section v-for="group in navigation" :key="group.label" class="nav-group">
|
|
<p>{{ group.label }}</p>
|
|
<button
|
|
v-for="item in group.items"
|
|
:key="item.id"
|
|
class="nav-link"
|
|
:class="{ active: activeView === item.id }"
|
|
:title="sidebarCollapsed ? item.label : undefined"
|
|
@click="navigate(item.id)"
|
|
>
|
|
<component :is="item.icon" :size="18" />
|
|
<span>{{ item.label }}</span>
|
|
<em v-if="item.badge">{{ item.badge }}</em>
|
|
</button>
|
|
</section>
|
|
</nav>
|
|
|
|
<div class="sidebar-footer">
|
|
<div v-if="portal === 'user'" class="balance-card">
|
|
<span>可用额度</span>
|
|
<strong>{{ availableCredit === undefined ? '--' : `¥ ${availableCredit.toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 8 })}` }}</strong>
|
|
<small>人民币 CNY</small>
|
|
</div>
|
|
<div v-else class="security-card"><ShieldCheck :size="17" /><span>管理操作均写入审计日志</span></div>
|
|
<button class="nav-link" title="打开帮助与接入文档" @click="openHelp"><CircleHelp :size="18" /><span>帮助中心</span></button>
|
|
<button class="collapse-control desktop-only" @click="sidebarCollapsed = !sidebarCollapsed">
|
|
<ChevronRight v-if="sidebarCollapsed" :size="17" />
|
|
<ChevronLeft v-else :size="17" />
|
|
<span>收起侧栏</span>
|
|
</button>
|
|
</div>
|
|
</aside>
|
|
|
|
<div v-if="mobileNavigationOpen" class="mobile-overlay" @click="mobileNavigationOpen = false"></div>
|
|
|
|
<div class="workspace">
|
|
<header class="topbar">
|
|
<div class="topbar-left">
|
|
<button class="icon-button mobile-only" aria-label="打开菜单" @click="mobileNavigationOpen = true"><Menu :size="20" /></button>
|
|
<div class="breadcrumb"><span>{{ portal === 'admin' ? '管理端' : '用户端' }}</span><ChevronRight :size="14" /><strong>{{ activeLabel }}</strong></div>
|
|
</div>
|
|
<div class="topbar-actions">
|
|
<button class="global-search desktop-only" title="搜索模型、用户或功能" @click="openGlobalSearch"><Search :size="16" /><span>搜索模型、用户或功能</span><kbd>⌘ K</kbd></button>
|
|
<button v-if="['admin', 'super_admin'].includes(authUser?.role || '')" class="portal-switch" @click="switchPortal"><ArrowLeftRight :size="16" /><span>{{ portal === 'admin' ? '返回用户端' : '进入管理端' }}</span></button>
|
|
<button class="icon-button desktop-only" aria-label="通知" @click="notify('当前没有新的通知')"><Bell :size="18" /></button>
|
|
<div class="account-menu-wrap">
|
|
<button class="user-menu" :class="{ active: accountMenuOpen }" @click="accountMenuOpen = !accountMenuOpen"><span>{{ userName.slice(0, 1) }}</span><div><strong>{{ userName }}</strong><small>{{ authUser?.role === 'super_admin' ? '超级管理员' : authUser?.role === 'admin' ? '平台管理员' : '普通用户' }}</small></div></button>
|
|
<Transition name="menu-pop"><div v-if="accountMenuOpen" class="account-popover"><header><span>{{ userName.slice(0, 1) }}</span><div><strong>{{ userName }}</strong><small>{{ authUser?.account }}</small></div></header><button @click="openAccountSettings"><Settings :size="16" />账号设置</button><button class="logout-button" @click="logout"><LogOut :size="16" />退出登录</button></div></Transition>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="content">
|
|
<UserConsole v-if="portal === 'user'" :view="activeView" :display-name="userName" @navigate="navigate" @notify="notify" @balance="availableCredit = $event" />
|
|
<AdminConsole v-else :view="activeView" :current-user="authUser" @navigate="navigate" @notify="notify" />
|
|
</main>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<AccountSettings
|
|
v-if="accountSettingsOpen && authUser"
|
|
:user="authUser"
|
|
@close="accountSettingsOpen = false"
|
|
@saved="accountSaved"
|
|
@reauthenticate="passwordChanged"
|
|
@notify="notify"
|
|
/>
|
|
|
|
<Transition name="toast"><div v-if="toastMessage" class="toast"><ShieldCheck :size="17" /><span>{{ toastMessage }}</span><button @click="toastMessage = ''"><X :size="15" /></button></div></Transition>
|
|
</template>
|