ImageImportForm .vue 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <Dialog v-model="dialogVisible" title="标本图片批量导入" width="500">
  3. <el-upload
  4. ref="uploadRef"
  5. v-model:file-list="fileList"
  6. drag
  7. :action="importUrl + '?updateSupport=' + updateSupport"
  8. :on-success="submitFormSuccess"
  9. style="max-width:250px;max-height: 30vh;margin-left: 20px"
  10. :auto-upload="false"
  11. :disabled="formLoading"
  12. :limit="1"
  13. :on-error="submitFormError"
  14. :http-request="httpRequest"
  15. >
  16. <el-icon class="el-icon--upload"><upload-filled /></el-icon>
  17. <div class="el-upload__text">
  18. <em>点击批量上传图片</em>
  19. <span>仅允许导入jpg、png、gif 格式文件。</span>
  20. </div>
  21. <template #tip>
  22. <div class="el-upload__tip">
  23. <el-checkbox v-model="updateSupport" />
  24. 是否更新已经存在的标本图片
  25. </div>
  26. </template>
  27. </el-upload>
  28. <template #footer >
  29. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  30. <el-button @click="dialogVisible = false">取 消</el-button>
  31. </template>
  32. </Dialog>
  33. </template>
  34. <script lang="ts" setup>
  35. import {UploadFilled} from "@element-plus/icons-vue";
  36. import {useUpload} from "@/components/UploadFile/src/useUpload";
  37. const importUrl =
  38. import.meta.env.VITE_BASE_URL + import.meta.env.VITE_API_URL + '/photosmanage/photo-group/createe'
  39. defineOptions({ name: 'imageImportForm' })
  40. const dialogVisible = ref(false) // 弹窗的是否展示
  41. const formLoading = ref(false) // 表单的加载中
  42. const uploadRef = ref()
  43. const message = useMessage() // 消息弹窗
  44. const updateSupport = ref(0) // 是否更新已经存在的用户数据
  45. const fileList = ref([]) // 文件列表
  46. /** 打开弹窗 */
  47. const open = () => {
  48. dialogVisible.value = true
  49. updateSupport.value = 0
  50. fileList.value = []
  51. resetForm()
  52. }
  53. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  54. const { httpRequest } = useUpload()
  55. /** 图片上传成功 */
  56. const submitFormSuccess = (response: any) => {
  57. console.log('tj',response)
  58. if (response.code !== 0) {
  59. message.error(response.msg)
  60. formLoading.value = false
  61. return
  62. }
  63. formLoading.value = false
  64. dialogVisible.value = false
  65. // 发送操作成功的事件
  66. }
  67. /** 上传错误提示 */
  68. const submitFormError = (): void => {
  69. message.error('上传失败,请您重新上传!')
  70. formLoading.value = false
  71. }
  72. /** 提交表单 */
  73. const submitForm = async () => {
  74. if (fileList.value.length == 0) {
  75. message.error('请上传文件')
  76. return
  77. }
  78. formLoading.value = true
  79. uploadRef.value!.submit()
  80. }
  81. /** 重置表单 */
  82. const resetForm = async (): Promise<void> => {
  83. // 重置上传状态和文件
  84. formLoading.value = false
  85. await nextTick()
  86. uploadRef.value?.clearFiles()
  87. }
  88. </script>