addPhotoFile.vue 3.8 KB

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