UpLoadComponentWorkCollect.vue 14 KB

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