HandoverUpLoadComponent.vue 16 KB

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