|
| 1 | +<template> |
| 2 | + <div class="upload-container"> |
| 3 | + <el-button icon='upload' :style="{background:color,borderColor:color}" @click=" dialogVisible=true" type="primary">上传图片 |
| 4 | + </el-button> |
| 5 | + <el-dialog v-model="dialogVisible"> |
| 6 | + <el-upload |
| 7 | + class="editor-slide-upload" |
| 8 | + action="https://httpbin.org/post" |
| 9 | + :multiple="true" |
| 10 | + :file-list="fileList" |
| 11 | + :show-file-list="true" |
| 12 | + list-type="picture-card" |
| 13 | + :on-remove="handleRemove" |
| 14 | + :on-success="handleSuccess" |
| 15 | + :before-upload="beforeUpload"> |
| 16 | + <el-button size="small" type="primary">点击上传</el-button> |
| 17 | + </el-upload> |
| 18 | + <el-button @click="dialogVisible = false">取 消</el-button> |
| 19 | + <el-button type="primary" @click="handleSubmit">确 定</el-button> |
| 20 | + </el-dialog> |
| 21 | + </div> |
| 22 | +</template> |
| 23 | +<script> |
| 24 | +// import { getToken } from 'api/qiniu' |
| 25 | +
|
| 26 | +export default { |
| 27 | + name: 'editorSlideUpload', |
| 28 | + props: { |
| 29 | + color: { |
| 30 | + type: String, |
| 31 | + default: '#20a0ff' |
| 32 | + } |
| 33 | + }, |
| 34 | + data() { |
| 35 | + return { |
| 36 | + dialogVisible: false, |
| 37 | + listObj: {}, |
| 38 | + fileList: [] |
| 39 | + } |
| 40 | + }, |
| 41 | + methods: { |
| 42 | + checkAllSuccess() { |
| 43 | + return Object.keys(this.listObj).every(item => this.listObj[item].hasSuccess) |
| 44 | + }, |
| 45 | + handleSubmit() { |
| 46 | + const arr = Object.keys(this.listObj).map(v => this.listObj[v]) |
| 47 | + if (!this.checkAllSuccess()) { |
| 48 | + this.$message('请等待所有图片上传成功 或 出现了网络问题,请刷新页面重新上传!') |
| 49 | + return |
| 50 | + } |
| 51 | + console.log(arr) |
| 52 | + this.$emit('successCBK', arr) |
| 53 | + this.listObj = {} |
| 54 | + this.fileList = [] |
| 55 | + this.dialogVisible = false |
| 56 | + }, |
| 57 | + handleSuccess(response, file) { |
| 58 | + const uid = file.uid |
| 59 | + const objKeyArr = Object.keys(this.listObj) |
| 60 | + for (let i = 0, len = objKeyArr.length; i < len; i++) { |
| 61 | + if (this.listObj[objKeyArr[i]].uid === uid) { |
| 62 | + this.listObj[objKeyArr[i]].url = response.files.file |
| 63 | + this.listObj[objKeyArr[i]].hasSuccess = true |
| 64 | + return |
| 65 | + } |
| 66 | + } |
| 67 | + }, |
| 68 | + handleRemove(file) { |
| 69 | + const uid = file.uid |
| 70 | + const objKeyArr = Object.keys(this.listObj) |
| 71 | + for (let i = 0, len = objKeyArr.length; i < len; i++) { |
| 72 | + if (this.listObj[objKeyArr[i]].uid === uid) { |
| 73 | + delete this.listObj[objKeyArr[i]] |
| 74 | + return |
| 75 | + } |
| 76 | + } |
| 77 | + }, |
| 78 | + beforeUpload(file) { |
| 79 | + const _self = this |
| 80 | + const _URL = window.URL || window.webkitURL |
| 81 | + const fileName = file.uid |
| 82 | + this.listObj[fileName] = {} |
| 83 | + return new Promise((resolve, reject) => { |
| 84 | + const img = new Image() |
| 85 | + img.src = _URL.createObjectURL(file) |
| 86 | + img.onload = function() { |
| 87 | + _self.listObj[fileName] = { hasSuccess: false, uid: file.uid, width: this.width, height: this.height } |
| 88 | + } |
| 89 | + resolve(true) |
| 90 | + }) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +</script> |
| 95 | + |
| 96 | +<style rel="stylesheet/scss" lang="scss" scoped> |
| 97 | + .upload-container { |
| 98 | + .editor-slide-upload { |
| 99 | + margin-bottom: 20px; |
| 100 | + } |
| 101 | + } |
| 102 | +</style> |
0 commit comments