398 lines
11 KiB
Vue
398 lines
11 KiB
Vue
<template>
|
||
<div class="admin-layout">
|
||
<a-layout>
|
||
<a-layout-sider
|
||
v-model:collapsed="collapsed"
|
||
:trigger="null"
|
||
collapsible
|
||
class="sider"
|
||
width="240"
|
||
>
|
||
<div class="logo">
|
||
<h1 v-if="!collapsed">后台管理</h1>
|
||
<h1 v-else>后台</h1>
|
||
</div>
|
||
<a-menu
|
||
:selectedKeys="selectedKeys"
|
||
mode="inline"
|
||
theme="dark"
|
||
@click="handleMenuClick"
|
||
>
|
||
<a-menu-item
|
||
v-for="item in filteredMenuItems"
|
||
:key="item.key"
|
||
>
|
||
<template #icon>
|
||
<component :is="item.icon" />
|
||
</template>
|
||
<span>{{ item.label }}</span>
|
||
</a-menu-item>
|
||
</a-menu>
|
||
</a-layout-sider>
|
||
|
||
<a-layout>
|
||
<a-layout-header class="header">
|
||
<div class="header-shell">
|
||
<div class="header-left">
|
||
<MenuUnfoldOutlined v-if="collapsed" class="trigger" @click="toggleCollapsed" />
|
||
<MenuFoldOutlined v-else class="trigger" @click="toggleCollapsed" />
|
||
|
||
<div v-if="isBootstrapAdmin" class="shop-hint">
|
||
<span>当前暂无可用门店,请先前往店铺管理创建</span>
|
||
<a-button type="link" @click="router.push('/admin/shops')">去创建</a-button>
|
||
</div>
|
||
|
||
<div v-else-if="shopOptions.length > 1" class="shop-switcher">
|
||
<span class="shop-switcher-label">当前门店</span>
|
||
<a-select
|
||
v-model:value="selectedShopId"
|
||
class="shop-select"
|
||
:options="shopSelectOptions"
|
||
:loading="shopSwitchLoading"
|
||
@change="handleShopChange"
|
||
/>
|
||
</div>
|
||
|
||
<div v-else class="shop-pill">
|
||
<ShopOutlined />
|
||
<span>{{ currentShopName }}</span>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="header-right">
|
||
<a-dropdown>
|
||
<a class="user-dropdown" @click.prevent>
|
||
<a-avatar :size="32" :src="userInfo?.avatar || DEFAULT_AVATAR" />
|
||
<span class="username">{{ userInfo?.nickname || '管理员' }}</span>
|
||
<DownOutlined />
|
||
</a>
|
||
<template #overlay>
|
||
<a-menu>
|
||
<a-menu-item key="home" @click="router.push('/admin')">
|
||
<template #icon>
|
||
<UserOutlined />
|
||
</template>
|
||
工作台
|
||
</a-menu-item>
|
||
<a-menu-divider />
|
||
<a-menu-item key="logout" @click="handleLogout">
|
||
<template #icon>
|
||
<LogoutOutlined />
|
||
</template>
|
||
退出登录
|
||
</a-menu-item>
|
||
</a-menu>
|
||
</template>
|
||
</a-dropdown>
|
||
</div>
|
||
</div>
|
||
</a-layout-header>
|
||
|
||
<a-layout-content class="content">
|
||
<div class="content-shell">
|
||
<router-view />
|
||
</div>
|
||
</a-layout-content>
|
||
</a-layout>
|
||
</a-layout>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, onMounted, ref, watch } from 'vue'
|
||
import { useRoute, useRouter } from 'vue-router'
|
||
import {
|
||
DashboardOutlined,
|
||
DownOutlined,
|
||
GiftOutlined,
|
||
HomeOutlined,
|
||
LogoutOutlined,
|
||
MenuFoldOutlined,
|
||
MenuUnfoldOutlined,
|
||
SafetyCertificateOutlined,
|
||
ShopOutlined,
|
||
ShoppingOutlined,
|
||
TagOutlined,
|
||
TeamOutlined,
|
||
UserOutlined
|
||
} from '@ant-design/icons-vue'
|
||
import { message, Modal } from 'ant-design-vue'
|
||
import { adminAuthApi, type UserVO } from '@/api/admin'
|
||
import { DEFAULT_AVATAR } from '@/constants'
|
||
import { usePermission } from '@/utils/permission'
|
||
|
||
const router = useRouter()
|
||
const route = useRoute()
|
||
|
||
const collapsed = ref(false)
|
||
const userInfo = ref<UserVO | null>(null)
|
||
const selectedKeys = ref<string[]>([route.path])
|
||
const selectedShopId = ref<string>()
|
||
const shopSwitchLoading = ref(false)
|
||
const { loadUserPermissions, hasPermission, clearPermissions, userPermissions } = usePermission()
|
||
|
||
const menuItems = [
|
||
{ key: '/admin', icon: HomeOutlined, label: '首页', permissionCode: '' },
|
||
{ key: '/admin/dashboard', icon: DashboardOutlined, label: '仪表盘', permissionCode: 'dashboard:view' },
|
||
{ key: '/admin/users', icon: UserOutlined, label: '用户管理', permissionCode: 'user:view' },
|
||
{ key: '/admin/products', icon: ShopOutlined, label: '作品管理', permissionCode: 'product:view' },
|
||
{ key: '/admin/coupons', icon: GiftOutlined, label: '优惠券管理', permissionCode: 'coupon:view' },
|
||
{ key: '/admin/shops', icon: HomeOutlined, label: '店铺管理', permissionCode: 'shop:view' },
|
||
{ key: '/admin/permissions', icon: SafetyCertificateOutlined, label: '权限管理', permissionCode: 'permission:view' },
|
||
{ key: '/admin/tags', icon: TagOutlined, label: '标签管理', permissionCode: 'tag:view' },
|
||
{ key: '/admin/orders', icon: ShoppingOutlined, label: '订单管理', permissionCode: 'order:view' },
|
||
{ key: '/admin/nail-artists', icon: TeamOutlined, label: '美甲师管理', permissionCode: 'nail_artist:view' }
|
||
]
|
||
|
||
const isBootstrapAdmin = computed(() => userInfo.value?.type === 4 && !userInfo.value?.shopId)
|
||
const shopOptions = computed(() => {
|
||
const ids = userInfo.value?.shopIds || []
|
||
const names = userInfo.value?.shopNames || []
|
||
return ids.map((id, index) => ({
|
||
id: String(id),
|
||
name: names[index] || `门店 ${id}`
|
||
}))
|
||
})
|
||
const shopSelectOptions = computed(() => shopOptions.value.map(shop => ({ label: shop.name, value: shop.id })))
|
||
const currentShopName = computed(() => userInfo.value?.shopName || '未选择门店')
|
||
|
||
const filteredMenuItems = computed(() => {
|
||
userPermissions.value
|
||
let visibleItems = menuItems.filter(item => !item.permissionCode || hasPermission(item.permissionCode))
|
||
if (isBootstrapAdmin.value) {
|
||
visibleItems = visibleItems.filter(item => item.key === '/admin' || item.key === '/admin/shops')
|
||
}
|
||
return visibleItems
|
||
})
|
||
|
||
const persistAdminSession = (payload: any) => {
|
||
localStorage.setItem('adminAccessToken', payload.accessToken)
|
||
localStorage.setItem('adminRefreshToken', payload.refreshToken)
|
||
localStorage.setItem('adminUserInfo', JSON.stringify(payload.userInfo))
|
||
sessionStorage.setItem('adminAccessToken', payload.accessToken)
|
||
}
|
||
|
||
const toggleCollapsed = () => {
|
||
collapsed.value = !collapsed.value
|
||
}
|
||
|
||
const handleMenuClick = (info: any) => {
|
||
router.push(info.key)
|
||
}
|
||
|
||
const handleLogout = () => {
|
||
Modal.confirm({
|
||
title: '确认退出登录',
|
||
content: '退出后需要重新登录后台。',
|
||
okText: '确认',
|
||
cancelText: '取消',
|
||
onOk: async () => {
|
||
try {
|
||
await adminAuthApi.logout()
|
||
} catch (error) {
|
||
console.error('退出失败:', error)
|
||
} finally {
|
||
localStorage.removeItem('adminAccessToken')
|
||
localStorage.removeItem('adminRefreshToken')
|
||
localStorage.removeItem('adminUserInfo')
|
||
localStorage.removeItem('accessToken')
|
||
localStorage.removeItem('refreshToken')
|
||
localStorage.removeItem('userInfo')
|
||
sessionStorage.removeItem('adminAccessToken')
|
||
sessionStorage.removeItem('accessToken')
|
||
clearPermissions()
|
||
message.success('已退出登录')
|
||
window.location.href = '/admin/login'
|
||
}
|
||
}
|
||
})
|
||
}
|
||
|
||
const fetchUserInfo = async () => {
|
||
try {
|
||
const cache = localStorage.getItem('adminUserInfo')
|
||
if (cache) {
|
||
userInfo.value = JSON.parse(cache)
|
||
}
|
||
if (!userInfo.value) {
|
||
return
|
||
}
|
||
selectedShopId.value = userInfo.value.shopId ? String(userInfo.value.shopId) : undefined
|
||
} catch (error) {
|
||
console.error('获取后台用户信息失败:', error)
|
||
}
|
||
}
|
||
|
||
const handleShopChange = async (shopId: unknown) => {
|
||
if (typeof shopId !== 'string' && typeof shopId !== 'number') {
|
||
return
|
||
}
|
||
const normalizedShopId = String(shopId)
|
||
if (!normalizedShopId || normalizedShopId === String(userInfo.value?.shopId || '')) {
|
||
return
|
||
}
|
||
shopSwitchLoading.value = true
|
||
try {
|
||
const response = await adminAuthApi.switchShop(normalizedShopId)
|
||
persistAdminSession(response as any)
|
||
await fetchUserInfo()
|
||
await loadUserPermissions()
|
||
message.success('门店上下文已切换')
|
||
window.location.reload()
|
||
} catch (error: any) {
|
||
console.error('切换门店失败:', error)
|
||
selectedShopId.value = userInfo.value?.shopId ? String(userInfo.value.shopId) : undefined
|
||
message.error(error?.message || '切换门店失败')
|
||
} finally {
|
||
shopSwitchLoading.value = false
|
||
}
|
||
}
|
||
|
||
watch(
|
||
() => route.path,
|
||
newPath => {
|
||
selectedKeys.value = [newPath]
|
||
},
|
||
{ immediate: true }
|
||
)
|
||
|
||
onMounted(async () => {
|
||
await fetchUserInfo()
|
||
await loadUserPermissions()
|
||
})
|
||
</script>
|
||
|
||
<style scoped>
|
||
.admin-layout {
|
||
height: 100vh;
|
||
}
|
||
|
||
.sider {
|
||
background: #001529;
|
||
overflow: auto;
|
||
height: 100vh;
|
||
position: fixed;
|
||
left: 0;
|
||
top: 0;
|
||
bottom: 0;
|
||
z-index: 1000;
|
||
}
|
||
|
||
.logo {
|
||
height: 64px;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
background: rgba(255, 255, 255, 0.08);
|
||
}
|
||
|
||
.logo h1 {
|
||
margin: 0;
|
||
color: white;
|
||
font-size: 18px;
|
||
font-weight: 700;
|
||
}
|
||
|
||
.header {
|
||
position: sticky;
|
||
top: 0;
|
||
z-index: 100;
|
||
padding: 0 24px;
|
||
background: white;
|
||
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
|
||
}
|
||
|
||
.header-shell {
|
||
width: 100%;
|
||
max-width: var(--admin-content-max-width);
|
||
margin: 0 auto;
|
||
min-height: 64px;
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
gap: 16px;
|
||
}
|
||
|
||
.header-left {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 16px;
|
||
min-width: 0;
|
||
}
|
||
|
||
.trigger {
|
||
font-size: 18px;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.shop-hint,
|
||
.shop-switcher,
|
||
.shop-pill {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
min-height: 40px;
|
||
padding: 0 14px;
|
||
border-radius: 999px;
|
||
background: #f5f7fb;
|
||
color: #334155;
|
||
}
|
||
|
||
.shop-hint {
|
||
flex-wrap: wrap;
|
||
}
|
||
|
||
.shop-switcher-label {
|
||
font-size: 13px;
|
||
color: #64748b;
|
||
}
|
||
|
||
.shop-select {
|
||
min-width: 220px;
|
||
}
|
||
|
||
.shop-pill {
|
||
font-weight: 600;
|
||
}
|
||
|
||
.header-right {
|
||
display: flex;
|
||
align-items: center;
|
||
}
|
||
|
||
.user-dropdown {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 12px;
|
||
padding: 8px 12px;
|
||
border-radius: 10px;
|
||
transition: background 0.2s ease;
|
||
}
|
||
|
||
.user-dropdown:hover {
|
||
background: #f8fafc;
|
||
}
|
||
|
||
.username {
|
||
color: #1f2937;
|
||
}
|
||
|
||
.content {
|
||
margin-left: 240px;
|
||
min-height: calc(100vh - 64px);
|
||
padding: 24px;
|
||
background: #f0f2f5;
|
||
transition: margin-left 0.2s;
|
||
}
|
||
|
||
.content-shell {
|
||
width: 100%;
|
||
max-width: var(--admin-content-max-width);
|
||
margin: 0 auto;
|
||
}
|
||
|
||
.admin-layout :deep(.ant-layout-sider-collapsed) + .ant-layout .content {
|
||
margin-left: 80px;
|
||
}
|
||
</style>
|