1
0

addPhotoFile.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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="photoFormats"
  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. photoFormats,
  53. photoFormatDesc,
  54. FileDrawType,
  55. OtherFormatDesc,
  56. OtherFormats,
  57. } from "@/constant/caseFile";
  58. import { uploadFile } from "@/store/system";
  59. import { maxFileSize } from "@/constant/caseFile";
  60. import { useUpload } from "@/hook/upload";
  61. import { saveOrUpdate, CaseImg } from "@/store/case";
  62. import { ElMessage } from "element-plus";
  63. import { computed, ref, watch, watchEffect } from "vue";
  64. import { QuiskExpose } from "@/helper/mount";
  65. const props = defineProps<{
  66. caseId: number;
  67. data: CaseImg;
  68. }>();
  69. const caseFile = ref<CaseImg>({
  70. caseId: props.caseId,
  71. id: props.data?.id,
  72. imgUrl: props.data.imgUrl,
  73. imgInfo: props.data.imgInfo,
  74. sort: props.data?.sort || '',
  75. } as any);
  76. const { size, fileList, upload, removeFile, previewFile, file, accept } = useUpload({
  77. maxSize: maxFileSize,
  78. formats: photoFormats,
  79. });
  80. const formatDesc = computed(() =>
  81. DrawFormatDesc
  82. );
  83. watch(props, newValue => {
  84. caseFile.value.id = newValue.data.id;
  85. caseFile.value.imgInfo = newValue.data.imgInfo;
  86. caseFile.value.imgUrl = newValue.data.imgUrl;
  87. caseFile.value.sort = newValue.data.sort;
  88. if(newValue.data.imgUrl){
  89. file.value = {
  90. name: newValue.data.imgInfo || '',
  91. url: newValue.data.imgUrl || '',
  92. }
  93. }
  94. },{ immediate: true })
  95. watchEffect(() => {
  96. if (file.value?.name) {
  97. caseFile.value.imgInfo = file.value?.name.substring(0, 50);
  98. }
  99. });
  100. const httpsApi = async ({file})=> {
  101. console.log('httpsApi', file)
  102. let fileUrl = await uploadFile(file);
  103. file.value = {
  104. name: file.name,
  105. url: fileUrl,
  106. }
  107. console.log('httpsApi', file, fileUrl)
  108. }
  109. defineExpose<QuiskExpose>({
  110. async submit() {
  111. if (!file.value) {
  112. ElMessage.error("请上传附件");
  113. throw "请上传附件";
  114. } else if (!caseFile.value.imgInfo.trim()) {
  115. ElMessage.error("附件标题不能为空!");
  116. throw "附件标题不能为空!";
  117. }
  118. console.log('defineExpose', caseFile.value, file.value)
  119. let imgUrl = file.value && file.value.value ? file.value.value?.url : file.value?.url
  120. await saveOrUpdate({ ...caseFile.value, imgUrl });
  121. return caseFile.value;
  122. },
  123. });
  124. </script>
  125. <style scoped lang="scss">
  126. .upload-demo {
  127. overflow: hidden;
  128. }
  129. .file {
  130. display: flex;
  131. justify-content: space-between;
  132. align-items: center;
  133. > div {
  134. display: flex;
  135. align-items: center;
  136. }
  137. .name {
  138. margin-left: 10px;
  139. }
  140. }
  141. </style>