<!-- Description: 设备管理-设备管理 Author: 李亚光 Date: 2024-07-19 --> <script lang="ts" setup name="DeviceManage"> import { ElLoading, ElMessage, ElMessageBox } from 'element-plus' import addDialog from './components/addDialog.vue' import { exportDevice, getDeviceListPage, removeDevice } from '@/api/home/device/device' import { getDeviceTypeListPage } from '@/api/home/device/type' import { getDictByCode } from '@/api/system/dict' import { getManufacturerListPage } from '@/api/home/device/product' import { exportFile } from '@/utils/exportUtils' // 表格数据 const list = ref([]) const total = ref(0) // 初始展示列 const columns = ref<any>([ { text: '状态', value: 'onlineState', align: 'center', isCustom: true, width: '70' }, { text: '设备编号', value: 'devcode', align: 'center', isRequired: true }, { text: '设备类型', value: 'deviceType', align: 'center', isRequired: true }, { text: '监测对象', value: 'watchObject', align: 'center' }, { text: '设备型号', value: 'deviceModel', align: 'center' }, { text: '厂商', value: 'manufactureName', align: 'center', isRequired: true }, { text: '安装位号', value: 'tagNumber', align: 'center', isRequired: true }, { text: '详细位置', value: 'position', align: 'center', isRequired: true }, { text: '管理单位', value: 'deptName', align: 'center', isRequired: true }, { text: '在用情况', value: 'valid', align: 'center', isRequired: true }, { text: '布防状态', value: '', align: 'center', isRequired: true }, { text: '电量', value: 'cell', align: 'center', isRequired: true }, { text: '最新上报时间', value: 'logtime', align: 'center', isRequired: true }, { text: '安装日期', value: 'installDate', align: 'center', isRequired: true }, ]) // 最终展示列 const columnsConfig = ref([]) // 修改列 const editColumns = (data: any) => { columnsConfig.value = data } const loadingTable = ref(true) // 查询条件 const listQuery = ref({ limit: 20, offset: 1, devCode: '', // 设备编号 devTypeId: '', // 设备类型 keys: '', // 设备类型 deptId: '', // 管理单位 manufacturerId: '', // 厂商 onlineState: '', // 在线状态 cell: '', // 电量情况 valid: '', // 设备在用情况 logTime1: '', logTime2: '', }) // 开始结束时间 const datetimerange = ref() watch(() => datetimerange.value, (newVal) => { listQuery.value.logTime1 = '' listQuery.value.logTime2 = '' if (Array.isArray(newVal)) { if (newVal.length) { listQuery.value.logTime1 = `${newVal[0]}` listQuery.value.logTime2 = `${newVal[1]}` } } }) // 查询数据 const fetchData = () => { loadingTable.value = true getDeviceListPage(listQuery.value).then((res) => { console.log(res.data, '数据列表') list.value = res.data.rows total.value = res.data.total loadingTable.value = false }).catch(() => { loadingTable.value = false }) } // 重置查询条件f const reset = () => { listQuery.value = { limit: 20, offset: 1, devCode: '', // 设备编号 devTypeId: '', // 设备类型 keys: '', // 设备类型 deptId: '', // 管理单位 manufacturerId: '', // 厂商 onlineState: '', // 在线状态 cell: '', // 电量情况 valid: '', // 设备在用情况 logTime1: '', logTime2: '', } fetchData() } // 页数发生变化后的操作,可能是页码变化,可能是每页容量变化,此函数必写 const changePage = (val: { size: number; page: number }) => { if (val && val.size) { listQuery.value.limit = val.size } if (val && val.page) { listQuery.value.offset = val.page } fetchData() } // 新建或者编辑 const addRef = ref() const editRow = (type: string, row: any) => { addRef.value.initDialog(type, row) } // 删除 const removeRow = (row: any) => { ElMessageBox.confirm( `确定要删除${row.deviceName}吗?`, '确认操作', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', }, ).then(() => { removeDevice([row.id]).then((response) => { if (response.code === 200) { ElMessage({ message: '删除成功', type: 'success', }) fetchData() } }) }) } // 导出列表 const exportList = () => { const loading = ElLoading.service({ lock: true, background: 'rgba(255, 255, 255, 0.8)', }) exportDevice({}).then((res) => { exportFile(res.data, '设备列表') loading.close() }).catch(() => { loading.close() }) } // 批量导入 const importList = () => { ElMessage.warning('敬请期待') } const isUsed = ref<{ id: string; name: string; value: string }[]>([]) // 是否启用 const deviceTypeList = ref<{ id: string; name: string; value: string }[]>([]) // 设备类型 const manufacturerList = ref<{ id: string; name: string; value: string }[]>([]) // 厂商列表 const cellList = ref<{ id: string; name: string; value: string }[]>([]) // 厂商列表 const useStatusList = ref<{ id: string; name: string; value: string }[]>([]) // 设备在用情况 const onlineStateList = ref<{ id: string; name: string; value: string }[]>([ { name: '在线', value: '1', id: '1', }, { name: '离线', value: '0', id: '0', }, ]) // 在线状态 // 获取字典 const fetchDict = async () => { // 是否启用 getDictByCode('isUsed').then((res) => { isUsed.value = res.data }) // 电量情况 getDictByCode('cellStatus').then((res) => { cellList.value = res.data }) // 设备在用情况 getDictByCode('useStatus').then((res) => { useStatusList.value = res.data }) // 设备类型 getDeviceTypeListPage({ limit: 9999, offset: 1 }).then((res) => { deviceTypeList.value = res.data.rows.map((item: any) => ({ id: item.id, name: item.typeName, value: item.id })) }) // 厂商 getManufacturerListPage().then((res) => { manufacturerList.value = res.data.map((item: any) => ({ name: item.name || '', id: item.id, value: item.id, })) }) } onMounted(async () => { fetchDict() fetchData() }) </script> <template> <!-- 布局 --> <app-container> <add-dialog ref="addRef" /> <!-- 筛选条件 --> <search-area :need-clear="true" @search="fetchData" @clear="reset"> <search-item> <el-input v-model="listQuery.devCode" placeholder="设备编号" clearable /> </search-item> <search-item> <el-select v-model="listQuery.devTypeId" placeholder="设备类型" filterable clearable class="select" style="width: 175px;" > <el-option v-for="item in deviceTypeList" :key="item.id" :label="item.name" :value="item.value" /> </el-select> </search-item> <search-item> <el-input v-model="listQuery.keys" placeholder="安装位置" clearable /> </search-item> <search-item> <dept-select v-model="listQuery.deptId" placeholder="管理单位" :clearable="true" class="select" style="width: 175px;" /> </search-item> <search-item> <el-select v-model="listQuery.manufacturerId" placeholder="厂商" filterable clearable class="select" style="width: 175px;" > <el-option v-for="item in manufacturerList" :key="item.id" :label="item.name" :value="item.value" /> </el-select> </search-item> <search-item> <el-select v-model="listQuery.onlineState" placeholder="在线状态" filterable clearable class="select" style="width: 175px;" > <el-option v-for="item in onlineStateList" :key="item.id" :label="item.name" :value="item.value" /> </el-select> </search-item> <search-item> <el-select v-model="listQuery.cell" placeholder="电量情况" filterable clearable class="select" style="width: 175px;" > <el-option v-for="item in cellList" :key="item.id" :label="item.name" :value="item.value" /> </el-select> </search-item> <search-item> <el-select v-model="listQuery.valid" placeholder="设备在用情况" filterable clearable class="select" style="width: 175px;" > <el-option v-for="item in useStatusList" :key="item.id" :label="item.name" :value="item.value" /> </el-select> </search-item> <search-item> <el-date-picker v-model="datetimerange" type="datetimerange" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" range-separator="至" start-placeholder="上报开始时间" end-placeholder="上报结束时间" clearable /> </search-item> </search-area> <!-- 表头标题 --> <table-container :is-config="true" config-title="device-device" :columns="columns" :config-columns="columnsConfig" :edit="editColumns" > <template #btns-right> <!-- 操作 --> <div> <el-button type="primary" @click="editRow('add', {})"> 新建 </el-button> <el-button type="primary" @click="importList"> 批量导入 </el-button> <el-button type="primary" @click="exportList"> 导出列表 </el-button> </div> </template> <!-- 查询结果Table显示 --> <normal-table :data="list" :total="total" :columns="columnsConfig" :query="listQuery" :list-loading="loadingTable" @change="changePage" > <template #preColumns> <el-table-column label="序号" width="55" align="center"> <template #default="scope"> {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }} </template> </el-table-column> </template> <template #isCustom="{ scope, column }"> <!-- 状态 --> <div v-if="column.text === '状态'" class="circle" :class="scope.row.onlineState === '1' ? 'online' : 'offline'" /> </template> <template #columns> <el-table-column label="操作" align="center" width="120"> <template #default="scope"> <el-button type="primary" link size="small" @click="editRow('detail', scope.row)"> 查看 </el-button> <el-button type="primary" link size="small" @click="editRow('edit', scope.row)"> 编辑 </el-button> <el-button type="danger" link size="small"> 撤防 </el-button> </template> </el-table-column> </template> </normal-table> </table-container> </app-container> </template> <style lang="scss"> .select { width: 175px; } </style> <style lang="scss" scoped> .circle { width: 18px; height: 18px; border-radius: 50%; margin: 0 auto; } .online { background-color: #95f204; } .offline { background-color: #7f7f7f; } </style>