UpLoadComponent.vue 14 KB

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