UploadItem.vue
2.41 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
<template>
<div>
<div class="img-container" v-for="(item,index) in fileList" :key="index">
<img :src="item.worksUrl" />
<i class="van-icon van-icon-delete" @click="deleteImageHandler(index)"></i>
</div>
<van-uploader
ref="vanUploader"
class="uploader"
:after-read="uploadSumit"
v-if="!fileList || fileList.length < 3"
>+</van-uploader>
</div>
</template>
<script>
import { request } from "@/api/fetch-api.js";
import Vue from "vue";
import { Uploader } from "vant";
Vue.use(Uploader);
/**
* 外层插件可通过监听:v-on:before-upload;v-on:after-upload两个事件监听上传结果
* after-upload(success, src)
*/
export default {
props: ["value"],
data() {
return {
fileList: this.value,
uploadUrl: "https://api.k.wxpai.cn/bizproxy/kdapi/file/upload",
};
},
methods: {
uploadSumit(params) {
console.log(params);
let data = {
path: "/pro/jiajiaChildrenHost/works",
file: params.file
};
request
.form(this.uploadUrl, data)
.then(result => {
let uploadResult = {
worksUrl: result.data.content,
worksType: params.file.type.indexOf("image") >= 0 ? "pic" : "video"
};
if (!this.fileList) {
this.fileList = [];
}
this.fileList.push(uploadResult);
this.$emit("input", this.fileList);
})
.catch(res => {});
},
deleteImageHandler(index) {
this.fileList.splice(index, 1);
}
},
computed: {
currentValue: function() {
return this.value;
}
}
};
</script>
<style lang="scss" scoped>
.uploader {
border: 1px solid #82acae;
border-radius: 6px;
position: relative;
overflow: hidden;
padding: 10px;
background-color: #addfe1;
width: 120px;
min-height: 120px;
font-size: 40px;
line-height: 120px;
text-align: center;
font-weight: bold;
}
.img-container {
border: 1px solid #82acae;
border-radius: 6px;
position: relative;
overflow: hidden;
padding: 10px;
background-color: #addfe1;
width: 120px;
text-align: center;
font-weight: bold;
margin-bottom: 10px;
img {
width: 100%;
}
.van-icon {
background-color: rgba($color: #000000, $alpha: 0.3);
position: absolute;
right: 10px;
bottom: 5px;
font-size: 40px;
}
}
.avatar {
max-width: 120px;
max-height: 120px;
display: block;
}
</style>