| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- <template>
- <div class="station-config-page">
- <a-card :bordered="false">
- <a-form :model="queryParams" layout="inline">
- <a-form-item v-if="hasStation" label="场站">
- <a-select v-model:value="queryParams.stationId" allow-clear show-search class="query-select" :options="stationOptions" :filter-option="filterOption" />
- </a-form-item>
- <a-form-item v-for="item in searchFields" :key="item.name" :label="item.label">
- <a-select v-if="item.type === 'select'" v-model:value="queryParams[item.name]" allow-clear class="query-select" :options="item.options" />
- <a-input v-else v-model:value="queryParams[item.name]" allow-clear :placeholder="'请输入' + item.label" />
- </a-form-item>
- <a-form-item>
- <a-space>
- <a-button type="primary" :loading="loading" @click="handleQuery">查询</a-button>
- <a-button @click="resetQuery">重置</a-button>
- <a-button v-hasPermi="[moduleConfig.perm + ':add']" type="primary" @click="openForm()">新增</a-button>
- <a-button v-hasPermi="[moduleConfig.perm + ':export']" @click="handleExport">导出</a-button>
- </a-space>
- </a-form-item>
- </a-form>
- </a-card>
- <a-card :bordered="false" class="section">
- <a-table :columns="columns" :data-source="rows" :loading="loading" :pagination="pagination" row-key="id" :scroll="tableScroll" @change="handleTableChange">
- <template #bodyCell="{ column, record }">
- <template v-if="column.key === 'operation'">
- <a-space>
- <a v-hasPermi="[moduleConfig.perm + ':edit']" @click="openForm(record)">编辑</a>
- <a-popconfirm :title="'确认删除该' + moduleConfig.title + '?'" @confirm="removeRecord(record)">
- <a v-hasPermi="[moduleConfig.perm + ':remove']" class="danger-link">删除</a>
- </a-popconfirm>
- </a-space>
- </template>
- </template>
- </a-table>
- </a-card>
- <a-modal v-model:open="formOpen" :title="form.id ? '编辑' + moduleConfig.title : '新增' + moduleConfig.title" width="860px" @ok="submitForm">
- <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 7 }" :wrapper-col="{ span: 15 }">
- <a-row :gutter="16">
- <a-col v-if="hasStation" :span="12">
- <a-form-item label="所属场站" name="stationId">
- <a-select v-model:value="form.stationId" show-search :options="stationOptions" :filter-option="filterOption" @change="handleStationChange" />
- </a-form-item>
- </a-col>
- <a-col v-for="item in moduleConfig.fields" :key="item[0]" :span="12">
- <a-form-item :label="item[1]" :name="item[0]">
- <a-select v-if="item[2] === 'select'" v-model:value="form[item[0]]" allow-clear :options="toOptions(item[4])" />
- <a-select v-else-if="item[2] === 'device'" v-model:value="form[deviceFieldName]" allow-clear show-search :options="deviceOptions" :filter-option="filterOption" @change="handleDeviceChange" />
- <a-input-number v-else-if="item[2] === 'number'" v-model:value="form[item[0]]" class="full-input" />
- <a-date-picker v-else-if="item[2] === 'date'" v-model:value="form[item[0]]" value-format="YYYY-MM-DD" class="full-input" />
- <a-input v-else v-model:value="form[item[0]]" allow-clear />
- </a-form-item>
- </a-col>
- </a-row>
- </a-form>
- </a-modal>
- </div>
- </template>
- <script setup>
- import { computed, reactive, ref } from 'vue';
- import { message } from 'ant-design-vue';
- import BearJiaUtil from '@/utils/BearJiaUtil';
- import { listStation } from '@/api/config/station';
- import { listStationDevice } from '@/api/config/stationDevice';
- const props = defineProps({ moduleConfig: { type: Object, required: true }, api: { type: Object, required: true } });
- const loading = ref(false);
- const rows = ref([]);
- const stationOptions = ref([]);
- const allDevices = ref([]);
- const formOpen = ref(false);
- const formRef = ref();
- const queryParams = reactive({ pageNum: 1, pageSize: 10, stationId: undefined });
- const pagination = reactive({ current: 1, pageSize: 10, total: 0, showSizeChanger: true });
- const form = reactive({});
- const hasStation = computed(() => props.moduleConfig.hasStation !== false);
- const deviceFieldName = computed(() => props.moduleConfig.fields.find((item) => item[2] === 'device')?.[0]);
- const fieldMap = computed(() => new Map(props.moduleConfig.fields.map((item) => [item[0], item])));
- const searchFields = computed(() => props.moduleConfig.search.filter((name) => name !== 'stationId').map((name) => ({ name, label: fieldMap.value.get(name)?.[1] || name, type: fieldMap.value.get(name)?.[2], options: toOptions(fieldMap.value.get(name)?.[4]) })));
- const columns = computed(() => [
- { title: 'ID', dataIndex: 'id', width: 80 },
- ...props.moduleConfig.columns.map((name) => ({ title: name === 'stationName' ? '场站' : fieldMap.value.get(name)?.[1] || name, dataIndex: name, width: 140, ellipsis: true })),
- { title: '操作', key: 'operation', width: 130, fixed: 'right' }
- ]);
- const tableScroll = computed(() => ({ x: columns.value.reduce((sum, col) => sum + (col.width || 140), 0) }));
- const rules = computed(() => ({
- ...(hasStation.value ? { stationId: [{ required: true, message: '请选择所属场站', trigger: 'change' }] } : {}),
- ...Object.fromEntries(props.moduleConfig.fields.filter((item) => item[3]).map((item) => [item[0], [{ required: true, message: '请输入' + item[1], trigger: item[2] === 'select' || item[2] === 'device' ? 'change' : 'blur' }]]))
- }));
- const deviceOptions = computed(() => allDevices.value
- .filter((item) => !form.stationId || String(item.stationId) === String(form.stationId))
- .map((item) => ({ label: item.deviceName || item.name || item.id, value: item.id, raw: item })));
- const toOptions = (items = []) => items.map((item) => ({ label: item[0], value: item[1] }));
- const filterOption = (input, option) => String(option?.label || '').toLowerCase().includes(input.toLowerCase());
- const resetObject = (target, source = {}) => { Object.keys(target).forEach((key) => delete target[key]); Object.assign(target, source); };
- const loadStations = async () => {
- if (!hasStation.value) return;
- const res = await listStation({ pageNum: 1, pageSize: 500 });
- const list = res?.rows || res?.data || [];
- stationOptions.value = list.map((item) => ({ label: item.stationName || item.name || item.id, value: item.id }));
- };
- const loadDevices = async () => {
- if (!deviceFieldName.value) return;
- const res = await listStationDevice({ pageNum: 1, pageSize: 500 });
- allDevices.value = res?.rows || res?.data || [];
- };
- const handleQuery = async () => {
- loading.value = true;
- try {
- const res = await props.api.list(queryParams);
- rows.value = res?.rows || res?.data || [];
- pagination.total = res?.total || rows.value.length;
- pagination.current = queryParams.pageNum;
- pagination.pageSize = queryParams.pageSize;
- } finally {
- loading.value = false;
- }
- };
- const resetQuery = () => {
- Object.keys(queryParams).forEach((key) => { if (!['pageNum', 'pageSize'].includes(key)) queryParams[key] = undefined; });
- queryParams.pageNum = 1;
- handleQuery();
- };
- const handleTableChange = (page) => {
- queryParams.pageNum = page.current;
- queryParams.pageSize = page.pageSize;
- handleQuery();
- };
- const openForm = async (record) => {
- await loadDevices();
- if (record?.id) {
- const res = await props.api.get(record.id);
- resetObject(form, { ...res.data });
- } else {
- resetObject(form, { stationId: queryParams.stationId });
- }
- formOpen.value = true;
- };
- const handleStationChange = () => {
- if (deviceFieldName.value) {
- form[deviceFieldName.value] = undefined;
- form.deviceName = undefined;
- }
- };
- const handleDeviceChange = (value) => {
- const device = allDevices.value.find((item) => String(item.id) === String(value));
- form.deviceName = device?.deviceName;
- };
- const submitForm = async () => {
- await formRef.value?.validate();
- if (form.id) await props.api.update(form); else await props.api.add(form);
- message.success('保存成功');
- formOpen.value = false;
- handleQuery();
- };
- const removeRecord = async (record) => {
- await props.api.del(record.id);
- message.success('删除成功');
- handleQuery();
- };
- const handleExport = () => BearJiaUtil.download('config/' + props.moduleConfig.api + '/export', queryParams, props.moduleConfig.exportName);
- loadStations();
- loadDevices();
- handleQuery();
- </script>
- <style lang="less" scoped>
- .station-config-page {
- .section { margin-top: 12px; }
- .query-select { width: 180px; }
- .full-input { width: 100%; }
- .danger-link { color: #ff4d4f; }
- }
- </style>
|