功能:完善多店铺登录与首次部署SQL

This commit is contained in:
xiaowang
2026-03-26 10:28:28 +08:00
parent 5601428087
commit 764bfadb51
79 changed files with 6524 additions and 2603 deletions
+166 -137
View File
@@ -10,7 +10,7 @@
>
<div class="logo">
<h1 v-if="!collapsed">后台管理</h1>
<h1 v-else>管</h1>
<h1 v-else>后台</h1>
</div>
<a-menu
:selectedKeys="selectedKeys"
@@ -29,21 +29,36 @@
</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"
/>
<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>
@@ -53,11 +68,11 @@
</a>
<template #overlay>
<a-menu>
<a-menu-item key="profile" @click="goToProfile">
<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">
@@ -72,6 +87,7 @@
</div>
</div>
</a-layout-header>
<a-layout-content class="content">
<div class="content-shell">
<router-view />
@@ -83,26 +99,25 @@
</template>
<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { computed, onMounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {
DashboardOutlined,
DownOutlined,
GiftOutlined,
HomeOutlined,
LogoutOutlined,
MenuFoldOutlined,
MenuUnfoldOutlined,
DashboardOutlined,
UserOutlined,
ShopOutlined,
HomeOutlined,
SafetyCertificateOutlined,
DownOutlined,
LogoutOutlined,
GiftOutlined,
TagOutlined,
ShopOutlined,
ShoppingOutlined,
TeamOutlined
TagOutlined,
TeamOutlined,
UserOutlined
} from '@ant-design/icons-vue'
import { message, Modal } from 'ant-design-vue'
import { userApi, type UserVO } from '@/api/admin'
import { authApi } from '@/api/auth'
import { adminAuthApi, type UserVO } from '@/api/admin'
import { DEFAULT_AVATAR } from '@/constants'
import { usePermission } from '@/utils/permission'
@@ -111,86 +126,52 @@ 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'
}
{ 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 selectedKeys = ref<string[]>([route.path])
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 以建立依赖关系
userPermissions.value
// 所有用户类型都根据实际权限过滤菜单
return menuItems.filter(item => {
// 空权限码的菜单项(如首页)总是可见
if (!item.permissionCode) {
return true
}
return hasPermission(item.permissionCode)
})
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
}
@@ -199,20 +180,17 @@ const handleMenuClick = (info: any) => {
router.push(info.key)
}
const goToProfile = () => {
router.push('/profile')
}
const handleLogout = () => {
Modal.confirm({
title: '确认退出',
content: '确定要退出登录吗?',
title: '确认退出登录',
content: '退出后需要重新登录后台。',
okText: '确认',
cancelText: '取消',
onOk: async () => {
try {
// 尝试调用登出API
await authApi.logout()
await adminAuthApi.logout()
} catch (error) {
console.error('登出失败:', error)
console.error('退出失败:', error)
} finally {
localStorage.removeItem('adminAccessToken')
localStorage.removeItem('adminRefreshToken')
@@ -232,21 +210,51 @@ const handleLogout = () => {
const fetchUserInfo = async () => {
try {
const adminUserInfoStr = localStorage.getItem('adminUserInfo')
if (adminUserInfoStr) {
userInfo.value = JSON.parse(adminUserInfoStr)
} else {
const res = await userApi.getUserInfo()
userInfo.value = res as unknown as UserVO
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)
console.error('获取后台用户信息失败:', error)
}
}
watch(() => route.path, (newPath) => {
selectedKeys.value = [newPath]
}, { immediate: true })
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()
@@ -275,51 +283,76 @@ onMounted(async () => {
display: flex;
align-items: center;
justify-content: center;
background: rgba(255, 255, 255, 0.1);
background: rgba(255, 255, 255, 0.08);
}
.logo h1 {
margin: 0;
color: white;
font-size: 18px;
margin: 0;
font-weight: 600;
}
:deep(.ant-menu) {
border-right: none;
font-weight: 700;
}
.header {
background: white;
padding: 0 24px;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
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;
transition: color 0.3s;
}
.trigger:hover {
color: #1890ff;
.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 {
@@ -331,29 +364,25 @@ onMounted(async () => {
display: flex;
align-items: center;
gap: 12px;
cursor: pointer;
padding: 8px 12px;
border-radius: 8px;
transition: background 0.3s;
border-radius: 10px;
transition: background 0.2s ease;
}
.user-dropdown:hover {
background: #f5f5f5;
background: #f8fafc;
}
.username {
font-size: 14px;
color: #333;
color: #1f2937;
}
.content {
margin-left: 240px;
min-height: calc(100vh - 64px);
padding: 24px;
background: #f0f2f5;
min-height: calc(100vh - 64px);
transition: margin-left 0.2s;
position: relative;
z-index: 1;
}
.content-shell {