潘志宝
2024-11-07 4c535b57671d7de2f754cdda8f9250106e1ec435
提交 | 用户 | 时间
b05c43 1 <template>
2   <el-drawer
3     v-model="drawer"
4     size="50%"
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"
136           :formatter="(row) => {if (row.dataValue === -2.0) {return '--';}return row.dataValue;}"
137         />
138         <el-table-column
139           prop="quality"
140           label="数据质量"
141           header-align="center"
142           align="center"
143         >
144           <template #default="scope">
145             <el-tag v-if="scope.row.dataValue === Number(-2.0)" type="danger" size="small">bad</el-tag>
146             <el-tag v-else size="small">good</el-tag>
147           </template>
148         </el-table-column>
b05c43 149         <el-table-column label="操作" align="center" min-width="110" fixed="right">
150           <template #default="scope">
151             <el-button
152               link
153               type="primary"
154               @click="openForm('update', scope.row.id)"
155               v-hasPermi="['data:channel-modbus:update']"
156             >
157               编辑
158             </el-button>
159             <el-button
160               link
161               type="danger"
162               @click="handleDelete(scope.row.id)"
163               v-hasPermi="['data:channel-modbus:delete']"
164             >
165               删除
166             </el-button>
167           </template>
168         </el-table-column>
169       </el-table>
170       <!-- 分页 -->
171       <Pagination
172         :total="total"
173         v-model:page="queryParams.pageNo"
174         v-model:limit="queryParams.pageSize"
175         @pagination="getList"
176       />
177     </ContentWrap>
178     <!-- 表单弹窗:添加/修改 -->
93ce03 179     <TagForm ref="formRef" @success="getList"/>
D 180     <TagImportForm ref="importFormRef" @success="getList"/>
b05c43 181   </el-drawer>
182 </template>
183 <script lang="ts" setup>
93ce03 184   import type {DrawerProps} from 'element-plus'
b05c43 185   import * as ModBusTagApi from "@/api/data/channel/modbus/tag";
186   import TagForm from './TagForm.vue'
6c2636 187   import download from "@/utils/download";
J 188   import {ref} from "vue";
189   import TagImportForm from '../../common/tag/TagImportForm.vue'
93ce03 190   import {onBeforeUnmount, onMounted} from "vue";
D 191   import * as HttpTagApi from "@/api/data/channel/http/tag";
b05c43 192
193   defineOptions({name: 'ModBusTag'})
194
195   const message = useMessage() // 消息弹窗
196   const {t} = useI18n() // 国际化
197
198   const drawer = ref(false)
199   const direction = ref<DrawerProps['direction']>('rtl')
200   const loading = ref(true) // 列表的加载中
201   const total = ref(0) // 列表的总页数
202   const list = ref([]) // 列表的数据
203   const queryParams = reactive({
204     pageNo: 1,
205     pageSize: 10,
93ce03 206     deviceId: undefined,
b05c43 207     device: undefined,
208     tagName: undefined,
93ce03 209     currentValue:false,
b05c43 210     address: undefined
211   })
212   const queryFormRef = ref() // 搜索的表单
213   const exportLoading = ref(false) // 导出的加载中
214
215   /** 查询列表 */
216   const getList = async () => {
217     loading.value = true
218     try {
219       const page = await ModBusTagApi.getModBusTagPage(queryParams)
220       list.value = page.list
221       total.value = page.total
222     } finally {
223       loading.value = false
224     }
225   }
226
227   /** 搜索按钮操作 */
228   const handleQuery = () => {
229     queryParams.pageNo = 1
230     getList()
231   }
232
233   /** 重置按钮操作 */
234   const resetQuery = () => {
235     queryFormRef.value.resetFields()
236     handleQuery()
237   }
238
239   /** 添加/修改操作 */
240   const formRef = ref()
241   const openForm = (type: string, id?: number) => {
242     formRef.value.open(type, id, queryParams.device)
243   }
244
245   /** 删除按钮操作 */
246   const handleDelete = async (id: number) => {
247     try {
248       // 删除的二次确认
249       await message.delConfirm()
250       // 发起删除
251       await ModBusTagApi.deleteModBusTag(id)
252       message.success(t('common.delSuccess'))
253       // 刷新列表
254       await getList()
255     } catch {
256     }
257   }
258
259   /** 打开弹窗 */
93ce03 260   const open = async (deviceId?: string,device?: string) => {
b05c43 261     resetForm()
262     drawer.value = true
263     queryParams.device = device
93ce03 264     queryParams.deviceId = deviceId
b05c43 265     if (device) {
266       getList()
267     }
268   }
269   defineExpose({open}) // 提供 open 方法,用于打开弹窗
270
271   /** 重置表单 */
272   const resetForm = () => {
273     queryParams.pageNo = 1
274     queryParams.pageSize = 10
275     queryParams.device = ''
276     queryParams.tagName = ''
277     queryParams.address = ''
278   }
279
280   const handleClose = (done: () => void) => {
281     drawer.value = false
282   }
6c2636 283
J 284   /** tag导入 */
285   const importFormRef = ref()
286   const handleImport = () => {
93ce03 287     if (queryParams.device) {
D 288       importFormRef.value.open(queryParams.device, '/data/channel/modbus/tag/import', ModBusTagApi.importModBusTagTemplate(), 'ModBus', queryParams.device)
6c2636 289     }
J 290   }
291
292   /** 导出按钮操作 */
293   const handleExport = async () => {
294     try {
295       // 导出的二次确认
296       await message.exportConfirm()
297       // 发起导出
298       exportLoading.value = true
299       const data = await ModBusTagApi.exportModBusTag(queryParams)
300       download.excel(data, 'ModBus_' + queryParams.device + '_Tag列表.xlsx')
301     } catch {
302     } finally {
303       exportLoading.value = false
304     }
305   }
93ce03 306   let intervalId;
D 307
308   onMounted(async () => {
309     // 创建定时器
310     intervalId = setInterval(async () => {
311       if(queryParams.currentValue){
312         const page = await ModBusTagApi.getModBusTagPage(queryParams)
313         list.value = page.list
314         total.value = page.total
315       }
316     }, 10000);
317   });
318
319   // 在组件卸载时清除定时器
320   onBeforeUnmount(() => {
321     if (intervalId) {
322       clearInterval(intervalId);
323     }
324   });
b05c43 325 </script>