UpLoadComponent.vue 13 KB

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