2022-10-20 20:58:57 +08:00
|
|
|
<script lang="ts" setup>
|
2024-02-03 16:07:17 +08:00
|
|
|
import { reactive, ref, watch } from "vue"
|
2022-10-20 20:58:57 +08:00
|
|
|
import { createTableDataApi, deleteTableDataApi, updateTableDataApi, getTableDataApi } from "@/api/table"
|
2024-07-22 11:40:25 +08:00
|
|
|
import { type CreateOrUpdateTableRequestData, type TableData } from "@/api/table/types/table"
|
2022-10-20 20:58:57 +08:00
|
|
|
import { type FormInstance, type FormRules, ElMessage, ElMessageBox } from "element-plus"
|
|
|
|
import { Search, Refresh, CirclePlus, Delete, Download, RefreshRight } from "@element-plus/icons-vue"
|
2022-10-24 13:17:43 +08:00
|
|
|
import { usePagination } from "@/hooks/usePagination"
|
2024-03-11 09:20:19 +08:00
|
|
|
import { cloneDeep } from "lodash-es"
|
2022-10-20 20:58:57 +08:00
|
|
|
|
2023-02-22 15:53:04 +08:00
|
|
|
defineOptions({
|
2023-06-20 09:13:47 +08:00
|
|
|
// 命名当前组件
|
2023-02-22 15:53:04 +08:00
|
|
|
name: "ElementPlus"
|
|
|
|
})
|
|
|
|
|
2022-10-20 20:58:57 +08:00
|
|
|
const loading = ref<boolean>(false)
|
2022-10-24 13:17:43 +08:00
|
|
|
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()
|
2022-10-20 20:58:57 +08:00
|
|
|
|
|
|
|
//#region 增
|
2024-02-03 16:07:17 +08:00
|
|
|
const DEFAULT_FORM_DATA: CreateOrUpdateTableRequestData = {
|
|
|
|
id: undefined,
|
2022-10-20 20:58:57 +08:00
|
|
|
username: "",
|
|
|
|
password: ""
|
2024-02-03 16:07:17 +08:00
|
|
|
}
|
|
|
|
const dialogVisible = ref<boolean>(false)
|
|
|
|
const formRef = ref<FormInstance | null>(null)
|
2024-03-11 09:20:19 +08:00
|
|
|
const formData = ref<CreateOrUpdateTableRequestData>(cloneDeep(DEFAULT_FORM_DATA))
|
2024-02-03 16:07:17 +08:00
|
|
|
const formRules: FormRules<CreateOrUpdateTableRequestData> = {
|
2022-10-20 20:58:57 +08:00
|
|
|
username: [{ required: true, trigger: "blur", message: "请输入用户名" }],
|
|
|
|
password: [{ required: true, trigger: "blur", message: "请输入密码" }]
|
2024-02-03 16:07:17 +08:00
|
|
|
}
|
2024-01-22 22:38:11 +08:00
|
|
|
const handleCreateOrUpdate = () => {
|
2023-06-20 09:13:47 +08:00
|
|
|
formRef.value?.validate((valid: boolean, fields) => {
|
2024-01-22 22:38:11 +08:00
|
|
|
if (!valid) return console.error("表单校验不通过", fields)
|
|
|
|
loading.value = true
|
2024-02-03 16:07:17 +08:00
|
|
|
const api = formData.value.id === undefined ? createTableDataApi : updateTableDataApi
|
|
|
|
api(formData.value)
|
2024-01-22 22:38:11 +08:00
|
|
|
.then(() => {
|
|
|
|
ElMessage.success("操作成功")
|
|
|
|
dialogVisible.value = false
|
|
|
|
getTableData()
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
loading.value = false
|
|
|
|
})
|
2022-10-20 20:58:57 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
const resetForm = () => {
|
2024-02-03 16:07:17 +08:00
|
|
|
formRef.value?.clearValidate()
|
2024-03-11 09:20:19 +08:00
|
|
|
formData.value = cloneDeep(DEFAULT_FORM_DATA)
|
2022-10-20 20:58:57 +08:00
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region 删
|
2024-07-22 11:40:25 +08:00
|
|
|
const handleDelete = (row: TableData) => {
|
2022-10-20 20:58:57 +08:00
|
|
|
ElMessageBox.confirm(`正在删除用户:${row.username},确认删除?`, "提示", {
|
|
|
|
confirmButtonText: "确定",
|
|
|
|
cancelButtonText: "取消",
|
|
|
|
type: "warning"
|
|
|
|
}).then(() => {
|
|
|
|
deleteTableDataApi(row.id).then(() => {
|
|
|
|
ElMessage.success("删除成功")
|
|
|
|
getTableData()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region 改
|
2024-07-22 11:40:25 +08:00
|
|
|
const handleUpdate = (row: TableData) => {
|
2022-10-20 20:58:57 +08:00
|
|
|
dialogVisible.value = true
|
2024-03-11 09:20:19 +08:00
|
|
|
formData.value = cloneDeep(row)
|
2022-10-20 20:58:57 +08:00
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
//#region 查
|
2024-07-22 11:40:25 +08:00
|
|
|
const tableData = ref<TableData[]>([])
|
2022-10-20 20:58:57 +08:00
|
|
|
const searchFormRef = ref<FormInstance | null>(null)
|
|
|
|
const searchData = reactive({
|
|
|
|
username: "",
|
|
|
|
phone: ""
|
|
|
|
})
|
|
|
|
const getTableData = () => {
|
|
|
|
loading.value = true
|
|
|
|
getTableDataApi({
|
|
|
|
currentPage: paginationData.currentPage,
|
2022-10-24 13:17:43 +08:00
|
|
|
size: paginationData.pageSize,
|
2022-10-25 17:29:28 +08:00
|
|
|
username: searchData.username || undefined,
|
|
|
|
phone: searchData.phone || undefined
|
2022-10-20 20:58:57 +08:00
|
|
|
})
|
2024-02-03 16:07:17 +08:00
|
|
|
.then(({ data }) => {
|
|
|
|
paginationData.total = data.total
|
|
|
|
tableData.value = data.list
|
2022-10-20 20:58:57 +08:00
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
tableData.value = []
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
loading.value = false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const handleSearch = () => {
|
2023-06-20 09:13:47 +08:00
|
|
|
paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
|
2022-10-20 20:58:57 +08:00
|
|
|
}
|
|
|
|
const resetSearch = () => {
|
|
|
|
searchFormRef.value?.resetFields()
|
2023-06-20 09:13:47 +08:00
|
|
|
handleSearch()
|
2022-10-20 20:58:57 +08:00
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2022-10-24 13:17:43 +08:00
|
|
|
/** 监听分页参数的变化 */
|
|
|
|
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
|
2022-10-20 20:58:57 +08:00
|
|
|
</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="phone" label="手机号">
|
|
|
|
<el-input v-model="searchData.phone" 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">批量删除</el-button>
|
|
|
|
</div>
|
|
|
|
<div>
|
2022-10-24 11:02:23 +08:00
|
|
|
<el-tooltip content="下载">
|
2022-10-20 20:58:57 +08:00
|
|
|
<el-button type="primary" :icon="Download" circle />
|
|
|
|
</el-tooltip>
|
2023-06-20 09:13:47 +08:00
|
|
|
<el-tooltip content="刷新当前页">
|
|
|
|
<el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
|
2022-10-20 20:58:57 +08:00
|
|
|
</el-tooltip>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="table-wrapper">
|
2022-10-28 14:27:35 +08:00
|
|
|
<el-table :data="tableData">
|
2022-10-20 20:58:57 +08:00
|
|
|
<el-table-column type="selection" width="50" align="center" />
|
|
|
|
<el-table-column prop="username" label="用户名" align="center" />
|
|
|
|
<el-table-column prop="roles" label="角色" align="center">
|
|
|
|
<template #default="scope">
|
2024-03-29 21:14:11 +08:00
|
|
|
<el-tag v-if="scope.row.roles === 'admin'" type="primary" effect="plain">admin</el-tag>
|
2022-10-20 20:58:57 +08:00
|
|
|
<el-tag v-else type="warning" effect="plain">{{ scope.row.roles }}</el-tag>
|
|
|
|
</template>
|
|
|
|
</el-table-column>
|
|
|
|
<el-table-column prop="phone" label="手机号" align="center" />
|
|
|
|
<el-table-column prop="email" label="邮箱" align="center" />
|
|
|
|
<el-table-column prop="status" label="状态" align="center">
|
|
|
|
<template #default="scope">
|
|
|
|
<el-tag v-if="scope.row.status" type="success" effect="plain">启用</el-tag>
|
|
|
|
<el-tag v-else type="danger" effect="plain">禁用</el-tag>
|
|
|
|
</template>
|
|
|
|
</el-table-column>
|
2022-10-24 13:17:43 +08:00
|
|
|
<el-table-column prop="createTime" label="创建时间" align="center" />
|
2022-10-20 20:58:57 +08:00
|
|
|
<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
|
2022-10-24 13:17:43 +08:00
|
|
|
:layout="paginationData.layout"
|
|
|
|
:page-sizes="paginationData.pageSizes"
|
2022-10-20 20:58:57 +08:00
|
|
|
:total="paginationData.total"
|
2022-10-24 13:17:43 +08:00
|
|
|
:page-size="paginationData.pageSize"
|
2022-10-20 20:58:57 +08:00
|
|
|
:currentPage="paginationData.currentPage"
|
|
|
|
@size-change="handleSizeChange"
|
|
|
|
@current-change="handleCurrentChange"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</el-card>
|
2022-10-24 13:17:43 +08:00
|
|
|
<!-- 新增/修改 -->
|
2022-10-20 20:58:57 +08:00
|
|
|
<el-dialog
|
|
|
|
v-model="dialogVisible"
|
2024-02-03 16:07:17 +08:00
|
|
|
:title="formData.id === undefined ? '新增用户' : '修改用户'"
|
2024-01-22 22:38:11 +08:00
|
|
|
@closed="resetForm"
|
2022-10-24 13:17:43 +08:00
|
|
|
width="30%"
|
2022-10-20 20:58:57 +08:00
|
|
|
>
|
|
|
|
<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>
|
2024-02-03 16:07:17 +08:00
|
|
|
<el-form-item prop="password" label="密码" v-if="formData.id === undefined">
|
2022-10-20 20:58:57 +08:00
|
|
|
<el-input v-model="formData.password" placeholder="请输入" />
|
|
|
|
</el-form-item>
|
|
|
|
</el-form>
|
|
|
|
<template #footer>
|
|
|
|
<el-button @click="dialogVisible = false">取消</el-button>
|
2024-01-22 22:38:11 +08:00
|
|
|
<el-button type="primary" @click="handleCreateOrUpdate" :loading="loading">确认</el-button>
|
2022-10-20 20:58:57 +08:00
|
|
|
</template>
|
|
|
|
</el-dialog>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
.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>
|