zhanghua
2025-04-14 829f5116884f98643ffc5b2a548a600d40c0cedb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<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>