detect-embeded/src/views/data/task/index.vue

266 lines
8.9 KiB
Vue
Raw Normal View History

2024-11-14 13:43:41 +08:00
<template>
<div>
2024-11-19 09:55:07 +08:00
<div class="header">
<SvgIcon size="19" name="list" />
2024-11-19 12:15:56 +08:00
<div class="title">任务列表</div>
2024-11-19 09:55:07 +08:00
</div>
2024-11-14 13:43:41 +08:00
<BasicTable @register="registerTable">
<template #toolbar>
2025-01-03 11:19:45 +08:00
<a-button type="primary" @click="handleCreate" :icon="h(PlusOutlined)"> </a-button>
2024-11-22 11:07:04 +08:00
<div style="width: 5px" />
2024-12-27 13:50:13 +08:00
<a-button type="default" :icon="h(ImportOutlined)" @click="handleOpenFileDialog">导入数据</a-button>
2025-01-03 11:19:45 +08:00
<input type="file" ref="fileInput" accept="image/bmp,image/jpeg,image/jpg,image/png" aria-hidden="true" @change="handleFileChange" style="display: none;" />
2024-11-22 11:07:04 +08:00
<div style="width: 5px" />
2024-11-19 15:58:46 +08:00
<a-button type="default" @click="handleDownload" :icon="h(DownloadOutlined)" :disabled="checkedKeys.length === 0">下发数据</a-button>
<a-button type="default" @click="handleSync" :icon="h(SyncOutlined)" :disabled="checkedKeys.length === 0">同步数据</a-button>
2024-11-14 13:43:41 +08:00
</template>
<template #bodyCell="{ column, record }">
<template v-if="column.dataIndex === 'action'">
<TableAction
:actions="[
{
label: '编辑',
icon: 'clarity:note-edit-line',
onClick: handleEdit.bind(null, record),
2024-11-15 17:30:24 +08:00
divider: true,
2024-11-22 11:07:04 +08:00
disabled: record.state !== 0
2024-11-14 13:43:41 +08:00
},
{
label: '删除',
icon: 'ant-design:delete-outlined',
color: 'error',
popConfirm: {
title: '是否确认删除',
confirm: handleDelete.bind(null, record),
placement: 'topRight',
},
2024-11-15 17:30:24 +08:00
ifShow: hasPermission('AUTH_DATA_TASK:DELETE'),
2024-11-22 13:30:45 +08:00
disabled: record.state !== 0,
divider: true,
2024-11-14 13:43:41 +08:00
},
2024-11-19 09:55:07 +08:00
{
label: '详情',
icon: 'ant-design:eye-outlined',
onClick: handleView.bind(null, record),
divider: true
},
2024-11-19 15:58:46 +08:00
{
label: '下发数据',
icon: 'ant-design:download-outlined',
onClick: handleDownload.bind(null, record),
2024-11-20 12:09:06 +08:00
divider: true,
2024-11-22 13:30:45 +08:00
disabled: record.state !== 0 || !getDeviceConnected(record.deviceSn)
2024-11-19 15:58:46 +08:00
},
{
label: '同步数据',
icon: 'ant-design:sync-outlined',
onClick: handleSync.bind(null, record),
2024-11-22 13:30:45 +08:00
divider: true,
disabled: !getDeviceConnected(record.deviceSn)
2024-11-19 15:58:46 +08:00
},
2024-11-14 13:43:41 +08:00
]"
/>
</template>
</template>
</BasicTable>
2024-11-15 17:30:24 +08:00
<TaskDrawer @register="registerDrawer" @success="handleSuccess" />
2024-11-14 13:43:41 +08:00
</div>
</template>
<script lang="ts" setup>
2024-11-15 17:30:24 +08:00
import { useGo } from '@/hooks/web/usePage';
2024-11-14 13:43:41 +08:00
import { usePermission } from '@/hooks/web/usePermission';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { isObject } from '@/utils/is';
import dayjs from 'dayjs';
2024-11-15 17:30:24 +08:00
import * as TaskApi from '@/api/data/taskApi';
2024-11-14 13:43:41 +08:00
import { useDrawer } from '@/components/Drawer';
2024-11-15 17:30:24 +08:00
import TaskDrawer from './drawer.vue';
2024-11-14 13:43:41 +08:00
import { columns, searchFormSchema } from './schema';
2024-11-19 09:55:07 +08:00
import {SvgIcon} from "@/components/Icon";
2024-11-19 15:58:46 +08:00
import {h, ref} from "vue";
2024-11-22 13:30:45 +08:00
import { onMounted, onUnmounted, computed } from "vue";
2024-11-21 16:27:39 +08:00
import {
DownloadOutlined,
ImportOutlined,
PlusOutlined,
SyncOutlined
} from "@ant-design/icons-vue";
2024-11-14 13:43:41 +08:00
const { hasPermission } = usePermission();
const go = useGo();
2024-11-19 15:58:46 +08:00
const checkedKeys = ref<Array<string | number>>([]);
const onSelectChange = (selectedRowKeys: (string | number)[]) => {
checkedKeys.value = selectedRowKeys.filter(k => typeof k !== 'undefined');
}
2024-11-14 13:43:41 +08:00
const [registerDrawer, { openDrawer }] = useDrawer();
2024-11-19 10:56:01 +08:00
const [registerTable, { reload }] = useTable({
2024-11-15 17:30:24 +08:00
api: (params) => TaskApi.search(handleParams(params)),
2024-11-14 13:43:41 +08:00
columns,
formConfig: {
2024-11-19 09:55:07 +08:00
labelWidth: 0,
2024-11-14 13:43:41 +08:00
schemas: searchFormSchema,
showAdvancedButton: false,
},
2024-11-19 15:58:46 +08:00
rowSelection: {
type: 'checkbox',
onChange: onSelectChange,
2024-11-20 12:09:06 +08:00
getCheckboxProps: (record) => ({ disabled: record.state !== 0})
2024-11-19 15:58:46 +08:00
},
2024-11-14 13:43:41 +08:00
useSearchForm: true,
showTableSetting: false,
2024-11-19 09:55:07 +08:00
bordered: true,
2024-11-14 13:43:41 +08:00
showIndexColumn: false,
canResize: false,
rowKey: (record: any) => record.id,
actionColumn: {
2024-11-19 15:58:46 +08:00
width: 420,
2024-11-14 13:43:41 +08:00
title: '操作',
dataIndex: 'action',
2024-11-19 09:55:07 +08:00
fixed: undefined,
2024-11-14 13:43:41 +08:00
},
});
2024-11-22 13:30:45 +08:00
2025-01-03 11:47:22 +08:00
const handleParams = (params: any) => {
2024-11-14 13:43:41 +08:00
const { pageNum, pageSize, field = 'id', order = 'descend', ...rest } = params;
const handledParams: any = { pageNum, pageSize, orderByClause: `${field} ${order === 'descend' ? 'desc' : 'asc'}` };
Object.keys(rest).forEach((key) => {
const schema = searchFormSchema.find((item) => item.field === key);
const value = params[key];
2024-11-19 09:55:07 +08:00
let paramKey = key;
2024-11-14 13:43:41 +08:00
if (schema) {
if (value !== undefined && value !== '') {
if (schema.component === 'Input') {
2024-11-15 17:30:24 +08:00
handledParams[paramKey] = `%${value.trim()}%`;
2024-11-14 13:43:41 +08:00
} else if (['Select', 'ApiSelect', 'ApiTreeSelect'].includes(schema.component)) {
handledParams[paramKey] = isObject(value) ? value.value : value;
} else if (schema.component === 'RangePicker') {
2024-11-19 09:55:07 +08:00
if (paramKey === 'startTimeQuery') {
paramKey = 'startTime';
}
2024-11-14 13:43:41 +08:00
handledParams[`${paramKey}From`] = dayjs(value[0]).startOf('d').format('YYYY-MM-DD HH:mm:ss');
handledParams[`${paramKey}To`] = dayjs(value[1]).endOf('d').format('YYYY-MM-DD HH:mm:ss');
} else if (schema.component === 'DatePicker') {
handledParams[paramKey] = dayjs(value).format((schema.componentProps as any).format || 'YYYY-MM-DD');
} else {
handledParams[paramKey] = value;
}
}
} else {
handledParams[paramKey] = value;
}
});
return handledParams;
};
const handleCreate = () => {
openDrawer(true, {
isUpdate: false,
});
};
const handleEdit = (record: Recordable) => {
openDrawer(true, {
record,
isUpdate: true,
});
};
const handleDelete = (record: Recordable) => {
2024-11-15 17:30:24 +08:00
TaskApi.remove(record.id).then((_) => {
2024-11-14 13:43:41 +08:00
reload();
});
};
const handleSuccess = () => {
reload();
};
2024-12-27 13:50:13 +08:00
const handleView = (record: any) => {
2024-11-19 10:56:01 +08:00
go('/data/task/' + record.id);
2024-11-14 13:43:41 +08:00
};
2024-11-19 15:58:46 +08:00
2024-12-27 13:50:13 +08:00
const fileInput = ref<HTMLInputElement>();
const handleOpenFileDialog = () => {
fileInput.value?.click();
};
const handleFileChange = (event: any) => {
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (e) => {
handleImport(e.target!.result);
fileInput.value!.value = '';
2024-11-21 16:27:39 +08:00
};
2024-12-27 13:50:13 +08:00
reader.readAsDataURL(file);
}
};
const handleImport = (base64ImageString: any) => {
TaskApi.importImageOCR(base64ImageString).then((res: any) => {
WebViewService.setMessage("数据导入成功!", "success").then(() => {});
const data = JSON.parse(res);
if (data.length === 0) return;
const record = {
name: '',
paramJson: JSON.stringify(data),
};
// 打开新增
openDrawer(true, {
record,
isUpdate: false,
});
}).catch(() => {
2024-11-26 17:35:13 +08:00
WebViewService.setMessage("数据导入失败!", "error").then(() => {});
2024-11-21 16:27:39 +08:00
});
}
2024-11-19 15:58:46 +08:00
const handleDownload = (record: Recordable) => {
const ids = [...new Set([...(record.id ? [record.id] : []), ...(checkedKeys.value.map(d => (d)))])]
2024-11-20 12:09:06 +08:00
DeviceClientService.assignTasks(ids).then(() => {
2024-11-26 17:35:13 +08:00
WebViewService.setMessage("下发任务成功!", "success").then(() => {});
2025-01-03 11:47:22 +08:00
}, (error: any) => {
2024-11-26 17:35:13 +08:00
WebViewService.setMessage("下发任务失败!", "error").then(() => {});
2024-11-20 12:09:06 +08:00
});
2024-11-19 15:58:46 +08:00
};
const handleSync = (record: Recordable) => {
const ids = [...new Set([...(record.id ? [record.id] : []), ...(checkedKeys.value.map(d => (d)))])]
2024-11-20 12:09:06 +08:00
DeviceClientService.syncTasks(ids).then(() => {
2024-11-26 17:35:13 +08:00
WebViewService.setMessage("同步任务成功!", "success").then(() => {});
2024-11-20 12:09:06 +08:00
reload();
2025-01-03 11:47:22 +08:00
}, (error: any) => {
2024-11-26 17:35:13 +08:00
WebViewService.setMessage("同步任务失败!", "error").then(() => {});
2024-11-20 12:09:06 +08:00
});
2024-11-19 15:58:46 +08:00
};
2024-11-22 13:30:45 +08:00
const deviceConnectedList = ref([])
const getDeviceConnected = computed(() => {
2025-01-03 11:47:22 +08:00
return (sn: any) => {
2024-11-22 13:30:45 +08:00
return deviceConnectedList.value[sn] ? deviceConnectedList.value[sn] : false;
};
});
const timer = ref(0)
onMounted(()=> {
timer.value = setInterval(() => {
DeviceClientService.getDeviceConnected().then((d) => {
deviceConnectedList.value = d;
}, error => {
2024-11-26 17:35:13 +08:00
WebViewService.setMessage("获取设备连接出错", "error").then(() => {});
2024-11-22 13:30:45 +08:00
});
}, 300);
2024-11-26 17:35:13 +08:00
WebViewService.setIsLoading(false).then(() => {});
2024-11-22 13:30:45 +08:00
});
onUnmounted(()=> {
clearInterval(timer.value);
});
2024-11-14 13:43:41 +08:00
</script>
2024-11-15 17:30:24 +08:00
<style lang="scss" scoped>
@use '@/assets/custom.scss';
</style>