addPhotoFileAll.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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="true"
  12. :limit="10"
  13. :before-upload="upload"
  14. v-model:file-list="fileList"
  15. :http-request="httpsApi"
  16. :on-preview="previewFile"
  17. :accept="photoFormats"
  18. :on-remove="handleRemove"
  19. >
  20. <el-button type="primary" :disabled="!!file">
  21. <el-icon><Upload /></el-icon>上传
  22. </el-button>
  23. <template v-slot:tip>
  24. <div class="el-upload__tip">注:可上传{{ size }}以内的{{ photoFormatDesc }}</div>
  25. </template>
  26. <template v-slot:file="{ file }">
  27. <div class="file" @click.stop="previewFile()">
  28. <div>
  29. <el-icon><Document /></el-icon>
  30. <span class="name">{{ file.name }}</span>
  31. </div>
  32. <el-icon @click.stop="removeFile(file)"><Close /></el-icon>
  33. </div>
  34. </template>
  35. </el-upload>
  36. </el-form-item>
  37. <!-- <el-form-item label="附件标题:" class="mandatory">
  38. <el-input
  39. v-model="caseFile.imgInfo"
  40. placeholder="请输入最多不能超过50字"
  41. maxlength="50"
  42. show-word-limit
  43. ></el-input>
  44. </el-form-item> -->
  45. </el-form>
  46. </template>
  47. <script setup lang="ts">
  48. import {
  49. DrawFormatDesc,
  50. DrawFormats,
  51. photoFormats,
  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 { saveOrAndSave, 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. import type { UploadProps } from 'element-plus'
  65. const props = defineProps<{
  66. caseId: number;
  67. data: CaseImg;
  68. }>();
  69. const defaultUpload = (
  70. file: File,
  71. onPercentage: (percentage: number) => void
  72. ) => {
  73. onPercentage(100);
  74. };
  75. const caseFile = ref<CaseImg>({
  76. caseId: props.caseId,
  77. id: props.data?.id,
  78. imgUrl: props.data.imgUrl,
  79. imgInfo: props.data.imgInfo,
  80. sort: props.data?.sort || '',
  81. } as any);
  82. const { size, previewFile, file, accept, percentage, format } = useUpload({
  83. maxSize: maxFileSize,
  84. formats: DrawFormats,
  85. });
  86. const upload = async (file: File) => {
  87. const fileType = file.name
  88. .substring(file.name.lastIndexOf("."))
  89. .toUpperCase();
  90. if (!DrawFormats.some((type) => type.toUpperCase() === fileType)) {
  91. ElMessage.error(`请上传${format.value}`);
  92. return false;
  93. } else if (file.size > maxFileSize) {
  94. ElMessage.error(`请上传${size.value}以内的文件`);
  95. return false;
  96. } else {
  97. console.log('file', file)
  98. fileList.value.push(file);
  99. await defaultUpload(
  100. file,
  101. (val) => (percentage.value = val)
  102. );
  103. if (fileType === ".RAW") {
  104. }
  105. percentage.value = undefined;
  106. return true;
  107. }
  108. };
  109. const fileList = ref([])
  110. const formatDesc = computed(() =>
  111. DrawFormatDesc
  112. );
  113. watch(props, newValue => {
  114. caseFile.value.id = newValue.data.id;
  115. caseFile.value.imgInfo = newValue.data.imgInfo;
  116. caseFile.value.imgUrl = newValue.data.imgUrl;
  117. caseFile.value.sort = newValue.data.sort;
  118. if(newValue.data.imgUrl){
  119. file.value = {
  120. name: newValue.data.imgInfo || '',
  121. url: newValue.data.imgUrl || '',
  122. }
  123. }
  124. },{ immediate: true })
  125. watchEffect(() => {
  126. if (file.value?.name) {
  127. caseFile.value.imgInfo = file.value?.name.substring(0, 50);
  128. }
  129. });
  130. const handleRemove: UploadProps['onRemove'] = (uploadFile, uploadFiles) => {
  131. console.log(uploadFile, uploadFiles)
  132. }
  133. const removeFile = (file) => {
  134. fileList.value = fileList.value.filter((item) => item.raw.url !== file.raw.url);
  135. };
  136. const httpsApi = async ({file})=> {
  137. console.log('httpsApi', file)
  138. let fileUrl = await uploadFile(file);
  139. file.url = fileUrl
  140. console.log('httpsApi', file, fileUrl)
  141. }
  142. defineExpose<QuiskExpose>({
  143. async submit() {
  144. console.log('defineExpose', fileList.value)
  145. if (!fileList.value.length) {
  146. ElMessage.error("请上传照片");
  147. throw "请上传照片";
  148. }
  149. console.log('defineExpose', caseFile.value, file.value)
  150. let imgUrls = fileList.value.map(item => {
  151. return {
  152. imgUrl: item.raw && item.raw.url,
  153. imgInfo: item.name.replace(/\.[^/.]+$/, ""),
  154. caseId: props.caseId,
  155. }
  156. })
  157. await saveOrAndSave({ imgUrls });
  158. return caseFile.value;
  159. },
  160. });
  161. </script>
  162. <style scoped lang="scss">
  163. .upload-demo {
  164. overflow: hidden;
  165. }
  166. .file {
  167. display: flex;
  168. justify-content: space-between;
  169. align-items: center;
  170. > div {
  171. display: flex;
  172. align-items: center;
  173. }
  174. .name {
  175. margin-left: 10px;
  176. }
  177. }
  178. </style>