用户管理一些问题修复

This commit is contained in:
xiaowang
2026-02-26 23:28:24 +08:00
parent 4b28baa80f
commit 9607c186bd
13 changed files with 985 additions and 57 deletions
+207
View File
@@ -0,0 +1,207 @@
<template>
<div class="home">
<a-page-header title="欢迎使用后台管理系统" :sub-title="welcomeMessage" />
<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="quick-actions">
<template #title>
快捷操作
</template>
<a-grid :columns="3" :colGap="16" :rowGap="16">
<a-grid-item v-for="item in quickActions" :key="item.key">
<a-card hoverable @click="navigateTo(item.path)">
<template #cover>
<div class="action-icon">
<component :is="item.icon" />
</div>
</template>
<template #title>
{{ item.title }}
</template>
</a-card>
</a-grid-item>
</a-grid>
</a-card>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { DashboardOutlined, UserOutlined, ShopOutlined, SafetyCertificateOutlined, HomeOutlined } from '@ant-design/icons-vue'
import { userApi, type UserVO } from '@/api/admin'
import { DEFAULT_AVATAR } from '@/constants'
const router = useRouter()
const currentTime = ref('')
const userInfo = ref<UserVO | null>(null)
const welcomeMessage = computed(() => {
const hour = new Date().getHours()
if (hour < 12) {
return '早上好!'
} else if (hour < 18) {
return '下午好!'
} else {
return '晚上好!'
}
})
const quickActions = computed(() => {
const actions = [
{
key: 'dashboard',
title: '仪表盘',
path: '/admin/dashboard',
icon: DashboardOutlined
},
{
key: 'users',
title: '用户管理',
path: '/admin/users',
icon: UserOutlined
},
{
key: 'products',
title: '商品管理',
path: '/admin/products',
icon: ShopOutlined
},
{
key: 'shops',
title: '店铺管理',
path: '/admin/shops',
icon: HomeOutlined
},
{
key: 'permissions',
title: '权限管理',
path: '/admin/permissions',
icon: SafetyCertificateOutlined
}
]
// 根据用户类型过滤操作
if (userInfo.value?.type === 3) { // 美甲师
return actions.filter(item => ['products', 'users'].includes(item.key))
}
return actions
})
const getUserTypeText = (type: number | undefined) => {
switch (type) {
case 4:
return '管理员'
case 2:
return '店长'
case 3:
return '美甲师'
default:
return '普通用户'
}
}
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'
})
}
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)
}
}
const navigateTo = (path: string) => {
router.push(path)
}
onMounted(() => {
updateCurrentTime()
setInterval(updateCurrentTime, 1000)
fetchUserInfo()
})
</script>
<style scoped>
.home {
padding: 0;
}
.time-card {
margin-bottom: 24px;
}
.info-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: #666;
}
.quick-actions {
margin-top: 24px;
}
.action-icon {
display: flex;
align-items: center;
justify-content: center;
height: 80px;
font-size: 32px;
color: #1890ff;
background: #f0f2f5;
}
</style>
+17 -10
View File
@@ -334,22 +334,29 @@ const getCurrentUserInfo = () => {
const fetchShops = async () => {
loadingShops.value = true
try {
const res = await adminShopApi.listAllShops()
const currentUser = getCurrentUserInfo()
// 根据用户权限控制店铺列表的显示
if (currentUser && currentUser.type !== 4 && currentUser.shopId) {
// 如果是店长,只显示用户所属的店铺
shops.value = res?.filter(shop => shop.id === currentUser.shopId) || []
// 默认选择用户所属的店铺,并且不可修改
// 检查用户类型和权限
if (currentUser && currentUser.type === 3 && currentUser.shopId) {
// 美甲师直接使用自己的店铺ID,不需要获取店铺列表
shops.value = []
formState.shopId = currentUser.shopId
} else {
// 如果是管理员,显示所有店铺
shops.value = res || []
const res = await adminShopApi.listAllShops()
// 根据用户权限控制店铺列表的显示
if (currentUser && currentUser.type !== 4 && currentUser.shopId) {
// 如果是店长,只显示用户所属的店铺
shops.value = res?.filter(shop => shop.id === currentUser.shopId) || []
// 默认选择用户所属的店铺,并且不可修改
formState.shopId = currentUser.shopId
} else {
// 如果是管理员,显示所有店铺
shops.value = res || []
}
}
} catch (error) {
console.error('获取店铺列表失败', error)
message.error('获取店铺列表失败')
// 权限不足时不显示错误提示
console.error('获取店铺列表失败:', error)
} finally {
loadingShops.value = false
}
+86 -9
View File
@@ -55,7 +55,7 @@
重置密码
</a-button>
<a-popconfirm
v-if="hasPermission('user:delete')"
v-if="hasPermission('user:delete') && record.type !== 4"
title="确定要删除这个用户吗?"
@confirm="handleDelete(record)"
>
@@ -240,13 +240,21 @@ const getUserTypeColor = (type: number) => {
const fetchShopList = async () => {
shopLoading.value = true
try {
const res = await adminShopApi.listAllShops()
shopList.value = res || []
if (currentUserInfo && currentUserInfo.type !== 4 && currentUserInfo.shopId) {
// 检查用户类型和权限
if (currentUserInfo && currentUserInfo.type === 3 && currentUserInfo.shopId) {
// 美甲师直接使用自己的店铺ID,不需要获取店铺列表
shopList.value = []
formState.shopId = currentUserInfo.shopId
} else {
const res = await adminShopApi.listAllShops()
shopList.value = res || []
if (currentUserInfo && currentUserInfo.type !== 4 && currentUserInfo.shopId) {
formState.shopId = currentUserInfo.shopId
}
}
} catch (error) {
message.error('获取店铺列表失败')
// 权限不足时不显示错误提示
console.error('获取店铺列表失败:', error)
} finally {
shopLoading.value = false
}
@@ -258,7 +266,8 @@ const fetchUserList = async () => {
const params: any = {
page: pagination.current,
size: pagination.pageSize,
keyword: searchText.value
keyword: searchText.value,
status: 1 // 只获取未被删除的用户
}
if (currentUserInfo && currentUserInfo.type !== 4 && currentUserInfo.shopId) {
@@ -313,23 +322,75 @@ const showEditModal = (record: UserVO) => {
modalVisible.value = true
}
const checkAccountExists = async (account: string) => {
try {
const res = await userApi.listUsers({ page: 1, size: 1000, keyword: account, status: 1 })
console.log('检查账号是否存在:', account, res.list)
// 只检查未被删除的账号(status为1表示正常,0表示删除)
return res.list?.some(user => user.account === account)
} catch (error) {
console.error('检查账号是否存在失败:', error)
return false
}
}
const handleModalOk = async () => {
try {
await formRef.value?.validate()
// 新增用户时检查账号是否已存在
if (!isEdit.value) {
const accountExists = await checkAccountExists(formState.account)
if (accountExists) {
message.error('账号已存在,请使用其他账号')
return
}
}
modalLoading.value = true
if (isEdit.value && currentUser.value) {
await userApi.updateUser(currentUser.value.id, formState)
message.success('更新成功')
} else {
await userApi.createUser(formState)
message.success('创建成功')
try {
await userApi.createUser(formState)
message.success('创建成功')
} catch (createError: any) {
// 检查是否为账号重复错误
if (createError.response?.data?.message?.includes('账号重复') ||
createError.message?.includes('账号重复')) {
// 尝试恢复已删除的账号
const deletedUser = await userApi.listUsers({
page: 1,
size: 1000,
keyword: formState.account,
status: 0 // 只查询已删除的用户
})
if (deletedUser.list && deletedUser.list.length > 0) {
// 找到已删除的账号,恢复它
const user = deletedUser.list[0]
await userApi.updateUserStatus(user.id, 1)
message.success('账号已恢复')
} else {
message.error('账号已存在,请使用其他账号')
}
} else {
throw createError
}
}
}
modalVisible.value = false
fetchUserList()
} catch (error) {
} catch (error: any) {
console.error(error)
if (error.message?.includes('账号重复')) {
message.error('账号已存在,请使用其他账号')
} else {
message.error('操作失败')
}
} finally {
modalLoading.value = false
}
@@ -368,6 +429,22 @@ const handleResetPassword = (record: UserVO) => {
const handleDelete = async (record: UserVO) => {
try {
if (record.type === 4) {
message.warning('管理员账号无法被删除')
return
}
// 在删除前先重命名账号,添加删除时间戳
const timestamp = new Date().getTime()
const newAccount = `${record.account}_deleted_${timestamp}`
try {
await userApi.updateUser(record.id, { account: newAccount })
} catch (renameError) {
console.error('重命名账号失败:', renameError)
// 重命名失败也继续删除流程
}
await userApi.deleteUser(record.id)
message.success('删除成功')
fetchUserList()