潘志宝
2024-11-22 df90c0c5cfa4de114798015b92120ad8ba8b4826
提交 | 用户 | 时间
b05c43 1 <template>
2   <el-drawer
3     v-model="drawer"
ebf2c1 4     size="60%"
b05c43 5     title="ModBus Tag"
6     :direction="direction"
7     :before-close="handleClose"
8   >
9     <!-- 搜索 -->
10     <ContentWrap>
11       <el-form
12         class="-mb-15px"
13         :model="queryParams"
14         ref="queryFormRef"
15         :inline="true"
16         label-width="68px"
17       >
18         <el-form-item label="Tag名称" prop="tagName">
19           <el-input
20             v-model="queryParams.tagName"
21             placeholder="请输入Tag名称"
22             clearable
23             @keyup.enter="handleQuery"
24             class="!w-240px"
25           />
26         </el-form-item>
27         <el-form-item label="地址" prop="address">
28           <el-input
29             v-model="queryParams.address"
30             placeholder="请输入Modbus地址"
31             clearable
32             @keyup.enter="handleQuery"
33             class="!w-240px"
34           />
35         </el-form-item>
36         <el-form-item>
37           <el-button @click="handleQuery">
93ce03 38             <Icon icon="ep:search" class="mr-5px"/>
b05c43 39             搜索
40           </el-button>
41           <el-button @click="resetQuery">
93ce03 42             <Icon icon="ep:refresh" class="mr-5px"/>
b05c43 43             重置
44           </el-button>
45           <el-button
46             type="primary"
47             plain
48             @click="openForm('create')"
6c2636 49             v-hasPermi="['data:channel-modbus:create']">
93ce03 50             <Icon icon="ep:plus" class="mr-5px"/>
b05c43 51             新增
6c2636 52           </el-button>
J 53           <el-button
54             type="warning"
55             plain
56             @click="handleImport"
57             v-hasPermi="['data:channel-modbus-tag:import']">
93ce03 58             <Icon icon="ep:upload"/>
D 59             导入
6c2636 60           </el-button>
J 61           <el-button
62             type="success"
63             plain
64             @click="handleExport"
65             :loading="exportLoading"
66             v-hasPermi="['data:channel-modbus-tag:export']">
93ce03 67             <Icon icon="ep:download"/>
D 68             导出
b05c43 69           </el-button>
93ce03 70         </el-form-item>
D 71         <el-form-item label="更新当前值" label-width="100px">
72           <el-switch
73             v-model="queryParams.currentValue"
74             active-color="#13ce66"
75             inactive-color="#ff4949"/>
b05c43 76         </el-form-item>
77       </el-form>
78     </ContentWrap>
79     <!-- 列表 -->
80     <ContentWrap>
81       <el-table v-loading="loading" :data="list">
82         <el-table-column
83           prop="tagName"
84           label="Tag名称"
85           header-align="center"
86           align="left"
87           min-width="150"
88         />
89         <el-table-column
90           prop="tagDesc"
91           label="Tag描述"
92           header-align="center"
93           align="left"
94           min-width="150"
95         />
96         <el-table-column
97           prop="dataType"
98           label="数据类型"
99           header-align="center"
100           align="center"
101         />
102         <el-table-column
103           prop="address"
104           label="地址"
105           header-align="center"
106           align="center"
107         />
108         <el-table-column
109           prop="format"
110           label="大小端"
111           header-align="center"
112           align="center"
113         />
114         <el-table-column
115           prop="samplingRate"
116           label="采集频率"
117           header-align="center"
118           align="center"
119         />
120         <el-table-column
121           prop="enabled"
122           label="是否启用"
123           header-align="center"
124           align="center"
125         >
126           <template #default="scope">
6c2636 127             <el-tag v-if="scope.row.enabled === 1" size="small">是</el-tag>
b05c43 128             <el-tag v-else size="small" type="danger">否</el-tag>
129           </template>
130         </el-table-column>
93ce03 131         <el-table-column
D 132           prop="dataValue"
133           label="数据值"
134           header-align="center"
135           align="center"
ebf2c1 136           min-width="100"
93ce03 137           :formatter="(row) => {if (row.dataValue === -2.0) {return '--';}return row.dataValue;}"
D 138         />
139         <el-table-column
ebf2c1 140           prop="dataTime"
141           label="数据时间"
142           header-align="center"
143           align="center"
144           min-width="150"
145         />
146         <el-table-column
147           prop="dataQuality"
93ce03 148           label="数据质量"
D 149           header-align="center"
150           align="center"
ebf2c1 151         />
b05c43 152         <el-table-column label="操作" align="center" min-width="110" fixed="right">
153           <template #default="scope">
154             <el-button
155               link
156               type="primary"
157               @click="openForm('update', scope.row.id)"
158               v-hasPermi="['data:channel-modbus:update']"
159             >
160               编辑
161             </el-button>
162             <el-button
163               link
164               type="danger"
165               @click="handleDelete(scope.row.id)"
166               v-hasPermi="['data:channel-modbus:delete']"
167             >
168               删除
169             </el-button>
170           </template>
171         </el-table-column>
172       </el-table>
173       <!-- 分页 -->
174       <Pagination
175         :total="total"
176         v-model:page="queryParams.pageNo"
177         v-model:limit="queryParams.pageSize"
178         @pagination="getList"
179       />
180     </ContentWrap>
181     <!-- 表单弹窗:添加/修改 -->
93ce03 182     <TagForm ref="formRef" @success="getList"/>
D 183     <TagImportForm ref="importFormRef" @success="getList"/>
b05c43 184   </el-drawer>
185 </template>
186 <script lang="ts" setup>
93ce03 187   import type {DrawerProps} from 'element-plus'
b05c43 188   import * as ModBusTagApi from "@/api/data/channel/modbus/tag";
189   import TagForm from './TagForm.vue'
6c2636 190   import download from "@/utils/download";
J 191   import {ref} from "vue";
192   import TagImportForm from '../../common/tag/TagImportForm.vue'
93ce03 193   import {onBeforeUnmount, onMounted} from "vue";
D 194   import * as HttpTagApi from "@/api/data/channel/http/tag";
b05c43 195
196   defineOptions({name: 'ModBusTag'})
197
198   const message = useMessage() // 消息弹窗
199   const {t} = useI18n() // 国际化
200
201   const drawer = ref(false)
202   const direction = ref<DrawerProps['direction']>('rtl')
203   const loading = ref(true) // 列表的加载中
204   const total = ref(0) // 列表的总页数
205   const list = ref([]) // 列表的数据
206   const queryParams = reactive({
207     pageNo: 1,
208     pageSize: 10,
93ce03 209     deviceId: undefined,
b05c43 210     device: undefined,
211     tagName: undefined,
93ce03 212     currentValue:false,
b05c43 213     address: undefined
214   })
215   const queryFormRef = ref() // 搜索的表单
216   const exportLoading = ref(false) // 导出的加载中
217
218   /** 查询列表 */
219   const getList = async () => {
220     loading.value = true
221     try {
222       const page = await ModBusTagApi.getModBusTagPage(queryParams)
223       list.value = page.list
224       total.value = page.total
225     } finally {
226       loading.value = false
227     }
228   }
229
230   /** 搜索按钮操作 */
231   const handleQuery = () => {
232     queryParams.pageNo = 1
233     getList()
234   }
235
236   /** 重置按钮操作 */
237   const resetQuery = () => {
238     queryFormRef.value.resetFields()
239     handleQuery()
240   }
241
242   /** 添加/修改操作 */
243   const formRef = ref()
244   const openForm = (type: string, id?: number) => {
245     formRef.value.open(type, id, queryParams.device)
246   }
247
248   /** 删除按钮操作 */
249   const handleDelete = async (id: number) => {
250     try {
251       // 删除的二次确认
252       await message.delConfirm()
253       // 发起删除
254       await ModBusTagApi.deleteModBusTag(id)
255       message.success(t('common.delSuccess'))
256       // 刷新列表
257       await getList()
258     } catch {
259     }
260   }
261
262   /** 打开弹窗 */
93ce03 263   const open = async (deviceId?: string,device?: string) => {
b05c43 264     resetForm()
265     drawer.value = true
266     queryParams.device = device
93ce03 267     queryParams.deviceId = deviceId
b05c43 268     if (device) {
269       getList()
270     }
271   }
272   defineExpose({open}) // 提供 open 方法,用于打开弹窗
273
274   /** 重置表单 */
275   const resetForm = () => {
276     queryParams.pageNo = 1
277     queryParams.pageSize = 10
278     queryParams.device = ''
279     queryParams.tagName = ''
280     queryParams.address = ''
281   }
282
283   const handleClose = (done: () => void) => {
284     drawer.value = false
285   }
6c2636 286
J 287   /** tag导入 */
288   const importFormRef = ref()
289   const handleImport = () => {
93ce03 290     if (queryParams.device) {
D 291       importFormRef.value.open(queryParams.device, '/data/channel/modbus/tag/import', ModBusTagApi.importModBusTagTemplate(), 'ModBus', queryParams.device)
6c2636 292     }
J 293   }
294
295   /** 导出按钮操作 */
296   const handleExport = async () => {
297     try {
298       // 导出的二次确认
299       await message.exportConfirm()
300       // 发起导出
301       exportLoading.value = true
302       const data = await ModBusTagApi.exportModBusTag(queryParams)
303       download.excel(data, 'ModBus_' + queryParams.device + '_Tag列表.xlsx')
304     } catch {
305     } finally {
306       exportLoading.value = false
307     }
308   }
93ce03 309   let intervalId;
D 310
311   onMounted(async () => {
312     // 创建定时器
313     intervalId = setInterval(async () => {
314       if(queryParams.currentValue){
315         const page = await ModBusTagApi.getModBusTagPage(queryParams)
316         list.value = page.list
317         total.value = page.total
318       }
319     }, 10000);
320   });
321
322   // 在组件卸载时清除定时器
323   onBeforeUnmount(() => {
324     if (intervalId) {
325       clearInterval(intervalId);
326     }
327   });
b05c43 328 </script>