share-with-system.uvue 14.0 KB
Newer Older
M
mahaifeng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<template>
	<!-- #ifdef APP -->
	<scroll-view direction="vertical" style="flex:1; margin-bottom: 50px;">
	<!-- #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>

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

			<button class="button" type="warn" @click="sharePubImg()">选择图片并分享</button>
			<button class="button" type="warn" @click="sharePubMedias()">选择video并分享</button>
			<button class="button" type="warn" @click="shareSnapShot()">指定view截图并分享</button>
		</view>
	</scroll-view>
</template>

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

				uni.shareWithSystem({
zhaofengliang920817's avatar
zhaofengliang920817 已提交
45
					audioPaths: [path1, path2],
M
mahaifeng 已提交
46
					type:'audio',
M
mahaifeng 已提交
47
					success() {
zhaofengliang920817's avatar
zhaofengliang920817 已提交
48 49 50 51 52 53 54 55 56
						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 已提交
57
					complete(_) {
zhaofengliang920817's avatar
zhaofengliang920817 已提交
58 59 60 61

					}
				})

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

				uni.shareWithSystem({
M
mahaifeng 已提交
69
					filePaths: [path1, path2],
M
mahaifeng 已提交
70
					type:'file',
M
mahaifeng 已提交
71
					success(_) {
M
mahaifeng 已提交
72 73 74 75 76 77 78 79 80
						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 已提交
81
					complete(_) {
M
mahaifeng 已提交
82 83 84 85 86 87 88 89 90

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

				uni.shareWithSystem({
M
mahaifeng 已提交
91
					filePaths: [path1],
M
mahaifeng 已提交
92
					type:'file',
M
mahaifeng 已提交
93
					success(_) {
M
mahaifeng 已提交
94 95 96 97 98 99 100 101 102
						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 已提交
103
					complete(_) {
M
mahaifeng 已提交
104 105 106 107 108 109 110 111 112 113

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

				uni.shareWithSystem({
M
mahaifeng 已提交
114
					filePaths: [path1, path2],
M
mahaifeng 已提交
115
					type:'file',
M
mahaifeng 已提交
116
					success(_) {
M
mahaifeng 已提交
117 118 119 120 121 122 123 124 125
						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 已提交
126
					complete(_) {
M
mahaifeng 已提交
127 128 129 130 131 132 133 134 135

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

				uni.shareWithSystem({
M
mahaifeng 已提交
136
					audioPaths: [path1],
M
mahaifeng 已提交
137
					type:'audio',
M
mahaifeng 已提交
138
					success(_) {
M
mahaifeng 已提交
139 140 141 142 143 144 145 146 147
						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 已提交
148
					complete(_) {
M
mahaifeng 已提交
149 150 151 152 153 154 155 156 157 158

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

				uni.shareWithSystem({
M
mahaifeng 已提交
159
					audioPaths: [path1, path2],
M
mahaifeng 已提交
160
					type:'audio',
M
mahaifeng 已提交
161
					success(_) {
M
mahaifeng 已提交
162 163 164 165 166 167 168 169 170
						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 已提交
171
					complete(_) {
M
mahaifeng 已提交
172 173 174 175 176 177 178 179 180 181

					}
				})
			},
			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 已提交
182
					videoPaths: [path1, path2],
M
mahaifeng 已提交
183
					type:'video',
M
mahaifeng 已提交
184
					success(_) {
M
mahaifeng 已提交
185 186 187 188 189 190 191 192 193
						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 已提交
194
					complete(_) {
M
mahaifeng 已提交
195 196 197 198 199 200 201 202 203

					}
				})

			},
			sharePrivateVideo() {
				uni.hideToast()
				const path1 : string = "/static/test-video/10second-demo.mp4";
				uni.shareWithSystem({
M
mahaifeng 已提交
204
					videoPaths: [path1],
M
mahaifeng 已提交
205
					type:'video',
M
mahaifeng 已提交
206
					success(_) {
M
mahaifeng 已提交
207 208 209 210 211 212 213 214 215
						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 已提交
216
					complete(_) {
M
mahaifeng 已提交
217 218 219 220 221 222 223 224 225 226 227

					}
				})

			},
			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 已提交
228
					videoPaths: [path1, path2],
M
mahaifeng 已提交
229
					type:'video',
M
mahaifeng 已提交
230
					success(_) {
M
mahaifeng 已提交
231 232 233 234 235 236 237 238 239
						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 已提交
240
					complete(_) {
M
mahaifeng 已提交
241 242 243 244 245 246 247 248 249 250 251

					}
				})

			},
			sharePubMedias() {
				uni.hideToast()

				uni.chooseVideo({
					success(res) {
						uni.shareWithSystem({
M
mahaifeng 已提交
252
							videoPaths: [res.tempFilePath],
M
mahaifeng 已提交
253
							type:'video',
M
mahaifeng 已提交
254
							success(_) {
M
mahaifeng 已提交
255 256 257 258 259 260 261 262 263
								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 已提交
264
							complete(_) {
M
mahaifeng 已提交
265 266 267 268 269 270 271 272 273 274

							}
						})
					}

				})
			},
			shareText() {
				uni.hideToast()
				uni.shareWithSystem({
M
mahaifeng 已提交
275
					summary: this.summary,
M
mahaifeng 已提交
276
					type:'text',
M
mahaifeng 已提交
277
					success(_) {
M
mahaifeng 已提交
278 279 280 281 282 283 284 285 286
						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 已提交
287
					complete(_) {
M
mahaifeng 已提交
288 289 290 291 292 293 294 295 296

					}
				})
			},
			shareLink() {
				uni.hideToast()
				uni.shareWithSystem({
					type:'text',
					href: 'https://uniapp.dcloud.io',
M
mahaifeng 已提交
297
					success(_) {
M
mahaifeng 已提交
298 299 300 301 302 303 304 305 306
						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 已提交
307
					complete(_) {
M
mahaifeng 已提交
308 309 310 311 312 313 314

					}
				})
			},
			sharePrivateImg() {
				uni.hideToast()
				const imageSrc : string = "/static/test-image/logo.gif";
M
mahaifeng 已提交
315
				uni.shareWithSystem({
M
mahaifeng 已提交
316 317
					type:'image',
					imageUrl: imageSrc,
M
mahaifeng 已提交
318
					success(_) {
M
mahaifeng 已提交
319 320 321 322 323 324 325 326 327 328 329
						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 已提交
330
					complete(_) {
M
mahaifeng 已提交
331 332 333 334 335 336 337

					}
				})
			},
			sharePrivateErrorImg() {
				uni.hideToast()
				const imageSrc : string = "/static/test-image/logo.jpg11";
M
mahaifeng 已提交
338 339
				uni.shareWithSystem({
					imageUrl: imageSrc,
M
mahaifeng 已提交
340
					type:'image',
M
mahaifeng 已提交
341
					success(_) {
M
mahaifeng 已提交
342 343 344 345 346 347 348 349 350 351 352
						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 已提交
353
					complete(_) {
M
mahaifeng 已提交
354 355 356 357 358 359 360 361 362

					}
				})
			},
			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 已提交
363
				let imageUrlList : string[] = new Array()
M
mahaifeng 已提交
364 365 366 367 368
				imageUrlList.push(errorImageSrc2)
				imageUrlList.push(imageSrc)
				// imageUrlList.push(errorImageSrc1)

				uni.shareWithSystem({
M
mahaifeng 已提交
369
					imagePaths: imageUrlList,
M
mahaifeng 已提交
370
					type:'image',
M
mahaifeng 已提交
371
					success(_) {
M
mahaifeng 已提交
372 373 374 375 376 377 378 379 380 381
						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 已提交
382
					complete(_) {
M
mahaifeng 已提交
383 384 385 386 387 388 389 390 391

					}
				})
			},
			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 已提交
392
				let imageUrlList : string[] =new Array()
M
mahaifeng 已提交
393 394 395 396
				imageUrlList.push(imageSrc)
				imageUrlList.push(errorImageSrc1)
				imageUrlList.push(errorImageSrc2)
				uni.shareWithSystem({
M
mahaifeng 已提交
397
					imagePaths: imageUrlList,
M
mahaifeng 已提交
398
					type:'image',
M
mahaifeng 已提交
399
					success(_) {
M
mahaifeng 已提交
400 401 402 403 404 405 406 407 408 409
						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 已提交
410
					complete(_) {
M
mahaifeng 已提交
411 412 413 414 415 416 417

					}
				})
			},
			shareAll() {
				uni.hideToast()
				const imageSrc : string = "/static/test-video/fast-forward.png";
M
mahaifeng 已提交
418
				let imageUrlList : string[] = new Array()
M
mahaifeng 已提交
419 420 421 422
				imageUrlList.push(imageSrc)
				uni.shareWithSystem({
					summary: this.summary,
					href: 'https://uniapp.dcloud.io',
M
mahaifeng 已提交
423
					imagePaths: imageUrlList,
M
mahaifeng 已提交
424
					type:'image',
M
mahaifeng 已提交
425
					success(_) {
M
mahaifeng 已提交
426 427 428 429 430 431 432 433 434 435
						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 已提交
436
					complete(_) {
M
mahaifeng 已提交
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451

					}
				})
			},
			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 已提交
452
							imagePaths: e.tempFilePaths,
M
mahaifeng 已提交
453
							type:'image',
M
mahaifeng 已提交
454
							success(_) {
M
mahaifeng 已提交
455 456 457 458 459 460 461 462 463 464 465
								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 已提交
466
							complete(_) {
M
mahaifeng 已提交
467 468 469 470 471 472 473 474 475 476 477 478

							}
						})
					}
				})
			},
			shareSnapShot() {
				uni.hideToast()
				uni.getElementById("viewshot")?.takeSnapshot(
					{
						success: function (res) {
							uni.shareWithSystem({
M
mahaifeng 已提交
479
								imageUrl: res.tempFilePath,
M
mahaifeng 已提交
480
								type:'image',
M
mahaifeng 已提交
481
								success(_) {
M
mahaifeng 已提交
482 483 484 485 486 487 488 489 490 491 492
									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 已提交
493
								complete(_) {
M
mahaifeng 已提交
494 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

								}
							})

						},
						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 已提交
520
</style>