UpLoadComponent.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <!--文件上传组件-->
  2. <template>
  3. <div>
  4. <el-divider 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. <el-button type="info" size="mini" :slot="auth==='view'&&uploadFlag===false?'tip':'trigger'" :disabled="auth==='view'">点击上传</el-button>
  16. </el-upload>
  17. <div style="height: calc(100% - 80px);margin-top: 10px">
  18. <!-- 进度条 -->
  19. <el-progress style="margin-left: 5em" v-if="progressFlag" :percentage="loadProgress"></el-progress>
  20. <el-table
  21. ref="uploadTable"
  22. :key="tableKey"
  23. :data="dataListNew">
  24. <el-table-column type="seq" width="40"></el-table-column>
  25. <el-table-column label="文件名称" prop="name" align="center">
  26. <template scope="scope">
  27. <div v-if="ifName(scope.row) === true">
  28. <el-image
  29. style="width: 30px; height: 30px;padding-top: 4px"
  30. :src="scope.row.lsUrl"
  31. :preview-src-list="[scope.row.lsUrl]"
  32. ></el-image>
  33. </div>
  34. <div v-else>
  35. <el-link type="primary" :underline="false" @click="showFile(scope.row)">{{scope.row.name}}</el-link>
  36. </div>
  37. </template>
  38. </el-table-column>
  39. <el-table-column label="创建人" prop="createBy.name" align="center"></el-table-column>
  40. <el-table-column label="创建时间" prop="createDate" align="center"></el-table-column>
  41. <el-table-column label="文件大小" prop="size" align="center">
  42. <template slot-scope="scope">
  43. {{getSize(scope.row.size)}}
  44. </template>
  45. </el-table-column>
  46. <el-table-column label="操作" width="200px" fixed="right" align="center">
  47. <template scope="scope">
  48. <el-button type="text" icon="el-icon-edit" size="small" @click="toHref(scope.row)" :disabled="false">下载</el-button>
  49. <el-button type="text" icon="el-icon-delete" size="small" @click="deleteById(scope.row, scope.$index)" :disabled="auth==='view'&&delFlag === false">删除</el-button>
  50. </template>
  51. </el-table-column>
  52. </el-table>
  53. </div>
  54. <el-image-viewer v-if="showViewer" :on-close="closeViewer" :url-list="[url]" :zIndex=9999></el-image-viewer>
  55. </div>
  56. </template>
  57. <script>
  58. // eslint-disable-next-line no-unused-vars
  59. import OSSSerivce, {
  60. httpRequest,
  61. // eslint-disable-next-line no-unused-vars
  62. handleRemove,
  63. fileName,
  64. // eslint-disable-next-line no-unused-vars
  65. beforeAvatarUpload,
  66. // eslint-disable-next-line no-unused-vars
  67. openWindowOnUrl,
  68. // eslint-disable-next-line no-unused-vars
  69. toHref
  70. } from '@/api/sys/OSSService'
  71. import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
  72. import moment from 'moment'
  73. export default {
  74. data () {
  75. return {
  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. }
  93. },
  94. watch: {
  95. },
  96. created () {
  97. this.ossService = new OSSSerivce()
  98. },
  99. components: {
  100. ElImageViewer
  101. },
  102. mounted () {
  103. window.onPreview = this.onPreview
  104. },
  105. methods: {
  106. /**
  107. * 组件中divider的名称赋值
  108. **/
  109. setDividerName (dividerName) {
  110. if (this.commonJS.isNotEmpty(dividerName)) {
  111. this.dividerName = dividerName
  112. }
  113. },
  114. /**
  115. * 文件上传组件初始化
  116. * @param auth
  117. * auth的值为"view"时,不可上传/编辑文件
  118. * auth为其他值时,可上传/编辑文件
  119. * @param fileList 要显示到文件上传列表中的文件。
  120. * 注:文件必须要有url属性并且文件的url属性值必须是在oss中的路径值
  121. * 例:'/attachment-file/xxx/xxx/2022/9/08/xxx.jpg'
  122. * @param directory 要存放到oss的哪个文件夹下。
  123. * 注:值为空时,默认存放到"public"文件夹
  124. * @param maxValue 上传文件允许的最大值,单位:MB
  125. * 注:值为空时,默认值为300MB
  126. * @param dividerName 组件中divider的名称
  127. * 注:值为空时,默认值为‘附件’
  128. * @param uploadFlag ‘上传文件’按钮是否禁用
  129. * 注:值为空时,默认值为false
  130. * auth=view&&uploadFlag=false ‘上传文件’按钮禁用
  131. * @param delFlag ‘删除’按钮是否禁用
  132. * 注:值为空时,默认值为false
  133. * auth=view&&delFlag=false ‘删除’按钮禁用
  134. */
  135. async newUpload (auth, fileList, directory, maxValue, dividerName, uploadFlag, delFlag) {
  136. await this.fileLoadingFalse()
  137. if (this.commonJS.isEmpty(dividerName)) {
  138. this.dividerName = '附件'
  139. } else {
  140. this.dividerName = dividerName
  141. }
  142. if (directory !== undefined && directory !== null && directory !== '' && directory !== {}) {
  143. this.directory = directory
  144. } else {
  145. this.directory = 'public'
  146. }
  147. if (maxValue !== undefined && maxValue !== null && maxValue !== '' && maxValue !== 0) {
  148. this.maxValue = maxValue
  149. } else {
  150. this.maxValue = 300
  151. }
  152. this.auth = auth
  153. if (this.commonJS.isEmpty(uploadFlag)) {
  154. this.uploadFlag = false
  155. }
  156. if (this.commonJS.isEmpty(delFlag)) {
  157. this.delFlag = false
  158. }
  159. if (this.commonJS.isEmpty(fileList)) {
  160. this.fileLoading = true
  161. }
  162. for await (let item of fileList) {
  163. await this.ossService.getFileSizeByUrl(item.url).then((data) => {
  164. item.lsUrl = data.data.url
  165. item.size = data.data.size
  166. this.dataList.push(item)
  167. this.dataListNew.push(item)
  168. if (this.dataList.length === fileList.length) {
  169. this.fileLoading = true
  170. }
  171. })
  172. }
  173. // this.dataList = JSON.parse(JSON.stringify(fileList))
  174. // this.dataListNew = JSON.parse(JSON.stringify(fileList))
  175. },
  176. async httpRequest (file) {
  177. await httpRequest(file, fileName(file), this.directory, this.maxValue)
  178. },
  179. uploadVideoProcess (event, file, fileList) {
  180. this.progressFlag = true // 显示进度条
  181. this.loadProgress = parseInt(event.percent) // 动态获取文件上传进度
  182. if (this.loadProgress >= 100) {
  183. this.loadProgress = 100
  184. setTimeout(() => {
  185. this.progressFlag = false
  186. }, 1000) // 一秒后关闭进度条
  187. }
  188. },
  189. getSize (value) {
  190. if (value === 0) {
  191. return '0 B'
  192. }
  193. let k = 1024
  194. let sizes = ['B', 'KB', 'MB', 'GB', 'PB', 'TB', 'EB', 'ZB', 'YB']
  195. let i = Math.floor(Math.log(value) / Math.log(k))
  196. return (value / Math.pow(k, i)).toPrecision(3) + '' + sizes[i]
  197. },
  198. async changes (file, fileList) {
  199. // if (file.status !== 'ready') {
  200. // return
  201. // }
  202. if (!beforeAvatarUpload(file, fileList, this.maxValue)) {
  203. this.$message.error('文件大小不能超过 ' + this.maxValue + ' MB!')
  204. return
  205. }
  206. this.dataListNew = []
  207. this.dataList.forEach((item) => {
  208. this.dataListNew.push(item)
  209. })
  210. for (let item of fileList) {
  211. item.createDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
  212. item.createBy = {
  213. id: '',
  214. name: ''
  215. }
  216. item.createBy.id = this.$store.state.user.id
  217. item.createBy.name = this.$store.state.user.name
  218. this.dataListNew.push(item)
  219. }
  220. for await (let item of this.dataListNew) {
  221. if (item.raw !== undefined && item.raw !== null && item.raw !== {}) {
  222. item.url = item.raw.url
  223. if (item.raw.url !== undefined && item.raw.url !== null && item.raw.url !== {}) {
  224. await this.ossService.getTemporaryUrl(item.raw.url).then((data) => {
  225. item.lsUrl = data.data
  226. })
  227. }
  228. }
  229. }
  230. this.tableKey = Math.random()
  231. },
  232. showFile (row) {
  233. openWindowOnUrl(row)
  234. },
  235. onPreview (url) {
  236. this.url = url
  237. this.showViewer = true
  238. },
  239. // 关闭查看器
  240. closeViewer () {
  241. this.url = ''
  242. this.showViewer = false
  243. },
  244. toHref (row) {
  245. toHref(row)
  246. },
  247. deleteById (row, index) {
  248. this.dataListNew.splice(index, 1)
  249. if (row.id !== null && row.id !== '' && row.id !== undefined) {
  250. this.dataList.splice(index, 1)
  251. // this.ossService.deleteMsgById(row.id)
  252. } else {
  253. let num
  254. if (this.dataList.length > 0) {
  255. num = this.dataList.length - 1
  256. } else {
  257. num = 0
  258. }
  259. this.$refs.upload.uploadFiles.splice(index - num, 1)
  260. }
  261. },
  262. /**
  263. * 关闭dialog时使用 清除el-upload中上传的文件
  264. */
  265. clearUpload () {
  266. this.$refs.upload.uploadFiles = []
  267. this.dataList = []
  268. this.dataListNew = []
  269. },
  270. /**
  271. * 获取当前文件列表中的文件数据
  272. */
  273. getDataList () {
  274. return this.dataListNew
  275. },
  276. /**
  277. * 判断进度条是否结束,附件是否加载完成
  278. * @returns {boolean}
  279. */
  280. checkProgress () {
  281. if (this.progressFlag === true) {
  282. this.$message.warning('请等待附件上传完成再进行操作')
  283. return true
  284. }
  285. if (this.fileLoading === false) {
  286. this.$message.warning('请等待附件加载完成再进行操作')
  287. return true
  288. }
  289. return false
  290. },
  291. ifName (row) {
  292. if (this.commonJS.isEmpty(row.name)) {
  293. row.name = '---'
  294. return false
  295. }
  296. let suffix = row.name.substring(row.name.lastIndexOf('.') + 1)
  297. if (suffix === 'jpg' || suffix === 'png' || suffix === 'gif' || suffix === 'bmp' || suffix === 'jpeg') {
  298. return true
  299. } else {
  300. return false
  301. }
  302. },
  303. fileLoadingFalse () {
  304. this.fileLoading = false
  305. }
  306. }
  307. }
  308. </script>