oss-upload.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <template>
  2. <el-dialog
  3. title="上传文件"
  4. :close-on-click-modal="false"
  5. @close="closeHandle"
  6. :visible.sync="visible">
  7. <el-upload
  8. drag
  9. :action="url"
  10. :before-upload="beforeUploadHandle"
  11. :on-success="successHandle"
  12. multiple
  13. :file-list="fileList"
  14. style="text-align: center;">
  15. <i class="el-icon-upload"></i>
  16. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  17. <div class="el-upload__tip" slot="tip">只支持jpg、png、gif格式的图片!</div>
  18. </el-upload>
  19. </el-dialog>
  20. </template>
  21. <script>
  22. export default {
  23. data () {
  24. return {
  25. visible: false,
  26. url: '',
  27. num: 0,
  28. successNum: 0,
  29. fileList: []
  30. }
  31. },
  32. methods: {
  33. init (id) {
  34. this.url = this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`)
  35. this.visible = true
  36. },
  37. // 上传之前
  38. beforeUploadHandle (file) {
  39. if (file.type !== 'image/jpg' && file.type !== 'image/jpeg' && file.type !== 'image/png' && file.type !== 'image/gif') {
  40. this.$message.error('只支持jpg、png、gif格式的图片!')
  41. return false
  42. }
  43. this.num++
  44. },
  45. // 上传成功
  46. successHandle (response, file, fileList) {
  47. this.fileList = fileList
  48. this.successNum++
  49. if (response && response.code === 0) {
  50. if (this.num === this.successNum) {
  51. this.$confirm('操作成功, 是否继续操作?', '提示', {
  52. confirmButtonText: '确定',
  53. cancelButtonText: '取消',
  54. type: 'warning'
  55. }).catch(() => {
  56. this.visible = false
  57. })
  58. }
  59. } else {
  60. this.$message.error(response.msg)
  61. }
  62. },
  63. // 弹窗关闭时
  64. closeHandle () {
  65. this.fileList = []
  66. this.$emit('refreshDataList')
  67. }
  68. }
  69. }
  70. </script>