jiangping
2024-05-09 b62578f8b43f15be89b3d14f463f11aed03e6fb9
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<template>
    <div class="file">
        <div class="file_list">
            <div class="file_list_item" :style="{width: width, height: height}" v-for="(item, index) in list" :key="index">
                <div class="dele" @click="deleItem(index)">
                    <i class="el-icon-close"></i>
                </div>
                <img :src="item.url" v-if="fileType(item.url) === 'img'" />
                <video controls autoplay :src="item.url" v-else></video>
            </div>
            <div class="file_list_item" :style="{width: width, height: height, cursor: 'pointer'}" @click="$refs.file.click()">
                <i class="el-icon-plus"></i>
            </div>
        </div>
        <input type="file" ref="file" :accept="accept" @change="getFile" />
    </div>
</template>
 
<script>
    import axios from 'axios';
    export default {
        props: {
            width: {
                type: String,
                default: '90px'
            },
            height: {
                type: String,
                default: '90px'
            },
            list: {
                type: Array,
                default: []
            },
            accept: {
                type: String,
                default: ''
            },
            folder: {
                type: String,
                default: ''
            }
        },
        data() {
            return {
                uploadImgUrl: process.env.VUE_APP_API_PREFIX + '/public/upload'
            }
        },
 
        methods: {
            fileType(url) {
                if (url.indexOf('.mp4') !== -1) {
                    return 'video'
                } else {
                    return 'img'
                }
            },
            getFile(e) {
                if (e.target && e.target.files.length > 0) {
                    this.$emit('loading')
                    const formdate = new FormData()
                    formdate.append('file', e.target.files[0])
                    formdate.append('folder', this.folder)
                    axios.post(this.uploadImgUrl, formdate)
                        .then(res => {
                            this.$emit('success', res.data.data)
                        })
                        .catch(e => {
                            this.$message.error(e)
                        })
                        .finally(() => {
                            this.$refs.file.value = null
                        })
                }
            },
            deleItem(index) {
                this.$emit('dele', index)
            }
        }
    }
</script>
 
<style lang="scss" scoped>
    .file {
        /*width: 100%;*/
        /*height: 90px;*/
        margin: 10px 0;
        input {
            opacity: 0;
        }
        .file_list {
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            .file_list_item {
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
                overflow: hidden;
                border-radius: 5px;
                border: 1px solid #d5d5d5;
                margin-left: 15px;
                position: relative;
                &:first-child {
                    margin: 0 !important;
                }
                .dele {
                    position: absolute;
                    right: 0;
                    top: 0;
                    width: 20px;
                    height: 20px;
                    background: red;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    cursor: pointer;
                    .el-icon-close {
                        color: #ffffff;
                        font-size: 19px;
                    }
                }
                .el-icon-plus {
                    font-size: 30px;
                    color: black;
                }
                img {
                    width: 100%;
                }
                video {
                    width: 100%;
                    height: 100%;
                }
            }
        }
    }
</style>