489 lines
14 KiB
Vue
489 lines
14 KiB
Vue
<template>
|
||
<div class="home">
|
||
<template v-if="!isMobile">
|
||
<a-page-header title="欢迎使用后台管理系统" :sub-title="welcomeMessage" />
|
||
|
||
<a-alert
|
||
v-if="isBootstrapAdmin"
|
||
class="bootstrap-alert"
|
||
type="info"
|
||
show-icon
|
||
message="当前处于平台总览模式"
|
||
description="当前后台仅面向平台管理员,负责维护店铺、用户账号、角色归属和平台运行状态。"
|
||
/>
|
||
|
||
<a-alert
|
||
v-if="isShopAuthorizationExpired"
|
||
class="bootstrap-alert"
|
||
type="error"
|
||
show-icon
|
||
message="授权已到期,请联系管理员"
|
||
description="当前店铺已超过授权有效期,后台业务功能已暂时禁用。"
|
||
/>
|
||
|
||
<a-card class="time-card">
|
||
<a-statistic title="当前时间" :value="currentTime" />
|
||
</a-card>
|
||
|
||
<a-card class="info-card">
|
||
<a-descriptions bordered>
|
||
<a-descriptions-item label="用户信息">
|
||
<div class="user-info">
|
||
<a-avatar :size="64" :src="userInfo?.avatar || DEFAULT_AVATAR" />
|
||
<div class="user-details">
|
||
<h3>{{ userInfo?.nickname || '管理员' }}</h3>
|
||
<p>{{ getUserTypeText(userInfo?.type) }}</p>
|
||
<p v-if="userInfo?.shopName">所属店铺:{{ userInfo?.shopName }}</p>
|
||
</div>
|
||
</div>
|
||
</a-descriptions-item>
|
||
</a-descriptions>
|
||
</a-card>
|
||
|
||
<a-card class="permissions-card">
|
||
<template #title>
|
||
<div class="card-title">
|
||
<SafetyCertificateOutlined class="title-icon" />
|
||
<span>我的权限</span>
|
||
</div>
|
||
</template>
|
||
<div class="permissions-content" v-if="userPermissions.length > 0">
|
||
<div v-for="(group, groupName) in groupedPermissions" :key="groupName" class="permission-group">
|
||
<div class="group-title">{{ groupName }}</div>
|
||
<div class="permission-tags">
|
||
<a-tag v-for="permission in group" :key="permission.id" color="blue" class="permission-tag">
|
||
{{ permission.permissionName }}
|
||
</a-tag>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<div class="no-permissions" v-else>暂无权限信息</div>
|
||
</a-card>
|
||
|
||
<a-card class="quick-actions">
|
||
<template #title>
|
||
快捷操作
|
||
</template>
|
||
<a-row :gutter="[16, 16]">
|
||
<a-col v-for="item in quickActions" :key="item.key" :span="8">
|
||
<a-card hoverable @click="navigateTo(item.path, item.permissionCode)">
|
||
<template #cover>
|
||
<div class="action-icon">
|
||
<component :is="item.icon" />
|
||
</div>
|
||
</template>
|
||
<template #title>
|
||
{{ item.title }}
|
||
</template>
|
||
</a-card>
|
||
</a-col>
|
||
</a-row>
|
||
</a-card>
|
||
</template>
|
||
|
||
<template v-else>
|
||
<section class="admin-page-intro">
|
||
<span class="admin-page-intro__eyebrow">{{ userInfo?.nickname || userInfo?.account || '后台账号' }}</span>
|
||
<h1 class="admin-page-intro__title">{{ userInfo?.shopName || (isBootstrapAdmin ? '平台总览模式' : '游客模式') }}</h1>
|
||
<div class="admin-page-intro__meta">
|
||
<span class="admin-page-chip">{{ currentTime }}</span>
|
||
<span class="admin-page-chip">{{ getUserTypeText(userInfo?.type) }}</span>
|
||
<span v-if="isBootstrapAdmin" class="admin-page-chip">平台总览</span>
|
||
</div>
|
||
</section>
|
||
|
||
<section v-if="isBootstrapAdmin" class="admin-mobile-section-card">
|
||
<div class="admin-mobile-section-card__title">平台总览模式</div>
|
||
<a-button type="primary" size="large" block @click="navigateTo('/admin/shops', 'shop:view')">进入店铺管理</a-button>
|
||
</section>
|
||
|
||
<div class="admin-mobile-grid admin-mobile-grid--two">
|
||
<section class="admin-mobile-stat">
|
||
<div class="admin-mobile-stat__label">权限数量</div>
|
||
<div class="admin-mobile-stat__value">{{ userPermissions.length }}</div>
|
||
</section>
|
||
<section class="admin-mobile-stat">
|
||
<div class="admin-mobile-stat__label">快捷入口</div>
|
||
<div class="admin-mobile-stat__value">{{ quickActions.length }}</div>
|
||
</section>
|
||
</div>
|
||
|
||
<section class="admin-mobile-section-card">
|
||
<div class="admin-mobile-section-card__title">快捷入口</div>
|
||
<div class="mobile-actions">
|
||
<button
|
||
v-for="item in quickActions"
|
||
:key="item.key"
|
||
class="mobile-action"
|
||
type="button"
|
||
@click="navigateTo(item.path, item.permissionCode)"
|
||
>
|
||
<div class="mobile-action__icon">
|
||
<component :is="item.icon" />
|
||
</div>
|
||
<div class="mobile-action__body">
|
||
<div class="mobile-action__title">{{ item.title }}</div>
|
||
</div>
|
||
</button>
|
||
</div>
|
||
</section>
|
||
|
||
<section class="admin-mobile-section-card">
|
||
<div class="admin-mobile-section-card__title">权限分组</div>
|
||
<div v-if="userPermissions.length > 0" class="mobile-permission-groups">
|
||
<div v-for="(group, groupName) in groupedPermissions" :key="groupName" class="mobile-permission-group">
|
||
<div class="mobile-permission-group__title">{{ groupName }}</div>
|
||
<div class="permission-tags">
|
||
<a-tag v-for="permission in group" :key="permission.id" color="blue" class="permission-tag">
|
||
{{ permission.permissionName }}
|
||
</a-tag>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<a-empty v-else description="暂无权限信息" />
|
||
</section>
|
||
</template>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
/**
|
||
* HomeView page script.
|
||
* Call guide: the template binds the reactive state below, and the methods are triggered by button clicks, lifecycle hooks, or router events.
|
||
*/
|
||
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
||
import { useRouter } from 'vue-router'
|
||
import {
|
||
HomeOutlined,
|
||
SafetyCertificateOutlined,
|
||
UserOutlined
|
||
} from '@ant-design/icons-vue'
|
||
/**
|
||
* Purpose: UserVO supports HomeView.vue by holding or executing the user vo page or API role.
|
||
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
|
||
*/
|
||
import { userApi, type UserVO } from '@/api/admin'
|
||
/**
|
||
* Purpose: PermissionVO supports HomeView.vue by holding or executing the permission vo page or API role.
|
||
* Usage: import, call, read, or update this type from the owning page/module while preserving the surrounding loading and permission flow.
|
||
*/
|
||
import { permissionApi, type PermissionVO } from '@/api/permission'
|
||
import { DEFAULT_AVATAR } from '@/constants'
|
||
import { message } from 'ant-design-vue'
|
||
import { useAdminViewport } from '@/composables/useAdminViewport'
|
||
import { isBootstrapAdminUser } from './admin-helpers'
|
||
import { isAllowedForShoplessPlatformAdmin } from '@/utils/platform-admin-permissions'
|
||
|
||
/**
|
||
* Purpose: router supports HomeView.vue by holding or executing the router page or API role.
|
||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||
*/
|
||
const router = useRouter()
|
||
const { isMobile } = useAdminViewport()
|
||
/** currentTime is a reactive state variable used by the template and event handlers. */
|
||
const currentTime = ref('')
|
||
/**
|
||
* Purpose: userInfo supports HomeView.vue by holding or executing the user info page or API role.
|
||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||
*/
|
||
const userInfo = ref<UserVO | null>(null)
|
||
/**
|
||
* Purpose: userPermissions supports HomeView.vue by holding or executing the user permissions page or API role.
|
||
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
||
*/
|
||
const userPermissions = ref<PermissionVO[]>([])
|
||
let timer: number | null = null
|
||
|
||
/** welcomeMessage is a reactive state variable used by the template and event handlers. */
|
||
const welcomeMessage = computed(() => {
|
||
const hour = new Date().getHours()
|
||
if (hour < 12) {
|
||
return '早上好!'
|
||
} else if (hour < 18) {
|
||
return '下午好!'
|
||
} else {
|
||
return '晚上好!'
|
||
}
|
||
})
|
||
|
||
/** isBootstrapAdmin is a reactive state variable used by the template and event handlers. */
|
||
const isBootstrapAdmin = computed(() => isBootstrapAdminUser(userInfo.value))
|
||
/** isShopAuthorizationExpired is a reactive state variable used by the template and event handlers. */
|
||
const isShopAuthorizationExpired = computed(() => Boolean(userInfo.value?.shopAuthorizationExpired) && userInfo.value?.type !== 4)
|
||
|
||
/** groupedPermissions is a reactive state variable used by the template and event handlers. */
|
||
const groupedPermissions = computed(() => {
|
||
const groups: Record<string, PermissionVO[]> = {}
|
||
userPermissions.value.forEach(permission => {
|
||
const groupName = permission.permissionGroup
|
||
if (!groups[groupName]) {
|
||
groups[groupName] = []
|
||
}
|
||
groups[groupName]!.push(permission)
|
||
})
|
||
return groups
|
||
})
|
||
|
||
/** quickActions is a reactive state variable used by the template and event handlers. */
|
||
const quickActions = computed(() => {
|
||
if (isShopAuthorizationExpired.value) {
|
||
return []
|
||
}
|
||
const actions = [
|
||
{
|
||
key: 'users',
|
||
title: '店长账号',
|
||
path: '/admin/users',
|
||
icon: UserOutlined,
|
||
permissionCode: 'user:view'
|
||
},
|
||
{
|
||
key: 'shops',
|
||
title: '店铺管理',
|
||
path: '/admin/shops',
|
||
icon: HomeOutlined,
|
||
permissionCode: 'shop:view'
|
||
}
|
||
]
|
||
|
||
const grantedPermissions = new Set(userPermissions.value.map(permission => permission.permissionCode))
|
||
if (isBootstrapAdmin.value) {
|
||
return actions.filter(item => isAllowedForShoplessPlatformAdmin(item.permissionCode))
|
||
}
|
||
return actions.filter(item => grantedPermissions.has(item.permissionCode))
|
||
})
|
||
|
||
/** getUserTypeText is a local helper used by this page script. */
|
||
const getUserTypeText = (type: number | undefined) => {
|
||
switch (type) {
|
||
case 4:
|
||
return '平台管理员'
|
||
case 2:
|
||
return '店长'
|
||
case 3:
|
||
return '美甲师'
|
||
default:
|
||
return '后台账号'
|
||
}
|
||
}
|
||
|
||
/** updateCurrentTime is a local helper used by this page script. */
|
||
const updateCurrentTime = () => {
|
||
const now = new Date()
|
||
currentTime.value = now.toLocaleString('zh-CN', {
|
||
year: 'numeric',
|
||
month: '2-digit',
|
||
day: '2-digit',
|
||
hour: '2-digit',
|
||
minute: '2-digit',
|
||
second: '2-digit'
|
||
})
|
||
}
|
||
|
||
/** fetchUserInfo is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||
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
|
||
}
|
||
} catch (error) {
|
||
console.error('获取用户信息失败:', error)
|
||
}
|
||
}
|
||
|
||
/** fetchUserPermissions is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
|
||
const fetchUserPermissions = async () => {
|
||
try {
|
||
const res = await permissionApi.getUserPermissions()
|
||
userPermissions.value = res as unknown as PermissionVO[]
|
||
} catch (error) {
|
||
console.error('获取用户权限失败:', error)
|
||
}
|
||
}
|
||
|
||
/** navigateTo is a local helper used by this page script. */
|
||
const navigateTo = (path: string, permissionCode?: string) => {
|
||
if (permissionCode) {
|
||
if (isBootstrapAdmin.value && isAllowedForShoplessPlatformAdmin(permissionCode)) {
|
||
router.push(path)
|
||
return
|
||
}
|
||
const hasPermission = userPermissions.value.some(p => p.permissionCode === permissionCode)
|
||
if (!hasPermission) {
|
||
message.warning('您没有该操作的权限')
|
||
return
|
||
}
|
||
}
|
||
router.push(path)
|
||
}
|
||
|
||
onMounted(() => {
|
||
updateCurrentTime()
|
||
timer = window.setInterval(updateCurrentTime, 1000)
|
||
void fetchUserInfo()
|
||
void fetchUserPermissions()
|
||
})
|
||
|
||
onBeforeUnmount(() => {
|
||
if (timer !== null) {
|
||
window.clearInterval(timer)
|
||
}
|
||
})
|
||
</script>
|
||
|
||
<style scoped>
|
||
.home {
|
||
padding: 0;
|
||
}
|
||
|
||
.time-card,
|
||
.bootstrap-alert,
|
||
.info-card,
|
||
.permissions-card {
|
||
margin-bottom: 24px;
|
||
}
|
||
|
||
.user-info {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 16px;
|
||
}
|
||
|
||
.user-details {
|
||
flex: 1;
|
||
}
|
||
|
||
.user-details h3 {
|
||
margin: 0 0 8px 0;
|
||
font-size: 18px;
|
||
font-weight: 600;
|
||
}
|
||
|
||
.user-details p {
|
||
margin: 4px 0;
|
||
color: var(--text-secondary);
|
||
}
|
||
|
||
.card-title {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
color: var(--admin-mobile-ink);
|
||
}
|
||
|
||
.title-icon {
|
||
font-size: 20px;
|
||
color: var(--admin-mobile-accent);
|
||
}
|
||
|
||
.permissions-content,
|
||
.mobile-permission-groups {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 16px;
|
||
}
|
||
|
||
.permission-group,
|
||
.mobile-permission-group {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 10px;
|
||
}
|
||
|
||
.group-title,
|
||
.mobile-permission-group__title {
|
||
font-size: 14px;
|
||
font-weight: 600;
|
||
color: var(--theme-primary);
|
||
margin-bottom: 4px;
|
||
}
|
||
|
||
.permission-tags {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 8px;
|
||
}
|
||
|
||
.permission-tag {
|
||
font-size: 13px;
|
||
padding: 4px 12px;
|
||
border-radius: 12px;
|
||
margin: 0;
|
||
}
|
||
|
||
.no-permissions {
|
||
text-align: center;
|
||
padding: 20px;
|
||
color: var(--text-secondary);
|
||
}
|
||
|
||
.quick-actions {
|
||
margin-top: 24px;
|
||
}
|
||
|
||
.action-icon {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
height: 80px;
|
||
font-size: 32px;
|
||
color: var(--theme-primary);
|
||
background: var(--admin-mobile-accent-soft);
|
||
}
|
||
|
||
.mobile-actions {
|
||
display: grid;
|
||
gap: 12px;
|
||
margin-top: 14px;
|
||
}
|
||
|
||
.mobile-action {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 14px;
|
||
padding: 14px 0;
|
||
border: 0;
|
||
border-bottom: 1px solid var(--admin-mobile-border);
|
||
background: transparent;
|
||
text-align: left;
|
||
}
|
||
|
||
.mobile-action:last-child {
|
||
border-bottom: 0;
|
||
}
|
||
|
||
.mobile-action__icon {
|
||
display: inline-flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
width: 42px;
|
||
height: 42px;
|
||
border-radius: 14px;
|
||
background: var(--admin-mobile-accent-soft);
|
||
color: var(--theme-primary);
|
||
}
|
||
|
||
.mobile-action__body {
|
||
flex: 1;
|
||
min-width: 0;
|
||
}
|
||
|
||
.mobile-action__title {
|
||
font-size: 15px;
|
||
font-weight: 600;
|
||
color: var(--admin-mobile-ink);
|
||
}
|
||
|
||
.mobile-action__meta {
|
||
margin-top: 4px;
|
||
color: var(--text-secondary);
|
||
font-size: 12px;
|
||
}
|
||
</style>
|