123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- <template>
- <div class="pageWrap bgc">
- <div :class="['h-full mr-2 aside-wrap', isCollapsed ? 'aside-wrap--collapsed' : '']">
- <div :class="['trigger-bar', isCollapsed ? 'trigger-bar--collapsed' : '']" @click="isCollapsed = !isCollapsed">
- <div class="bar__top"></div>
- <div class="bar__bottom"></div>
- </div>
- <el-card class="h-full border-none menu-card" shadow="never">
- <template #header>
- <div class="flex justify-between">
- <div class="card-header">
- <span>字典分类</span>
- </div>
- <div class="">
- <el-space>
- <el-icon :style="{ color: 'var(--el-color-primary)' }" class="cursor-pointer" size="22" @click="addHandler('tree')"
- ><Plus />
- </el-icon>
- <el-icon class="cursor-pointer" size="22" @click="getGroup"><Refresh /></el-icon>
- </el-space>
- </div>
- </div>
- </template>
- <el-input v-model="groupFilterText" placeholder="输入关键字进行过滤" clearable />
- <el-tree
- ref="treeRef"
- v-loading="showGroupLoading"
- class="menu-tree-box py-3"
- :data="treeData"
- node-key="id"
- :current-node-key="''"
- :highlight-current="true"
- :expand-on-click-node="false"
- :props="{ label: 'name' }"
- default-expand-all
- :filter-node-method="filterNode"
- @node-click="leftTreeClick"
- >
- <template #default="{ node, data }">
- <span class="custom-tree-node">
- <span class="label">{{ node.label }}</span>
- <span class="code">{{ data.code }}</span>
- <span v-if="data.id" class="do">
- <el-icon :style="{ color: 'var(--el-color-primary)' }" size="24" @click.stop="dicEdit(data)"><Edit /></el-icon>
- <el-icon :style="{ color: 'var(--el-color-error)' }" size="24" @click.stop="dictDel(node, data)"><Delete /></el-icon>
- </span>
- </span>
- </template>
- </el-tree>
- </el-card>
- </div>
- <div class="content-warp flex-column-page-wrap">
- <!-- 公用搜索组件 -->
- <LeSearchForm ref="searchForm" v-model:searchData="searchData" :forms="forms" :loading="tableOpts.options.loading"></LeSearchForm>
- <!-- LeTable 组件使用 -->
- <LeTable
- ref="tableRef"
- v-model:searchParams="tableOpts.searchParams"
- v-bind="tableOpts"
- v-model:curRow="tableOpts.curRow"
- v-model:checked-options="checkedColumns"
- :columns="activeColumns"
- >
- <template #toolLeft>
- <el-button plain :icon="Plus" type="primary" @click="addHandler('table')">新增</el-button>
- <el-button plain :icon="Delete" :disabled="curSelectionRows.length === 0" type="danger" @click="batch_del">删除</el-button>
- </template>
- <template #filterAvatarSlot="scope">
- <el-avatar :src="scope.row.avatar" size="small"></el-avatar>
- </template>
- <template #statusSlot="scope">
- <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="0" @change="() => handleStatusChange(scope.row)"></el-switch>
- </template>
- <template #actionSlot="{ row }">
- <div class="flex flex-align-pack-center">
- <LeIcon
- class="text-lg text-icon-color cursor-pointer icon-mage--edit"
- icon-class="icon-processInfo-mage--edit"
- @click="table_edit(row)"
- />
- <LeIcon class="text-lg ml-2 text-rose-700 cursor-pointer" icon-class="icon-processInfo-iconoir--trash" @click="table_del(row)" />
- </div>
- </template>
- </LeTable>
- </div>
- <LeFormConfigDialog
- v-if="visible"
- ref="dialogDictRef"
- v-model="visible"
- :title="`${isCreate ? '新增' : '编辑'}项`"
- width="600px"
- :form-data="activeData"
- :form-options="formOptions"
- @submit="submitHandler"
- />
- </div>
- </template>
- <script lang="tsx" setup>
- import dict from '@/api/system/dict'
- import { computed, nextTick, ref, watch } from 'vue'
- import { ElMessage, ElTree, ElMessageBox } from 'element-plus'
- import { useTablePage } from '@/hooks/useTablePage'
- import { Plus, Delete, Edit, Refresh } from '@element-plus/icons-vue'
- const isCollapsed = ref(false)
- const visible = ref(false) // 弹窗显示隐藏
- const isCreate = ref(true)
- const activeData = ref({})
- const current_mode = ref('') // 当前模式 (tree | table)
- const formsDialog = [
- {
- prop: 'pid',
- label: '所属字典',
- itemType: 'select',
- filterable: true,
- options: [],
- rules: [{ required: true, message: '请选择所属字典', trigger: 'blur' }]
- },
- {
- prop: 'name',
- label: '字典名称',
- itemType: 'input',
- rules: [{ required: true, message: '请输入字典名称', trigger: 'blur' }]
- },
- {
- prop: 'content',
- label: '内容',
- itemType: 'input',
- rules: [{ required: true, message: '请输入内容', trigger: 'blur' }]
- },
- {
- prop: 'code',
- label: '字典编码',
- itemType: 'input',
- rules: [{ required: true, message: '请输入字典编码', trigger: 'blur' }]
- },
- {
- prop: 'status',
- label: '字典状态',
- itemType: 'switch',
- activeText: '正常',
- inactiveText: '禁用'
- },
- {
- prop: 'sort',
- label: '排序',
- itemType: 'inputNumber',
- rules: [{ required: true, message: '请输入排序', trigger: 'blur' }]
- },
- {
- prop: 'remark',
- label: '备注',
- itemType: 'input'
- }
- ]
- // 新增的表单 和 编辑的表单
- const formOptions = computed(() => {
- let tempForm = []
- if (current_mode.value === 'tree') {
- for (let i in formsDialog) {
- let fd = formsDialog[i]
- if (fd.prop == 'pid') {
- continue
- }
- tempForm.push(fd)
- }
- } else {
- tempForm = formsDialog
- }
- return {
- forms: tempForm,
- labelWidth: 120,
- span: 30,
- showResetBtn: true,
- formConfig: {
- showCancelBtn: true,
- submitLoading: false
- }
- }
- })
- const showGroupLoading = ref(false)
- const groupFilterText = ref('')
- const treeRef = ref<InstanceType<typeof ElTree>>()
- const filterNode = (value: string, data: Tree) => {
- if (!value) return true
- return data.name.includes(value)
- }
- const treeData = ref([])
- // 获取左侧菜单数据
- const getGroup = async () => {
- showGroupLoading.value = true
- let data = await dict.dictListParentApi()
- formOptions.value.forms[0].options = data.map(item => {
- return { value: item.id, label: item.name }
- })
- showGroupLoading.value = false
- data.unshift({ id: '', name: '所有' })
- treeData.value = data
- }
- // 左侧菜单点击
- const leftTreeClick = data => {
- searchData.value = { ...searchData.value, pid: data.id ? data.id : null }
- }
- // 表格搜索条件
- const forms = ref([
- {
- prop: 'name',
- label: '字典名称:',
- itemType: 'input',
- placeholder: '请输入字典名称'
- },
- {
- prop: 'code',
- label: '字典编码:',
- itemType: 'input',
- placeholder: '请输入字典编码'
- },
- {
- prop: 'status',
- label: '字典状态:',
- itemType: 'select',
- placeholder: '请选择字典状态',
- options: [
- {
- label: '禁用',
- value: 0
- },
- {
- label: '正常',
- value: 1
- }
- ]
- }
- ])
- // table列表数据请求
- const queryList = async () => {
- const { options, searchParams } = tableOpts
- options.loading = true
- try {
- const { records: list, total } = await dict.dictPageApi(searchParams)
- tableOpts.total = total
- tableOpts.list = list
- } catch {
- tableOpts.total = 0
- tableOpts.list = []
- } finally {
- options.loading = false
- }
- }
- // table 参数
- const columns = [
- {
- prop: 'name',
- label: '字典名称',
- minWidth: 80
- },
- {
- prop: 'code',
- label: '字典编码',
- minWidth: 100
- },
- {
- prop: 'content',
- label: '内容',
- minWidth: 150
- },
- {
- prop: 'sort',
- label: '排序'
- },
- {
- prop: 'status',
- label: '字典状态',
- slots: {
- default: 'statusSlot'
- }
- },
- {
- prop: 'remark',
- label: '备注',
- minWidth: 100
- },
- {
- prop: 'action',
- label: '操作',
- width: 100,
- fixed: 'right',
- align: 'center',
- slots: {
- default: 'actionSlot'
- }
- }
- ]
- const { searchData, tableOpts, checkedColumns, activeColumns, curSelectionRows, updateParams } = useTablePage(
- {
- options: {
- showIndex: false
- },
- // 需要展示的列
- columns,
- // 控制列配置
- columnsConfig: {
- columns
- }
- },
- {
- queryList,
- fetchImmediate: false
- }
- )
- // 删除
- const deleteItem = async ids => {
- // try {
- await dict.dictDeleteApi(ids)
- if (current_mode.value === 'tree') {
- getGroup()
- }
- updateParams()
- ElMessage.success(`删除成功~`)
- // } catch (e) {
- // ElMessage.error(`删除失败~`)
- // }
- }
- // 单个删除
- const table_del = (row: any) => {
- ElMessageBox.confirm(`确认删除「${row.name}」这条数据?`, '提示', {
- confirmButtonText: '确认',
- cancelButtonText: '取消',
- type: 'error'
- }).then(async () => {
- deleteItem([row.id])
- })
- }
- //批量删除
- const batch_del = () => {
- const ids = curSelectionRows.value.map(item => item.id) // 多选数据
- ElMessageBox.confirm('是否删除选中数据?', '提示', {
- confirmButtonText: '确认',
- cancelButtonText: '取消',
- type: 'error',
- buttonSize: 'default'
- }).then(() => {
- deleteItem(ids)
- })
- }
- const table_edit = async row => {
- isCreate.value = false
- activeData.value = { ...row, status: row.status ? true : false }
- visible.value = true
- }
- // 弹窗事件
- const submitHandler = async params => {
- try {
- formOptions.value.formConfig.submitLoading = true
- params.status = params.status ? 1 : 0
- params.id = activeData.value.id ? activeData.value.id : null
- await dict.dictAddOrEditSaveApi(params)
- ElMessage.success(`${isCreate.value ? '新增' : '修改'}成功~`)
- visible.value = false
- if (current_mode.value === 'tree') {
- getGroup()
- }
- updateParams()
- } finally {
- formOptions.value.formConfig.submitLoading = false
- }
- }
- const addHandler = mode => {
- isCreate.value = true
- activeData.value = {}
- visible.value = true
- current_mode.value = mode
- }
- // 删除左侧树
- const dictDel = (node, data) => {
- current_mode.value = 'tree'
- ElMessageBox.confirm(`确定删除 ${data.name} 项吗?`, '提示')
- .then(async () => {
- // try {
- deleteItem([data.id])
- // } catch (e) {
- // ElMessage.error(`删除失败~`)
- // }
- })
- .catch(() => {})
- }
- const dicEdit = data => {
- current_mode.value = 'tree'
- table_edit(data)
- }
- const handleStatusChange = (row: any) => {
- // 字典状态 0、禁用 1、正常
- const status = row.status
- let text = Number(row.status) === 0 ? '禁用' : '启用'
- ElMessageBox.confirm('请确认是否' + text + '当前数据?', '提示', { type: 'warning' })
- .then(async () => {
- await dict.dictStatusApi({ id: row.id, status }) // 接口请求
- ElMessage.success(`${text}成功`)
- })
- .catch(status => {
- if (status == 'cancel') {
- row.status = Number(row.status) === 0 ? 1 : 0
- }
- })
- }
- nextTick(() => {
- getGroup()
- queryList()
- })
- watch(groupFilterText, val => {
- treeRef.value!.filter(val)
- })
- </script>
- <style scoped lang="scss">
- .custom-tree-node {
- display: flex;
- flex: 1;
- align-items: center;
- justify-content: space-between;
- font-size: 14px;
- padding-right: 24px;
- height: 100%;
- &:hover {
- .code {
- display: none;
- }
- .do {
- display: inline-block;
- }
- }
- .do {
- display: none;
- }
- .code {
- font-size: 12px;
- color: var(--el-color-primary);
- }
- .do i {
- margin-left: 3px;
- color: #999;
- padding: 3px;
- font-size: 20px;
- &:hover {
- color: #333;
- }
- }
- }
- .content-warp {
- // 300 + 28
- width: calc(100% - 328px);
- }
- .aside-wrap--collapsed {
- margin-left: -10px;
- .menu-card {
- width: 0;
- }
- }
- @media (max-width: 750px) {
- .pageWrap {
- //overflow-y: auto;
- flex-direction: column;
- overflow-y: auto;
- :deep(.aside-wrap) {
- margin-bottom: 10px;
- margin-right: 0;
- //height: 50%;
- .menu-card {
- .el-card__body {
- width: 100%;
- }
- }
- .trigger-bar {
- display: none;
- }
- }
- .content-warp {
- width: 100%;
- }
- }
- }
- </style>
|