用户管理一些问题修复
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user