share-with-system.uvue 13.9 KB
Newer Older
M
mahaifeng 已提交
1 2
<template>
	<!-- #ifdef APP -->
M
mahaifeng 已提交
3
	<scroll-view direction="vertical" style="flex:1;">
M
mahaifeng 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
	<!-- #endif -->
		<view id="viewshot">
			<button class="button" @click="shareText()">分享文本</button>
			<button class="button" @click="shareLink()">分享链接</button>
			<button class="button" @click="sharePrivateImg()">分享单个本地图片</button>
			<button class="button" @click="sharePrivateImgs()">分享多个本地图片</button>
			<button class="button" @click="shareAll()">分享链接、文本、一张图片</button>
			<button class="button" @click="sharePrivateVideo()">分享video文件(单个)</button>
			<button class="button" @click="sharePrivateVideos()">分享video文件(多个)</button>
			<button class="button" @click="sharePrivateAudio()">分享Audio文件(单个)</button>
			<button class="button" @click="sharePrivateAudios()">分享Audio文件(多个)</button>
			<button class="button" @click="sharePrivateFile()">分享文件(单个)</button>
			<button class="button" @click="sharePrivateFiles()">分享文件(多个)</button>
M
mahaifeng 已提交
17 18 19 20 21 22 23 24 25
      <button class="button" @click="sharePubImg()">选择图片并分享</button>
      <button class="button" @click="sharePubMedias()">选择video并分享</button>
      <button class="button" @click="shareSnapShot()">指定view截图并分享</button>

			<button class="button" type="warn" @click="sharePrivateErrorImg()">分享单个本地图片(错误路径)</button>
			<button class="button" type="warn" @click="sharePrivateErrorImgs()">分享多个本地图片(含有错误路径)</button>
			<button class="button" type="warn" @click="sharePrivateErrorVideos()">分享Video文件(错误路径)</button>
			<button class="button" type="warn" @click="sharePrivateErrorAudios()">分享Audio文件(错误路径)</button>
			<button class="button" type="warn" @click="sharePrivateErrorFiles()">分享文件(错误路径)</button>
M
mahaifeng 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38


		</view>
	</scroll-view>
</template>

<script>
	export default {
		data() {
			return {
				summary: '欢迎使用hello uniapp-x'
			}
		},
zhaofengliang920817's avatar
zhaofengliang920817 已提交
39
		methods: {
zhaofengliang920817's avatar
zhaofengliang920817 已提交
40
			sharePrivateErrorAudios() {
zhaofengliang920817's avatar
zhaofengliang920817 已提交
41
				uni.hideToast()
zhaofengliang920817's avatar
zhaofengliang920817 已提交
42
				const path1 : string = "/static/test-audio/ForElise.mp3";
zhaofengliang920817's avatar
zhaofengliang920817 已提交
43 44 45
				const path2 : string = "/static/test-audio/ForElise.mp32";

				uni.shareWithSystem({
zhaofengliang920817's avatar
zhaofengliang920817 已提交
46
					audioPaths: [path1, path2],
M
mahaifeng 已提交
47
					type:'audio',
M
mahaifeng 已提交
48
					success() {
zhaofengliang920817's avatar
zhaofengliang920817 已提交
49 50 51 52 53 54 55 56 57
						console.log('Shared----------------------------success')
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
58
					complete(_) {
zhaofengliang920817's avatar
zhaofengliang920817 已提交
59 60 61 62

					}
				})

M
mahaifeng 已提交
63 64 65 66 67 68 69
			},
			sharePrivateErrorFiles() {
				uni.hideToast()
				const path1 : string = "/static/filemanager1/to.zip";
				const path2 : string = "/static/filemanager/11.txt.br";

				uni.shareWithSystem({
M
mahaifeng 已提交
70
					filePaths: [path1, path2],
M
mahaifeng 已提交
71
					type:'file',
M
mahaifeng 已提交
72
					success(_) {
M
mahaifeng 已提交
73 74 75 76 77 78 79 80 81
						console.log('Shared----------------------------success')
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
82
					complete(_) {
M
mahaifeng 已提交
83 84 85 86 87 88 89 90 91

					}
				})
			},
			sharePrivateFile() {
				uni.hideToast()
				const path1 : string = "/static/filemanager/to.zip";

				uni.shareWithSystem({
M
mahaifeng 已提交
92
					filePaths: [path1],
M
mahaifeng 已提交
93
					type:'file',
M
mahaifeng 已提交
94
					success(_) {
M
mahaifeng 已提交
95 96 97 98 99 100 101 102 103
						console.log('Shared----------------------------success')
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
104
					complete(_) {
M
mahaifeng 已提交
105 106 107 108 109 110 111 112 113 114

					}
				})
			},
			sharePrivateFiles() {
				uni.hideToast()
				const path1 : string = "/static/filemanager/to.zip";
				const path2 : string = "/static/filemanager/1.txt.br";

				uni.shareWithSystem({
M
mahaifeng 已提交
115
					filePaths: [path1, path2],
M
mahaifeng 已提交
116
					type:'file',
M
mahaifeng 已提交
117
					success(_) {
M
mahaifeng 已提交
118 119 120 121 122 123 124 125 126
						console.log('Shared----------------------------success')
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
127
					complete(_) {
M
mahaifeng 已提交
128 129 130 131 132 133 134 135 136

					}
				})
			},
			sharePrivateAudio() {
				uni.hideToast()
				const path1 : string = "/static/test-audio/ForElise.mp3";

				uni.shareWithSystem({
M
mahaifeng 已提交
137
					audioPaths: [path1],
M
mahaifeng 已提交
138
					type:'audio',
M
mahaifeng 已提交
139
					success(_) {
M
mahaifeng 已提交
140 141 142 143 144 145 146 147 148
						console.log('Shared----------------------------success')
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
149
					complete(_) {
M
mahaifeng 已提交
150 151 152 153 154 155 156 157 158 159

					}
				})
			},
			sharePrivateAudios() {
				uni.hideToast()
				const path1 : string = "/static/test-audio/ForElise.mp3";
				const path2 : string = "/static/test-audio/ForElise.mp3";

				uni.shareWithSystem({
M
mahaifeng 已提交
160
					audioPaths: [path1, path2],
M
mahaifeng 已提交
161
					type:'audio',
M
mahaifeng 已提交
162
					success(_) {
M
mahaifeng 已提交
163 164 165 166 167 168 169 170 171
						console.log('Shared----------------------------success')
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
172
					complete(_) {
M
mahaifeng 已提交
173 174 175 176 177 178 179 180 181 182

					}
				})
			},
			sharePrivateErrorVideos() {
				uni.hideToast()
				const path1 : string = "/static/test-video/10second-demo.mp4";
				const path2 : string = "/static/test-video/10second-demo1.mp4";

				uni.shareWithSystem({
M
mahaifeng 已提交
183
					videoPaths: [path1, path2],
M
mahaifeng 已提交
184
					type:'video',
M
mahaifeng 已提交
185
					success(_) {
M
mahaifeng 已提交
186 187 188 189 190 191 192 193 194
						console.log('Shared----------------------------success')
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
195
					complete(_) {
M
mahaifeng 已提交
196 197 198 199 200 201 202 203 204

					}
				})

			},
			sharePrivateVideo() {
				uni.hideToast()
				const path1 : string = "/static/test-video/10second-demo.mp4";
				uni.shareWithSystem({
M
mahaifeng 已提交
205
					videoPaths: [path1],
M
mahaifeng 已提交
206
					type:'video',
M
mahaifeng 已提交
207
					success(_) {
M
mahaifeng 已提交
208 209 210 211 212 213 214 215 216
						console.log('Shared----------------------------success')
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
217
					complete(_) {
M
mahaifeng 已提交
218 219 220 221 222 223 224 225 226 227 228

					}
				})

			},
			sharePrivateVideos() {
				uni.hideToast()
				const path1 : string = "/static/test-video/10second-demo.mp4";
				const path2 : string = "/static/test-video/10second-demo.mp4";

				uni.shareWithSystem({
M
mahaifeng 已提交
229
					videoPaths: [path1, path2],
M
mahaifeng 已提交
230
					type:'video',
M
mahaifeng 已提交
231
					success(_) {
M
mahaifeng 已提交
232 233 234 235 236 237 238 239 240
						console.log('Shared----------------------------success')
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
241
					complete(_) {
M
mahaifeng 已提交
242 243 244 245 246 247 248 249 250 251 252

					}
				})

			},
			sharePubMedias() {
				uni.hideToast()

				uni.chooseVideo({
					success(res) {
						uni.shareWithSystem({
M
mahaifeng 已提交
253
							videoPaths: [res.tempFilePath],
M
mahaifeng 已提交
254
							type:'video',
M
mahaifeng 已提交
255
							success(_) {
M
mahaifeng 已提交
256 257 258 259 260 261 262 263 264
								console.log('Shared----------------------------success')
							},
							fail(res) {
								console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
								uni.showToast({
									icon: "error",
									title: "errorCode=" + res.errCode
								})
							},
M
mahaifeng 已提交
265
							complete(_) {
M
mahaifeng 已提交
266 267 268 269 270 271 272 273 274 275

							}
						})
					}

				})
			},
			shareText() {
				uni.hideToast()
				uni.shareWithSystem({
M
mahaifeng 已提交
276
					summary: this.summary,
M
mahaifeng 已提交
277
					type:'text',
M
mahaifeng 已提交
278
					success(_) {
M
mahaifeng 已提交
279 280 281 282 283 284 285 286 287
						console.log('Shared----------------------------success')
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
288
					complete(_) {
M
mahaifeng 已提交
289 290 291 292 293 294 295 296 297

					}
				})
			},
			shareLink() {
				uni.hideToast()
				uni.shareWithSystem({
					type:'text',
					href: 'https://uniapp.dcloud.io',
M
mahaifeng 已提交
298
					success(_) {
M
mahaifeng 已提交
299 300 301 302 303 304 305 306 307
						console.log('Shared----------------------------success')
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
308
					complete(_) {
M
mahaifeng 已提交
309 310 311 312 313 314 315

					}
				})
			},
			sharePrivateImg() {
				uni.hideToast()
				const imageSrc : string = "/static/test-image/logo.gif";
M
mahaifeng 已提交
316
				uni.shareWithSystem({
M
mahaifeng 已提交
317 318
					type:'image',
					imageUrl: imageSrc,
M
mahaifeng 已提交
319
					success(_) {
M
mahaifeng 已提交
320 321 322 323 324 325 326 327 328 329 330
						console.log('Shared----------------------------success')
						// 分享完成,请注意此时不一定是成功分享
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
						// 分享失败
					},
M
mahaifeng 已提交
331
					complete(_) {
M
mahaifeng 已提交
332 333 334 335 336 337 338

					}
				})
			},
			sharePrivateErrorImg() {
				uni.hideToast()
				const imageSrc : string = "/static/test-image/logo.jpg11";
M
mahaifeng 已提交
339 340
				uni.shareWithSystem({
					imageUrl: imageSrc,
M
mahaifeng 已提交
341
					type:'image',
M
mahaifeng 已提交
342
					success(_) {
M
mahaifeng 已提交
343 344 345 346 347 348 349 350 351 352 353
						console.log('Shared----------------------------success')
						// 分享完成,请注意此时不一定是成功分享
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
						// 分享失败
					},
M
mahaifeng 已提交
354
					complete(_) {
M
mahaifeng 已提交
355 356 357 358 359 360 361 362 363

					}
				})
			},
			sharePrivateImgs() {
				uni.hideToast()
				const errorImageSrc1 : string = "/static/test-image/logo.gif";
				const errorImageSrc2 : string = "/static/test-image/logo.png";
				const imageSrc : string = "/static/test-image/logo.jpg";
M
mahaifeng 已提交
364
				let imageUrlList : string[] = new Array()
M
mahaifeng 已提交
365 366 367 368 369
				imageUrlList.push(errorImageSrc2)
				imageUrlList.push(imageSrc)
				// imageUrlList.push(errorImageSrc1)

				uni.shareWithSystem({
M
mahaifeng 已提交
370
					imagePaths: imageUrlList,
M
mahaifeng 已提交
371
					type:'image',
M
mahaifeng 已提交
372
					success(_) {
M
mahaifeng 已提交
373 374 375 376 377 378 379 380 381 382
						console.log('Shared----------------------------success')
						// 分享完成,请注意此时不一定是成功分享
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
383
					complete(_) {
M
mahaifeng 已提交
384 385 386 387 388 389 390 391 392

					}
				})
			},
			sharePrivateErrorImgs() {
				uni.hideToast()
				const errorImageSrc1 : string = "/static/test-image/logo.jpg1";
				const errorImageSrc2 : string = "/static/test-image/logo.jpg3";
				const imageSrc : string = "/static/test-image/logo.jpg";
M
mahaifeng 已提交
393
				let imageUrlList : string[] =new Array()
M
mahaifeng 已提交
394 395 396 397
				imageUrlList.push(imageSrc)
				imageUrlList.push(errorImageSrc1)
				imageUrlList.push(errorImageSrc2)
				uni.shareWithSystem({
M
mahaifeng 已提交
398
					imagePaths: imageUrlList,
M
mahaifeng 已提交
399
					type:'image',
M
mahaifeng 已提交
400
					success(_) {
M
mahaifeng 已提交
401 402 403 404 405 406 407 408 409 410
						console.log('Shared----------------------------success')
						// 分享完成,请注意此时不一定是成功分享
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
411
					complete(_) {
M
mahaifeng 已提交
412 413 414 415 416 417 418

					}
				})
			},
			shareAll() {
				uni.hideToast()
				const imageSrc : string = "/static/test-video/fast-forward.png";
M
mahaifeng 已提交
419
				let imageUrlList : string[] = new Array()
M
mahaifeng 已提交
420 421 422 423
				imageUrlList.push(imageSrc)
				uni.shareWithSystem({
					summary: this.summary,
					href: 'https://uniapp.dcloud.io',
M
mahaifeng 已提交
424
					imagePaths: imageUrlList,
M
mahaifeng 已提交
425
					type:'image',
M
mahaifeng 已提交
426
					success(_) {
M
mahaifeng 已提交
427 428 429 430 431 432 433 434 435 436
						console.log('Shared----------------------------success')
						// 分享完成,请注意此时不一定是成功分享
					},
					fail(res) {
						console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)
						uni.showToast({
							icon: "error",
							title: "errorCode=" + res.errCode
						})
					},
M
mahaifeng 已提交
437
					complete(_) {
M
mahaifeng 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

					}
				})
			},
			sharePubImg() {
				uni.hideToast()
				uni.chooseImage({
					count: 3,
					sourceType: ['camera', 'album'],
					success(e) {
						console.log(e)
						console.log(JSON.stringify(e))
						uni.shareWithSystem({
							// summary: "aa",
							// href: 'https://uniapp.dcloud.io',
M
mahaifeng 已提交
453
							imagePaths: e.tempFilePaths,
M
mahaifeng 已提交
454
							type:'image',
M
mahaifeng 已提交
455
							success(_) {
M
mahaifeng 已提交
456 457 458 459 460 461 462 463 464 465 466
								console.log('Shared----------------------------success')
								// 分享完成,请注意此时不一定是成功分享
							},
							fail(res) {
								console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)

								uni.showToast({
									icon: "error",
									title: "errorCode=" + res.errCode
								})
							},
M
mahaifeng 已提交
467
							complete(_) {
M
mahaifeng 已提交
468 469 470 471 472 473 474 475 476 477 478 479

							}
						})
					}
				})
			},
			shareSnapShot() {
				uni.hideToast()
				uni.getElementById("viewshot")?.takeSnapshot(
					{
						success: function (res) {
							uni.shareWithSystem({
M
mahaifeng 已提交
480
								imageUrl: res.tempFilePath,
M
mahaifeng 已提交
481
								type:'image',
M
mahaifeng 已提交
482
								success(_) {
M
mahaifeng 已提交
483 484 485 486 487 488 489 490 491 492 493
									console.log('Shared----------------------------success')
									// 分享完成,请注意此时不一定是成功分享
								},
								fail(res) {
									console.log('Share failed, ' + "res.errCode =" + res.errCode + '---res.errMsg= ' + res.errMsg)

									uni.showToast({
										icon: "error",
										title: "errorCode=" + res.errCode
									})
								},
M
mahaifeng 已提交
494
								complete(_) {
M
mahaifeng 已提交
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

								}
							})

						},
						fail: function (res) {
							console.log(res)
							uni.showToast({
								icon: 'error',
								title: '截图失败'
							})
						}
					}
				)
			},

		}
	}
</script>

<style>
	.button {
		margin-left: 30px;
		margin-right: 30px;
		margin-bottom: 15px;
	}
W
wanganxp 已提交
521
</style>