<template>
|
<el-upload :file-list="fileList" class="upload-demo" action="/sccg/file/medias" multiple
|
:show-file-list="flag" :before-upload="beforeUpload" :limit="50" :on-success="handleSuccess"
|
:headers="getToken()">
|
<div class="uploadBtn" v-if="picUrl===''">
|
<i class="el-icon-plus"></i>
|
<span>上传图片</span>
|
</div>
|
<MyImg :imgUrl="picUrl" width="105px" height="105px" radius="4px" v-else class="uploadBtn"></MyImg>
|
</el-upload>
|
</template>
|
<script>
|
import MyImg from '@/components/Img'
|
export default {
|
components: {
|
MyImg,
|
},
|
data() {
|
return {
|
fileList: [],
|
flag: false,
|
};
|
},
|
methods: {
|
beforeUpload(rawFile) {
|
if (rawFile.type !== 'image/png' && rawFile.type !== 'image/svg+xml' && rawFile.type !== 'image/jpg' && rawFile.type !== 'image/jpeg') {
|
this.$message.error('图片必须是 jpg/svg/jpeg/png 格式!')
|
return false
|
} else if (rawFile.size / 1024 / 1024 > 5) {
|
this.$message.error('上传图片不能超过 5MB!')
|
return false
|
}
|
return true
|
},
|
handleSuccess(res, file, filelist) {
|
this.$emit('getPicUrl', { obj: res.data, value: this.mykey });
|
},
|
getToken() {
|
const token = sessionStorage.getItem('token');
|
const tokenHead = sessionStorage.getItem('tokenHead');
|
if (token && tokenHead) {
|
return { Authorization: tokenHead + token }
|
}
|
}
|
},
|
watch:{
|
picUrl(newVal,oldVal){
|
console.log(newVal);
|
}
|
},
|
mounted() {
|
console.log(this.picUrl);
|
},
|
props: ['getPicUrl', 'mykey', 'picUrl']
|
}
|
</script>
|
<style lang="scss" scoped>
|
.upload-demo {
|
display: flex;
|
height: 120px;
|
}
|
|
.uploadBtn {
|
width: 120px;
|
height: 120px;
|
background-color: rgba(249, 249, 249, 1);
|
display: flex;
|
flex-direction: column;
|
justify-content: center;
|
align-items: center;
|
border-radius: 4px;
|
|
i {
|
font-size: 30px;
|
font-weight: 650;
|
}
|
|
span {
|
line-height: 22px;
|
}
|
}
|
</style>
|