addPhotoFile.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <el-form
  3. ref="form"
  4. :model="caseFile"
  5. label-width="90px"
  6. class="camera-from dispatch-file-from"
  7. >
  8. <el-form-item label="附件:" class="mandatory">
  9. <el-upload
  10. class="upload-demo"
  11. :multiple="false"
  12. :limit="1"
  13. :disabled="!!file"
  14. :before-upload="upload"
  15. :file-list="fileList"
  16. :http-request="httpsApi"
  17. :on-preview="previewFile"
  18. :accept="DrawFormats"
  19. :before-remove="removeFile"
  20. >
  21. <el-button type="primary" :disabled="!!file">
  22. <el-icon><Upload /></el-icon>上传
  23. </el-button>
  24. <template v-slot:tip>
  25. <div class="el-upload__tip">注:可上传{{ size }}以内的{{ photoFormatDesc }}</div>
  26. </template>
  27. <template v-slot:file="{ file }">
  28. <div class="file" @click.stop="previewFile()">
  29. <div>
  30. <el-icon><Document /></el-icon>
  31. <span class="name">{{ file.name }}</span>
  32. </div>
  33. <el-icon v-if="!caseFile.id" @click.stop="removeFile()"><Close /></el-icon>
  34. </div>
  35. </template>
  36. </el-upload>
  37. </el-form-item>
  38. <el-form-item label="附件标题:" class="mandatory">
  39. <el-input
  40. v-model="caseFile.imgInfo"
  41. placeholder="请输入最多不能超过50字"
  42. maxlength="50"
  43. show-word-limit
  44. ></el-input>
  45. </el-form-item>
  46. </el-form>
  47. </template>
  48. <script setup lang="ts">
  49. import {
  50. DrawFormatDesc,
  51. DrawFormats,
  52. photoFormatDesc,
  53. FileDrawType,
  54. OtherFormatDesc,
  55. OtherFormats,
  56. } from "@/constant/caseFile";
  57. import { uploadFile } from "@/store/system";
  58. import { maxFileSize } from "@/constant/caseFile";
  59. import { useUpload } from "@/hook/upload";
  60. import { saveOrUpDate, CaseImg } from "@/store/case";
  61. import { ElMessage } from "element-plus";
  62. import { computed, ref, watch, watchEffect } from "vue";
  63. import { QuiskExpose } from "@/helper/mount";
  64. const props = defineProps<{
  65. caseId: number;
  66. data: CaseImg;
  67. }>();
  68. const caseFile = ref<CaseImg>({
  69. caseId: props.caseId,
  70. id: props.data?.id,
  71. imgUrl: props.data.imgUrl,
  72. imgInfo: props.data.imgInfo,
  73. sort: props.data?.sort || '',
  74. } as any);
  75. const { size, fileList, upload, removeFile, previewFile, file, accept } = useUpload({
  76. maxSize: maxFileSize,
  77. formats: DrawFormats,
  78. });
  79. const formatDesc = computed(() =>
  80. DrawFormatDesc
  81. );
  82. watch(props, newValue => {
  83. caseFile.value.id = newValue.data.id;
  84. caseFile.value.imgInfo = newValue.data.imgInfo;
  85. caseFile.value.imgUrl = newValue.data.imgUrl;
  86. caseFile.value.sort = newValue.data.sort;
  87. if(newValue.data.imgUrl){
  88. file.value = {
  89. name: newValue.data.imgInfo || '',
  90. url: newValue.data.imgUrl || '',
  91. }
  92. }
  93. },{ immediate: true })
  94. watchEffect(() => {
  95. if (file.value?.name) {
  96. caseFile.value.imgInfo = file.value?.name.substring(0, 50);
  97. }
  98. });
  99. const httpsApi = async ({file})=> {
  100. console.log('httpsApi', file)
  101. let fileUrl = await uploadFile(file);
  102. file.value = {
  103. name: file.name,
  104. url: fileUrl,
  105. }
  106. console.log('httpsApi', file, fileUrl)
  107. }
  108. defineExpose<QuiskExpose>({
  109. async submit() {
  110. if (!file.value) {
  111. ElMessage.error("请上传附件");
  112. throw "请上传附件";
  113. } else if (!caseFile.value.imgInfo.trim()) {
  114. ElMessage.error("附件标题不能为空!");
  115. throw "附件标题不能为空!";
  116. }
  117. console.log('defineExpose', caseFile.value, file.value)
  118. let imgUrl = file.value && file.value.value ? file.value.value?.url : file.value?.url
  119. await saveOrUpDate({ ...caseFile.value, imgUrl });
  120. return caseFile.value;
  121. },
  122. });
  123. </script>
  124. <style scoped lang="scss">
  125. .upload-demo {
  126. overflow: hidden;
  127. }
  128. .file {
  129. display: flex;
  130. justify-content: space-between;
  131. align-items: center;
  132. > div {
  133. display: flex;
  134. align-items: center;
  135. }
  136. .name {
  137. margin-left: 10px;
  138. }
  139. }
  140. </style>