am-freescan/am-web/src/views/lab/md/sample/SampleImportForm.vue

100 lines
3.2 KiB
Vue
Raw Normal View History

2026-10-22 16:18:37 +08:00
<template>
<Dialog v-model="dialogVisible" title="样品导入" width="400">
<el-upload
ref="uploadRef"
v-model:file-list="fileList"
:action="importUrl + '?updateSupport=' + updateSupport"
:auto-upload="false"
:disabled="formLoading"
:headers="uploadHeaders"
:limit="1"
:on-error="submitFormError"
:on-exceed="handleExceed"
:on-success="submitFormSuccess"
accept=".xlsx, .xls"
drag
>
<Icon icon="ep:upload" />
<div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
<template #tip>
<div class="el-upload__tip text-center">
<div class="el-upload__tip">
<el-checkbox v-model="updateSupport" />
是否更新已经存在的样品数据
</div>
<span>仅允许导入 xls、xlsx 格式文件。</span>
<el-link :underline="false" style="font-size: 12px; vertical-align: baseline" type="primary" @click="importTemplate">
下载模板
</el-link>
</div>
</template>
</el-upload>
<template #footer>
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
<el-button @click="dialogVisible = false">取 消</el-button>
</template>
</Dialog>
</template>
<script lang="ts" setup>
import * as SampleApi from '@/api/lab/master/sample'
import { getAccessToken, getTenantId } from '@/utils/auth'
import download from '@/utils/download'
defineOptions({ name: 'LabSampleImportForm' })
const message = useMessage()
const dialogVisible = ref(false)
const formLoading = ref(false)
const uploadRef = ref()
const importUrl = import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/lab/master/sample/import'
const uploadHeaders = ref()
const fileList = ref([])
const updateSupport = ref(false)
const open = () => {
dialogVisible.value = true
updateSupport.value = false
fileList.value = []
formLoading.value = false
}
defineExpose({ open })
const submitForm = async () => {
if (fileList.value.length == 0) {
message.error('请上传文件')
return
}
uploadHeaders.value = {
Authorization: 'Bearer ' + getAccessToken(),
'tenant-id': getTenantId()
}
formLoading.value = true
uploadRef.value!.submit()
}
const emits = defineEmits(['success'])
const submitFormSuccess = (response: any) => {
if (response.code !== 0) {
message.error(response.msg)
formLoading.value = false
return
}
const data = response.data
let text = '新增成功:' + data.createCodes.length + ';更新成功:' + data.updateCodes.length
text += ';失败:' + Object.keys(data.failureCodes).length
for (const code in data.failureCodes) {
text += ';' + code + ': ' + data.failureCodes[code]
}
message.alert(text)
formLoading.value = false
dialogVisible.value = false
emits('success')
}
const submitFormError = () => {
message.error('上传失败,请您重新上传!')
formLoading.value = false
}
const handleExceed = () => message.error('最多只能上传一个文件!')
const importTemplate = async () => {
const res = await SampleApi.importSampleTemplate()
download.excel(res, '样品导入模板.xls')
}
</script>