vue-element-admin/src/components/Upload/singleImage3.vue

146 lines
3.1 KiB
Vue
Raw Normal View History

2017-04-18 07:09:13 +00:00
<template>
2017-07-06 09:56:17 +00:00
<div class="upload-container">
<el-upload class="image-uploader" :data="dataObj" drag :multiple="false" :show-file-list="false" action="https://httpbin.org/post"
:on-success="handleImageScucess">
<i class="el-icon-upload"></i>
<div class="el-upload__text">将文件拖到此处<em>点击上传</em></div>
</el-upload>
<div class="image-preview image-app-preview">
<div class="image-preview-wrapper" v-show="imageUrl.length>1">
<img :src="imageUrl">
<div class="image-preview-action">
<i @click="rmImage" class="el-icon-delete"></i>
</div>
</div>
</div>
<div class="image-preview">
<div class="image-preview-wrapper" v-show="imageUrl.length>1">
<img :src="imageUrl">
<div class="image-preview-action">
<i @click="rmImage" class="el-icon-delete"></i>
</div>
</div>
</div>
</div>
2017-04-18 07:09:13 +00:00
</template>
2017-07-06 09:56:17 +00:00
2017-04-18 07:09:13 +00:00
<script>
2017-08-28 05:12:44 +00:00
import { getToken } from '@/api/qiniu'
2017-08-22 07:43:34 +00:00
export default {
2017-08-28 05:12:44 +00:00
name: 'singleImageUpload',
props: {
value: String
2017-07-06 09:56:17 +00:00
},
2017-08-28 05:12:44 +00:00
computed: {
imageUrl() {
return this.value
2017-07-06 09:56:17 +00:00
}
},
2017-08-28 05:12:44 +00:00
data() {
return {
tempUrl: '',
dataObj: { token: '', key: '' }
2017-08-22 07:43:34 +00:00
}
2017-07-06 09:56:17 +00:00
},
2017-08-28 05:12:44 +00:00
methods: {
rmImage() {
this.emitInput('')
2017-07-06 09:56:17 +00:00
},
2017-08-28 05:12:44 +00:00
emitInput(val) {
this.$emit('input', val)
2017-07-06 09:56:17 +00:00
},
2017-08-28 05:12:44 +00:00
handleImageScucess(file) {
this.emitInput(file.files.file)
2017-07-06 09:56:17 +00:00
},
2017-08-28 05:12:44 +00:00
beforeUpload() {
const _self = this
2017-08-22 07:43:34 +00:00
return new Promise((resolve, reject) => {
2017-08-28 05:12:44 +00:00
getToken().then(response => {
const key = response.data.qiniu_key
2017-08-22 07:43:34 +00:00
const token = response.data.qiniu_token
_self._data.dataObj.token = token
_self._data.dataObj.key = key
this.tempUrl = response.data.qiniu_url
resolve(true)
2017-07-06 09:56:17 +00:00
}).catch(err => {
2017-08-28 05:12:44 +00:00
console.log(err)
2017-08-22 07:43:34 +00:00
reject(false)
})
})
2017-07-06 09:56:17 +00:00
}
}
2017-08-28 05:12:44 +00:00
}
2017-04-18 07:09:13 +00:00
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
2017-07-06 09:56:17 +00:00
@import "src/styles/mixin.scss";
.upload-container {
width: 100%;
position: relative;
@include clearfix;
.image-uploader {
width: 35%;
float: left;
}
.image-preview {
width: 200px;
height: 200px;
position: relative;
border: 1px dashed #d9d9d9;
float: left;
margin-left: 50px;
.image-preview-wrapper {
position: relative;
width: 100%;
height: 100%;
img {
width: 100%;
height: 100%;
}
}
.image-preview-action {
position: absolute;
width: 100%;
height: 100%;
left: 0;
top: 0;
cursor: default;
text-align: center;
color: #fff;
opacity: 0;
font-size: 20px;
background-color: rgba(0, 0, 0, .5);
transition: opacity .3s;
cursor: pointer;
text-align: center;
line-height: 200px;
.el-icon-delete {
font-size: 36px;
}
}
&:hover {
.image-preview-action {
opacity: 1;
}
}
}
.image-app-preview {
width: 320px;
height: 180px;
position: relative;
border: 1px dashed #d9d9d9;
float: left;
margin-left: 50px;
.app-fake-conver {
height: 44px;
position: absolute;
width: 100%; // background: rgba(0, 0, 0, .1);
text-align: center;
line-height: 64px;
color: #fff;
}
}
}
2017-04-18 07:09:13 +00:00
</style>