UpLoadComponent.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <!--文件上传组件-->
  2. <template>
  3. <div>
  4. <el-divider content-position="left"><i class="el-icon-document"></i> 附件</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'?'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'">删除</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: false
  89. }
  90. },
  91. watch: {
  92. },
  93. created () {
  94. this.ossService = new OSSSerivce()
  95. },
  96. components: {
  97. ElImageViewer
  98. },
  99. mounted () {
  100. window.onPreview = this.onPreview
  101. },
  102. methods: {
  103. /**
  104. * 文件上传组件初始化
  105. * @param auth
  106. * auth的值为"view"时,不可上传/编辑文件
  107. * auth为其他值时,可上传/编辑文件
  108. * @param fileList 要显示到文件上传列表中的文件。
  109. * 注:文件必须要有url属性并且文件的url属性值必须是在oss中的路径值
  110. * 例:'/attachment-file/xxx/xxx/2022/9/08/xxx.jpg'
  111. * @param directory 要存放到oss的哪个文件夹下。
  112. * 注:值为空时,默认存放到"public"文件夹
  113. * @param maxValue 上传文件允许的最大值,单位:MB
  114. * 注:值为空时,默认值为300MB
  115. */
  116. async newUpload (auth, fileList, directory, maxValue) {
  117. await this.fileLoadingFalse()
  118. if (directory !== undefined && directory !== null && directory !== '' && directory !== {}) {
  119. this.directory = directory
  120. } else {
  121. this.directory = 'public'
  122. }
  123. if (maxValue !== undefined && maxValue !== null && maxValue !== '' && maxValue !== 0) {
  124. this.maxValue = maxValue
  125. } else {
  126. this.maxValue = 300
  127. }
  128. this.auth = auth
  129. for await (let item of fileList) {
  130. await this.ossService.getFileSizeByUrl(item.url).then((data) => {
  131. item.lsUrl = data.data.url
  132. item.size = data.data.size
  133. this.dataList.push(item)
  134. this.dataListNew.push(item)
  135. })
  136. }
  137. this.fileLoading = true
  138. // this.dataList = JSON.parse(JSON.stringify(fileList))
  139. // this.dataListNew = JSON.parse(JSON.stringify(fileList))
  140. },
  141. async httpRequest (file) {
  142. await httpRequest(file, fileName(file), this.directory, this.maxValue)
  143. },
  144. uploadVideoProcess (event, file, fileList) {
  145. this.progressFlag = true // 显示进度条
  146. this.loadProgress = parseInt(event.percent) // 动态获取文件上传进度
  147. if (this.loadProgress >= 100) {
  148. this.loadProgress = 100
  149. setTimeout(() => {
  150. this.progressFlag = false
  151. }, 1000) // 一秒后关闭进度条
  152. }
  153. },
  154. getSize (value) {
  155. if (value === 0) {
  156. return '0 B'
  157. }
  158. let k = 1024
  159. let sizes = ['B', 'KB', 'MB', 'GB', 'PB', 'TB', 'EB', 'ZB', 'YB']
  160. let i = Math.floor(Math.log(value) / Math.log(k))
  161. return (value / Math.pow(k, i)).toPrecision(3) + '' + sizes[i]
  162. },
  163. async changes (file, fileList) {
  164. // if (file.status !== 'ready') {
  165. // return
  166. // }
  167. if (!beforeAvatarUpload(file, fileList, this.maxValue)) {
  168. this.$message.error('文件大小不能超过 ' + this.maxValue + ' MB!')
  169. return
  170. }
  171. this.dataListNew = []
  172. this.dataList.forEach((item) => {
  173. this.dataListNew.push(item)
  174. })
  175. for (let item of fileList) {
  176. item.createDate = moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
  177. item.createBy = {
  178. id: '',
  179. name: ''
  180. }
  181. item.createBy.id = this.$store.state.user.id
  182. item.createBy.name = this.$store.state.user.name
  183. this.dataListNew.push(item)
  184. }
  185. for await (let item of this.dataListNew) {
  186. if (item.raw !== undefined && item.raw !== null && item.raw !== {}) {
  187. item.url = item.raw.url
  188. if (item.raw.url !== undefined && item.raw.url !== null && item.raw.url !== {}) {
  189. await this.ossService.getTemporaryUrl(item.raw.url).then((data) => {
  190. item.lsUrl = data.data
  191. })
  192. }
  193. }
  194. }
  195. this.tableKey = Math.random()
  196. },
  197. showFile (row) {
  198. openWindowOnUrl(row)
  199. },
  200. onPreview (url) {
  201. this.url = url
  202. this.showViewer = true
  203. },
  204. // 关闭查看器
  205. closeViewer () {
  206. this.url = ''
  207. this.showViewer = false
  208. },
  209. toHref (row) {
  210. toHref(row)
  211. },
  212. deleteById (row, index) {
  213. this.dataListNew.splice(index, 1)
  214. if (row.id !== null && row.id !== '' && row.id !== undefined) {
  215. this.dataList.splice(index, 1)
  216. // this.ossService.deleteMsgById(row.id)
  217. } else {
  218. let num
  219. if (this.dataList.length > 0) {
  220. num = this.dataList.length - 1
  221. } else {
  222. num = 0
  223. }
  224. this.$refs.upload.uploadFiles.splice(index - num, 1)
  225. }
  226. },
  227. /**
  228. * 关闭dialog时使用 清除el-upload中上传的文件
  229. */
  230. clearUpload () {
  231. this.$refs.upload.uploadFiles = []
  232. this.dataList = []
  233. this.dataListNew = []
  234. },
  235. /**
  236. * 获取当前文件列表中的文件数据
  237. */
  238. getDataList () {
  239. return this.dataListNew
  240. },
  241. /**
  242. * 判断进度条是否结束,附件是否加载完成
  243. * @returns {boolean}
  244. */
  245. checkProgress () {
  246. if (this.progressFlag === true) {
  247. this.$message.warning('请等待附件上传完成再进行操作')
  248. return true
  249. }
  250. if (this.fileLoading === false) {
  251. this.$message.warning('请等待附件加载完成再进行操作')
  252. return true
  253. }
  254. return false
  255. },
  256. ifName (row) {
  257. if (this.commonJS.isEmpty(row.name)) {
  258. row.name = '---'
  259. return false
  260. }
  261. let suffix = row.name.substring(row.name.lastIndexOf('.') + 1)
  262. if (suffix === 'jpg' || suffix === 'png' || suffix === 'gif' || suffix === 'bmp' || suffix === 'jpeg') {
  263. return true
  264. } else {
  265. return false
  266. }
  267. },
  268. fileLoadingFalse () {
  269. this.fileLoading = false
  270. }
  271. }
  272. }
  273. </script>