439 lines
11 KiB
Vue
Raw Normal View History

2025-03-03 16:50:51 +08:00
<template>
2025-03-25 07:08:37 +01:00
<div class="custom-page">
<el-row :gutter="20">
2025-03-25 07:53:09 +01:00
<splitpanes class="default-theme">
2025-05-26 16:10:52 +08:00
<!-- <pane size="16">
2025-03-25 07:53:09 +01:00
<el-col>
<custom-table-tree title="土地用途分类信息" :data="treeData" :option="treeOption" filter @node-click="handleNodeClick">
<template #default="{ data }">
<div :class="{ 'text-primary': data.id == treeSelected.id || data.id == treeSelected.pId }">
{{ data.landType }}
</div>
</template>
</custom-table-tree>
</el-col>
2025-05-26 16:10:52 +08:00
</pane> -->
2025-03-25 07:53:09 +01:00
<pane size="84">
<el-col>
<avue-crud
ref="crudRef"
v-model="state.form"
v-model:search="state.query"
v-model:page="state.pageData"
:table-loading="state.loading"
:data="state.data"
:option="state.options"
@refresh-change="refreshChange"
@search-reset="searchChange"
@search-change="searchChange"
@selection-change="selectionChange"
@current-change="currentChange"
@size-change="sizeChange"
@row-del="rowDel"
@row-save="rowSave"
@row-update="rowUpdate"
>
2025-05-26 16:10:52 +08:00
<template #icon="{ row }">
<i :class="row.icon" style="font-size: 24px"></i>
</template>
<!-- <template #icon="{ row }">
<i :class="row.icon" style="font-size: 24px"></i>
</template>
<template #menu="{ row, size }">
<el-button :size="size" text type="primary" @click="handleAdd(row)">新增子级</el-button>
</template> -->
2025-03-25 07:53:09 +01:00
<template #menu-left>
<el-button type="success" icon="download" @click="onExport">导出</el-button>
</template>
2025-03-04 16:24:17 +08:00
2025-03-25 07:53:09 +01:00
<template #status="{ row }">
<el-tag v-if="row.status == 1" type="success">启用</el-tag>
<el-tag v-if="row.status == 0" type="danger">禁用</el-tag>
</template>
2025-03-25 07:08:37 +01:00
2025-03-25 07:53:09 +01:00
<template #menu="scope">
<custom-table-operate :actions="state.options.actions" :data="scope" />
</template>
</avue-crud>
2025-05-26 16:10:52 +08:00
<!-- <avue-crud
ref="crudRef"
v-model="state.form"
v-model:search="state.query"
v-model:page="state.pageData"
:table-loading="state.loading"
:data="state.data"
:option="state.options"
@refresh-change="refreshChange"
@search-reset="searchChange"
@search-change="searchChange"
@selection-change="selectionChange"
@current-change="currentChange"
@size-change="sizeChange"
@row-del="rowDel"
@row-save="rowSave"
@row-update="rowUpdate"
>
<template #menu-left>
<el-button type="success" icon="download" @click="onExport">导出</el-button>
</template>
<template #status="{ row }">
<el-tag v-if="row.status == 1" type="success">启用</el-tag>
<el-tag v-if="row.status == 0" type="danger">禁用</el-tag>
</template>
<template #menu="scope">
<custom-table-operate :actions="state.options.actions" :data="scope" />
</template>
</avue-crud> -->
2025-03-25 07:53:09 +01:00
</el-col>
</pane>
</splitpanes>
2025-03-25 07:08:37 +01:00
</el-row>
</div>
2025-03-03 16:50:51 +08:00
</template>
2025-03-04 16:24:17 +08:00
<script setup>
2025-03-25 07:08:37 +01:00
import { ref, reactive, onMounted, nextTick, watch } from 'vue';
2025-03-03 16:50:51 +08:00
import { useApp } from '@/hooks';
import { CRUD_OPTIONS } from '@/config';
2025-03-14 17:33:44 +08:00
import { getLandTypeTree, landTypeSave, getLandType, exportLandType, delLandType, editLandType } from '@/apis/baseInfo';
2025-03-25 07:08:37 +01:00
import { useUserStore } from '@/store/modules/user';
2025-03-28 08:17:43 +01:00
import { isEmpty, getParentIds, flattenTree, setDicLabel, downloadFile } from '@/utils';
2025-03-03 16:50:51 +08:00
const { VITE_APP_BASE_API } = import.meta.env;
const app = useApp();
2025-03-25 07:08:37 +01:00
const UserStore = useUserStore();
2025-03-06 16:20:26 +08:00
const crudRef = ref(null);
2025-03-25 07:08:37 +01:00
const treeData = ref([]);
const treeOption = ref({
nodeKey: 'id',
props: { children: 'children', label: 'landType', id: 'id' },
});
const treeSelected = ref({});
const treeDicData = ref([]);
2025-03-03 16:50:51 +08:00
const state = reactive({
loading: false,
query: {
current: 1,
size: 10,
},
2025-03-25 07:08:37 +01:00
form: {
status: 1,
},
2025-03-03 16:50:51 +08:00
selection: [],
options: {
...CRUD_OPTIONS,
2025-05-26 16:10:52 +08:00
headerAlign: 'center',
align: 'center',
border: true,
index: true,
rowKey: 'id',
rowParentKey: 'pid',
2025-03-25 07:08:37 +01:00
dialogWidth: 600,
2025-03-14 17:33:44 +08:00
selection: false,
2025-03-03 16:50:51 +08:00
column: [
2025-05-26 16:10:52 +08:00
// { label: '编号', prop: 'id', addDisplay: false, editDisplay: false },
2025-03-06 16:20:26 +08:00
{
2025-03-25 07:08:37 +01:00
label: '分类名称',
2025-03-14 17:33:44 +08:00
prop: 'landType',
2025-03-25 07:08:37 +01:00
span: 24,
2025-03-14 17:33:44 +08:00
rules: [
{
required: true,
message: '请输入',
trigger: 'blur',
},
],
2025-03-25 07:08:37 +01:00
},
{
label: '用途分类',
prop: 'pid',
display: false,
2025-03-14 17:33:44 +08:00
render: ({ row }) => {
2025-03-25 07:08:37 +01:00
return setDicLabel(treeDicData.value, row.pid);
2025-03-14 17:33:44 +08:00
},
},
{
2025-03-25 07:08:37 +01:00
label: '用途分类',
prop: 'pids',
type: 'cascader',
checkStrictly: true,
hide: true,
addDisplay: true,
editDisplay: true,
viewDisplay: false,
props: {
label: 'landType',
value: 'id',
children: 'children',
},
dicUrl: `${VITE_APP_BASE_API}/land-resource/baseInfo/landTree`,
dicHeaders: {
authorization: UserStore.token,
},
2025-03-28 08:17:43 +01:00
dicFormatter: (res) => [{ id: '0', landType: '土地分类', children: res.data }],
2025-03-25 07:08:37 +01:00
span: 24,
2025-03-14 17:33:44 +08:00
rules: [
{
required: true,
message: '请输入',
trigger: 'blur',
},
],
2025-03-06 16:20:26 +08:00
},
2025-03-14 17:33:44 +08:00
{
label: '状态',
prop: 'status',
2025-03-25 07:08:37 +01:00
type: 'select',
dicData: [
{
label: '启用',
value: 1,
},
{
label: '禁用',
value: 0,
},
],
value: 1,
2025-03-14 17:33:44 +08:00
addDisplay: false,
editDisplay: false,
},
2025-03-03 16:50:51 +08:00
],
actions: [
{
2025-03-14 17:33:44 +08:00
name: ({ row }) => `${row.status == '0' ? '启' : '禁'}`,
2025-03-03 16:50:51 +08:00
icon: 'edit',
event: ({ row }) => rowStatus(row),
},
{
name: '编辑',
icon: 'edit',
event: ({ row }) => rowEdit(row),
},
{
type: 'danger',
name: '删除',
icon: 'delete',
event: ({ row }) => rowDel(row),
},
],
},
pageData: {
total: 0,
currentPage: 1,
pageSize: 10,
},
data: [],
currentRow: {},
});
2025-03-25 07:08:37 +01:00
watch(
() => treeData.value,
(val) => {
if (!isEmpty(val)) {
const list = flattenTree(val);
treeDicData.value = list.map((item) => {
2025-03-28 08:17:43 +01:00
return { label: item.landType, value: item.id, id: item.id, parentId: item.pid };
2025-03-25 07:08:37 +01:00
});
}
}
);
const getLandTree = async () => {
try {
const res = await getLandTypeTree();
if (res.code == 200) {
2025-05-26 16:10:52 +08:00
const { current, size, total, records } = res.data;
2025-03-28 08:17:43 +01:00
treeData.value = [{ id: '0', landType: '土地分类', children: res.data }];
2025-05-26 16:10:52 +08:00
state.data = treeData.value;
state.pageData = {
currentPage: current || 1,
pageSize: size || 10,
total: total,
};
2025-03-25 07:08:37 +01:00
}
} catch (err) {
app.$message.error(err.msg);
}
};
2025-03-03 16:50:51 +08:00
2025-03-25 07:08:37 +01:00
getLandTree();
2025-03-03 16:50:51 +08:00
2025-03-04 16:24:17 +08:00
const loadData = () => {
2025-03-14 17:33:44 +08:00
state.loading = true;
getLandType({
2025-03-28 08:17:43 +01:00
pid: treeSelected.value?.id ?? '0',
2025-03-14 17:33:44 +08:00
...state.query,
})
.then((res) => {
if (res.code === 200) {
const { current, size, total, records } = res.data;
state.data = records;
state.pageData = {
currentPage: current || 1,
pageSize: size || 10,
total: total,
};
}
})
.catch((err) => {
app.$message.error(err.msg);
state.data = [];
})
.finally(() => {
state.loading = false;
});
2025-03-04 16:24:17 +08:00
};
2025-03-25 07:08:37 +01:00
const handleNodeClick = (data, node) => {
treeSelected.value = data;
2025-05-26 16:10:52 +08:00
// loadData();
getLandTree();
2025-03-25 07:08:37 +01:00
};
2025-03-04 16:24:17 +08:00
onMounted(() => {
2025-05-26 16:10:52 +08:00
// loadData();
getLandTree();
2025-03-04 16:24:17 +08:00
});
// 页数
const currentChange = (current) => {
state.query.current = current;
2025-05-26 16:10:52 +08:00
// loadData();
getLandTree();
2025-03-04 16:24:17 +08:00
};
// 条数
const sizeChange = (size) => {
2025-03-14 17:33:44 +08:00
state.query.current = 1;
2025-03-04 16:24:17 +08:00
state.query.size = size;
2025-05-26 16:10:52 +08:00
// loadData();
getLandTree();
2025-03-04 16:24:17 +08:00
};
// 搜索
const searchChange = (params, done) => {
if (done) done();
state.query = params;
state.query.current = 1;
2025-05-26 16:10:52 +08:00
// loadData();
getLandTree();
2025-03-04 16:24:17 +08:00
};
// 刷新
const refreshChange = () => {
2025-05-26 16:10:52 +08:00
// loadData();
getLandTree();
2025-03-04 16:24:17 +08:00
app.$message.success('刷新成功');
};
// 选择
const selectionChange = (rows) => {
state.selection = rows;
};
2025-03-25 07:08:37 +01:00
// 启用
2025-03-14 17:33:44 +08:00
async function rowStatus(row) {
const { id } = row;
let status = row.status == '1' ? '0' : '1';
let params = {
id,
status,
};
let res = await editLandType(params);
if (res.code == 200) {
2025-03-25 07:08:37 +01:00
app.$message.success('操作成功!');
2025-03-14 17:33:44 +08:00
nextTick(() => {
2025-05-26 16:10:52 +08:00
row.status = status;
// loadData();
// getLandTree();
2025-03-14 17:33:44 +08:00
});
}
}
2025-03-03 16:50:51 +08:00
2025-03-25 07:08:37 +01:00
// 删除
2025-03-14 17:33:44 +08:00
async function rowDel(row, done, loading) {
let res = await delLandType(row.id);
if (res.code === 200) {
app.$message.success('已删除!');
2025-03-25 07:08:37 +01:00
getLandTree();
2025-05-26 16:10:52 +08:00
// loadData();
2025-03-14 17:33:44 +08:00
done();
2025-03-03 16:50:51 +08:00
}
2025-03-14 17:33:44 +08:00
loading();
}
2025-03-03 16:50:51 +08:00
2025-03-25 07:08:37 +01:00
const setPid = (row) => {
if (!isEmpty(row.pids)) {
const len = row.pids.length;
2025-03-28 08:17:43 +01:00
row.pid = row?.pids[len - 1] ?? '0';
2025-03-25 07:08:37 +01:00
}
2025-03-06 16:20:26 +08:00
};
2025-03-03 16:50:51 +08:00
2025-03-25 07:08:37 +01:00
// 新增
const rowSave = async (row, done, loading) => {
setPid(row);
landTypeSave(row)
.then((res) => {
if (res.code === 200) {
app.$message.success('添加成功!');
done();
getLandTree();
2025-05-26 16:10:52 +08:00
// loadData();
2025-03-25 07:08:37 +01:00
}
})
.catch((err) => {
app.$message.error(err.msg);
})
.finally(() => {
loading();
});
2025-03-04 16:24:17 +08:00
};
2025-03-25 07:08:37 +01:00
// 编辑
const rowEdit = (row) => {
2025-03-28 08:17:43 +01:00
row.pids = getParentIds(treeData.value, row.id);
console.log('pids=', row.pids);
2025-03-25 07:08:37 +01:00
crudRef.value && crudRef.value.rowEdit(row);
2025-03-04 16:24:17 +08:00
};
2025-03-06 16:20:26 +08:00
const rowUpdate = (row, index, done, loading) => {
2025-03-25 07:08:37 +01:00
setPid(row);
editLandType(row).then((res) => {
2025-03-14 17:33:44 +08:00
if (res.code === 200) {
app.$message.success('更新成功!');
2025-05-26 16:10:52 +08:00
// loadData();
2025-03-25 07:08:37 +01:00
getLandTree();
2025-03-14 17:33:44 +08:00
done();
}
});
loading();
2025-03-04 16:24:17 +08:00
};
2025-03-25 07:08:37 +01:00
// 导出
const onExport = () => {
if (isEmpty(state.data)) {
app.$message.error('请先选择用地分类!');
return;
2025-03-14 17:33:44 +08:00
}
2025-03-25 07:08:37 +01:00
state.loading = true;
const fileName = '用地分类.xlsx';
exportLandType({
pid: treeSelected.value.id,
})
.then((res) => {
if (res.status === 200) {
downloadFile(res.data, fileName, 'blob');
app.$message.success('导出成功!');
}
})
.catch((err) => {
app.$message.error('导出失败!');
})
.finally(() => {
state.loading = false;
});
};
</script>