gaoshp
2024-06-11 e87012567c674cd69f7a8f87df7202eac60a8208
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<template>
    <div class="sc-upload-multiple">
        <el-upload ref="uploader" list-type="picture-card"
            :auto-upload="autoUpload"
            :disabled="disabled"
            :action="action"
            :name="name"
            :data="data"
            :http-request="request"
            v-model:file-list="defaultFileList"
            :show-file-list="showFileList"
            :accept="accept"
            :multiple="multiple"
            :limit="limit"
            :before-upload="before"
            :on-success="success"
            :on-error="error"
            :on-preview="handlePreview"
            :on-exceed="handleExceed">
            <slot>
                <el-icon><el-icon-plus/></el-icon>
            </slot>
            <template #tip>
                <div v-if="tip" class="el-upload__tip">{{tip}}</div>
            </template>
            <template #file="{ file }">
                <div class="sc-upload-list-item">
                    <el-image class="el-upload-list__item-thumbnail" :src="file.url" fit="cover" :preview-src-list="preview" :initial-index="preview.findIndex(n=>n==file.url)" hide-on-click-modal append-to-body :z-index="9999">
                        <template #placeholder>
                            <div class="sc-upload-multiple-image-slot">
                                Loading...
                            </div>
                        </template>
                    </el-image>
                    <div v-if="!disabled && file.status=='success'" class="sc-upload__item-actions">
                        <span class="del" @click="handleRemove(file)"><el-icon><el-icon-delete /></el-icon></span>
                    </div>
                    <div v-if="file.status=='ready' || file.status=='uploading'" class="sc-upload__item-progress">
                        <el-progress :percentage="file.percentage" :text-inside="true" :stroke-width="16"/>
                    </div>
                </div>
            </template>
        </el-upload>
        <span style="display:none!important"><el-input v-model="value"></el-input></span>
    </div>
</template>
 
<script>
    import config from "@/config/upload"
    import Sortable from 'sortablejs'
 
    export default {
        props: {
            modelValue: { type: [String, Array], default: "" },
            tip: { type: String, default: "" },
            action: { type: String, default: "" },
            apiObj: { type: Object, default: () => {} },
            name: { type: String, default: config.filename },
            data: { type: Object, default: () => {} },
            accept: { type: String, default: "image/gif, image/jpeg, image/png" },
            maxSize: { type: Number, default: config.maxSizeFile },
            limit: { type: Number, default: 0 },
            autoUpload: { type: Boolean, default: true },
            showFileList: { type: Boolean, default: true },
            multiple: { type: Boolean, default: true },
            disabled: { type: Boolean, default: false },
            draggable: { type: Boolean, default: false },
            onSuccess: { type: Function, default: () => { return true } }
        },
        data(){
            return {
                value: "",
                defaultFileList: []
            }
        },
        watch:{
            modelValue(val){
                if(Array.isArray(val)){
                    if (JSON.stringify(val) != JSON.stringify(this.formatArr(this.defaultFileList))) {
                        this.defaultFileList = val
                        this.value = val
                    }
                }else{
                    if (val != this.toStr(this.defaultFileList)) {
                        this.defaultFileList = this.toArr(val)
                        this.value = val
                    }
                }
            },
            defaultFileList: {
                handler(val){
                    this.$emit('update:modelValue', Array.isArray(this.modelValue) ? this.formatArr(val) : this.toStr(val))
                    this.value = this.toStr(val)
                },
                deep: true
            }
        },
        computed: {
            preview(){
                return this.defaultFileList.map(v => v.url)
            }
        },
        mounted() {
            this.defaultFileList = Array.isArray(this.modelValue) ? this.modelValue : this.toArr(this.modelValue)
            this.value = this.modelValue
            if(!this.disabled && this.draggable){
                this.rowDrop()
            }
        },
        methods: {
            //默认值转换为数组
            toArr(str){
                var _arr = [];
                var arr = str.split(",");
                arr.forEach(item => {
                    if(item){
                        var urlArr = item.split('/');
                        var fileName = urlArr[urlArr.length - 1]
                        _arr.push({
                            name: fileName,
                            url: item
                        })
                    }
                })
                return _arr;
            },
            //数组转换为原始值
            toStr(arr){
                return arr.map(v => v.url).join(",")
            },
            //格式化数组值
            formatArr(arr){
                var _arr = []
                arr.forEach(item => {
                    if(item){
                        _arr.push({
                            name: item.name,
                            url: item.url
                        })
                    }
                })
                return _arr
            },
            //拖拽
            rowDrop(){
                const _this = this
                const itemBox = this.$refs.uploader.$el.querySelector('.el-upload-list')
                Sortable.create(itemBox, {
                    handle: ".el-upload-list__item",
                    animation: 200,
                    ghostClass: "ghost",
                    onEnd({ newIndex, oldIndex }) {
                        const tableData = _this.defaultFileList
                        const currRow = tableData.splice(oldIndex, 1)[0]
                        tableData.splice(newIndex, 0, currRow)
                    }
                })
            },
            before(file){
                if(!['image/jpeg','image/png','image/gif'].includes(file.type)){
                    this.$message.warning(`选择的文件类型 ${file.type} 非图像类文件`);
                    return false;
                }
                const maxSize = file.size / 1024 / 1024 < this.maxSize;
                if (!maxSize) {
                    this.$message.warning(`上传文件大小不能超过 ${this.maxSize}MB!`);
                    return false;
                }
            },
            success(res, file){
                var os = this.onSuccess(res, file)
                if(os!=undefined && os==false){
                    return false
                }
                var response = config.parseData(res)
                file.name = response.fileName
                file.url = response.src
            },
            error(err){
                this.$notify.error({
                    title: '上传文件未成功',
                    message: err
                })
            },
            beforeRemove(uploadFile){
                return this.$confirm(`是否移除 ${uploadFile.name} ?`, '提示', {
                    type: 'warning',
                }).then(() => {
                    return true
                }).catch(() => {
                    return false
                })
            },
            handleRemove(file){
                this.$refs.uploader.handleRemove(file)
                //this.defaultFileList.splice(this.defaultFileList.findIndex(item => item.uid===file.uid), 1)
            },
            handleExceed(){
                this.$message.warning(`当前设置最多上传 ${this.limit} 个文件,请移除后上传!`)
            },
            handlePreview(uploadFile){
                window.open(uploadFile.url)
            },
            request(param){
                var apiObj = config.apiObj;
                if(this.apiObj){
                    apiObj = this.apiObj;
                }
                const data = new FormData();
                data.append(param.filename, param.file);
                for (const key in param.data) {
                    data.append(key, param.data[key]);
                }
                apiObj.post(data, {
                    onUploadProgress: e => {
                        const complete = parseInt(((e.loaded / e.total) * 100) | 0, 10)
                        param.onProgress({percent: complete})
                    }
                }).then(res => {
                    var response = config.parseData(res);
                    if(response.code == config.successCode){
                        param.onSuccess(res)
                    }else{
                        param.onError(response.msg || "未知错误")
                    }
                }).catch(err => {
                    param.onError(err)
                })
            }
        }
    }
</script>
 
<style scoped>
    .el-form-item.is-error .sc-upload-multiple:deep(.el-upload--picture-card) {border-color: var(--el-color-danger);}
    :deep(.el-upload-list__item) {transition:none;border-radius: 0;}
    .sc-upload-multiple:deep(.el-upload-list__item.el-list-leave-active) {position: static!important;}
    .sc-upload-multiple:deep(.el-upload--picture-card) {border-radius: 0;}
    .sc-upload-list-item {width: 100%;height: 100%;position: relative;}
    .sc-upload-multiple .el-image {display: block;}
    .sc-upload-multiple .el-image:deep(img) {-webkit-user-drag: none;}
    .sc-upload-multiple-image-slot {display: flex;justify-content: center;align-items: center;width: 100%;height: 100%;font-size: 12px;}
    .sc-upload-multiple .el-upload-list__item:hover .sc-upload__item-actions{display: block;}
    .sc-upload__item-actions {position: absolute;top:0;right: 0;display: none;}
    .sc-upload__item-actions span {display: flex;justify-content: center;align-items: center;;width: 25px;height:25px;cursor: pointer;color: #fff;}
    .sc-upload__item-actions span i {font-size: 12px;}
    .sc-upload__item-actions .del {background: #F56C6C;}
    .sc-upload__item-progress {position: absolute;width: 100%;height: 100%;top: 0;left: 0;background-color: var(--el-overlay-color-lighter);}
</style>