UpLoadComponent.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <!--文件上传组件-->
  2. <template>
  3. <div :key="uploadKey">
  4. <el-divider v-if="showDivider" content-position="left"><i class="el-icon-document"></i> {{ dividerName
  5. }}</el-divider>
  6. <el-upload ref="upload" style="display: inline-block;" :show-header="'status'" action="" :limit="999"
  7. :http-request="httpRequest" multiple :on-exceed="(files, fileList) => {
  8. $message.warning(`当前限制选择 999 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`)
  9. }" :show-file-list="false" :on-change="changes" :on-progress="uploadVideoProcess" :file-list="fileList">
  10. <template v-if="auth === 'view' && uploadFlag === false" #tip>
  11. <el-button :loading="loading" type="primary" size="default" :disabled="true"> 点击上传 </el-button>
  12. </template>
  13. <template v-else #trigger>
  14. <el-button :loading="loading" type="primary" size="default"> 点击上传 </el-button>
  15. </template>
  16. <AttachmentLib v-if="auth !== 'view'" :directory="directory" @onDownToOss="handleOnDownToOss">
  17. </AttachmentLib>
  18. <span style="color: red;font-size: 16px;font-weight: 600;" v-if="isReimbursement == true">
  19. (此附件上传不是数电发票上传。数电发票请在报销单后操作栏处上传)
  20. </span>
  21. </el-upload>
  22. <div style="height: calc(100% - 80px);margin-top: 10px">
  23. <!-- 进度条 -->
  24. <el-progress style="margin-left: 5em" v-if="progressFlag" :percentage="loadProgress"></el-progress>
  25. <el-table ref="uploadTable" v-loading="loading" :key="tableKey" :data="dataListNew">
  26. <el-table-column type="seq" width="40"></el-table-column>
  27. <el-table-column label="文件名称" prop="name" align="center">
  28. <template #default="scope">
  29. <div v-if="ifName(scope.row) === true">
  30. <el-image style="width: 30px; height: 30px;padding-top: 4px;" :src="scope.row.lsUrl"
  31. :preview-src-list="[scope.row.lsUrl]" :preview-teleported="true"></el-image>
  32. </div>
  33. <div v-else>
  34. <el-link type="primary" :underline="false" @click="showFile(scope.row)">{{ scope.row.name }}</el-link>
  35. </div>
  36. </template>
  37. </el-table-column>
  38. <el-table-column label="创建人" prop="createBy.name" align="center"></el-table-column>
  39. <el-table-column label="创建时间" prop="createTime" align="center"></el-table-column>
  40. <el-table-column label="文件大小" prop="size" align="center">
  41. <template #default="scope">
  42. {{ getSize(scope.row.size) }}
  43. </template>
  44. </el-table-column>
  45. <el-table-column label="操作" width="200px" fixed="right" align="center">
  46. <template #default="scope">
  47. <el-button text type="primary" key="01" icon="el-icon-download" size="small"
  48. @click="toHref(scope.row)">下载</el-button>
  49. <el-button text type="primary" key="02" icon="el-icon-delete" size="small"
  50. @click="deleteById(scope.row, scope.$index, fileList)"
  51. :disabled="auth === 'view' && delFlag === false && createBy !== scope.row.createBy.name">删除</el-button>
  52. <!-- <el-button v-if="createBy===scope.row.createBy.name" type="text" icon="el-icon-delete" size="small" @click="deleteById(scope.row, scope.$index)" :disabled="auth==='view'&&delFlag === false">删除2</el-button>-->
  53. </template>
  54. </el-table-column>
  55. </el-table>
  56. </div>
  57. <!-- <el-image-viewer v-if="showViewer" :on-close="closeViewer" :url-list="[url]" :zIndex=9999></el-image-viewer>-->
  58. </div>
  59. </template>
  60. <script>
  61. // eslint-disable-next-line no-unused-vars
  62. import OSSSerivce, {
  63. httpRequest,
  64. // eslint-disable-next-line no-unused-vars
  65. handleRemove,
  66. fileName,
  67. // eslint-disable-next-line no-unused-vars
  68. beforeAvatarUpload,
  69. // eslint-disable-next-line no-unused-vars
  70. openWindowOnUrl,
  71. // eslint-disable-next-line no-unused-vars
  72. toHref
  73. } from '@/api/sys/OSSService'
  74. // import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
  75. import moment from 'moment'
  76. import AttachmentLib from './AttachmentLib.vue'
  77. export default {
  78. data() {
  79. return {
  80. uploadKey: '',
  81. progressFlag: false,
  82. loadProgress: 0,
  83. fileList: [],
  84. dataList: [],
  85. dataListNew: [],
  86. url: '',
  87. showViewer: false,
  88. ossService: null,
  89. auth: '',
  90. directory: 'public',
  91. maxValue: 300,
  92. tableKey: '',
  93. fileLoading: true,
  94. dividerName: '',
  95. uploadFlag: false,
  96. delFlag: false,
  97. createBy: '',
  98. showDivider: true,
  99. loading: false,
  100. dataListLength: '',
  101. isReimbursement: false // 是否是报销附件上传
  102. }
  103. },
  104. watch: {
  105. },
  106. created() {
  107. this.ossService = new OSSSerivce()
  108. },
  109. components: {
  110. AttachmentLib
  111. // ElImageViewer
  112. },
  113. mounted() {
  114. window.onPreview = this.onPreview
  115. },
  116. methods: {
  117. /**
  118. * dividerName: 组件中divider的名称赋值
  119. * showDivider: ‘附件‘Divider是否展示
  120. * 注:值为空时,默认值为true
  121. * showDivider=false时 ‘附件‘Divider隐藏
  122. **/
  123. setDividerName(dividerName, showDivider) {
  124. if (this.commonJS.isNotEmpty(dividerName)) {
  125. this.dividerName = dividerName
  126. }
  127. if (this.commonJS.isNotEmpty(showDivider)) {
  128. if (showDivider === false) {
  129. this.showDivider = false
  130. } else {
  131. this.showDivider = true
  132. }
  133. } else {
  134. this.showDivider = true
  135. }
  136. },
  137. /**
  138. * 文件上传组件初始化
  139. * @param auth
  140. * auth的值为"view"时,不可上传/编辑文件
  141. * auth为其他值时,可上传/编辑文件
  142. * @param fileList 要显示到文件上传列表中的文件。
  143. * 注:文件必须要有url属性并且文件的url属性值必须是在oss中的路径值
  144. * 例:'/attachment-file/xxx/xxx/2022/9/08/xxx.jpg'
  145. * @param directory 要存放到oss的哪个文件夹下。
  146. * 注:值为空时,默认存放到"public"文件夹
  147. * @param maxValue 上传文件允许的最大值,单位:MB
  148. * 注:值为空时,默认值为300MB
  149. * @param dividerName 组件中divider的名称
  150. * 注:值为空时,默认值为‘附件’
  151. * @param uploadFlag ‘上传文件’按钮是否禁用
  152. * 注:值为空时,默认值为false
  153. * auth=view&&uploadFlag=false时 ‘上传文件’按钮禁用
  154. * @param delFlag ‘删除’按钮是否禁用
  155. * 注:值为空时,默认值为false
  156. * auth=view&&delFlag=false时 ‘删除’按钮禁用
  157. * @param showDivider ‘附件‘Divider是否展示
  158. * 注:值为空时,默认值为true
  159. * showDivider=false时 ‘附件‘Divider隐藏
  160. */
  161. async newUpload(auth, fileList, directory, maxValue, dividerName, uploadFlag, delFlag, showDivider) {
  162. console.log(fileList);
  163. await this.fileLoadingFalse()
  164. if (this.commonJS.isEmpty(fileList)) {
  165. this.fileLoading = true
  166. } else {
  167. this.dataListLength = fileList.length
  168. }
  169. if (this.commonJS.isEmpty(dividerName)) {
  170. this.dividerName = '附件'
  171. } else {
  172. this.dividerName = dividerName
  173. }
  174. if (directory !== undefined && directory !== null && directory !== '' && directory !== {}) {
  175. this.directory = directory
  176. } else {
  177. this.directory = 'public'
  178. }
  179. if (maxValue !== undefined && maxValue !== null && maxValue !== '' && maxValue !== 0) {
  180. this.maxValue = maxValue
  181. } else {
  182. this.maxValue = 300
  183. }
  184. this.auth = auth
  185. if (this.commonJS.isEmpty(uploadFlag)) {
  186. this.uploadFlag = false
  187. } else {
  188. if (uploadFlag !== true && uploadFlag !== false) {
  189. this.uploadFlag = false
  190. } else {
  191. this.uploadFlag = uploadFlag
  192. }
  193. }
  194. if (this.commonJS.isEmpty(delFlag)) {
  195. this.delFlag = false
  196. } else {
  197. if (delFlag !== true && delFlag !== false) {
  198. this.delFlag = false
  199. this.createBy = delFlag
  200. } else {
  201. this.delFlag = delFlag
  202. }
  203. }
  204. for await (let item of fileList) {
  205. console.log(11111)
  206. if (item.url && item.url !== null && item.url !== undefined && item.url !== "") {
  207. await this.ossService.getFileSizeByUrl(item.url).then((data) => {
  208. item.lsUrl = data.url
  209. item.size = data.size
  210. this.dataList.push(item)
  211. this.dataListNew.push(item)
  212. this.fileList.push(item)
  213. if (this.dataListNew.length === fileList.length) {
  214. this.fileLoading = true
  215. }
  216. })
  217. }
  218. // else {
  219. // this.dataList.push(item)
  220. // this.dataListNew.push(item)
  221. // this.fileList.push(item)
  222. // this.fileLoading = true
  223. // }
  224. }
  225. // this.dataList = JSON.parse(JSON.stringify(fileList))
  226. // this.dataListNew = JSON.parse(JSON.stringify(fileList))
  227. if (this.commonJS.isEmpty(showDivider)) {
  228. this.showDivider = true
  229. } else {
  230. if (showDivider === false) {
  231. this.showDivider = false
  232. } else {
  233. this.showDivider = true
  234. }
  235. }
  236. },
  237. addMyAttachment(row) {
  238. this.$confirm(`确定收藏到个人资料库?`, "提示", {
  239. confirmButtonText: "确定",
  240. cancelButtonText: "取消",
  241. type: "warning",
  242. }).then(() => {
  243. let inputForm = {
  244. classification: "",
  245. fileDescription: "",
  246. workAttachments: []
  247. }
  248. inputForm.workAttachments.push(row)
  249. this.ossService.downloadAndUploadToMyCollect({
  250. url: row.lsUrl,
  251. fileName: row.name,
  252. dir: this.directory,
  253. workCollectAccessoryDTO: {
  254. ...inputForm
  255. }
  256. }).then(res => {
  257. this.$message.success(`操作成功`);
  258. })
  259. });
  260. },
  261. async httpRequest(file) {
  262. await httpRequest(file, fileName(file), this.directory, this.maxValue)
  263. },
  264. handleOnDownToOss(row, url) {
  265. console.log(222222)
  266. let flag = false
  267. this.fileList.forEach(item => {
  268. if (row.fileName == item.name) {
  269. flag = true
  270. }
  271. })
  272. console.log(this.fileList);
  273. if (flag) {
  274. this.$message.error(`${row.fileName}已存在,无法重复上传`);
  275. return
  276. }
  277. this.ossService.getFileSizeByUrl(url).then((data) => {
  278. let file = {
  279. lsUrl: data.url,
  280. size: data.size,
  281. createBy: {
  282. id: this.$store.state.user.id,
  283. name: this.$store.state.user.name,
  284. },
  285. createTime: moment(new Date()).format('YYYY-MM-DD HH:mm:ss'),
  286. name: row.fileName,
  287. percentage: 100,
  288. status: "success",
  289. url: url
  290. }
  291. this.dataList.push(file)
  292. this.dataListNew.push(file)
  293. this.fileList.push(file)
  294. })
  295. },
  296. uploadVideoProcess(event, file, fileList) {
  297. this.progressFlag = true // 显示进度条
  298. this.loadProgress = parseInt(event.percent) // 动态获取文件上传进度
  299. if (this.loadProgress >= 100) {
  300. this.loadProgress = 100
  301. setTimeout(() => {
  302. this.progressFlag = false
  303. }, 1000) // 一秒后关闭进度条
  304. }
  305. },
  306. getSize(value) {
  307. if (this.commonJS.isEmpty(value)) {
  308. return '0 B'
  309. } else {
  310. let val = parseInt(value)
  311. if (this.commonJS.isEmpty(val)) {
  312. return '0 B'
  313. }
  314. if (isNaN(val)) {
  315. return '0 B'
  316. }
  317. if (val === 0) {
  318. return '0 B'
  319. }
  320. let k = 1024
  321. let sizes = ['B', 'KB', 'MB', 'GB', 'PB', 'TB', 'EB', 'ZB', 'YB']
  322. let i = Math.floor(Math.log(val) / Math.log(k))
  323. let result = val / Math.pow(k, i);
  324. let kb = parseFloat(result.toPrecision(3));
  325. return kb + '' + sizes[i]
  326. }
  327. },
  328. async changes(file, fileList) {
  329. console.log(33333)
  330. if (file.status == 'ready') return //阻止上传多次触发changes
  331. let fileListNamesList = []
  332. this.fileList.forEach(fileItem => {
  333. fileListNamesList.push(fileItem.name)
  334. })
  335. if (fileListNamesList.indexOf(file.name) != -1) {
  336. this.$message.error(`${file.name}已存在,无法重复上传`)
  337. return
  338. } else {
  339. if (!beforeAvatarUpload(file, fileList, this.maxValue)) {
  340. this.$message.error('文件大小不能超过 ' + this.maxValue + ' MB!')
  341. return
  342. }
  343. let item1 = JSON.parse(JSON.stringify(file))
  344. // if (fileListNames.length === 0 || fileListNames.indexOf(item1.name) === -1) {
  345. const fileUrl = item1.raw ? item1.raw.url : item1.url;
  346. // 检查临时 URL 是否有效
  347. if (fileUrl && fileUrl.trim() !== '') {
  348. try {
  349. const temporaryUrl = await this.ossService.getTemporaryUrl(fileUrl);
  350. if (!temporaryUrl || temporaryUrl.trim() === '') {
  351. // 如果临时 URL 无效,则标记该文件为无效并删除
  352. this.$message.warning({ message: `文件:${file.name}获取失败,上传失败。` });
  353. return
  354. } else {
  355. // 如果临时 URL 有效,继续添加文件到有效文件列表
  356. file.lsUrl = temporaryUrl;
  357. }
  358. } catch (error) {
  359. // 如果临时 URL 获取失败,也标记该文件为无效
  360. this.$message.warning({ message: `文件:${file.name}获取失败,上传失败。` });
  361. return
  362. }
  363. }
  364. if(!item1.lsUrl && file.lsUrl){
  365. item1.lsUrl = file.lsUrl
  366. }
  367. // 添加url
  368. if (item1.raw && item1.raw.url) {
  369. item1.url = item1.raw.url
  370. }
  371. this.fileList.push(item1)
  372. // }
  373. item1.createTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
  374. item1.createBy = {
  375. id: '',
  376. name: ''
  377. }
  378. item1.createBy.id = this.$store.state.user.id
  379. item1.createBy.name = this.$store.state.user.name
  380. this.dataListNew.push(item1)
  381. this.dataList.push(item1)
  382. }
  383. },
  384. showFile(row) {
  385. console.log('row', row)
  386. openWindowOnUrl(row)
  387. },
  388. onPreview(url) {
  389. this.url = url
  390. this.showViewer = true
  391. },
  392. // 关闭查看器
  393. closeViewer() {
  394. this.url = ''
  395. this.showViewer = false
  396. },
  397. toHref(row) {
  398. toHref(row)
  399. },
  400. deleteById(row, index, fileList) {
  401. this.dataListNew.splice(index, 1)
  402. this.dataList.splice(index, 1)
  403. if (this.commonJS.isNotEmpty(row.id)) {
  404. this.ossService.deleteMsgById(row.id)
  405. }
  406. var newFileList = [];
  407. for (var i = 0; i < fileList.length; i++) {
  408. if (fileList[i].name !== row.name) {
  409. newFileList.push(fileList[i])
  410. }
  411. }
  412. this.fileList = newFileList;
  413. },
  414. /**
  415. * 关闭dialog时使用 清除el-upload中上传的文件
  416. */
  417. clearUpload() {
  418. this.$refs.upload.clearFiles()
  419. this.dataList = []
  420. this.dataListNew = []
  421. this.fileList = []
  422. this.createBy = ''
  423. },
  424. /**
  425. * 获取当前文件列表中的文件数据
  426. */
  427. getDataList() {
  428. console.log(444444)
  429. return this.dataListNew
  430. },
  431. /**
  432. * 判断进度条是否结束,附件是否加载完成
  433. * @returns {boolean}
  434. */
  435. checkProgress() {
  436. console.log(555555)
  437. if (this.progressFlag === true) {
  438. this.$message.warning('请等待附件上传完成再进行操作')
  439. return true
  440. }
  441. if (this.fileLoading === false) {
  442. this.$message.warning('请等待附件加载完成再进行操作')
  443. if (this.dataListLength === this.dataListNew.length) {
  444. this.fileLoading = true
  445. }
  446. return true
  447. }
  448. const invalidFile = this.dataListNew.find(file => !file.url); // 找到第一个 URL 为空的文件
  449. if (invalidFile) {
  450. this.$message.warning(`${invalidFile.name}的URL为空,请检查后重新上传`)
  451. return true; // 只要有一个 url 为空,直接 return
  452. }
  453. return false
  454. },
  455. ifName(row) {
  456. if (this.commonJS.isEmpty(row.name)) {
  457. row.name = '---'
  458. return false
  459. }
  460. let suffix = row.name.substring(row.name.lastIndexOf('.') + 1)
  461. if (suffix === 'jpg' || suffix === 'png' || suffix === 'gif' || suffix === 'bmp' || suffix === 'jpeg') {
  462. return true
  463. } else {
  464. return false
  465. }
  466. },
  467. fileLoadingFalse() {
  468. this.fileLoading = false
  469. },
  470. // 开启/关闭页面的加载中状态
  471. changeLoading(loading) {
  472. if (this.commonJS.isNotEmpty(loading)) {
  473. this.loading = loading
  474. } else {
  475. this.loading = false
  476. }
  477. }
  478. }
  479. }
  480. </script>
  481. <style>
  482. .el-divider__text {
  483. font-size: 16px;
  484. font-weight: bold;
  485. }
  486. </style>