reportListUploadModal.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. <!--
  2. * @Author: zhuMingXi
  3. * @Date: 2025/09/13 15:27:57
  4. * @LastEditors: zhuMingXi
  5. * @LastEditTime: 2025/09/13 15:27:57
  6. * @Description: 检验项目添加上传附件功能
  7. * @FilePath: FilePath
  8. -->
  9. <template>
  10. <Dialog
  11. v-model="dialogVisible"
  12. width="60%"
  13. :title="isEdit ? '附件上传' : '查看附件'"
  14. :dialogAttrs="{ zIndex: 10006 }"
  15. style="height: 90vh; display: flex; flex-direction: column"
  16. align-center
  17. class="upload-modal"
  18. @close="closeModal"
  19. @confirm="handleConfirm"
  20. >
  21. <el-select
  22. class="mb-2 !w-[240px]"
  23. v-model="currentReportId"
  24. placeholder="请选择报告"
  25. clearable
  26. @change="handleReportChange"
  27. >
  28. <el-option
  29. v-for="item in filterReportList"
  30. :key="item.id"
  31. :label="item.reportName"
  32. :value="item.id"
  33. />
  34. </el-select>
  35. <!-- 内容显示区域 -->
  36. <div class="group">
  37. <div class="group-title">图片</div>
  38. <div
  39. class="group-item"
  40. v-if="currentReportId && changedReportMap[currentReportId].image.length > 0"
  41. >
  42. <div
  43. v-for="(path, index) in changedReportMap[currentReportId].image"
  44. :key="index"
  45. class="group-item-view"
  46. >
  47. <img class="group-item-view-img" :src="buildFileUrl(path)" />
  48. <div class="group-item-view-detail">
  49. <el-icon class="icon" @click.stop.prevent="handlePreview(buildFileUrl(path), 'image')"
  50. ><View
  51. /></el-icon>
  52. <el-icon v-if="isEdit" class="icon" @click.stop.prevent="handleDelete(index, 'image')"
  53. ><Delete
  54. /></el-icon>
  55. </div>
  56. </div>
  57. </div>
  58. <el-empty
  59. v-else
  60. description="暂无图片"
  61. :image-size="100"
  62. style="--el-empty-padding: 0 0 20px"
  63. />
  64. </div>
  65. <div class="group">
  66. <div class="group-title">视频</div>
  67. <div
  68. class="group-item"
  69. v-if="currentReportId && changedReportMap[currentReportId].video.length > 0"
  70. >
  71. <div
  72. v-for="(path, index) in changedReportMap[currentReportId].video"
  73. :key="index"
  74. class="group-item-view"
  75. >
  76. <video class="group-item-view-img" :src="buildFileUrl(path)" alt=""></video>
  77. <div class="group-item-view-detail">
  78. <el-icon class="icon" @click.stop.prevent="handlePreview(buildFileUrl(path), 'video')"
  79. ><View
  80. /></el-icon>
  81. <el-icon v-if="isEdit" class="icon" @click.stop.prevent="handleDelete(index, 'video')"
  82. ><Delete
  83. /></el-icon>
  84. </div>
  85. </div>
  86. </div>
  87. <el-empty
  88. v-else
  89. description="暂无视频"
  90. :image-size="100"
  91. style="--el-empty-padding: 0 0 20px"
  92. />
  93. </div>
  94. <div class="group">
  95. <div class="group-title">文件</div>
  96. <div
  97. v-if="currentReportId && changedReportMap[currentReportId].attachment.length > 0"
  98. class="attr-list"
  99. >
  100. <div
  101. v-for="(path, index) in changedReportMap[currentReportId].attachment"
  102. :key="index"
  103. class="flex items-center w-full p-[10px] hover:bg-gray-100 rounded-6px"
  104. >
  105. <!-- <el-tooltip :content="path"> -->
  106. <span class="flex-1 text-ellipsis overflow-hidden whitespace-nowrap">{{ changedReportMap[currentReportId].attachmentName[index] || path }}</span>
  107. <!-- </el-tooltip> -->
  108. <div class="ml-10px flex gap-[10px] cursor-pointer">
  109. <el-icon
  110. class="icon hover:c-[#489fc7]"
  111. @click.stop.prevent="handlePreview(buildFileUrl(path), 'attachment')"
  112. ><View
  113. /></el-icon>
  114. <el-icon
  115. v-if="isEdit"
  116. class="icon hover:c-[#489fc7]"
  117. @click.stop.prevent="handleDelete(index, 'attachment')"
  118. ><Delete
  119. /></el-icon>
  120. </div>
  121. </div>
  122. </div>
  123. <el-empty
  124. v-else
  125. description="暂无文件"
  126. :image-size="100"
  127. style="--el-empty-padding: 0 0 20px"
  128. />
  129. </div>
  130. <template v-if="isEdit" #footer>
  131. <!-- 附件上传区域 -->
  132. <el-upload
  133. ref="uploadRef"
  134. class="w-full mt-40px"
  135. v-model:file-list="fileList"
  136. :disabled="!currentReportId"
  137. v-bind="uploadConfig"
  138. >
  139. <Icon icon="ep:upload" />
  140. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  141. <template #tip>
  142. <div class="el-upload__tip text-center">
  143. <span>{{ `仅允许导入${uploadConfig.accept} 格式文件。` }}</span>
  144. </div>
  145. </template>
  146. </el-upload>
  147. <el-button :loading="loading" type="primary" @click="handleConfirm">确认</el-button>
  148. </template>
  149. </Dialog>
  150. <!-- 图片预览对话框 -->
  151. <el-dialog
  152. v-model="previewVisible"
  153. class="preview-dialog !bg-transparent !p-0 !w-auto"
  154. style=""
  155. title=""
  156. width="auto"
  157. :show-footer="false"
  158. :show-close="false"
  159. align-center
  160. >
  161. <el-scrollbar v-if="previewType === 'attachment'" height="100%" class="preview-container">
  162. <vue-office-docx
  163. v-if="getFileExtension(previewUrl) === 'docx'"
  164. ref="docxRef"
  165. :src="previewUrl"
  166. style="width: 950px"
  167. />
  168. <VuePdfEmbed
  169. v-else-if="getFileExtension(previewUrl) === 'pdf'"
  170. :height="0"
  171. :width="950"
  172. :source="previewUrl"
  173. :text-layer="false"
  174. :annotation-layer="false"
  175. />
  176. </el-scrollbar>
  177. <div v-else class="preview-container">
  178. <video
  179. class="preview-video"
  180. v-if="previewType === 'video'"
  181. :src="previewUrl"
  182. auto-play
  183. controls
  184. ></video>
  185. <img class="preview-image" v-else :src="previewUrl" />
  186. </div>
  187. </el-dialog>
  188. </template>
  189. <script setup lang="ts">
  190. import { ReportItemVO, BoilerTaskOrderApi } from '@/api/pressure2/boilertaskorder'
  191. import { useUpload } from '@/components/UploadFile/src/useUpload'
  192. import { ElUpload, UploadFile, UploadFiles } from 'element-plus'
  193. import { View, Delete } from '@element-plus/icons-vue'
  194. import { buildFileUrl } from '@/utils'
  195. import VueOfficeDocx from '@vue-office/docx/lib/v3/vue-office-docx.mjs'
  196. import '@vue-office/docx/lib/v3/index.css'
  197. import VuePdfEmbed from 'vue-pdf-embed'
  198. import { PressureReportType } from '@/utils/constants'
  199. const props = defineProps({
  200. /**所有报告 */
  201. reportList: {
  202. type: Array as PropType<ReportItemVO[]>,
  203. default: () => []
  204. },
  205. /**当前选中的项目 */
  206. selectedItem: {
  207. type: Object as PropType<ReportItemVO>,
  208. default: () => {}
  209. }
  210. })
  211. const emit = defineEmits(['confirm'])
  212. const filterReportList = computed(() => {
  213. return props.reportList.filter((x) => {
  214. return ![PressureReportType['INSPECTIONPLAN'], PressureReportType['WORKINSTRUCTION']].includes(
  215. x.reportType
  216. )
  217. })
  218. })
  219. const dialogVisible = ref(false)
  220. const openModal = (canEdit: boolean = false, data = props.selectedItem) => {
  221. isEdit.value = canEdit
  222. changedReportMap.value = {}
  223. setCurrentReportIdFile(data)
  224. dialogVisible.value = true
  225. }
  226. const closeModal = () => {
  227. dialogVisible.value = false
  228. }
  229. const loading = ref(false)
  230. const handleConfirm = () => {
  231. const reportIds = Object.keys(unref(changedReportMap))
  232. const params = reportIds.map((item) => ({
  233. reportId: item,
  234. attachment: changedReportMap.value[item].attachment.join(','),
  235. attachmentName: changedReportMap.value[item].attachmentName.join(','),
  236. image: changedReportMap.value[item].image.join(','),
  237. imageName: changedReportMap.value[item].imageName.join(','),
  238. video: changedReportMap.value[item].video.join(','),
  239. videoName: changedReportMap.value[item].videoName.join(',')
  240. }))
  241. loading.value = true
  242. BoilerTaskOrderApi.reportItemUploadBatch({ items: params })
  243. .then((res) => {
  244. emit('confirm')
  245. closeModal()
  246. })
  247. .finally(() => {
  248. loading.value = false
  249. })
  250. }
  251. const isEdit = ref(false)
  252. const uploadRef = ref<InstanceType<typeof ElUpload>>()
  253. // 支持上传docx pdf 图片和视频
  254. const accept = '.docx,.pdf,.png,.jpg,.jpeg,.mp4'
  255. const { httpRequest } = useUpload()
  256. const fileList = ref<any[]>([])
  257. const uploadConfig = {
  258. action: '#',
  259. accept,
  260. // limit: 1,
  261. drag: true,
  262. onSuccess: (response: any, uploadFile: any) => {
  263. console.log('response:', response)
  264. if (response.code !== 0) return ElMessage.error(response.msg)
  265. ElMessage.success('附件上传成功')
  266. // 根据文件类型,根据当前选中的报告id,添加到不同的数组中
  267. const fileType = response.data.split('.').pop()?.toLowerCase()
  268. const originalName = uploadFile?.name || response.data.substring(response.data.lastIndexOf('/') + 1)
  269. if (['docx', 'pdf'].includes(fileType)) {
  270. changedReportMap.value[currentReportId.value].attachment.push(response.data)
  271. changedReportMap.value[currentReportId.value].attachmentName.push(originalName)
  272. } else if (['png', 'jpg', 'jpeg'].includes(fileType)) {
  273. changedReportMap.value[currentReportId.value].image.push(response.data)
  274. changedReportMap.value[currentReportId.value].imageName.push(originalName)
  275. } else if (['mp4'].includes(fileType)) {
  276. changedReportMap.value[currentReportId.value].video.push(response.data)
  277. changedReportMap.value[currentReportId.value].videoName.push(originalName)
  278. }
  279. },
  280. onError: (error: Error, uploadFile: UploadFile, uploadFiles: UploadFiles): void => {
  281. ElMessage.error('上传失败,请您重新上传!')
  282. uploadRef.value?.handleRemove(uploadFile)
  283. },
  284. onExceed: (): void => {
  285. ElMessage.error('最多只能上传一个文件!')
  286. },
  287. beforeUpload: (file) => {
  288. // 允许的文件类型扩展名
  289. const allowedExtensions = accept.toLowerCase().split(',')
  290. // 获取文件扩展名(转为小写)
  291. const fileExt = file.name.split('.').pop()?.toLowerCase()
  292. if (!fileExt || !allowedExtensions.includes('.' + fileExt)) {
  293. ElMessage.error(`不支持的文件类型,仅允许上传: ${allowedExtensions.join(',')}类型文件`)
  294. return false // 阻止上传
  295. }
  296. return true // 允许上传
  297. },
  298. httpRequest: httpRequest
  299. }
  300. /**当前选中的报告id */
  301. const currentReportId = ref('')
  302. const handleReportChange = (val) => {
  303. if (val) setCurrentReportIdFile(props.reportList.find((item) => item.id === val))
  304. }
  305. const changedReportMap = ref<
  306. Record<string, { image: string[]; imageName: string[]; video: string[]; videoName: string[]; attachment: string[]; attachmentName: string[] }>
  307. >({}) // 已经编辑过的报告的数据统计
  308. const setCurrentReportIdFile = (selectedItem) => {
  309. const { id, image, imageName, video, videoName, attachment, attachmentName } = selectedItem
  310. currentReportId.value = filterReportList.value.find((item) => item.id === id)?.id || ''
  311. if (!changedReportMap.value[id]) {
  312. changedReportMap.value[id] = {
  313. image: image ? image.split(',') : [],
  314. imageName: imageName ? imageName.split(',') : [],
  315. video: video ? video.split(',') : [],
  316. videoName: videoName ? videoName.split(',') : [],
  317. attachment: attachment ? attachment.split(',') : [],
  318. attachmentName: attachmentName ? attachmentName.split(',') : []
  319. }
  320. }
  321. }
  322. // 图片视频预览
  323. const previewVisible = ref(false)
  324. const previewType = ref('')
  325. const previewUrl = ref('')
  326. const handleDelete = (index: number, type: 'image' | 'video' | 'attachment') => {
  327. if (type === 'image') {
  328. changedReportMap.value[currentReportId.value].image.splice(index, 1)
  329. changedReportMap.value[currentReportId.value].imageName.splice(index, 1)
  330. } else if (type === 'video') {
  331. changedReportMap.value[currentReportId.value].video.splice(index, 1)
  332. changedReportMap.value[currentReportId.value].videoName.splice(index, 1)
  333. } else {
  334. changedReportMap.value[currentReportId.value].attachment.splice(index, 1)
  335. changedReportMap.value[currentReportId.value].attachmentName.splice(index, 1)
  336. }
  337. }
  338. const handlePreview = (path: string, type: 'video' | 'image' | 'attachment') => {
  339. previewUrl.value = path
  340. previewType.value = type
  341. previewVisible.value = true
  342. }
  343. // 获取文件类型
  344. const getFileExtension = (fileName: string) => {
  345. console.log(fileName, 'FileExt:log', fileName.split('.').pop()?.toLowerCase())
  346. return fileName.split('.').pop()?.toLowerCase()
  347. }
  348. defineExpose({
  349. closeModal,
  350. openModal
  351. })
  352. </script>
  353. <style lang="scss" scoped>
  354. .group {
  355. .group-title {
  356. width: 100%;
  357. line-height: 32px;
  358. display: flex;
  359. align-items: center;
  360. background: #effaff;
  361. // margin-bottom: 12px;
  362. margin: 12px 0;
  363. &::before {
  364. content: '';
  365. display: inline-block;
  366. width: 4px;
  367. height: 16px;
  368. background: var(--el-color-primary);
  369. margin-right: 8px;
  370. }
  371. }
  372. .group-item {
  373. width: 100%;
  374. display: flex;
  375. flex-wrap: wrap;
  376. gap: 10px;
  377. min-height: 150px;
  378. &-view {
  379. width: 100px;
  380. height: 100px;
  381. position: relative;
  382. border-radius: 8px;
  383. overflow: hidden;
  384. display: flex;
  385. align-items: center;
  386. background: #f1f1f1;
  387. &-img {
  388. width: 100px;
  389. // height: 100px;
  390. }
  391. &-detail {
  392. position: absolute;
  393. top: 0;
  394. left: 0;
  395. width: 100%;
  396. height: 100%;
  397. z-index: 99;
  398. transition: all 0.5s ease;
  399. cursor: pointer;
  400. display: flex;
  401. justify-content: center;
  402. align-items: center;
  403. gap: 5px;
  404. &:hover {
  405. backdrop-filter: blur(2px);
  406. background-color: #00000076;
  407. z-index: 1;
  408. .icon {
  409. display: inline-block;
  410. opacity: 1;
  411. &:hover {
  412. color: #ffffff;
  413. transition: all 0.5s ease;
  414. }
  415. }
  416. }
  417. .icon {
  418. color: #ffffff99;
  419. opacity: 0;
  420. transition: all 0.5s ease;
  421. font-size: 16px;
  422. }
  423. }
  424. }
  425. }
  426. }
  427. .preview-container {
  428. padding: 0;
  429. position: relative;
  430. }
  431. .preview-image,
  432. .preview-video {
  433. height: 65vh;
  434. max-width: 100vw;
  435. // object-fit: cover;
  436. }
  437. :deep(.preview-dialog) {
  438. background-color: transparent !important;
  439. width: auto !important;
  440. padding: 0 !important;
  441. :deep(.el-dialog__header) {
  442. display: none;
  443. }
  444. .el-dialog__body {
  445. padding: 0;
  446. .preview-container {
  447. padding: 0;
  448. position: relative;
  449. }
  450. .preview-image,
  451. .preview-video {
  452. height: 65vh;
  453. max-width: 100vw;
  454. // object-fit: cover;
  455. }
  456. }
  457. }
  458. .attr-list {
  459. // display: grid;
  460. // grid-template-columns: 1fr;
  461. // gap: 4px;
  462. min-height: 150px;
  463. }
  464. .attr-list-item {
  465. width: 100%;
  466. display: flex;
  467. align-items: center;
  468. }
  469. :deep(.el-upload-list) {
  470. display: none !important;
  471. }
  472. </style>
  473. <style>
  474. .upload-modal > .el-dialog__body {
  475. flex: 1;
  476. overflow-y: auto;
  477. }
  478. .preview-dialog {
  479. background-color: transparent !important;
  480. width: auto !important;
  481. padding: 0 !important;
  482. }
  483. .preview-dialog .el-dialog__header {
  484. display: none;
  485. }
  486. .preview-dialog .el-dialog__body {
  487. padding: 0;
  488. }
  489. </style>