Files
JayuCloud/Frontend/src/views/admin/UsersView.vue
T

393 lines
11 KiB
Vue
Raw Normal View History

2026-02-25 10:37:16 +08:00
<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 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>
2026-02-25 11:38:31 +08:00
<template v-else-if="column.key === 'type'">
<a-tag :color="getUserTypeColor(record.type)">
{{ getUserTypeText(record.type) }}
2026-02-25 10:37:16 +08:00
</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 type="link" size="small" @click="showEditModal(record)">
编辑
</a-button>
<a-button type="link" size="small" @click="handleResetPassword(record)">
重置密码
</a-button>
<a-popconfirm
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 }"
>
2026-02-25 11:38:31 +08:00
<a-form-item label="账号" name="account">
<a-input v-model:value="formState.account" placeholder="请输入账号" />
2026-02-25 10:37:16 +08:00
</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>
2026-02-25 11:38:31 +08:00
<a-form-item label="用户类型" name="type">
<a-select v-model:value="formState.type" placeholder="请选择用户类型">
2026-02-25 10:37:16 +08:00
<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 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"
allowClear
>
<a-select-option
v-for="shop in shopList"
:key="shop.id"
:value="shop.id"
>
{{ shop.shopName }}
</a-select-option>
</a-select>
</a-form-item>
2026-02-25 10:37:16 +08:00
</a-form>
</a-modal>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, computed } from 'vue'
2026-02-25 10:37:16 +08:00
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 } from '@/api/user'
import { adminShopApi, type Shop } from '@/api/admin'
2026-02-25 10:37:16 +08:00
const loading = ref(false)
const shopLoading = ref(false)
2026-02-25 10:37:16 +08:00
const searchText = ref('')
const userList = ref<UserVO[]>([])
const shopList = ref<Shop[]>([])
2026-02-25 10:37:16 +08:00
const modalVisible = ref(false)
const modalLoading = ref(false)
const isEdit = ref(false)
const formRef = ref<FormInstance>()
const currentUser = ref<UserVO | null>(null)
const currentUserInfo = getCurrentUserInfo()
2026-02-25 10:37:16 +08:00
const pagination = reactive({
current: 1,
pageSize: 10,
total: 0,
showSizeChanger: true,
showQuickJumper: true,
showTotal: (total: number) => `共 ${total} 条`
})
const formState = reactive({
2026-02-25 11:38:31 +08:00
account: '',
2026-02-25 10:37:16 +08:00
nickname: '',
phone: '',
password: '',
type: 0,
shopId: undefined as number | undefined
})
const isShopRequired = computed(() => {
return currentUserInfo?.type !== 4
2026-02-25 10:37:16 +08:00
})
const formRules = {
2026-02-25 11:38:31 +08:00
account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
2026-02-25 10:37:16 +08:00
nickname: [{ required: true, message: '请输入昵称', trigger: 'blur' }],
phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
type: [{ required: true, message: '请选择用户类型', trigger: 'change' }],
shopId: [{ required: isShopRequired.value, message: '请选择店铺', trigger: 'change' }]
2026-02-25 10:37:16 +08:00
}
const columns = [
{ title: '头像', key: 'avatar', width: 80 },
2026-02-25 11:38:31 +08:00
{ title: '账号', dataIndex: 'account', key: 'account' },
2026-02-25 10:37:16 +08:00
{ title: '昵称', dataIndex: 'nickname', key: 'nickname' },
{ title: '手机号', dataIndex: 'phone', key: 'phone' },
{ title: '所属店铺', key: 'shopName', width: 150 },
2026-02-25 11:38:31 +08:00
{ title: '用户类型', key: 'type', width: 120 },
2026-02-25 10:37:16 +08:00
{ 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 getCurrentUserInfo = () => {
try {
const userInfoStr = localStorage.getItem('adminUserInfo')
if (userInfoStr) {
return JSON.parse(userInfoStr)
}
} catch (error) {
console.error('获取当前用户信息失败', error)
}
return null
}
const fetchShopList = async () => {
shopLoading.value = true
try {
const params: any = {
page: 1,
size: 100
}
if (currentUserInfo && currentUserInfo.type !== 4 && currentUserInfo.shopId) {
params.shopId = currentUserInfo.shopId
}
const res = await adminShopApi.listShops(params)
shopList.value = res.list || []
} catch (error) {
message.error('获取店铺列表失败')
} finally {
shopLoading.value = false
}
}
2026-02-25 10:37:16 +08:00
const fetchUserList = async () => {
loading.value = true
try {
const currentUser = getCurrentUserInfo()
const params: any = {
2026-02-25 10:37:16 +08:00
page: pagination.current,
size: pagination.pageSize,
keyword: searchText.value
}
if (currentUser && currentUser.type !== 4 && currentUser.shopId) {
params.shopId = currentUser.shopId
}
const res = await userApi.listUsers(params)
2026-02-25 10:37:16 +08:00
userList.value = res.list || []
pagination.total = res.total || 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
const defaultShopId = currentUserInfo?.type !== 4 ? currentUserInfo?.shopId : undefined
2026-02-25 10:37:16 +08:00
Object.assign(formState, {
2026-02-25 11:38:31 +08:00
account: '',
2026-02-25 10:37:16 +08:00
nickname: '',
phone: '',
password: '',
type: 0,
shopId: defaultShopId
2026-02-25 10:37:16 +08:00
})
modalVisible.value = true
}
const showEditModal = (record: UserVO) => {
isEdit.value = true
currentUser.value = record
Object.assign(formState, {
2026-02-25 11:38:31 +08:00
account: record.account,
2026-02-25 10:37:16 +08:00
nickname: record.nickname,
phone: record.phone,
password: '',
type: record.type,
shopId: record.shopId
2026-02-25 10:37:16 +08:00
})
modalVisible.value = true
}
const handleModalOk = async () => {
try {
await formRef.value?.validate()
modalLoading.value = true
if (isEdit.value && currentUser.value) {
await userApi.updateUser(currentUser.value.id, formState)
message.success('更新成功')
} else {
await userApi.createUser(formState)
message.success('创建成功')
}
modalVisible.value = false
fetchUserList()
} catch (error) {
console.error(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} 的密码重置为 88888888 吗?`,
onOk: async () => {
try {
await userApi.resetPassword(record.id)
message.success('密码重置成功')
} catch (error) {
message.error('密码重置失败')
}
}
})
}
const handleDelete = async (record: UserVO) => {
try {
await userApi.deleteUser(record.id)
message.success('删除成功')
fetchUserList()
} catch (error) {
message.error('删除失败')
}
}
onMounted(() => {
fetchShopList()
2026-02-25 10:37:16 +08:00
fetchUserList()
})
</script>
<style scoped>
.users {
padding: 0;
}
</style>