Jacob
1 天以前 106fc09e267f67bb35a279ed1bf71068a61e73c4
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
250
251
252
253
254
255
256
257
258
259
260
<template>
   <view>
       <view class="upload">
           <block v-for="(upload,index) in uploads" :key="index">
               <view class="uplode-file">
                   <image v-if="types == 'image'" class="uploade-img" :src="upload" :data-src="upload" @tap="previewImage"></image>
                   <image v-if="types == 'image'" class="clear-one-icon" :src="clearIcon" @tap="delImage(index)"></image>
                   <video v-if="types == 'video'" class="uploade-img" :src="upload" controls>
                       <cover-image v-if="types == 'video'" class="clear-one-icon" :src="clearIcon" @tap="delImage(index)"></cover-image>
                   </video>
               </view>
           </block>
           <view class="uploader-input-box" v-if="uploads.length < uploadCount">
               <view class="uploader-input" @tap="chooseUploads"></view>
           </view>
       </view>    
       <button type="primary" v-if="types == 'image'" @tap="upload">上传</button>
   </view>
</template>
 
<script>
   export default{
       props: {
           types: {
               type: String,
               default: 'image'
           },
           dataList: {
               type: Array,
               default: function() {
                   return []
               }
           },
           clearIcon: {
               type: String,
               default: 'http://img1.imgtn.bdimg.com/it/u=451604666,2295832001&fm=26&gp=0.jpg'
           },
           uploadUrl: {
               type: String,
               default: ''
           },
           deleteUrl: {
               type: String,
               default: ''
           },
           uploadCount: {
               type: Number,
               default: 1
           },
           //上传图片大小 默认3M
           upload_max: {
               type: Number,
               default: 3
           }
       },
       data(){
           return {
               //上传的图片地址
               uploadImages: [],
               //展示的图片地址
               uploads: [],
               // 超出限制数组
               exceeded_list: [],
           }
       },
       watch:{
           dataList() {
               this.uploads = this.dataList
           }
       },
       methods:{
           previewImage (e) {
               var current = e.target.dataset.src
               uni.previewImage({
                   current: current,
                   urls: this.dataList
               })
           },
           chooseUploads(){
               switch (this.types){
                   case 'image': 
                       uni.chooseImage({
                           count: this.uploadCount - this.uploads.length, //默认9
                           sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
                           sourceType: ['album', 'camera'], //从相册选择
                           success: (res) => {
                               for(let i = 0; i< res.tempFiles.length; i++){
                                   if(Math.ceil(res.tempFiles[i].size / 1024) < this.upload_max * 1024){
                                       this.uploads.push(res.tempFiles[i].path)
                                       this.uploadImages.push(res.tempFiles[i].path)
                                   }else {
                                       this.exceeded_list.push(i === 0 ? 1 : i + 1);
                                       uni.showModal({
                                           title: '提示',
                                           content: `第${[...new Set(this.exceeded_list)].join(',')}张图片超出限制${this.upload_max}MB,已过滤`
                                       });
                                   }
                               }
                           },
                           fail: (err) => {
                               uni.showModal({
                                   content: JSON.stringify(err)
                               });
                           }
                       });
                   break;
                   case 'video' :
                       uni.chooseVideo({
                           sourceType: ['camera', 'album'],
                           success: (res) => {
                               if(Math.ceil(res.size / 1024) < this.upload_max * 1024){
                                   this.uploads.push(res.tempFilePath)
                                   uni.uploadFile({
                                       url: this.uploadUrl, //仅为示例,非真实的接口地址
                                       filePath: res.tempFilePath,
                                       name: 'file',
                                       //请求参数
                                       formData: {
                                           'user': 'test'
                                       },
                                       success: (uploadFileRes) => {
                                           this.$emit('successVideo',uploadFileRes)
                                       }
                                   });
                               }else {
                                   uni.showModal({
                                       title: '提示',
                                       content: `第${[...new Set(this.exceeded_list)].join(',')}张视频超出限制${this.upload_max}MB,已过滤`
                                   });
                               }
                           },
                           fail: (err) => {
                               uni.showModal({
                                   content: JSON.stringify(err)
                               });
                           }
                       });
                   break;
               }
           },
           delImage(index){
               if(this.uploads[index].substring(0,4) !== 'http'){
                   this.uploads.splice(index,1)
                   return;
               };
               if(!this.deleteUrl) {
                   uni.showModal({
                       content: '请填写删除接口'
                   });
                   return;
               };
               uni.request({
                   url: this.deleteUrl,
                   method: 'DELETE',
                   data: {
                       image: this.dataList[index]
                   },
                   success: res => {
                       this.uploads.splice(index,1)
                   },
               });
           },
           upload(){
               if(!this.uploadUrl) {
                   uni.showModal({
                       content: '请填写上传接口'
                   });
                   return;
               };
               for (let i of this.uploadImages) {
                   uni.uploadFile({
                       url: this.uploadUrl, //仅为示例,非真实的接口地址
                       filePath: i,
                       name: 'file',
                       //请求参数
                       formData: {
                           'user': 'test'
                       },
                       success: (uploadFileRes) => {
                           this.$emit('successImage',uploadFileRes)
                       }
                   });
               }
           }
       }
   }
</script>
 
<style scoped>
   .upload {
       display: flex;
       flex-direction: row;
       flex-wrap: wrap;
   }
   .uplode-file {
       margin: 10upx;
       width: 210upx;
       height: 210upx;
       position: relative;
   }
   .uploade-img {
       display: block;
       width: 210upx;
       height: 210upx;
   }
   .clear-one{
       position: absolute;
       top: -10rpx;
       right: 0;
   }
   .clear-one-icon{
       position: absolute;
       width: 20px;
       height: 20px;
       top: 0;
       right: 0;
       z-index: 9;
   }
   .uploader-input-box {
       position: relative;
       margin:10upx;
       width: 208upx;
       height: 208upx;
       border: 2upx solid #D9D9D9;
   }
   .uploader-input-box:before,
   .uploader-input-box:after {
       content: " ";
       position: absolute;
       top: 50%;
       left: 50%;
       -webkit-transform: translate(-50%, -50%);
       transform: translate(-50%, -50%);
       background-color: #D9D9D9;
   }
   .uploader-input-box:before {
       width: 4upx;
       height: 79upx;
   }
   .uploader-input-box:after {
       width: 79upx;
       height: 4upx;
   }
   .uploader-input-box:active {
       border-color: #999999;
   }
   .uploader-input-box:active:before,
   .uploader-input-box:active:after {
       background-color: #999999;
   }
   .uploader-input {
       position: absolute;
       z-index: 1;
       top: 0;
       left: 0;
       width: 100%;
       height: 100%;
       opacity: 0;
   }
</style>