UpLoadComponent.vue 13 KB

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