index.vue 7.13 KB
<template>
  <div class="dict-management">
    <!-- 搜索表单 -->
    <el-card class="search-form">
      <el-form :model="searchForm" inline>
        <el-form-item label="字典名称">
          <el-input v-model="searchForm.dictName" placeholder="请输入字典名称" clearable />
        </el-form-item>
        <el-form-item label="字典类型">
          <el-input v-model="searchForm.dictType" placeholder="请输入字典类型" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="正常" :value="1" />
            <el-option label="停用" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="handleReset">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作按钮 -->
    <el-card class="operation-buttons">
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon>
        新增字典
      </el-button>
      <el-button type="danger" :disabled="!multipleSelection.length" @click="handleBatchDelete">
        <el-icon><Delete /></el-icon>
        批量删除
      </el-button>
      <el-button type="success" @click="handleRefreshCache">
        <el-icon><Refresh /></el-icon>
        刷新缓存
      </el-button>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-container">
      <el-table
        v-loading="loading"
        :data="tableData"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="dictId" label="字典ID" width="80" />
        <el-table-column prop="dictName" label="字典名称" width="200" />
        <el-table-column prop="dictType" label="字典类型" width="200" />
        <el-table-column prop="status" label="状态" width="80">
          <template #default="{ row }">
            <el-tag :type="row.status === 1 ? 'success' : 'danger'">
              {{ row.status === 1 ? '正常' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button type="success" size="small" @click="handleManageItems(row)">
              字典项
            </el-button>
            <el-button type="danger" size="small" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="pagination.current"
        v-model:page-size="pagination.size"
        :total="pagination.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { DictType } from '@/types'

// 搜索表单
const searchForm = reactive({
  dictName: '',
  dictType: '',
  status: undefined
})

// 表格数据
const tableData = ref<DictType[]>([])
const loading = ref(false)
const multipleSelection = ref<DictType[]>([])

// 分页
const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

// 获取字典列表
const getDictList = async () => {
  loading.value = true
  try {
    // 模拟数据
    tableData.value = [
      {
        dictId: 1,
        dictName: '用户性别',
        dictType: 'sys_user_sex',
        status: 1,
        remark: '用户性别列表',
        createTime: '2024-01-01 10:00:00',
        updateTime: '2024-01-01 10:00:00'
      },
      {
        dictId: 2,
        dictName: '菜单状态',
        dictType: 'sys_show_hide',
        status: 1,
        remark: '菜单状态列表',
        createTime: '2024-01-01 10:00:00',
        updateTime: '2024-01-01 10:00:00'
      },
      {
        dictId: 3,
        dictName: '系统开关',
        dictType: 'sys_normal_disable',
        status: 1,
        remark: '系统开关列表',
        createTime: '2024-01-01 10:00:00',
        updateTime: '2024-01-01 10:00:00'
      }
    ]
    pagination.total = 3
  } catch (error) {
    ElMessage.error('获取字典列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  pagination.current = 1
  getDictList()
}

// 重置
const handleReset = () => {
  Object.assign(searchForm, {
    dictName: '',
    dictType: '',
    status: undefined
  })
  handleSearch()
}

// 新增字典
const handleAdd = () => {
  ElMessage.info('新增字典功能待实现')
}

// 编辑字典
const handleEdit = (row: DictType) => {
  ElMessage.info(`编辑字典: ${row.dictName}`)
}

// 管理字典项
const handleManageItems = (row: DictType) => {
  ElMessage.info(`管理字典项: ${row.dictName}`)
}

// 删除字典
const handleDelete = async (row: DictType) => {
  try {
    await ElMessageBox.confirm(`确定要删除字典 "${row.dictName}" 吗?`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    ElMessage.success('删除成功')
    getDictList()
  } catch (error) {
    // 用户取消删除
  }
}

// 批量删除
const handleBatchDelete = async () => {
  try {
    await ElMessageBox.confirm(`确定要删除选中的 ${multipleSelection.value.length} 个字典吗?`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    ElMessage.success('批量删除成功')
    getDictList()
  } catch (error) {
    // 用户取消删除
  }
}

// 刷新缓存
const handleRefreshCache = async () => {
  try {
    await ElMessageBox.confirm('确定要刷新字典缓存吗?', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    ElMessage.success('缓存刷新成功')
  } catch (error) {
    // 用户取消操作
  }
}

// 选择变化
const handleSelectionChange = (selection: DictType[]) => {
  multipleSelection.value = selection
}

// 分页大小变化
const handleSizeChange = (size: number) => {
  pagination.size = size
  getDictList()
}

// 当前页变化
const handleCurrentChange = (current: number) => {
  pagination.current = current
  getDictList()
}

// 组件挂载时获取数据
onMounted(() => {
  getDictList()
})
</script>

<style lang="scss" scoped>
.dict-management {
  .search-form {
    margin-bottom: 16px;
  }
  
  .operation-buttons {
    margin-bottom: 16px;
  }
  
  .table-container {
    .el-pagination {
      margin-top: 16px;
      text-align: right;
    }
  }
}
</style>