StationConfigCrud.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <div class="station-config-page">
  3. <a-card :bordered="false">
  4. <a-form :model="queryParams" layout="inline">
  5. <a-form-item v-if="hasStation" label="场站">
  6. <a-select v-model:value="queryParams.stationId" allow-clear show-search class="query-select" :options="stationOptions" :filter-option="filterOption" />
  7. </a-form-item>
  8. <a-form-item v-for="item in searchFields" :key="item.name" :label="item.label">
  9. <a-select v-if="item.type === 'select'" v-model:value="queryParams[item.name]" allow-clear class="query-select" :options="item.options" />
  10. <a-input v-else v-model:value="queryParams[item.name]" allow-clear :placeholder="'请输入' + item.label" />
  11. </a-form-item>
  12. <a-form-item>
  13. <a-space>
  14. <a-button type="primary" :loading="loading" @click="handleQuery">查询</a-button>
  15. <a-button @click="resetQuery">重置</a-button>
  16. <a-button v-hasPermi="[moduleConfig.perm + ':add']" type="primary" @click="openForm()">新增</a-button>
  17. <a-button v-hasPermi="[moduleConfig.perm + ':export']" @click="handleExport">导出</a-button>
  18. </a-space>
  19. </a-form-item>
  20. </a-form>
  21. </a-card>
  22. <a-card :bordered="false" class="section">
  23. <a-table :columns="columns" :data-source="rows" :loading="loading" :pagination="pagination" row-key="id" :scroll="tableScroll" @change="handleTableChange">
  24. <template #bodyCell="{ column, record }">
  25. <template v-if="column.key === 'operation'">
  26. <a-space>
  27. <a v-hasPermi="[moduleConfig.perm + ':edit']" @click="openForm(record)">编辑</a>
  28. <a-popconfirm :title="'确认删除该' + moduleConfig.title + '?'" @confirm="removeRecord(record)">
  29. <a v-hasPermi="[moduleConfig.perm + ':remove']" class="danger-link">删除</a>
  30. </a-popconfirm>
  31. </a-space>
  32. </template>
  33. </template>
  34. </a-table>
  35. </a-card>
  36. <a-modal v-model:open="formOpen" :title="form.id ? '编辑' + moduleConfig.title : '新增' + moduleConfig.title" width="860px" @ok="submitForm">
  37. <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 7 }" :wrapper-col="{ span: 15 }">
  38. <a-row :gutter="16">
  39. <a-col v-if="hasStation" :span="12">
  40. <a-form-item label="所属场站" name="stationId">
  41. <a-select v-model:value="form.stationId" show-search :options="stationOptions" :filter-option="filterOption" @change="handleStationChange" />
  42. </a-form-item>
  43. </a-col>
  44. <a-col v-for="item in moduleConfig.fields" :key="item[0]" :span="12">
  45. <a-form-item :label="item[1]" :name="item[0]">
  46. <a-select v-if="item[2] === 'select'" v-model:value="form[item[0]]" allow-clear :options="toOptions(item[4])" />
  47. <a-select v-else-if="item[2] === 'device'" v-model:value="form[deviceFieldName]" allow-clear show-search :options="deviceOptions" :filter-option="filterOption" @change="handleDeviceChange" />
  48. <a-input-number v-else-if="item[2] === 'number'" v-model:value="form[item[0]]" class="full-input" />
  49. <a-date-picker v-else-if="item[2] === 'date'" v-model:value="form[item[0]]" value-format="YYYY-MM-DD" class="full-input" />
  50. <a-input v-else v-model:value="form[item[0]]" allow-clear />
  51. </a-form-item>
  52. </a-col>
  53. </a-row>
  54. </a-form>
  55. </a-modal>
  56. </div>
  57. </template>
  58. <script setup>
  59. import { computed, reactive, ref } from 'vue';
  60. import { message } from 'ant-design-vue';
  61. import BearJiaUtil from '@/utils/BearJiaUtil';
  62. import { listStation } from '@/api/config/station';
  63. import { listStationDevice } from '@/api/config/stationDevice';
  64. const props = defineProps({ moduleConfig: { type: Object, required: true }, api: { type: Object, required: true } });
  65. const loading = ref(false);
  66. const rows = ref([]);
  67. const stationOptions = ref([]);
  68. const allDevices = ref([]);
  69. const formOpen = ref(false);
  70. const formRef = ref();
  71. const queryParams = reactive({ pageNum: 1, pageSize: 10, stationId: undefined });
  72. const pagination = reactive({ current: 1, pageSize: 10, total: 0, showSizeChanger: true });
  73. const form = reactive({});
  74. const hasStation = computed(() => props.moduleConfig.hasStation !== false);
  75. const deviceFieldName = computed(() => props.moduleConfig.fields.find((item) => item[2] === 'device')?.[0]);
  76. const fieldMap = computed(() => new Map(props.moduleConfig.fields.map((item) => [item[0], item])));
  77. 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]) })));
  78. const columns = computed(() => [
  79. { title: 'ID', dataIndex: 'id', width: 80 },
  80. ...props.moduleConfig.columns.map((name) => ({ title: name === 'stationName' ? '场站' : fieldMap.value.get(name)?.[1] || name, dataIndex: name, width: 140, ellipsis: true })),
  81. { title: '操作', key: 'operation', width: 130, fixed: 'right' }
  82. ]);
  83. const tableScroll = computed(() => ({ x: columns.value.reduce((sum, col) => sum + (col.width || 140), 0) }));
  84. const rules = computed(() => ({
  85. ...(hasStation.value ? { stationId: [{ required: true, message: '请选择所属场站', trigger: 'change' }] } : {}),
  86. ...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' }]]))
  87. }));
  88. const deviceOptions = computed(() => allDevices.value
  89. .filter((item) => !form.stationId || String(item.stationId) === String(form.stationId))
  90. .map((item) => ({ label: item.deviceName || item.name || item.id, value: item.id, raw: item })));
  91. const toOptions = (items = []) => items.map((item) => ({ label: item[0], value: item[1] }));
  92. const filterOption = (input, option) => String(option?.label || '').toLowerCase().includes(input.toLowerCase());
  93. const resetObject = (target, source = {}) => { Object.keys(target).forEach((key) => delete target[key]); Object.assign(target, source); };
  94. const loadStations = async () => {
  95. if (!hasStation.value) return;
  96. const res = await listStation({ pageNum: 1, pageSize: 500 });
  97. const list = res?.rows || res?.data || [];
  98. stationOptions.value = list.map((item) => ({ label: item.stationName || item.name || item.id, value: item.id }));
  99. };
  100. const loadDevices = async () => {
  101. if (!deviceFieldName.value) return;
  102. const res = await listStationDevice({ pageNum: 1, pageSize: 500 });
  103. allDevices.value = res?.rows || res?.data || [];
  104. };
  105. const handleQuery = async () => {
  106. loading.value = true;
  107. try {
  108. const res = await props.api.list(queryParams);
  109. rows.value = res?.rows || res?.data || [];
  110. pagination.total = res?.total || rows.value.length;
  111. pagination.current = queryParams.pageNum;
  112. pagination.pageSize = queryParams.pageSize;
  113. } finally {
  114. loading.value = false;
  115. }
  116. };
  117. const resetQuery = () => {
  118. Object.keys(queryParams).forEach((key) => { if (!['pageNum', 'pageSize'].includes(key)) queryParams[key] = undefined; });
  119. queryParams.pageNum = 1;
  120. handleQuery();
  121. };
  122. const handleTableChange = (page) => {
  123. queryParams.pageNum = page.current;
  124. queryParams.pageSize = page.pageSize;
  125. handleQuery();
  126. };
  127. const openForm = async (record) => {
  128. await loadDevices();
  129. if (record?.id) {
  130. const res = await props.api.get(record.id);
  131. resetObject(form, { ...res.data });
  132. } else {
  133. resetObject(form, { stationId: queryParams.stationId });
  134. }
  135. formOpen.value = true;
  136. };
  137. const handleStationChange = () => {
  138. if (deviceFieldName.value) {
  139. form[deviceFieldName.value] = undefined;
  140. form.deviceName = undefined;
  141. }
  142. };
  143. const handleDeviceChange = (value) => {
  144. const device = allDevices.value.find((item) => String(item.id) === String(value));
  145. form.deviceName = device?.deviceName;
  146. };
  147. const submitForm = async () => {
  148. await formRef.value?.validate();
  149. if (form.id) await props.api.update(form); else await props.api.add(form);
  150. message.success('保存成功');
  151. formOpen.value = false;
  152. handleQuery();
  153. };
  154. const removeRecord = async (record) => {
  155. await props.api.del(record.id);
  156. message.success('删除成功');
  157. handleQuery();
  158. };
  159. const handleExport = () => BearJiaUtil.download('config/' + props.moduleConfig.api + '/export', queryParams, props.moduleConfig.exportName);
  160. loadStations();
  161. loadDevices();
  162. handleQuery();
  163. </script>
  164. <style lang="less" scoped>
  165. .station-config-page {
  166. .section { margin-top: 12px; }
  167. .query-select { width: 180px; }
  168. .full-input { width: 100%; }
  169. .danger-link { color: #ff4d4f; }
  170. }
  171. </style>