| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <template>
- <div class="page-container">
- <el-card class="search-card" shadow="never">
- <el-form :model="query" inline>
- <el-form-item label="关键字">
- <el-input
- v-model="query.keyword"
- placeholder="分类名称/备注"
- clearable
- style="width: 220px"
- @keyup.enter="handleSearch"
- />
- </el-form-item>
- <el-form-item>
- <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
- <el-button :icon="Refresh" @click="handleReset">重置</el-button>
- </el-form-item>
- </el-form>
- </el-card>
- <el-card class="table-card" shadow="never">
- <div class="toolbar">
- <el-button type="primary" :icon="Plus" @click="handleAdd">新增用户分类</el-button>
- </div>
- <el-table v-loading="loading" :data="tableData" border stripe>
- <el-table-column label="序号" type="index" width="70" align="center" />
- <el-table-column label="分类ID" prop="id" width="180" align="center" />
- <el-table-column label="分类名称" prop="name" min-width="180" />
- <el-table-column label="备注" prop="remark" min-width="260" show-overflow-tooltip>
- <template #default="{ row }">{{ row.remark || '-' }}</template>
- </el-table-column>
- <el-table-column label="创建时间" prop="createTime" width="170" align="center" :formatter="dateTimeFormatter" />
- <el-table-column label="操作" width="150" align="center" fixed="right">
- <template #default="{ row }">
- <el-button type="primary" link size="small" @click="handleEdit(row)">编辑</el-button>
- <el-button type="danger" link size="small" @click="handleDelete(row)">删除</el-button>
- </template>
- </el-table-column>
- </el-table>
- <div class="pagination">
- <el-pagination
- v-model:current-page="query.pageNum"
- v-model:page-size="query.pageSize"
- :page-sizes="[10, 20, 50]"
- :total="total"
- :hide-on-single-page="false"
- layout="total, sizes, prev, pager, next, jumper"
- background
- @size-change="loadData"
- @current-change="loadData"
- />
- </div>
- </el-card>
- <el-dialog
- v-model="formVisible"
- :title="formMode === 'add' ? '新增用户分类' : '编辑用户分类'"
- width="520px"
- :close-on-click-modal="false"
- @close="handleFormClose"
- >
- <el-form ref="formRef" :model="formData" :rules="formRules" label-width="90px">
- <el-form-item label="分类名称" prop="name">
- <el-input v-model.trim="formData.name" placeholder="请输入分类名称" maxlength="50" show-word-limit />
- </el-form-item>
- <el-form-item label="备注" prop="remark">
- <el-input
- v-model="formData.remark"
- type="textarea"
- :rows="3"
- placeholder="请输入备注"
- maxlength="255"
- show-word-limit
- />
- </el-form-item>
- </el-form>
- <template #footer>
- <el-button @click="formVisible = false">取消</el-button>
- <el-button type="primary" :loading="submitLoading" @click="handleSubmit">确定</el-button>
- </template>
- </el-dialog>
- </div>
- </template>
- <script setup>
- import { ref, reactive, onMounted } from 'vue'
- import { ElMessage, ElMessageBox } from 'element-plus'
- import { Search, Refresh, Plus } from '@element-plus/icons-vue'
- import { userCategoryApi } from '@/api'
- import { dateTimeFormatter } from '@/utils/date'
- import { parsePageData } from '@/utils/pagination'
- const loading = ref(false)
- const tableData = ref([])
- const total = ref(0)
- const query = reactive({
- keyword: '',
- pageNum: 1,
- pageSize: 10
- })
- async function loadData() {
- loading.value = true
- try {
- const res = await userCategoryApi.getUserCategoryPage({
- keyword: query.keyword || undefined,
- pageNum: query.pageNum,
- pageSize: query.pageSize
- })
- const { records, total: pageTotal } = parsePageData(res.data)
- tableData.value = records
- total.value = pageTotal
- } finally {
- loading.value = false
- }
- }
- function handleSearch() {
- query.pageNum = 1
- loadData()
- }
- function handleReset() {
- query.keyword = ''
- query.pageNum = 1
- loadData()
- }
- const formVisible = ref(false)
- const formMode = ref('add')
- const submitLoading = ref(false)
- const formRef = ref()
- const formData = reactive({
- id: null,
- name: '',
- remark: ''
- })
- const formRules = {
- name: [
- { required: true, message: '请输入分类名称', trigger: 'blur' },
- { max: 50, message: '分类名称最长50字符', trigger: 'blur' }
- ]
- }
- function resetForm() {
- Object.assign(formData, {
- id: null,
- name: '',
- remark: ''
- })
- }
- function handleAdd() {
- formMode.value = 'add'
- resetForm()
- formVisible.value = true
- }
- function handleEdit(row) {
- formMode.value = 'edit'
- Object.assign(formData, {
- id: row.id,
- name: row.name,
- remark: row.remark || ''
- })
- formVisible.value = true
- }
- function handleFormClose() {
- formRef.value?.resetFields()
- }
- async function handleSubmit() {
- await formRef.value?.validate()
- submitLoading.value = true
- try {
- if (formMode.value === 'add') {
- await userCategoryApi.addUserCategory({ ...formData })
- ElMessage.success('新增用户分类成功')
- } else {
- await userCategoryApi.updateUserCategory({ ...formData })
- ElMessage.success('修改成功')
- }
- formVisible.value = false
- loadData()
- } finally {
- submitLoading.value = false
- }
- }
- async function handleDelete(row) {
- await ElMessageBox.confirm(`确认删除用户分类【${row.name}】吗?此操作不可恢复`, '警告', {
- confirmButtonText: '确认删除',
- cancelButtonText: '取消',
- type: 'warning'
- })
- await userCategoryApi.deleteUserCategory(row.id)
- ElMessage.success('删除成功')
- loadData()
- }
- onMounted(() => {
- loadData()
- })
- </script>
- <style lang="scss" scoped>
- .page-container {
- display: flex;
- flex-direction: column;
- gap: 16px;
- }
- .search-card :deep(.el-card__body) {
- padding-bottom: 0;
- }
- .toolbar {
- display: flex;
- align-items: center;
- margin-bottom: 16px;
- }
- .pagination {
- margin-top: 16px;
- display: flex;
- justify-content: flex-end;
- }
- </style>
|