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

219 lines
5.9 KiB
Vue
Raw Normal View History

2024-11-14 13:43:41 +08:00
<template>
2024-12-27 13:50:13 +08:00
<BasicDrawer v-bind="$attrs" @register="registerDrawer" showFooter :title="getTitle" width="900px" @ok="handleSubmit">
2024-11-20 11:38:57 +08:00
<BasicForm @register="registerForm">
<template #paramJson="{ model }">
<CodeEditor
2024-11-21 17:15:37 +08:00
style="height: 500px"
:auto-height="false"
2024-11-20 11:38:57 +08:00
v-model:value="model.paramJson"
@change="model.paramJson = $event;"
/>
</template>
</BasicForm>
2024-12-27 13:50:13 +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-12-27 13:50:13 +08:00
<div style="width: 5px" />
<a-button type="primary" :icon="h(EyeOutlined)" @click="handleOpenFileDialog">OCR识别</a-button>
2025-01-03 11:19:45 +08:00
<input type="file" ref="fileInput" aria-hidden="true" accept="image/bmp,image/jpeg,image/jpg,image/png" @change="handleFileChange" style="display: none;" />
2024-12-27 13:50:13 +08:00
<div style="width: 5px" />
</template>
<template #bodyCell="{ column, record }">
<template v-if="column.dataIndex === 'action'">
<TableAction
:actions="[
{
label: '编辑',
icon: 'clarity:note-edit-line',
onClick: handleEdit.bind(null, record),
divider: true,
},
{
label: '删除',
icon: 'ant-design:delete-outlined',
color: 'error',
popConfirm: {
title: '是否确认删除',
confirm: handleDelete.bind(null, record),
placement: 'topRight',
},
divider: true,
},
]"
/>
</template>
</template>
</BasicTable>
2025-01-03 11:19:45 +08:00
<Modal @register="register" @success="updateParamData" />
2024-11-14 13:43:41 +08:00
</BasicDrawer>
</template>
<script lang="ts" setup>
2024-12-27 13:50:13 +08:00
import {defineEmits, ref, computed, unref, h} from 'vue';
import { BasicForm, useForm } from '@/components/Form/index';
import { formSchema } from './schema';
import { BasicDrawer, useDrawerInner } from '@/components/Drawer';
import { CodeEditor } from "@/components/CodeEditor";
import * as TaskApi from '@/api/data/taskApi';
import { BasicTable, useTable, TableAction } from '@/components/Table';
import { EyeOutlined, PlusOutlined } from "@ant-design/icons-vue";
import Modal from "@/views/data/task/modal.vue";
import { useModal } from '/@/components/Modal';
const [register, { openModal }] = useModal();
const columns = [
{
title: '预埋件编号',
dataIndex: 'code',
key: 'code',
},
{
title: '类型',
dataIndex: 'type',
key: 'type',
},
{
title: 'X(mm)',
dataIndex: 'x',
key: 'x',
width: '80px',
},
{
title: 'Y(mm)',
dataIndex: 'y',
key: 'y',
width: '80px',
},
{
title: 'w(mm)',
dataIndex: 'w',
key: 'w',
width: '80px',
},
{
title: 'h(mm)',
dataIndex: 'h',
key: 'h',
width: '80px',
},
];
2025-01-03 11:19:45 +08:00
const paramData = ref<any>([]);
2024-11-14 13:43:41 +08:00
2024-12-27 13:50:13 +08:00
const emit = defineEmits(['success', 'register']);
const isUpdate = ref(true);
const entity = ref();
2024-11-14 13:43:41 +08:00
2024-12-27 13:50:13 +08:00
const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
labelWidth: 120,
schemas: formSchema,
showActionButtonGroup: false,
});
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
await resetFields();
setDrawerProps({ confirmLoading: false });
isUpdate.value = !!data?.isUpdate;
entity.value = data?.record;
2025-01-03 11:19:45 +08:00
paramData.value = [];
const items = entity.value && entity.value.paramJson ? JSON.parse(entity.value.paramJson) : [];
items.forEach((d: any, index: number) => {
paramData.value.push({ index, ...d });
});
2024-12-27 13:50:13 +08:00
await setFieldsValue({
...data.record,
2024-11-14 13:43:41 +08:00
});
2024-12-27 13:50:13 +08:00
});
const [registerTable] = useTable({
title: '预埋件列表',
columns,
dataSource: paramData,
useSearchForm: false,
showTableSetting: false,
bordered: true,
showIndexColumn: false,
canResize: false,
pagination: false,
rowKey: (record: any) => record.code,
actionColumn: {
width: 180,
title: '操作',
dataIndex: 'action',
fixed: undefined,
},
});
const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
2024-11-14 13:43:41 +08:00
2024-12-27 13:50:13 +08:00
async function handleSubmit() {
try {
const values = await validate();
setDrawerProps({ confirmLoading: true });
const {
...rest
} = values;
const action = !unref(isUpdate) ? TaskApi.add : TaskApi.update;
const data = !unref(isUpdate)
? {
...rest,
}
: Object.assign({},
{
...unref(entity),
...rest,
},
);
await action(data);
closeDrawer();
emit('success');
2025-01-03 11:19:45 +08:00
} catch {} finally {
2024-11-14 13:43:41 +08:00
setDrawerProps({ confirmLoading: false });
2024-12-27 13:50:13 +08:00
}
2025-01-03 11:19:45 +08:00
}
2024-11-14 13:43:41 +08:00
2024-12-27 13:50:13 +08:00
const handleCreate = () => {
openModal(true, { isUpdate: false });
};
2025-01-03 11:19:45 +08:00
const handleEdit = (record: Recordable, index: number) => {
openModal(true, { record, isUpdate: true });
2024-12-27 13:50:13 +08:00
};
2024-11-14 13:43:41 +08:00
2024-12-27 13:50:13 +08:00
const handleDelete = (record: Recordable) => {
paramData.value = paramData.value.filter(d => d.code !== record.code);
};
2024-11-14 13:43:41 +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 = '';
};
reader.readAsDataURL(file);
2024-11-14 13:43:41 +08:00
}
2024-12-27 13:50:13 +08:00
};
const handleImport = (base64ImageString: any) => {
TaskApi.importImageOCR(base64ImageString).then((res: any) => {
WebViewService.setMessage("数据导入成功!", "success").then(() => {});
paramData.value = JSON.parse(res);
}).catch(() => {
WebViewService.setMessage("数据导入失败!", "error").then(() => {});
});
};
2025-01-03 11:19:45 +08:00
const updateParamData = (data: any) => {
paramData.value[data.index] = { ...data };
}
2024-11-14 13:43:41 +08:00
</script>
2024-11-19 09:55:07 +08:00
<style lang="scss" scoped>
@use '@/assets/custom.scss';
</style>