customUploadMulti.vue
4.82 KB
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
<template>
<div>
<el-input clearable v-model.trim="uploadImgUrl" size="mini" @change="changeInput">
<template slot="append">
<i class="iconfont iconfolder-o" @click="dialogVisible = true"></i>
</template>
</el-input>
<!-- 弹窗 -->
<el-dialog title="图库" :visible.sync="dialogVisible" top="1vh">
<el-button size="medium" type="info" icon="el-icon-upload2" @click="uploadHandler">点击上传</el-button>
<input type="file" class="file" ref="files" @change="getImages" />
<div class="title">可选列表</div>
<div class="image-list">
<el-image class="image" v-for="item in fileList" :key="item.fileId" :src="item.urlPath" lazy
@click="imageClickHander(item)"></el-image>
</div>
<div class="title">已选列表</div>
<div class="image-selected-list">
<template v-for="item in selectedFileList">
<el-image class="image" :key="item.fileId" :src="item.urlPath"></el-image>
<i class="close el-icon-delete" @click="deleteFileHandler(item)"></i>
</template>
</div>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取 消</el-button>
<el-button type="primary" @click="confirm">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import { getFielList } from "@/api/file"
export default {
name: 'custom-upload-multi',
model: {
prop: "value",
event: "input"
},
props: {
value: {
type: "",
default: ""
}
},
data() {
return {
dialogVisible: false, // 弹窗状态
requestUrl: process.env.BASE_API + "/file/upload",
headers: {
Authorization: getToken()
},
fileList: [], // 图片列表
selectedFileList: [], // 选中的图片列表
uploadImgUrl: ""
};
},
created() {
this.uploadImgUrl = this.value;
this.getFielList();
},
methods: {
// 获取图片列表
async getFielList() {
const rs = await getFielList();
if (rs.code === 200 || rs.code === "200") {
let fileList = rs.data;
fileList.reverse();
this.fileList = fileList;
}
},
uploadHandler() {
this.$refs.files.click();
},
getImages(el) {
const file = el.target.files[0];
const type = file.type.split("/")[0];
if (type === "image") {
this.upload(file);
} else {
this.$message.warning("只能上传图片格式")
}
},
// 图片上传
upload(file) {
let that = this;
let formdata = new FormData();
formdata.append("file", file);
axios
.post(this.requestUrl, formdata, {
headers: that.headers
})
.then(response => {
let res = response.data;
if (res.code === "200") {
this.getFielList();
}
});
},
// 文本框url改变
changeInput() {
this.$emit("input", this.uploadImgUrl);
this.$emit("change", this.uploadImgUrl);
},
// 图片点击事件
imageClickHander(item) {
if (!this.selectedFileList.includes(item)) {
this.selectedFileList.push(item);
}
},
// 删除已选图片
deleteFileHandler(item) {
this.selectedFileList = this.selectedFileList.filter(value => {
return value.fileId !== item.fileId;
});
},
// 确定按钮
confirm() {
const urlList = this.selectedFileList.map(item => {return item.urlPath});
const urlPath = urlList.toString();
this.$emit("input", urlPath);
this.$emit("change", urlPath);
this.dialogVisible = false;
}
}
};
</script>
<style lang="scss" scoped>
.file {
display: none;
}
/deep/.el-input-group__append,
/deep/.el-input-group__prepend {
padding: 0 10px !important;
overflow: hidden;
}
.iconfont {
cursor: pointer;
font-size: 12px;
}
/deep/ .el-dialog .el-dialog__header,
/deep/ .el-dialog__body,
/deep/.el-dialog__footer {
background: #1b1e25;
}
.image-list {
margin-top: 20px;
height: 40vh;
overflow: auto;
&::-webkit-scrollbar {
/*滚动条整体样式*/
width: 8px;
/*高宽分别对应横竖滚动条的尺寸*/
height: 1px;
}
&::-webkit-scrollbar-thumb {
/*滚动条里面小方块*/
border-radius: 10px;
background: #535353;
}
&::-webkit-scrollbar-track-piece {
/*滚动条里面轨道*/
border-radius: 10px;
background: #1b1e25;
}
.image {
width: 20%;
padding: 0 0.5px 0 0.5px;
min-height: 90px;
/deep/ .el-image__error {
width: 100%;
height: 100%;
min-height: 90px;
}
}
}
.image-selected-list {
@extend .image-list;
height: 20vh;
position: relative;
.close {
color: #FFFFFF;
cursor: pointer;
}
}
.title {
color: #909399;
}
</style>