fileUploader.vue
3.4 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
<template>
<view>
<view class="dz-from-fj">
<view class="dz-from-fj-edit" @tap="uploadBtn()">
<view class="iconfont icon-jia"></view>
<view>上传图片</view>
</view>
<view class="dz-from-fj-edit" v-for="(item,index) in setArr" :key="index">
<block v-if="item[0] == 'image'">
<image :src='item[1]' class="dz-from-fj-img-img"></image>
<view class="dz-from-jian">
<view class="iconfont icon-jia" @tap="deleteImg(index)"></view>
</view>
</block>
</view>
</view>
<uni-popup ref="popup" type="center">图片大小不能超过{{size}}M</uni-popup>
</view>
</template>
<script>
import uniPopup from "@/components/uni-popup/uni-popup.vue";
import { mapState } from 'vuex';
import $ from 'jquery'
export default {
components: {
uniPopup
},
props: ["limitsize","formData"],
data() {
return {
Arr:[],
imgArr:[],
};
},
onShow(){
},
// 计算属性
computed: {
size: function() {
return this.limitsize / 1024;
},
...mapState([
"userInfo",
]),
setArr(){
this.imgArr=[];
for (let i = 0, leng = this.Arr.length; i < leng; i++) {
this.imgArr.push(this.Arr[i][1])
}
this.$emit('get-img',this.imgArr)
return this.Arr
}
},
methods: {
deleteImg(index){
this.Arr.splice(index,1)
},
openPopup() {
this.$refs.popup.open()
},
closePopup() {
this.$refs.popup.close()
},
// 上传附件按钮 绑定file的点击事件
uploadBtn() {
let that = this
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
success: function(res) {
let size = res.tempFiles[0].size;
let tempFilePaths = res.tempFilePaths;
console.log(size)
if (size > that.limitsize*1024) {
that.openPopup();
return
}
const uploadTask = uni.uploadFile({
url:that.$lib.$config.api + that.$lib.$urlMap.upload, //
filePath: tempFilePaths[0],
name: "file",
fileType:'image',
formData: {
"token": that.userInfo.token
},
success: function(res) {
let data = JSON.parse(res.data);
if (data.code == "1") {
that.Arr.push(['image',data.data.fullurl]);
}
},
fail: function (err) {
console.log(err)
}
});
}
});
},
}
}
</script>
<style>
.dz-from-fj{
display: flex;
flex-wrap: wrap;
}
.dz-from-fj-edit{
width: 80px;
border: solid 1px #efefef;
display: flex;
flex-direction: column;
align-items: center;
height: 80px;
color: #adadad;
font-size: 14px;
margin-right: 10px;
justify-content: center;
position: relative;
margin-bottom:10px
}
.dz-from-fj-edit .iconfont{
font-size:24px;
margin-bottom:1px
}
.dz-from-fj-img-img{
width: 80px;
height: 80px;
}
.dz-from-wj{
color: #448aff;
}
.dz-from-jian{
position: absolute;
right: 2px;
top: 2px;
}
.dz-from-jian .iconfont{
color:rgb(255, 255, 255);
font-size:11px;
transform:rotate(45deg);
border-radius:50%;
padding:3px;
background:rgb(68, 138, 255);
}
.file {
font-size: 32upx;
}
.filename {
color: #808080;
}
.del {
color: #0E90CE;
float: right;
}
.btn {
width: 100%;
margin: 20upx 0;
color: #007AFF;
background-color: #FFFFFF;
border: #007AFF 2upx solid;
}
</style>