568 lines
18 KiB
Vue
568 lines
18 KiB
Vue
<template>
|
|
<div class="users">
|
|
<a-page-header title="用户管理" sub-title="管理系统用户" />
|
|
|
|
<a-card :bordered="false">
|
|
<template #extra>
|
|
<a-space>
|
|
<a-input-search
|
|
v-model:value="searchText"
|
|
placeholder="搜索用户名或手机号"
|
|
style="width: 250px"
|
|
@search="handleSearch"
|
|
/>
|
|
<a-button v-if="hasPermission('user:create')" type="primary" @click="showAddModal">
|
|
<template #icon>
|
|
<PlusOutlined />
|
|
</template>
|
|
新增用户
|
|
</a-button>
|
|
</a-space>
|
|
</template>
|
|
|
|
<a-table
|
|
:columns="columns"
|
|
:data-source="userList"
|
|
:loading="loading"
|
|
:pagination="pagination"
|
|
@change="handleTableChange"
|
|
row-key="id"
|
|
>
|
|
<template #bodyCell="{ column, record }">
|
|
<template v-if="column.key === 'avatar'">
|
|
<a-avatar :size="40" :src="record.avatar || DEFAULT_AVATAR" />
|
|
</template>
|
|
<template v-else-if="column.key === 'shopName'">
|
|
{{ record.shopName || '-' }}
|
|
</template>
|
|
<template v-else-if="column.key === 'type'">
|
|
<a-tag :color="getUserTypeColor(record.type)">
|
|
{{ getUserTypeText(record.type) }}
|
|
</a-tag>
|
|
</template>
|
|
<template v-else-if="column.key === 'status'">
|
|
<a-switch
|
|
:checked="record.status === 1"
|
|
@change="(checked) => handleStatusChange(record, checked)"
|
|
/>
|
|
</template>
|
|
<template v-else-if="column.key === 'actions'">
|
|
<a-space>
|
|
<a-button v-if="hasPermission('user:view')" type="link" size="small" @click="showViewModal(record)">
|
|
查看
|
|
</a-button>
|
|
<a-button v-if="hasPermission('user:edit')" type="link" size="small" @click="showEditModal(record)">
|
|
编辑
|
|
</a-button>
|
|
<a-button v-if="hasPermission('user:reset_password')" type="link" size="small" @click="handleResetPassword(record)">
|
|
重置密码
|
|
</a-button>
|
|
<a-popconfirm
|
|
v-if="hasPermission('user:delete') && record.type !== 4"
|
|
title="确定要删除这个用户吗?"
|
|
@confirm="handleDelete(record)"
|
|
>
|
|
<a-button type="link" size="small" danger>
|
|
删除
|
|
</a-button>
|
|
</a-popconfirm>
|
|
</a-space>
|
|
</template>
|
|
</template>
|
|
</a-table>
|
|
</a-card>
|
|
|
|
<a-modal
|
|
v-model:open="modalVisible"
|
|
:title="isEdit ? '编辑用户' : '新增用户'"
|
|
@ok="handleModalOk"
|
|
@cancel="handleModalCancel"
|
|
:confirmLoading="modalLoading"
|
|
>
|
|
<a-form
|
|
ref="formRef"
|
|
:model="formState"
|
|
:rules="formRules"
|
|
:label-col="{ span: 6 }"
|
|
:wrapper-col="{ span: 16 }"
|
|
>
|
|
<a-form-item label="账号" name="account">
|
|
<a-input v-model:value="formState.account" placeholder="请输入账号" />
|
|
</a-form-item>
|
|
<a-form-item label="昵称" name="nickname">
|
|
<a-input v-model:value="formState.nickname" placeholder="请输入昵称" />
|
|
</a-form-item>
|
|
<a-form-item label="手机号" name="phone">
|
|
<a-input v-model:value="formState.phone" placeholder="请输入手机号" />
|
|
</a-form-item>
|
|
<a-form-item v-if="!isEdit" label="密码" name="password">
|
|
<a-input-password v-model:value="formState.password" placeholder="请输入密码" />
|
|
</a-form-item>
|
|
<a-form-item v-if="currentUserInfo?.type !== 4" label="所属店铺" name="shopId">
|
|
<a-select
|
|
v-model:value="formState.shopId"
|
|
placeholder="请选择店铺"
|
|
:loading="shopLoading"
|
|
:disabled="currentUserInfo?.type !== 4 && currentUserInfo?.shopId"
|
|
>
|
|
<a-select-option
|
|
v-for="shop in shopList"
|
|
:key="shop.id"
|
|
:value="shop.id"
|
|
>
|
|
{{ shop.shopName }}
|
|
</a-select-option>
|
|
</a-select>
|
|
</a-form-item>
|
|
<a-form-item v-else label="所属店铺" name="shopId">
|
|
<a-select
|
|
v-model:value="formState.shopId"
|
|
placeholder="请选择店铺"
|
|
:loading="shopLoading"
|
|
>
|
|
<a-select-option
|
|
v-for="shop in shopList"
|
|
:key="shop.id"
|
|
:value="shop.id"
|
|
>
|
|
{{ shop.shopName }}
|
|
</a-select-option>
|
|
</a-select>
|
|
</a-form-item>
|
|
<a-form-item label="用户类型" name="type">
|
|
<a-select v-model:value="formState.type" placeholder="请选择用户类型">
|
|
<!-- 管理员可以新建所有类型 -->
|
|
<a-select-option v-if="currentUserInfo?.type === 4" :value="0">用户</a-select-option>
|
|
<a-select-option v-if="currentUserInfo?.type === 4" :value="2">店长</a-select-option>
|
|
<a-select-option v-if="currentUserInfo?.type === 4" :value="3">美甲师</a-select-option>
|
|
<a-select-option v-if="currentUserInfo?.type === 4" :value="4">管理员</a-select-option>
|
|
|
|
<!-- 店长可以新建美甲师和用户 -->
|
|
<a-select-option v-else-if="currentUserInfo?.type === 2" :value="0">用户</a-select-option>
|
|
<a-select-option v-else-if="currentUserInfo?.type === 2" :value="3">美甲师</a-select-option>
|
|
|
|
<!-- 美甲师只能新建用户 -->
|
|
<a-select-option v-else-if="currentUserInfo?.type === 3" :value="0">用户</a-select-option>
|
|
</a-select>
|
|
</a-form-item>
|
|
</a-form>
|
|
</a-modal>
|
|
|
|
<!-- 查看用户详情模态框 -->
|
|
<a-modal
|
|
v-model:open="viewModalVisible"
|
|
title="查看用户详情"
|
|
@cancel="handleViewModalCancel"
|
|
width="600px"
|
|
>
|
|
<a-form
|
|
:model="viewFormState"
|
|
:label-col="{ span: 6 }"
|
|
:wrapper-col="{ span: 16 }"
|
|
>
|
|
<a-form-item label="账号">
|
|
<a-input v-model:value="viewFormState.account" :disabled="true" />
|
|
</a-form-item>
|
|
<a-form-item label="昵称">
|
|
<a-input v-model:value="viewFormState.nickname" :disabled="true" />
|
|
</a-form-item>
|
|
<a-form-item label="手机号">
|
|
<a-input v-model:value="viewFormState.phone" :disabled="true" />
|
|
</a-form-item>
|
|
<a-form-item label="所属店铺">
|
|
<a-select v-model:value="viewFormState.shopId" :disabled="true">
|
|
<a-select-option
|
|
v-for="shop in shopList"
|
|
:key="shop.id"
|
|
:value="shop.id"
|
|
>
|
|
{{ shop.shopName }}
|
|
</a-select-option>
|
|
</a-select>
|
|
</a-form-item>
|
|
<a-form-item label="用户类型">
|
|
<a-select v-model:value="viewFormState.type" :disabled="true">
|
|
<a-select-option :value="0">用户</a-select-option>
|
|
<a-select-option :value="2">店长</a-select-option>
|
|
<a-select-option :value="3">美甲师</a-select-option>
|
|
<a-select-option :value="4">管理员</a-select-option>
|
|
</a-select>
|
|
</a-form-item>
|
|
<a-form-item label="创建时间">
|
|
<a-input v-model:value="viewFormState.createTime" :disabled="true" />
|
|
</a-form-item>
|
|
</a-form>
|
|
</a-modal>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { ref, reactive, onMounted, computed } from 'vue'
|
|
import { message, Modal } from 'ant-design-vue'
|
|
import type { FormInstance } from 'ant-design-vue'
|
|
import { PlusOutlined } from '@ant-design/icons-vue'
|
|
import { DEFAULT_AVATAR } from '@/constants'
|
|
import { userApi, type UserVO, adminShopApi, type Shop } from '@/api/admin'
|
|
import { usePermission } from '@/utils/permission'
|
|
|
|
const { hasPermission, loadUserPermissions } = usePermission()
|
|
|
|
const getCurrentUserInfo = () => {
|
|
try {
|
|
const userInfoStr = localStorage.getItem('adminUserInfo')
|
|
if (userInfoStr) {
|
|
const userInfo = JSON.parse(userInfoStr)
|
|
// 判断用户是否为管理员(类型为4)
|
|
isAdmin.value = userInfo.type === 4
|
|
return userInfo
|
|
}
|
|
} catch (error) {
|
|
console.error('获取当前用户信息失败', error)
|
|
}
|
|
return null
|
|
}
|
|
|
|
const loading = ref(false)
|
|
const shopLoading = ref(false)
|
|
const searchText = ref('')
|
|
const userList = ref<UserVO[]>([])
|
|
const shopList = ref<Shop[]>([])
|
|
const modalVisible = ref(false)
|
|
const modalLoading = ref(false)
|
|
const viewModalVisible = ref(false)
|
|
const isEdit = ref(false)
|
|
const isAdmin = ref(false)
|
|
const formRef = ref<FormInstance>()
|
|
const currentUser = ref<UserVO | null>(null)
|
|
const currentUserInfo = getCurrentUserInfo()
|
|
|
|
const viewFormState = reactive({
|
|
account: '',
|
|
nickname: '',
|
|
phone: '',
|
|
shopId: undefined as number | undefined,
|
|
type: 0,
|
|
createTime: ''
|
|
})
|
|
|
|
const pagination = reactive({
|
|
current: 1,
|
|
pageSize: 10,
|
|
total: 0,
|
|
showSizeChanger: true,
|
|
showQuickJumper: true,
|
|
showTotal: (total: number) => `共 ${total} 条`
|
|
})
|
|
|
|
const formState = reactive({
|
|
account: '',
|
|
nickname: '',
|
|
phone: '',
|
|
password: '',
|
|
type: 0,
|
|
shopId: undefined as number | undefined
|
|
})
|
|
|
|
const formRules = {
|
|
account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
|
|
nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
|
|
phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
|
|
password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
|
|
shopId: [{ required: true, message: '请选择店铺', trigger: 'change' }],
|
|
type: [{ required: true, message: '请选择用户类型', trigger: 'change' }]
|
|
}
|
|
|
|
const columns = [
|
|
{ title: '头像', key: 'avatar', width: 80 },
|
|
{ title: '账号', dataIndex: 'account', key: 'account' },
|
|
{ title: '昵称', dataIndex: 'nickname', key: 'nickname' },
|
|
{ title: '手机号', dataIndex: 'phone', key: 'phone' },
|
|
{ title: '所属店铺', key: 'shopName', width: 150 },
|
|
{ title: '用户类型', key: 'type', width: 120 },
|
|
{ title: '状态', key: 'status', width: 100 },
|
|
{ title: '创建时间', dataIndex: 'createTime', key: 'createTime' },
|
|
{ title: '操作', key: 'actions', width: 200, fixed: 'right' as const }
|
|
]
|
|
|
|
const getUserTypeText = (type: number) => {
|
|
const map: Record<number, string> = {
|
|
0: '用户',
|
|
2: '店长',
|
|
3: '美甲师',
|
|
4: '管理员'
|
|
}
|
|
return map[type] || '未知'
|
|
}
|
|
|
|
const getUserTypeColor = (type: number) => {
|
|
const map: Record<number, string> = {
|
|
0: 'default',
|
|
2: 'blue',
|
|
3: 'green',
|
|
4: 'purple'
|
|
}
|
|
return map[type] || 'default'
|
|
}
|
|
|
|
const fetchShopList = async () => {
|
|
shopLoading.value = true
|
|
try {
|
|
// 检查用户类型和权限
|
|
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) {
|
|
// 权限不足时不显示错误提示
|
|
console.error('获取店铺列表失败:', error)
|
|
} finally {
|
|
shopLoading.value = false
|
|
}
|
|
}
|
|
|
|
const fetchUserList = async () => {
|
|
loading.value = true
|
|
try {
|
|
const params: any = {
|
|
page: pagination.current,
|
|
size: pagination.pageSize,
|
|
keyword: searchText.value,
|
|
status: 1 // 只获取未被删除的用户
|
|
}
|
|
|
|
if (currentUserInfo && currentUserInfo.type !== 4 && currentUserInfo.shopId) {
|
|
params.shopId = currentUserInfo.shopId
|
|
}
|
|
|
|
// 后端只支持单个type参数,所以这里不设置类型过滤
|
|
// 依赖前端的二次过滤来确保只显示级别比当前用户低的用户
|
|
|
|
const res = await userApi.listUsers(params)
|
|
// 前端再次过滤,确保只显示级别比当前用户低的用户
|
|
let filteredList = res.list || []
|
|
if (currentUserInfo && currentUserInfo.type !== 4) {
|
|
filteredList = filteredList.filter(user => {
|
|
if (currentUserInfo.type === 2) {
|
|
// 店长可以看到美甲师(3)和用户(0)
|
|
return user.type === 3 || user.type === 0
|
|
} else if (currentUserInfo.type === 3) {
|
|
// 美甲师只能看到用户(0)
|
|
return user.type === 0
|
|
}
|
|
return true
|
|
})
|
|
}
|
|
userList.value = filteredList
|
|
pagination.total = filteredList.length || 0
|
|
} catch (error) {
|
|
message.error('获取用户列表失败')
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
const handleSearch = () => {
|
|
pagination.current = 1
|
|
fetchUserList()
|
|
}
|
|
|
|
const handleTableChange = (pag: any) => {
|
|
pagination.current = pag.current
|
|
pagination.pageSize = pag.pageSize
|
|
fetchUserList()
|
|
}
|
|
|
|
const showAddModal = () => {
|
|
isEdit.value = false
|
|
Object.assign(formState, {
|
|
account: '',
|
|
nickname: '',
|
|
phone: '',
|
|
password: '',
|
|
type: 0,
|
|
shopId: currentUserInfo && currentUserInfo.type !== 4 && currentUserInfo.shopId ? currentUserInfo.shopId : undefined
|
|
})
|
|
modalVisible.value = true
|
|
}
|
|
|
|
const showEditModal = (record: UserVO) => {
|
|
isEdit.value = true
|
|
currentUser.value = record
|
|
Object.assign(formState, {
|
|
account: record.account,
|
|
nickname: record.nickname,
|
|
phone: record.phone,
|
|
password: '',
|
|
type: record.type,
|
|
shopId: record.shopId
|
|
})
|
|
modalVisible.value = true
|
|
}
|
|
|
|
const showViewModal = (record: UserVO) => {
|
|
Object.assign(viewFormState, {
|
|
account: record.account,
|
|
nickname: record.nickname,
|
|
phone: record.phone,
|
|
shopId: record.shopId,
|
|
type: record.type,
|
|
createTime: record.createTime
|
|
})
|
|
viewModalVisible.value = true
|
|
}
|
|
|
|
const handleViewModalCancel = () => {
|
|
viewModalVisible.value = false
|
|
}
|
|
|
|
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 {
|
|
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: any) {
|
|
console.error(error)
|
|
if (error.message?.includes('账号重复')) {
|
|
message.error('账号已存在,请使用其他账号')
|
|
} else {
|
|
message.error('操作失败')
|
|
}
|
|
} finally {
|
|
modalLoading.value = false
|
|
}
|
|
}
|
|
|
|
const handleModalCancel = () => {
|
|
modalVisible.value = false
|
|
formRef.value?.resetFields()
|
|
}
|
|
|
|
const handleStatusChange = async (record: UserVO, checked: boolean) => {
|
|
try {
|
|
await userApi.updateUserStatus(record.id, checked ? 1 : 0)
|
|
message.success('状态更新成功')
|
|
fetchUserList()
|
|
} catch (error) {
|
|
message.error('状态更新失败')
|
|
record.status = record.status === 1 ? 0 : 1
|
|
}
|
|
}
|
|
|
|
const handleResetPassword = (record: UserVO) => {
|
|
Modal.confirm({
|
|
title: '确认重置密码',
|
|
content: `确定要将用户 ${record.nickname} 的密码重置为 888888 吗?`,
|
|
onOk: async () => {
|
|
try {
|
|
await userApi.resetPassword(record.id)
|
|
message.success('密码重置成功')
|
|
} catch (error) {
|
|
message.error('密码重置失败')
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
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()
|
|
} catch (error) {
|
|
message.error('删除失败')
|
|
}
|
|
}
|
|
|
|
onMounted(async () => {
|
|
await loadUserPermissions()
|
|
fetchShopList()
|
|
fetchUserList()
|
|
})
|
|
</script>
|
|
|
|
<style scoped>
|
|
.users {
|
|
padding: 0;
|
|
}
|
|
</style>
|