538385ddddebea02083193eb2d02e3ba1e1858e2.svn-base 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <div class="upload-container">
  3. <el-button :style="{background:color,borderColor:color}" icon="el-icon-upload" size="mini" type="primary" @click=" dialogVisible=true">
  4. upload
  5. </el-button>
  6. <el-dialog :visible.sync="dialogVisible">
  7. <el-upload
  8. :multiple="true"
  9. :file-list="fileList"
  10. :show-file-list="true"
  11. :on-remove="handleRemove"
  12. :on-success="handleSuccess"
  13. :before-upload="beforeUpload"
  14. class="editor-slide-upload"
  15. action="https://httpbin.org/post"
  16. list-type="picture-card"
  17. >
  18. <el-button size="small" type="primary">
  19. Click upload
  20. </el-button>
  21. </el-upload>
  22. <el-button @click="dialogVisible = false">
  23. Cancel
  24. </el-button>
  25. <el-button type="primary" @click="handleSubmit">
  26. Confirm
  27. </el-button>
  28. </el-dialog>
  29. </div>
  30. </template>
  31. <script>
  32. // import { getToken } from 'api/qiniu'
  33. export default {
  34. name: 'EditorSlideUpload',
  35. props: {
  36. color: {
  37. type: String,
  38. default: '#1890ff'
  39. }
  40. },
  41. data() {
  42. return {
  43. dialogVisible: false,
  44. listObj: {},
  45. fileList: []
  46. }
  47. },
  48. methods: {
  49. checkAllSuccess() {
  50. return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess)
  51. },
  52. handleSubmit() {
  53. const arr = Object.keys(this.listObj).map(v => this.listObj[v])
  54. if (!this.checkAllSuccess()) {
  55. this.$message('Please wait for all images to be uploaded successfully. If there is a network problem, please refresh the page and upload again!')
  56. return
  57. }
  58. this.$emit('successCBK', arr)
  59. this.listObj = {}
  60. this.fileList = []
  61. this.dialogVisible = false
  62. },
  63. handleSuccess(response, file) {
  64. const uid = file.uid
  65. const objKeyArr = Object.keys(this.listObj)
  66. for (let i = 0, len = objKeyArr.length; i < len; i++) {
  67. if (this.listObj[objKeyArr[i]].uid === uid) {
  68. this.listObj[objKeyArr[i]].url = response.files.file
  69. this.listObj[objKeyArr[i]].hasSuccess = true
  70. return
  71. }
  72. }
  73. },
  74. handleRemove(file) {
  75. const uid = file.uid
  76. const objKeyArr = Object.keys(this.listObj)
  77. for (let i = 0, len = objKeyArr.length; i < len; i++) {
  78. if (this.listObj[objKeyArr[i]].uid === uid) {
  79. delete this.listObj[objKeyArr[i]]
  80. return
  81. }
  82. }
  83. },
  84. beforeUpload(file) {
  85. const _self = this
  86. const _URL = window.URL || window.webkitURL
  87. const fileName = file.uid
  88. this.listObj[fileName] = {}
  89. return new Promise((resolve, reject) => {
  90. const img = new Image()
  91. img.src = _URL.createObjectURL(file)
  92. img.onload = function() {
  93. _self.listObj[fileName] = { hasSuccess: false, uid: file.uid, width: this.width, height: this.height }
  94. }
  95. resolve(true)
  96. })
  97. }
  98. }
  99. }
  100. </script>
  101. <style lang="scss" scoped>
  102. .editor-slide-upload {
  103. margin-bottom: 20px;
  104. /deep/ .el-upload--picture-card {
  105. width: 100%;
  106. }
  107. }
  108. </style>