UpLoadComponent.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <!--文件上传组件-->
  2. <template>
  3. <div>
  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)" :disabled="false">下载</el-button>
  56. <el-button text type="primary" key="02" icon="el-icon-delete" size="small" @click="deleteById(scope.row, scope.$index)" :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. // eslint-disable-next-line no-unused-vars
  75. openWindowOnUrl,
  76. // eslint-disable-next-line no-unused-vars
  77. toHref
  78. } from '@/api/sys/OSSService'
  79. // import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
  80. import moment from 'moment'
  81. export default {
  82. data () {
  83. return {
  84. progressFlag: false,
  85. loadProgress: 0,
  86. fileList: [],
  87. dataList: [],
  88. dataListNew: [],
  89. url: '',
  90. showViewer: false,
  91. ossService: null,
  92. auth: '',
  93. directory: 'public',
  94. maxValue: 300,
  95. tableKey: '',
  96. fileLoading: true,
  97. dividerName: '',
  98. uploadFlag: false,
  99. delFlag: false,
  100. createBy: '',
  101. showDivider: true,
  102. loading: false,
  103. dataListLength: ''
  104. }
  105. },
  106. watch: {
  107. },
  108. created () {
  109. this.ossService = new OSSSerivce()
  110. },
  111. components: {
  112. // ElImageViewer
  113. },
  114. mounted () {
  115. window.onPreview = this.onPreview
  116. },
  117. methods: {
  118. /**
  119. * dividerName: 组件中divider的名称赋值
  120. * showDivider: ‘附件‘Divider是否展示
  121. * 注:值为空时,默认值为true
  122. * showDivider=false时 ‘附件‘Divider隐藏
  123. **/
  124. setDividerName (dividerName, showDivider) {
  125. if (this.commonJS.isNotEmpty(dividerName)) {
  126. this.dividerName = dividerName
  127. }
  128. if (this.commonJS.isNotEmpty(showDivider)) {
  129. if (showDivider === false) {
  130. this.showDivider = false
  131. } else {
  132. this.showDivider = true
  133. }
  134. } else {
  135. this.showDivider = true
  136. }
  137. },
  138. /**
  139. * 文件上传组件初始化
  140. * @param auth
  141. * auth的值为"view"时,不可上传/编辑文件
  142. * auth为其他值时,可上传/编辑文件
  143. * @param fileList 要显示到文件上传列表中的文件。
  144. * 注:文件必须要有url属性并且文件的url属性值必须是在oss中的路径值
  145. * 例:'/attachment-file/xxx/xxx/2022/9/08/xxx.jpg'
  146. * @param directory 要存放到oss的哪个文件夹下。
  147. * 注:值为空时,默认存放到"public"文件夹
  148. * @param maxValue 上传文件允许的最大值,单位:MB
  149. * 注:值为空时,默认值为300MB
  150. * @param dividerName 组件中divider的名称
  151. * 注:值为空时,默认值为‘附件’
  152. * @param uploadFlag ‘上传文件’按钮是否禁用
  153. * 注:值为空时,默认值为false
  154. * auth=view&&uploadFlag=false时 ‘上传文件’按钮禁用
  155. * @param delFlag ‘删除’按钮是否禁用
  156. * 注:值为空时,默认值为false
  157. * auth=view&&delFlag=false时 ‘删除’按钮禁用
  158. * @param showDivider ‘附件‘Divider是否展示
  159. * 注:值为空时,默认值为true
  160. * showDivider=false时 ‘附件‘Divider隐藏
  161. */
  162. async newUpload (auth, fileList, directory, maxValue, dividerName, uploadFlag, delFlag, showDivider) {
  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. await this.ossService.getFileSizeByUrl(item.url).then((data) => {
  206. item.lsUrl = data.url
  207. item.size = data.size
  208. this.dataList.push(item)
  209. this.dataListNew.push(item)
  210. if (this.dataListNew.length === fileList.length) {
  211. this.fileLoading = true
  212. }
  213. })
  214. }
  215. // this.dataList = JSON.parse(JSON.stringify(fileList))
  216. // this.dataListNew = JSON.parse(JSON.stringify(fileList))
  217. if (this.commonJS.isEmpty(showDivider)) {
  218. this.showDivider = true
  219. } else {
  220. if (showDivider === false) {
  221. this.showDivider = false
  222. } else {
  223. this.showDivider = true
  224. }
  225. }
  226. },
  227. async httpRequest (file) {
  228. await httpRequest(file, fileName(file), this.directory, this.maxValue)
  229. },
  230. uploadVideoProcess (event, file, fileList) {
  231. this.progressFlag = true // 显示进度条
  232. this.loadProgress = parseInt(event.percent) // 动态获取文件上传进度
  233. if (this.loadProgress >= 100) {
  234. this.loadProgress = 100
  235. setTimeout(() => {
  236. this.progressFlag = false
  237. }, 1000) // 一秒后关闭进度条
  238. }
  239. },
  240. getSize (value) {
  241. if (this.commonJS.isEmpty(value)) {
  242. return '0 B'
  243. } else {
  244. let val = parseInt(value)
  245. if (this.commonJS.isEmpty(val)) {
  246. return '0 B'
  247. }
  248. if (isNaN(val)) {
  249. return '0 B'
  250. }
  251. if (val === 0) {
  252. return '0 B'
  253. }
  254. let k = 1024
  255. let sizes = ['B', 'KB', 'MB', 'GB', 'PB', 'TB', 'EB', 'ZB', 'YB']
  256. let i = Math.floor(Math.log(val) / Math.log(k))
  257. let result = val / Math.pow(k, i);
  258. let kb = parseFloat(result.toPrecision(3));
  259. return kb + '' + sizes[i]
  260. }
  261. },
  262. async changes (file, fileList) {
  263. // if (file.status !== 'ready') {
  264. // return
  265. // }
  266. if (!beforeAvatarUpload(file, fileList, this.maxValue)) {
  267. this.$message.error('文件大小不能超过 ' + this.maxValue + ' MB!')
  268. return
  269. }
  270. this.dataListNew = []
  271. this.dataList.forEach((item) => {
  272. this.dataListNew.push(item)
  273. })
  274. for (let item of fileList) {
  275. item.createTime = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
  276. item.createBy = {
  277. id: '',
  278. name: ''
  279. }
  280. item.createBy.id = this.$store.state.user.id
  281. item.createBy.name = this.$store.state.user.name
  282. this.dataListNew.push(item)
  283. }
  284. for await (let item of this.dataListNew) {
  285. if (item.raw !== undefined && item.raw !== null && item.raw !== {}) {
  286. item.url = item.raw.url
  287. if (item.raw.url !== undefined && item.raw.url !== null && item.raw.url !== {}) {
  288. await this.ossService.getTemporaryUrl(item.raw.url).then((data) => {
  289. item.lsUrl = data
  290. })
  291. }
  292. }
  293. }
  294. this.tableKey = Math.random()
  295. },
  296. showFile (row) {
  297. openWindowOnUrl(row)
  298. },
  299. onPreview (url) {
  300. this.url = url
  301. this.showViewer = true
  302. },
  303. // 关闭查看器
  304. closeViewer () {
  305. this.url = ''
  306. this.showViewer = false
  307. },
  308. toHref (row) {
  309. toHref(row)
  310. },
  311. async deleteById (row, index) {
  312. // this.$refs.upload.handleRemove(this.dataListNew[index])
  313. await this.dataListNew.splice(index, 1)
  314. await this.dataList.splice(index, 1)
  315. if (this.commonJS.isNotEmpty(row.id)) {
  316. this.ossService.deleteMsgById(row.id)
  317. }
  318. this.tableKey = Math.random()
  319. },
  320. /**
  321. * 关闭dialog时使用 清除el-upload中上传的文件
  322. */
  323. clearUpload () {
  324. this.$refs.upload.clearFiles()
  325. this.dataList = []
  326. this.dataListNew = []
  327. this.createBy = ''
  328. },
  329. /**
  330. * 获取当前文件列表中的文件数据
  331. */
  332. getDataList () {
  333. return this.dataListNew
  334. },
  335. /**
  336. * 判断进度条是否结束,附件是否加载完成
  337. * @returns {boolean}
  338. */
  339. checkProgress () {
  340. if (this.progressFlag === true) {
  341. this.$message.warning('请等待附件上传完成再进行操作')
  342. return true
  343. }
  344. if (this.fileLoading === false) {
  345. this.$message.warning('请等待附件加载完成再进行操作')
  346. if (this.dataListLength === this.dataListNew.length) {
  347. this.fileLoading = true
  348. }
  349. return true
  350. }
  351. return false
  352. },
  353. ifName (row) {
  354. if (this.commonJS.isEmpty(row.name)) {
  355. row.name = '---'
  356. return false
  357. }
  358. let suffix = row.name.substring(row.name.lastIndexOf('.') + 1)
  359. if (suffix === 'jpg' || suffix === 'png' || suffix === 'gif' || suffix === 'bmp' || suffix === 'jpeg') {
  360. return true
  361. } else {
  362. return false
  363. }
  364. },
  365. fileLoadingFalse () {
  366. this.fileLoading = false
  367. },
  368. // 开启/关闭页面的加载中状态
  369. changeLoading (loading) {
  370. if (this.commonJS.isNotEmpty(loading)) {
  371. this.loading = loading
  372. } else {
  373. this.loading = false
  374. }
  375. }
  376. }
  377. }
  378. </script>
  379. <style>
  380. .el-divider__text {
  381. font-size: 16px;
  382. font-weight: bold;
  383. }
  384. </style>