2025-03-28 22:45:42 +08:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import type { CreateOrUpdateTableRequestData, TableData } from "@@/apis/tables/type"
|
|
|
|
import type { FormInstance, FormRules } from "element-plus"
|
|
|
|
import { createTableDataApi, deleteTableDataApi, getTableDataApi, updateTableDataApi } from "@@/apis/tables"
|
|
|
|
import { usePagination } from "@@/composables/usePagination"
|
|
|
|
import { CirclePlus, Delete, Refresh, RefreshRight, Search } from "@element-plus/icons-vue"
|
|
|
|
import { cloneDeep } from "lodash-es"
|
|
|
|
|
|
|
|
defineOptions({
|
|
|
|
// 命名当前组件
|
2025-04-05 22:04:05 +08:00
|
|
|
name: "UserManagement"
|
2025-03-28 22:45:42 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
const loading = ref<boolean>(false)
|
|
|
|
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
|
|
|
|
|
|
|
|
// #region 增
|
|
|
|
const DEFAULT_FORM_DATA: CreateOrUpdateTableRequestData = {
|
|
|
|
id: undefined,
|
|
|
|
username: "",
|
|
|
|
email: "",
|
|
|
|
password: ""
|
|
|
|
}
|
|
|
|
const dialogVisible = ref<boolean>(false)
|
|
|
|
const formRef = ref<FormInstance | null>(null)
|
|
|
|
const formData = ref<CreateOrUpdateTableRequestData>(cloneDeep(DEFAULT_FORM_DATA))
|
|
|
|
const formRules: FormRules<CreateOrUpdateTableRequestData> = {
|
|
|
|
username: [{ required: true, trigger: "blur", message: "请输入用户名" }],
|
|
|
|
email: [
|
|
|
|
{ required: true, trigger: "blur", message: "请输入邮箱" },
|
|
|
|
{
|
|
|
|
type: "email",
|
|
|
|
message: "请输入正确的邮箱格式",
|
|
|
|
trigger: ["blur", "change"]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
password: [{ required: true, trigger: "blur", message: "请输入密码" }]
|
|
|
|
}
|
|
|
|
function handleCreateOrUpdate() {
|
|
|
|
formRef.value?.validate((valid) => {
|
|
|
|
if (!valid) {
|
|
|
|
ElMessage.error("表单校验不通过")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
loading.value = true
|
|
|
|
const api = formData.value.id === undefined ? createTableDataApi : updateTableDataApi
|
|
|
|
api(formData.value).then(() => {
|
|
|
|
ElMessage.success("操作成功")
|
|
|
|
dialogVisible.value = false
|
|
|
|
getTableData()
|
|
|
|
}).finally(() => {
|
|
|
|
loading.value = false
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
function resetForm() {
|
|
|
|
formRef.value?.clearValidate()
|
|
|
|
formData.value = cloneDeep(DEFAULT_FORM_DATA)
|
|
|
|
}
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region 删
|
|
|
|
function handleDelete(row: TableData) {
|
|
|
|
ElMessageBox.confirm(`正在删除用户:${row.username},确认删除?`, "提示", {
|
|
|
|
confirmButtonText: "确定",
|
|
|
|
cancelButtonText: "取消",
|
|
|
|
type: "warning"
|
|
|
|
}).then(() => {
|
|
|
|
deleteTableDataApi(row.id).then(() => {
|
|
|
|
ElMessage.success("删除成功")
|
|
|
|
getTableData()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region 改
|
|
|
|
function handleUpdate(row: TableData) {
|
|
|
|
dialogVisible.value = true
|
|
|
|
formData.value = cloneDeep(row)
|
|
|
|
}
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// #region 查
|
|
|
|
const tableData = ref<TableData[]>([])
|
|
|
|
const searchFormRef = ref<FormInstance | null>(null)
|
|
|
|
const searchData = reactive({
|
|
|
|
username: "",
|
|
|
|
email: ""
|
|
|
|
})
|
|
|
|
|
|
|
|
// 存储多选的表格数据
|
|
|
|
const multipleSelection = ref<TableData[]>([])
|
|
|
|
|
|
|
|
function getTableData() {
|
|
|
|
loading.value = true
|
|
|
|
getTableDataApi({
|
|
|
|
currentPage: paginationData.currentPage,
|
|
|
|
size: paginationData.pageSize,
|
|
|
|
username: searchData.username,
|
|
|
|
email: searchData.email
|
|
|
|
}).then(({ data }) => {
|
|
|
|
paginationData.total = data.total
|
|
|
|
tableData.value = data.list
|
|
|
|
// 清空选中数据
|
|
|
|
multipleSelection.value = []
|
|
|
|
}).catch(() => {
|
|
|
|
tableData.value = []
|
|
|
|
}).finally(() => {
|
|
|
|
loading.value = false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
function handleSearch() {
|
|
|
|
paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
|
|
|
|
}
|
|
|
|
function resetSearch() {
|
|
|
|
searchFormRef.value?.resetFields()
|
|
|
|
handleSearch()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 表格多选事件处理
|
|
|
|
function handleSelectionChange(selection: TableData[]) {
|
|
|
|
multipleSelection.value = selection
|
|
|
|
}
|
|
|
|
|
|
|
|
// 批量删除方法
|
|
|
|
function handleBatchDelete() {
|
|
|
|
if (multipleSelection.value.length === 0) {
|
|
|
|
ElMessage.warning("请至少选择一条记录")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ElMessageBox.confirm(`确认删除选中的 ${multipleSelection.value.length} 条记录吗?`, "提示", {
|
|
|
|
confirmButtonText: "确定",
|
|
|
|
cancelButtonText: "取消",
|
|
|
|
type: "warning"
|
|
|
|
}).then(async () => {
|
|
|
|
loading.value = true
|
|
|
|
try {
|
|
|
|
// 使用 Promise.all 并行处理所有删除请求
|
|
|
|
await Promise.all(
|
|
|
|
multipleSelection.value.map(row => deleteTableDataApi(row.id))
|
|
|
|
)
|
|
|
|
ElMessage.success("批量删除成功")
|
|
|
|
getTableData()
|
|
|
|
} catch {
|
|
|
|
ElMessage.error("批量删除失败")
|
|
|
|
} finally {
|
|
|
|
loading.value = false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// #endregion
|
|
|
|
|
|
|
|
// 监听分页参数的变化
|
|
|
|
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<template>
|
|
|
|
<div class="app-container">
|
|
|
|
<el-card v-loading="loading" shadow="never" class="search-wrapper">
|
|
|
|
<el-form ref="searchFormRef" :inline="true" :model="searchData">
|
|
|
|
<el-form-item prop="username" label="用户名">
|
|
|
|
<el-input v-model="searchData.username" placeholder="请输入" />
|
|
|
|
</el-form-item>
|
|
|
|
<el-form-item prop="email" label="邮箱">
|
|
|
|
<el-input v-model="searchData.email" placeholder="请输入" />
|
|
|
|
</el-form-item>
|
|
|
|
<el-form-item>
|
|
|
|
<el-button type="primary" :icon="Search" @click="handleSearch">
|
|
|
|
查询
|
|
|
|
</el-button>
|
|
|
|
<el-button :icon="Refresh" @click="resetSearch">
|
|
|
|
重置
|
|
|
|
</el-button>
|
|
|
|
</el-form-item>
|
|
|
|
</el-form>
|
|
|
|
</el-card>
|
|
|
|
<el-card v-loading="loading" shadow="never">
|
|
|
|
<div class="toolbar-wrapper">
|
|
|
|
<div>
|
|
|
|
<el-button type="primary" :icon="CirclePlus" @click="dialogVisible = true">
|
|
|
|
新增用户
|
|
|
|
</el-button>
|
|
|
|
<el-button type="danger" :icon="Delete" @click="handleBatchDelete">
|
|
|
|
批量删除
|
|
|
|
</el-button>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<el-tooltip content="刷新当前页">
|
|
|
|
<el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
|
|
|
|
</el-tooltip>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="table-wrapper">
|
|
|
|
<el-table :data="tableData" @selection-change="handleSelectionChange">
|
|
|
|
<el-table-column type="selection" width="50" align="center" />
|
|
|
|
<el-table-column prop="username" label="用户名" align="center" />
|
|
|
|
<el-table-column prop="email" label="邮箱" align="center" />
|
|
|
|
<el-table-column prop="createTime" label="创建时间" align="center" />
|
|
|
|
<el-table-column prop="updateTime" label="更新时间" align="center" />
|
|
|
|
<el-table-column fixed="right" label="操作" width="150" align="center">
|
|
|
|
<template #default="scope">
|
|
|
|
<el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">
|
|
|
|
修改
|
|
|
|
</el-button>
|
|
|
|
<el-button type="danger" text bg size="small" @click="handleDelete(scope.row)">
|
|
|
|
删除
|
|
|
|
</el-button>
|
|
|
|
</template>
|
|
|
|
</el-table-column>
|
|
|
|
</el-table>
|
|
|
|
</div>
|
|
|
|
<div class="pager-wrapper">
|
|
|
|
<el-pagination
|
|
|
|
background
|
|
|
|
:layout="paginationData.layout"
|
|
|
|
:page-sizes="paginationData.pageSizes"
|
|
|
|
:total="paginationData.total"
|
|
|
|
:page-size="paginationData.pageSize"
|
|
|
|
:current-page="paginationData.currentPage"
|
|
|
|
@size-change="handleSizeChange"
|
|
|
|
@current-change="handleCurrentChange"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</el-card>
|
|
|
|
<!-- 新增/修改 -->
|
|
|
|
<el-dialog
|
|
|
|
v-model="dialogVisible"
|
|
|
|
:title="formData.id === undefined ? '新增用户' : '修改用户'"
|
|
|
|
width="30%"
|
|
|
|
@closed="resetForm"
|
|
|
|
>
|
|
|
|
<el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px" label-position="left">
|
|
|
|
<el-form-item prop="username" label="用户名">
|
|
|
|
<el-input v-model="formData.username" placeholder="请输入" />
|
|
|
|
</el-form-item>
|
|
|
|
<el-form-item v-if="formData.id === undefined" prop="email" label="用户邮箱">
|
|
|
|
<el-input v-model="formData.email" placeholder="请输入" />
|
|
|
|
</el-form-item>
|
|
|
|
<el-form-item v-if="formData.id === undefined" prop="password" label="密码">
|
|
|
|
<el-input v-model="formData.password" placeholder="请输入" />
|
|
|
|
</el-form-item>
|
|
|
|
</el-form>
|
|
|
|
<template #footer>
|
|
|
|
<el-button @click="dialogVisible = false">
|
|
|
|
取消
|
|
|
|
</el-button>
|
|
|
|
<el-button type="primary" :loading="loading" @click="handleCreateOrUpdate">
|
|
|
|
确认
|
|
|
|
</el-button>
|
|
|
|
</template>
|
|
|
|
</el-dialog>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.el-alert {
|
|
|
|
margin-bottom: 20px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.search-wrapper {
|
|
|
|
margin-bottom: 20px;
|
|
|
|
:deep(.el-card__body) {
|
|
|
|
padding-bottom: 2px;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
.toolbar-wrapper {
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
margin-bottom: 20px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.table-wrapper {
|
|
|
|
margin-bottom: 20px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.pager-wrapper {
|
|
|
|
display: flex;
|
|
|
|
justify-content: flex-end;
|
|
|
|
}
|
|
|
|
</style>
|