Blame view

components/robby-image-upload/robby-image-upload.vue 10.6 KB
c7add6cf   “wangming”   初始版本开发完毕
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
  <template>

  	<view class="imageUploadContainer">

  		<view class="imageUploadList">

  			<view class="imageItem" v-bind:key="index" v-for="(path,index) in imageListData">

  				<image :src="BASE_URL+path.url" :class="{'dragging':isDragging(index)}" draggable="true"

  					@tap="previewImage" :data-index="index" @touchstart="start" @touchmove.stop.prevent="move"

  					@touchend="stop"></image>

  				<view v-if="isShowDel" class="imageDel" @tap="deleteImage" :data-index="index">x</view>

  			</view>

  			<view v-if="isShowAdd" class="imageUpload" style="border: 0px;" @tap="selectImage">

  				<image src="/static/voucher_bg.png" class="imageUpload" style="border: 0px;"></image>

  			</view>

  		</view>

  		<image v-if="showMoveImage" class="moveImage" :style="{left:posMoveImageLeft, top:posMoveImageTop}"

  			:src="moveImagePath"></image>

  	</view>

  </template>

  

  <script>

  	import BASE_URL from '@/common/config.js'

  	export default {

  

  		name: 'robby-image-upload',

  		props: ['value', 'enableDel', 'enableAdd', 'enableDrag', 'serverUrl', 'formData', 'header', 'limit', 'fileKeyName',

  			'showUploadProgress', 'serverUrlDeleteImage'

  		],

  		data() {

  			return {

  				BASE_URL,

  				imageBasePos: {

  					x0: -1,

  					y0: -1,

  					w: -1,

  					h: -1,

  				},

  				showMoveImage: false,

  				moveImagePath: '',

  				moveLeft: 0,

  				moveTop: 0,

  				deltaLeft: 0,

  				deltaTop: 0,

  				dragIndex: null,

  				targetImageIndex: null,

  				imageList: [],

  				isDestroyed: false

  			}

  		},

  		mounted: function() {

  			this.imageList = this.value

  

  			if (this.showUploadProgress === false) {

  				this.showUploadProgress = false

  			} else {

  				this.showUploadProgress = true

  			}

  		},

  		destroyed: function() {

  			this.isDestroyed = true

  		},

  		computed: {

  			imageListData: function() {

  				if (this.value) {

  					return this.value

  				}

  			},

  			posMoveImageLeft: function() {

  				return this.moveLeft + 'px'

  			},

  			posMoveImageTop: function() {

  				return this.moveTop + 'px'

  			},

  			isShowDel: function() {

  				if (this.enableDel === false) {

  					return false

  				} else {

  					return true

  				}

  			},

  			isShowAdd: function() {

  				if (this.enableAdd === false) {

  					return false

  				}

  

  				if (this.limit && this.imageList.length >= this.limit) {

  					return false

  				}

  

  				return true

  			},

  			isDragable: function() {

  				if (this.enableDrag === false) {

  					return false

  				} else {

  					return true

  				}

  			}

  		},

  		methods: {

  			selectImage: function() {

  				var _self = this

  				if (!_self.imageList) {

  					_self.imageList = []

  				}

  

  				uni.chooseImage({

  					count: _self.limit ? (_self.limit - _self.imageList.length) : 999,

  					success: function(e) {

  						var imagePathArr = e.tempFilePaths

  

  						//如果设置了limit限制,在web上count参数无效,这里做判断控制选择的数量是否合要求

  						//在非微信小程序里,虽然可以选多张,但选择的结果会被截掉

  						//在app里,会自动做选择数量的限制

  						if (_self.limit) {

  							var availableImageNumber = _self.limit - _self.imageList.length

  							if (availableImageNumber < imagePathArr.length) {

  								uni.showToast({

  									title: '图片总数限制为' + _self.limit + '张,当前还可以选' +

  										availableImageNumber + '张',

  									icon: 'none',

  									mask: false,

  									duration: 2000

  								});

  								return

  							}

  						}

  

  						//检查服务器地址是否设置,设置即表示图片要上传到服务器

  						if (_self.serverUrl) {

  							uni.showToast({

  								title: '上传进度:0/' + imagePathArr.length,

  								icon: 'none',

  								mask: false

  							});

  

  							var remoteIndexStart = _self.imageList.length - imagePathArr.length

  							var promiseWorkList = []

  							var keyname = (_self.fileKeyName ? _self.fileKeyName : 'upload-images')

  							var completeImages = 0

  

  							for (let i = 0; i < imagePathArr.length; i++) {

  								promiseWorkList.push(new Promise((resolve, reject) => {

  									let remoteUrlIndex = remoteIndexStart + i

  									uni.uploadFile({

  										url: _self.serverUrl,

  										fileType: 'image',

  										header: _self.header,

  										formData: _self.formData,

  										filePath: imagePathArr[i],

  										name: keyname,

  										success: function(res) {

  											if (res.statusCode === 200) {

  												if (_self.isDestroyed) {

  													return

  												}

  

  												completeImages++

  

  												if (_self.showUploadProgress) {

  													uni.showToast({

  														title: '上传进度:' +

  															completeImages +

  															'/' +

  															imagePathArr

  															.length,

  														icon: 'none',

  														mask: false,

  														duration: 500

  													});

  												}

  												console.log(

  													'success to upload image: ' +

  													res.data)

  												resolve(res.data)

  											} else {

  												console.log(

  													'fail to upload image:' +

  													res.data)

  												reject('fail to upload image:' +

  													remoteUrlIndex)

  											}

  										},

  										fail: function(res) {

  											console.log('fail to upload image:' +

  												res)

  											reject('fail to upload image:' +

  												remoteUrlIndex)

  										}

  									})

  								}))

  							}

  							Promise.all(promiseWorkList).then((result) => {

  								if (_self.isDestroyed) {

  									return

  								}

  

  								for (let i = 0; i < result.length; i++) {

  									_self.imageList.push(JSON.parse(result[i]).data)

  								}

  

  								_self.$emit('add', {

  									currentImages: imagePathArr,

  									allImages: _self.imageList

  								})

  								_self.$emit('input', _self.imageList)

  							})

  						} else {

  							for (let i = 0; i < imagePathArr.length; i++) {

  								_self.imageList.push(imagePathArr[i])

  							}

  

  							_self.$emit('add', {

  								currentImages: imagePathArr,

  								allImages: _self.imageList

  							})

  							_self.$emit('input', _self.imageList)

  						}

  					}

  				})

  			},

  			deleteImage: function(e) {

  				var imageIndex = e.currentTarget.dataset.index

  				var deletedImagePath = this.imageList[imageIndex]

  				this.imageList.splice(imageIndex, 1)

  

  				//检查删除图片的服务器地址是否设置,如果设置则调用API,在服务器端删除该图片

  				if (this.serverUrlDeleteImage) {

  					uni.request({

  						url: this.serverUrlDeleteImage,

  						method: 'GET',

  						data: {

  							imagePath: deletedImagePath

  						},

  						success: res => {

  							console.log(res.data)

  						}

  					});

  				}

  

  				this.$emit('delete', {

  					currentImage: deletedImagePath,

  					allImages: this.imageList

  				})

  				this.$emit('input', this.imageList)

  			},

  			previewImage: function(e) {

  				var imageIndex = e.currentTarget.dataset.index

  				uni.previewImage({

  					current: this.imageList[imageIndex],

  					indicator: "number",

  					loop: "true",

  					urls: this.imageList

  				})

  			},

  			initImageBasePos: function() {

  				let paddingRate = 0.024

  				var _self = this

  				//计算图片基准位置

  				uni.getSystemInfo({

  					success: function(obj) {

  						let screenWidth = obj.screenWidth

  						let leftPadding = Math.ceil(paddingRate * screenWidth)

  						let imageWidth = Math.ceil((screenWidth - 2 * leftPadding) / 4)

  

  						_self.imageBasePos.x0 = leftPadding

  						_self.imageBasePos.w = imageWidth

  						_self.imageBasePos.h = imageWidth

  					}

  				})

  			},

  			findOverlapImage: function(posX, posY) {

  				let rows = Math.floor((posX - this.imageBasePos.x0) / this.imageBasePos.w)

  				let cols = Math.floor((posY - this.imageBasePos.y0) / this.imageBasePos.h)

  				let indx = cols * 4 + rows

  				return indx

  			},

  			isDragging: function(indx) {

  				return this.dragIndex === indx

  			},

  			start: function(e) {

  				console.log(this.isDragable)

  				if (!this.isDragable) {

  					return

  				}

  				this.dragIndex = e.currentTarget.dataset.index

  				this.moveImagePath = this.imageList[this.dragIndex]

  				this.showMoveImage = true

  

  				//计算纵向图片基准位置

  				if (this.imageBasePos.y0 === -1) {

  					this.initImageBasePos()

  

  					let basePosY = Math.floor(this.dragIndex / 4) * this.imageBasePos.h

  					let currentImageOffsetTop = e.currentTarget.offsetTop

  					this.imageBasePos.y0 = currentImageOffsetTop - basePosY

  				}

  

  				//设置选中图片当前左上角的坐标

  				this.moveLeft = e.target.offsetLeft

  				this.moveTop = e.target.offsetTop

  			},

  			move: function(e) {

  				if (!this.isDragable) {

  					return

  				}

  				const touch = e.touches[0]

  				this.targetImageIndex = this.findOverlapImage(touch.clientX, touch.clientY)

  

  				//初始化deltaLeft/deltaTop

  				if (this.deltaLeft === 0) {

  					this.deltaLeft = touch.clientX - this.moveLeft

  					this.deltaTop = touch.clientY - this.moveTop

  				}

  

  				//设置移动图片位置

  				this.moveLeft = touch.clientX - this.deltaLeft

  				this.moveTop = touch.clientY - this.deltaTop

  			},

  			stop: function(e) {

  				if (!this.isDragable) {

  					return

  				}

  				if (this.dragIndex !== null && this.targetImageIndex !== null) {

  					if (this.targetImageIndex < 0) {

  						this.targetImageIndex = 0

  					}

  

  					if (this.targetImageIndex >= this.imageList.length) {

  						this.targetImageIndex = this.imageList.length - 1

  					}

  					//交换图片

  					if (this.dragIndex !== this.targetImageIndex) {

  						this.imageList[this.dragIndex] = this.imageList[this.targetImageIndex]

  						this.imageList[this.targetImageIndex] = this.moveImagePath

  					}

  				}

  

  				this.dragIndex = null

  				this.targetImageIndex = null

  				this.deltaLeft = 0

  				this.deltaTop = 0

  				this.showMoveImage = false

  

  				this.$emit('input', this.imageList)

  			}

  		}

  	}

  </script>

  

  <style>

  	.imageUploadContainer {

  		padding: 10upx 5upx;

  		margin: 10upx 5upx;

  	}

  	.dragging {

  		transform: scale(1.2)

  	}

  	.imageUploadList {

  		display: flex;

  		flex-wrap: wrap;

  	}

  	.imageItem, .imageUpload {

  		width: 160rpx;

  		height: 160rpx;

  		border-radius: 10rpx;

  		margin-right: 20rpx;

  	}

  	.imageDel {

  		position: relative;

  		left: 120upx;

  		bottom: 165upx;

  		background-color: rgba(0, 0, 0, 0.5);

  		width: 36upx;

  		text-align: center;

  		line-height: 35upx;

  		border-radius: 17upx;

  		color: white;

  		font-size: 30upx;

  		padding-bottom: 2upx;

  	}

  	.imageItem image, .moveImage {

  		width: 160upx;

  		height: 160upx;

  		border-radius: 8upx;

  	}

  	.imageUpload {

  		line-height: 130upx;

  		text-align: center;

  		font-size: 150upx;

  		color: #D9D9D9;

  		border: 1px solid #D9D9D9;

  		border-radius: 8upx;

  	}

  	.moveImage {

  		position: absolute;

  	}

  </style>