阅行客电子档案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

610 lines
18 KiB

  1. <template>
  2. <div class="embed-upload" :class="{ 'has-file': fileList.length > 0 }">
  3. <div class="upload-header">
  4. <span class="upload-title">原文上传</span>
  5. <span class="upload-tip">单个文件不可超过10GB</span>
  6. </div>
  7. <div class="uploader-drop" @click="triggerFileInput" @dragover.prevent @drop.prevent="handleDrop">
  8. <div class="uploader-btn">
  9. <i class="iconfont icon-tianjiawenjian upload-icon" />
  10. <p>{{ fileList.length > 0 ? '点击继续添加文件' : '点击或拖拽上传文件' }}</p>
  11. <!-- :disabled="!isCaValid || isCheckingCa" -->
  12. <input
  13. ref="fileInput"
  14. type="file"
  15. :multiple="true"
  16. class="file-input"
  17. style="display: none;"
  18. @change="handleFileSelect"
  19. >
  20. </div>
  21. <div class="el-upload__tip">支持多文件上传最大10GB/</div>
  22. </div>
  23. <div v-if="fileList.length > 0" class="file-list-container">
  24. <div v-for="(fileItem, index) in fileList" :key="index" class="file-item">
  25. <div class="file-info">
  26. <i :class="getFileIcon(fileItem.file.name)" />
  27. <span class="file-name">{{ fileItem.file.name }}</span>
  28. <span class="file-size">{{ formatFileSize(fileItem.file.size) }}</span>
  29. </div>
  30. <div v-if="fileItem.uploading || fileItem.merging" class="file-status">
  31. <span v-if="fileItem.uploading" class="progress-text">上传中 {{ fileItem.progress }}%</span>
  32. <span v-else class="progress-text">合并中...</span>
  33. <div v-if="fileItem.uploading" class="progress-bar" :style="{ width: fileItem.progress + '%' }" />
  34. </div>
  35. <p v-if="fileItem.errorMsg" class="error">{{ fileItem.errorMsg }}</p>
  36. <p v-if="fileItem.successMsg" class="success">{{ fileItem.successMsg }}</p>
  37. <i class="iconfont icon-shanchu delete-btn" @click.stop="handleDeleteFile(index)" />
  38. </div>
  39. </div>
  40. <div v-if="fileList.length > 0" class="upload-footer">
  41. <span class="file-count"> {{ fileList.length }} 个文件</span>
  42. <el-button
  43. :disabled="fileList.some(item => item.uploading || item.merging) || btnLoading"
  44. :loading="btnLoading"
  45. type="primary"
  46. @click="handleUploadConfirm"
  47. >
  48. 保存上传
  49. </el-button>
  50. </div>
  51. </div>
  52. </template>
  53. <script>
  54. import { FetchCheckCaValidity } from '@/api/system/auth2'
  55. import { FetchInitFileCategoryView } from '@/api/collect/collect'
  56. import { mapGetters } from 'vuex'
  57. import axios from 'axios'
  58. import SparkMD5 from 'spark-md5'
  59. import { getToken } from '@/utils/auth'
  60. import { getCurrentTime } from '@/utils/index'
  61. export default {
  62. name: 'EmbedUpload',
  63. props: {
  64. selectedCategory: {
  65. type: Object,
  66. default: () => ({})
  67. },
  68. arcId: {
  69. type: String,
  70. default: ''
  71. }
  72. },
  73. data() {
  74. return {
  75. fileList: [],
  76. CHUNK_SIZE: 5 * 1024 * 1024,
  77. isCaValid: false,
  78. isCheckingCa: false,
  79. btnLoading: false,
  80. originFileData: [],
  81. repeatFileData: [],
  82. tempSelectedFiles: null
  83. }
  84. },
  85. computed: {
  86. ...mapGetters(['baseApi'])
  87. },
  88. mounted() {
  89. this.getCheckCaValidity()
  90. },
  91. methods: {
  92. triggerFileInput() {
  93. this.$refs.fileInput.click()
  94. },
  95. getFileIcon(fileName) {
  96. const ext = fileName.split('.').pop().toLowerCase()
  97. const icons = {
  98. 'jpg': 'icon-image', 'jpeg': 'icon-image', 'png': 'icon-image', 'bmp': 'icon-image', 'gif': 'icon-image',
  99. 'xlsx': 'icon-excel', 'xls': 'icon-excel',
  100. 'docx': 'icon-word', 'doc': 'icon-word',
  101. 'pdf': 'icon-pdf',
  102. 'ppt': 'icon-ppt', 'pptx': 'icon-ppt',
  103. 'zip': 'icon-zip', 'rar': 'icon-zip',
  104. 'txt': 'icon-txt'
  105. }
  106. return `fileIcon ${icons[ext] || 'icon-other'}`
  107. },
  108. formatFileSize(bytes) {
  109. if (bytes === 0) return '0.00MB'
  110. const mb = bytes / (1024 * 1024)
  111. return mb.toFixed(2) + 'MB'
  112. },
  113. showMessage(message, type) {
  114. this.$message({ message, type, offset: 8 })
  115. },
  116. async getCheckCaValidity() {
  117. try {
  118. this.isCheckingCa = true
  119. const res = await FetchCheckCaValidity()
  120. this.isCaValid = !!res
  121. if (!this.isCaValid) {
  122. this.showMessage('CA证书不在有效期内,无法进行分片上传', 'error')
  123. }
  124. } catch (err) {
  125. this.isCaValid = false
  126. this.showMessage('CA证书校验失败:' + err.message, 'error')
  127. } finally {
  128. this.isCheckingCa = false
  129. }
  130. },
  131. async getFileList() {
  132. const params = {
  133. 'categoryId': this.selectedCategory.id,
  134. 'archivesId': this.arcId
  135. }
  136. const res = await FetchInitFileCategoryView(params)
  137. this.originFileData = res.returnlist || []
  138. },
  139. async handleDrop(e) {
  140. if (this.isCheckingCa) {
  141. this.showMessage('正在校验CA证书,请稍候...', 'warning')
  142. return
  143. }
  144. if (!this.isCaValid) {
  145. this.showMessage('CA证书不在有效期内,无法上传文件', 'error')
  146. return
  147. }
  148. const selectedFiles = Array.from(e.dataTransfer.files)
  149. if (selectedFiles.length === 0) return
  150. await this.getFileList()
  151. const existingFileNames = this.originFileData.map(file => file.file_name)
  152. this.repeatFileData = selectedFiles.filter(file => existingFileNames.includes(file.name))
  153. if (this.repeatFileData.length > 0) {
  154. this.tempSelectedFiles = selectedFiles
  155. this.$emit('show-repeat-modal', this.repeatFileData)
  156. return
  157. }
  158. this.addFiles(selectedFiles)
  159. },
  160. async handleFileSelect(e) {
  161. if (this.isCheckingCa) {
  162. this.showMessage('正在校验CA证书,请稍候...', 'warning')
  163. e.target.value = ''
  164. return
  165. }
  166. if (!this.isCaValid) {
  167. this.showMessage('CA证书不在有效期内,无法上传文件', 'error')
  168. e.target.value = ''
  169. return
  170. }
  171. const selectedFiles = Array.from(e.target.files)
  172. if (selectedFiles.length === 0) return
  173. await this.getFileList()
  174. const existingFileNames = this.originFileData.map(file => file.file_name)
  175. this.repeatFileData = selectedFiles.filter(file => existingFileNames.includes(file.name))
  176. if (this.repeatFileData.length > 0) {
  177. this.tempSelectedFiles = selectedFiles
  178. this.$emit('show-repeat-modal', this.repeatFileData)
  179. e.target.value = ''
  180. return
  181. }
  182. this.addFiles(selectedFiles)
  183. e.target.value = ''
  184. },
  185. addFiles(files) {
  186. const newFileList = files.map(file => ({
  187. file,
  188. uploading: false,
  189. merging: false,
  190. progress: 0,
  191. errorMsg: '',
  192. successMsg: '',
  193. md5: ''
  194. }))
  195. this.fileList = [...this.fileList, ...newFileList]
  196. },
  197. handleDeleteFile(index) {
  198. const fileItem = this.fileList[index]
  199. if (fileItem.uploading || fileItem.merging) {
  200. this.showMessage('当前文件正在上传/合并中,无法删除', 'warning')
  201. return
  202. }
  203. this.fileList.splice(index, 1)
  204. },
  205. handleRepeatFile(type) {
  206. if (!this.tempSelectedFiles) return
  207. let filesToUpload = []
  208. if (type === 0) {
  209. filesToUpload = this.tempSelectedFiles
  210. } else {
  211. const existingFileNames = this.originFileData.map(file => file.file_name)
  212. filesToUpload = this.tempSelectedFiles.filter(file => !existingFileNames.includes(file.name))
  213. if (filesToUpload.length === 0) {
  214. this.showMessage('当前所选文件去重后无可上传的文件', 'error')
  215. return
  216. }
  217. }
  218. this.addFiles(filesToUpload)
  219. this.tempSelectedFiles = null
  220. },
  221. calculateFileMd5(file) {
  222. return new Promise((resolve, reject) => {
  223. const spark = new SparkMD5.ArrayBuffer()
  224. const fileReader = new FileReader()
  225. const chunkSize = 2 * 1024 * 1024
  226. let offset = 0
  227. const loadNextChunk = () => {
  228. const blob = file.slice(offset, offset + chunkSize)
  229. fileReader.readAsArrayBuffer(blob)
  230. }
  231. fileReader.onload = (e) => {
  232. spark.append(e.target.result)
  233. offset += chunkSize
  234. if (offset < file.size) {
  235. loadNextChunk()
  236. } else {
  237. resolve(spark.end())
  238. }
  239. }
  240. fileReader.onerror = (err) => reject(err)
  241. loadNextChunk()
  242. })
  243. },
  244. async checkChunkExists(fileMd5, chunkIndex) {
  245. const linkSrc = process.env.NODE_ENV === 'production' ? window.g.ApiUrl : process.env.VUE_APP_BASE_API
  246. const response = await axios.get(`${linkSrc}/api/collect/chunk`, {
  247. params: { fileMd5, chunkIndex },
  248. headers: { 'Authorization': getToken() }
  249. })
  250. if (response.data.code !== 200) {
  251. throw new Error('检查分片失败: ' + response.data.msg)
  252. }
  253. return response.data.data
  254. },
  255. async uploadSingleChunk(file, fileMd5, chunkIndex) {
  256. const start = chunkIndex * this.CHUNK_SIZE
  257. const end = Math.min(start + this.CHUNK_SIZE, file.size)
  258. const chunkBlob = file.slice(start, end)
  259. const formData = new FormData()
  260. formData.append('file', chunkBlob, `${fileMd5}_${chunkIndex}`)
  261. formData.append('fileMd5', fileMd5)
  262. formData.append('chunkIndex', chunkIndex)
  263. const linkSrc = process.env.NODE_ENV === 'production' ? window.g.ApiUrl : process.env.VUE_APP_BASE_API
  264. const response = await axios.post(`${linkSrc}/api/collect/chunk`, formData, {
  265. headers: {
  266. 'Content-Type': 'multipart/form-data',
  267. 'Authorization': getToken()
  268. }
  269. })
  270. if (response.data.code !== 200) {
  271. throw new Error(`分片${chunkIndex}上传失败: ` + response.data.msg)
  272. }
  273. },
  274. async checkAllChunksExist(fileMd5, totalChunks) {
  275. for (let i = 0; i < totalChunks; i++) {
  276. const result = await this.checkChunkExists(fileMd5, i)
  277. if (!result.exists) {
  278. return false
  279. }
  280. }
  281. return true
  282. },
  283. async uploadFileChunks(fileItem) {
  284. const file = fileItem.file
  285. fileItem.uploading = true
  286. fileItem.progress = 0
  287. fileItem.errorMsg = ''
  288. fileItem.successMsg = ''
  289. try {
  290. const fileMd5 = await this.calculateFileMd5(file)
  291. fileItem.md5 = fileMd5
  292. const totalChunks = Math.ceil(file.size / this.CHUNK_SIZE)
  293. const uploadedChunks = []
  294. for (let i = 0; i < totalChunks; i++) {
  295. const checkResult = await this.checkChunkExists(fileMd5, i)
  296. if (!checkResult.exists) {
  297. await this.uploadSingleChunk(file, fileMd5, i)
  298. }
  299. uploadedChunks.push(i)
  300. fileItem.progress = Math.round((uploadedChunks.length / totalChunks) * 100)
  301. }
  302. fileItem.progress = 100
  303. } catch (err) {
  304. fileItem.errorMsg = '上传失败: ' + (err.message || '未知错误')
  305. } finally {
  306. fileItem.uploading = false
  307. }
  308. },
  309. async handleUploadConfirm() {
  310. if (this.fileList.length === 0) {
  311. this.showMessage('请先选择要上传的文件!', 'warning')
  312. return
  313. }
  314. const pendingFiles = this.fileList.filter(item => !item.successMsg && !item.errorMsg)
  315. if (pendingFiles.length === 0) {
  316. this.showMessage('暂无待上传的文件!', 'warning')
  317. return
  318. }
  319. this.btnLoading = true
  320. for (const fileItem of pendingFiles) {
  321. await this.uploadFileChunks(fileItem)
  322. }
  323. const validFiles = this.fileList.filter(item => !item.errorMsg && item.progress === 100)
  324. if (validFiles.length === 0) {
  325. this.btnLoading = false
  326. this.showMessage('无有效分片上传完成的文件!', 'error')
  327. return
  328. }
  329. validFiles.forEach(fileItem => {
  330. fileItem.merging = true
  331. })
  332. try {
  333. const processFiles = validFiles.map(async(fileItem) => {
  334. const file = fileItem.file
  335. const jsonString = {}
  336. if (file.type.startsWith('image')) {
  337. const reader = new FileReader()
  338. const base64 = await new Promise((resolve) => {
  339. reader.onload = (e) => resolve(e.target.result)
  340. reader.readAsDataURL(file)
  341. })
  342. const img = new Image()
  343. const imgRes = await new Promise((resolve) => {
  344. img.onload = () => resolve({ width: img.width, height: img.height })
  345. img.src = base64
  346. })
  347. jsonString.file_dpi = `${imgRes.width}px*${imgRes.height}px`
  348. } else {
  349. jsonString.file_dpi = ''
  350. }
  351. jsonString.file_name = file.name
  352. jsonString.file_size = file.size
  353. jsonString.file_type = file.name.split('.').pop() || ''
  354. jsonString.last_modified = file.lastModified
  355. jsonString.file_path = ''
  356. jsonString.sequence = null
  357. jsonString.archive_id = this.arcId
  358. jsonString.create_time = getCurrentTime()
  359. jsonString.id = null
  360. jsonString.file_thumbnail = ''
  361. const totalChunks = Math.ceil(file.size / this.CHUNK_SIZE)
  362. const chunksExist = await this.checkAllChunksExist(fileItem.md5, totalChunks)
  363. if (!chunksExist) {
  364. throw new Error(`部分分片未上传完成`)
  365. }
  366. return {
  367. categoryId: this.selectedCategory.id,
  368. archivesId: this.arcId,
  369. identifier: fileItem.md5,
  370. filename: file.name,
  371. totalChunks: totalChunks,
  372. totalSize: file.size,
  373. fileJsonString: JSON.stringify([jsonString])
  374. }
  375. })
  376. const jsonArray = await Promise.all(processFiles)
  377. const linkSrc = process.env.NODE_ENV === 'production' ? window.g.ApiUrl : process.env.VUE_APP_BASE_API
  378. const response = await axios.post(`${linkSrc}/api/collect/merge`, jsonArray, {
  379. headers: {
  380. 'Authorization': getToken(),
  381. 'Content-Type': 'application/json'
  382. }
  383. })
  384. if (response.data.code === 200) {
  385. this.showMessage('所有文件上传并合并成功', 'success')
  386. validFiles.forEach(fileItem => {
  387. fileItem.successMsg = '上传成功!'
  388. fileItem.merging = false
  389. })
  390. this.$emit('onUploadSuccess')
  391. } else {
  392. this.showMessage(`文件合并失败: ${response.data.message || '合并失败'}`, 'error')
  393. }
  394. } catch (err) {
  395. this.showMessage(`文件合并失败: ${err.message}`, 'error')
  396. validFiles.forEach(fileItem => {
  397. fileItem.merging = false
  398. fileItem.errorMsg = fileItem.errorMsg || `合并失败: ${err.message}`
  399. })
  400. } finally {
  401. this.btnLoading = false
  402. }
  403. }
  404. }
  405. }
  406. </script>
  407. <style scoped>
  408. .embed-upload {
  409. flex: 1;
  410. margin-left: 10px;
  411. border: 1px solid #e4e7ed;
  412. border-radius: 4px;
  413. background-color: #fff;
  414. display: flex;
  415. flex-direction: column;
  416. }
  417. .upload-header {
  418. padding: 12px 15px;
  419. border-bottom: 1px solid #e4e7ed;
  420. display: flex;
  421. justify-content: space-between;
  422. align-items: center;
  423. }
  424. .upload-title {
  425. font-weight: 500;
  426. font-size: 14px;
  427. color: #303133;
  428. }
  429. .upload-tip {
  430. font-size: 12px;
  431. color: #ff4949;
  432. }
  433. .uploader-drop {
  434. flex: 1;
  435. display: flex;
  436. flex-direction: column;
  437. justify-content: center;
  438. align-items: center;
  439. padding: 20px;
  440. cursor: pointer;
  441. min-height: 150px;
  442. }
  443. .uploader-btn {
  444. display: flex;
  445. flex-direction: column;
  446. align-items: center;
  447. color: #606266;
  448. }
  449. .upload-icon {
  450. font-size: 32px;
  451. color: #1F55EB;
  452. margin-bottom: 10px;
  453. }
  454. .uploader-btn p {
  455. margin: 0;
  456. font-size: 14px;
  457. }
  458. .el-upload__tip {
  459. font-size: 12px;
  460. color: #A6ADB6;
  461. margin-top: 10px;
  462. }
  463. .file-list-container {
  464. max-height: 200px;
  465. overflow-y: auto;
  466. padding: 0 15px;
  467. }
  468. .file-item {
  469. position: relative;
  470. display: flex;
  471. justify-content: space-between;
  472. align-items: center;
  473. padding: 10px;
  474. border: 1px dashed #409eff;
  475. border-radius: 4px;
  476. margin-bottom: 8px;
  477. }
  478. .file-info {
  479. display: flex;
  480. justify-content: space-between;
  481. align-items: center;
  482. gap: 8px;
  483. flex: 1;
  484. line-height: 32px;
  485. }
  486. .fileIcon {
  487. font-size: 16px;
  488. }
  489. .file-name {
  490. flex: 1;
  491. font-size: 13px;
  492. color: #303133;
  493. overflow: hidden;
  494. text-overflow: ellipsis;
  495. white-space: nowrap;
  496. }
  497. .file-size {
  498. font-size: 12px;
  499. color: #909399;
  500. }
  501. .file-status {
  502. margin-top: 8px;
  503. }
  504. .progress-text {
  505. font-size: 12px;
  506. color: #606266;
  507. }
  508. .progress-bar {
  509. height: 6px;
  510. background-color: #42b983;
  511. transition: width 0.3s ease;
  512. border-radius: 3px;
  513. margin-top: 4px;
  514. }
  515. .delete-btn {
  516. /* position: absolute;
  517. right: 10px;
  518. top: 10px; */
  519. font-size: 18px;
  520. color: #909399;
  521. cursor: pointer;
  522. padding-left: 10px;
  523. }
  524. .delete-btn:hover {
  525. color: #ff4444;
  526. }
  527. .success {
  528. margin: 4px 0 0 0;
  529. font-size: 12px;
  530. color: #00C851;
  531. }
  532. .error {
  533. margin: 4px 0 0 0;
  534. font-size: 12px;
  535. color: #ff4444;
  536. }
  537. .upload-footer {
  538. padding: 12px 15px;
  539. border-top: 1px solid #e4e7ed;
  540. display: flex;
  541. justify-content: space-between;
  542. align-items: center;
  543. }
  544. .file-count {
  545. font-size: 12px;
  546. color: #606266;
  547. }
  548. </style>