226 lines
7.1 KiB
Vue
226 lines
7.1 KiB
Vue
<template>
|
|
<div class="admin-profile">
|
|
<section class="admin-page-intro">
|
|
<span class="admin-page-intro__eyebrow">{{ userInfo?.account || '后台账号' }}</span>
|
|
<h1 class="admin-page-intro__title">{{ userInfo?.nickname || '未设置昵称' }}</h1>
|
|
<div class="admin-page-intro__meta">
|
|
<span class="admin-page-chip">权限 {{ permissionCount }}</span>
|
|
<span class="admin-page-chip">门店 {{ shopOptions.length || 0 }}</span>
|
|
<span class="admin-page-chip">{{ roleLabel }}</span>
|
|
<span v-if="currentShopName === '游客模式'" class="admin-page-chip">游客模式</span>
|
|
</div>
|
|
</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">{{ currentShopName }}</div>
|
|
</section>
|
|
<section class="admin-mobile-stat">
|
|
<div class="admin-mobile-stat__label">角色</div>
|
|
<div class="admin-mobile-stat__value">{{ roleLabel }}</div>
|
|
</section>
|
|
</div>
|
|
|
|
<section class="admin-mobile-section-card">
|
|
<div class="admin-mobile-section-card__title">账号信息</div>
|
|
<div class="profile-meta-list">
|
|
<div class="profile-meta-list__row">
|
|
<span>账号</span>
|
|
<strong>{{ userInfo?.account || '--' }}</strong>
|
|
</div>
|
|
<div class="profile-meta-list__row">
|
|
<span>昵称</span>
|
|
<strong>{{ userInfo?.nickname || '--' }}</strong>
|
|
</div>
|
|
<div class="profile-meta-list__row">
|
|
<span>权限数量</span>
|
|
<strong>{{ permissionCount }}</strong>
|
|
</div>
|
|
<div class="profile-meta-list__row">
|
|
<span>可登录门店</span>
|
|
<strong>{{ shopScopeText }}</strong>
|
|
</div>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="admin-mobile-section-card">
|
|
<div class="admin-mobile-section-card__title">门店切换</div>
|
|
<a-select
|
|
v-if="shopOptions.length > 1"
|
|
v-model:value="selectedShopId"
|
|
class="profile-shop-select"
|
|
:options="shopSelectOptions"
|
|
:loading="shopSwitchLoading"
|
|
@change="handleShopChange"
|
|
/>
|
|
<div v-else class="profile-static-shop">
|
|
{{ currentShopName }}
|
|
</div>
|
|
</section>
|
|
|
|
<section class="admin-mobile-section-card">
|
|
<div class="admin-mobile-section-card__title">常用入口</div>
|
|
<div class="profile-actions">
|
|
<a-button type="default" size="large" block @click="router.push('/admin/orders')">查看订单</a-button>
|
|
<a-button type="default" size="large" block @click="router.push('/admin/manage')">进入经营模块</a-button>
|
|
</div>
|
|
</section>
|
|
|
|
<section class="admin-mobile-section-card">
|
|
<div class="admin-mobile-section-card__title">退出登录</div>
|
|
<a-button danger size="large" block @click="handleLogout">退出当前账号</a-button>
|
|
</section>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, onMounted, ref } from 'vue'
|
|
import { useRouter } from 'vue-router'
|
|
import { message, Modal } from 'ant-design-vue'
|
|
import { adminAuthApi } from '@/api/admin'
|
|
import { usePermission } from '@/utils/permission'
|
|
import { runWithGlobalLoading } from '@/utils/action-loading'
|
|
import {
|
|
getAdminCurrentShopName,
|
|
getAdminRoleLabel,
|
|
getAdminShopOptions,
|
|
loadCachedAdminUserInfo
|
|
} from './admin-helpers'
|
|
|
|
const router = useRouter()
|
|
const { userPermissions, loadUserPermissions, clearPermissions } = usePermission()
|
|
|
|
const userInfo = ref(loadCachedAdminUserInfo())
|
|
const selectedShopId = ref(userInfo.value?.shopId ? String(userInfo.value.shopId) : undefined)
|
|
const shopSwitchLoading = ref(false)
|
|
|
|
const currentShopName = computed(() => getAdminCurrentShopName(userInfo.value))
|
|
const roleLabel = computed(() => getAdminRoleLabel(userInfo.value?.type))
|
|
const shopOptions = computed(() => getAdminShopOptions(userInfo.value))
|
|
const shopSelectOptions = computed(() => shopOptions.value.map(shop => ({ label: shop.name, value: shop.id })))
|
|
const permissionCount = computed(() => userPermissions.value.length)
|
|
const shopScopeText = computed(() => {
|
|
if (!shopOptions.value.length) {
|
|
return '游客模式'
|
|
}
|
|
return shopOptions.value.map(item => item.name).join(' / ')
|
|
})
|
|
|
|
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 refreshLocalUserInfo = () => {
|
|
userInfo.value = loadCachedAdminUserInfo()
|
|
selectedShopId.value = userInfo.value?.shopId ? String(userInfo.value.shopId) : undefined
|
|
}
|
|
|
|
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 {
|
|
await runWithGlobalLoading(async () => {
|
|
const response = await adminAuthApi.switchShop(normalizedShopId)
|
|
persistAdminSession(response as any)
|
|
refreshLocalUserInfo()
|
|
await loadUserPermissions()
|
|
})
|
|
message.success('门店切换成功')
|
|
window.location.reload()
|
|
} catch (error: any) {
|
|
selectedShopId.value = userInfo.value?.shopId ? String(userInfo.value.shopId) : undefined
|
|
message.error(error?.message || '切换门店失败')
|
|
} finally {
|
|
shopSwitchLoading.value = false
|
|
}
|
|
}
|
|
|
|
const handleLogout = () => {
|
|
Modal.confirm({
|
|
title: '确认退出登录',
|
|
content: '退出后需要重新登录后台。',
|
|
okText: '确认',
|
|
cancelText: '取消',
|
|
onOk: async () => {
|
|
await runWithGlobalLoading(async () => {
|
|
try {
|
|
await adminAuthApi.logout()
|
|
} catch (error) {
|
|
console.error('退出失败:', error)
|
|
} finally {
|
|
localStorage.removeItem('adminAccessToken')
|
|
localStorage.removeItem('adminRefreshToken')
|
|
localStorage.removeItem('adminUserInfo')
|
|
localStorage.removeItem('userPermissions')
|
|
sessionStorage.removeItem('adminAccessToken')
|
|
clearPermissions()
|
|
message.success('已退出登录')
|
|
window.location.href = '/admin/login'
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
onMounted(async () => {
|
|
refreshLocalUserInfo()
|
|
await loadUserPermissions()
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
.admin-profile {
|
|
display: grid;
|
|
gap: 18px;
|
|
}
|
|
|
|
.profile-meta-list {
|
|
display: grid;
|
|
gap: 10px;
|
|
margin-top: 14px;
|
|
}
|
|
|
|
.profile-meta-list__row {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
gap: 12px;
|
|
color: #64707a;
|
|
}
|
|
|
|
.profile-meta-list__row strong {
|
|
color: var(--admin-mobile-ink);
|
|
}
|
|
|
|
.profile-shop-select {
|
|
width: 100%;
|
|
margin-top: 14px;
|
|
}
|
|
|
|
.profile-static-shop {
|
|
margin-top: 14px;
|
|
min-height: 46px;
|
|
display: flex;
|
|
align-items: center;
|
|
padding: 0 14px;
|
|
border-radius: 16px;
|
|
background: var(--admin-mobile-accent-soft);
|
|
color: var(--admin-mobile-ink);
|
|
}
|
|
|
|
.profile-actions {
|
|
display: grid;
|
|
gap: 10px;
|
|
margin-top: 14px;
|
|
}
|
|
</style>
|