Files
JayuCloud/YumiNail-Frontend/src/views/admin/ProfileView.vue
T

259 lines
9.3 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<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="isPlatformOverview" 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">{{ isPlatformOverview ? '管理范围' : '门店切换' }}</div>
<div v-if="isPlatformOverview" class="profile-static-shop">
平台总览模式,店铺业务仅可查看
</div>
<a-select
v-else-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">
/**
* ProfileView 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, 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,
isBootstrapAdminUser,
loadCachedAdminUserInfo
} from './admin-helpers'
/**
* Purpose: router supports ProfileView.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 { userPermissions, loadUserPermissions, clearPermissions } = usePermission()
/** userInfo is a reactive state variable used by the template and event handlers. */
const userInfo = ref(loadCachedAdminUserInfo())
/** selectedShopId is a reactive state variable used by the template and event handlers. */
const selectedShopId = ref(userInfo.value?.shopId ? String(userInfo.value.shopId) : undefined)
/** shopSwitchLoading is a reactive state variable used by the template and event handlers. */
const shopSwitchLoading = ref(false)
/** currentShopName is a reactive state variable used by the template and event handlers. */
const currentShopName = computed(() => getAdminCurrentShopName(userInfo.value))
/**
* Purpose: isPlatformOverview supports ProfileView.vue by holding or executing the is platform overview 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 isPlatformOverview = computed(() => isBootstrapAdminUser(userInfo.value))
/** roleLabel is a reactive state variable used by the template and event handlers. */
const roleLabel = computed(() => getAdminRoleLabel(userInfo.value?.type))
/** shopOptions is a reactive state variable used by the template and event handlers. */
const shopOptions = computed(() => getAdminShopOptions(userInfo.value))
/** shopSelectOptions is a reactive state variable used by the template and event handlers. */
const shopSelectOptions = computed(() => {
const options = shopOptions.value.map(shop => ({ label: shop.name, value: shop.id }))
return options
})
/** permissionCount is a reactive state variable used by the template and event handlers. */
const permissionCount = computed(() => userPermissions.value.length)
/** shopScopeText is a reactive state variable used by the template and event handlers. */
const shopScopeText = computed(() => {
if (!shopOptions.value.length) {
return isPlatformOverview.value ? '平台总览模式' : '游客模式'
}
return shopOptions.value.map(item => item.name).join(' / ')
})
/** persistAdminSession is a local helper used by this page script. */
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)
}
/** refreshLocalUserInfo is a local helper used by this page script. */
const refreshLocalUserInfo = () => {
userInfo.value = loadCachedAdminUserInfo()
selectedShopId.value = userInfo.value?.shopId ? String(userInfo.value.shopId) : undefined
}
/** handleShopChange is an async page action. Trigger it from the template or lifecycle hook, then await the result. */
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
}
}
/** handleLogout is a local helper used by this page script. */
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>