share.uvue 2.8 KB
Newer Older
W
wanganxp 已提交
1
<template>
M
mahaifeng 已提交
2 3 4 5 6 7
	<view id="viewshot">
		<button class="button" @click="shareText()">分享文本</button>
		<button class="button" @click="sharePrivateImg()">分享私有目录图片</button>
		<button class="button" @click="sharePubImg()">选择图片并分享</button>
		<button class="button" @click="shareSnapShot()">指定view截图并分享</button>
	</view>
W
wanganxp 已提交
8 9 10
</template>

<script>
M
mahaifeng 已提交
11 12 13 14 15 16 17 18 19
	import Intent from 'android.content.Intent';
	import File from 'java.io.File';
	export default {
		data() {
			return {
				summary: '欢迎使用hello uniapp-x'
			}
		},
		methods: {
W
wanganxp 已提交
20

M
mahaifeng 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33
			shareText() {
				uni.shareWithSystem({
					summary: this.summary,
					href: 'https://uniapp.dcloud.io',
					success(res) {
						console.log('success')
						// 分享完成,请注意此时不一定是成功分享
					},
					fail(res) {
						console.log('fail')
						// 分享失败
					},
					complete(res) {
W
wanganxp 已提交
34

M
mahaifeng 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
					}
				} )
			},
			sharePrivateImg() {
				const imageSrc : string = "/static/logo.png";
				uni.shareWithSystem({
					summary: this.summary,
					href: 'https://uniapp.dcloud.io',
					imageUrl:imageSrc,
					success(res) {
						console.log('success')
						// 分享完成,请注意此时不一定是成功分享
					},
					fail(res) {
						console.log('fail')
						// 分享失败
					},
					complete(res) {
53

M
mahaifeng 已提交
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
					}
				} )
			},
			sharePubImg() {
				uni.chooseImage({
					count: 3,
					success(e) {
						console.log(JSON.stringify(e))
						// const imageSrc : string = "/static/logo.png";
						// var imageList = new UTSArray<string>()
						// imageList.push(imageSrc)
						// imageList.push(imageSrc)
						uni.shareWithSystem({
							summary: this.summary,
							href: 'https://uniapp.dcloud.io',
							imageUrlList:e.tempFilePaths,
							success(res) {
								console.log('success')
								// 分享完成,请注意此时不一定是成功分享
							},
							fail(res) {
								console.log('fail')
								// 分享失败
							},
							complete(res) {

							}
						} )
					}
				})
			},
			shareSnapShot() {
				uni.getElementById("viewshot")?.takeSnapshot(
					{
						success: function (res) {
							// 打印截图文件临时路径
							console.log(res.tempFilePath)
							const context = UTSAndroid.getUniActivity()!;
							const intent = new Intent(Intent.ACTION_SEND)
							intent.setType("image/*");
							let file = new File(res.tempFilePath)
							const uri = UTSAndroid.getFileProviderUri(file) //3.99支持
							intent.putExtra(Intent.EXTRA_STREAM, uri);
							context.startActivity(Intent.createChooser(intent, "分享到"));
						},
						fail: function (res) {
							console.log(res)
							uni.showToast({
								icon: 'error',
								title: '截图失败'
							})
						}
					}
				)
			},

		}
	}
W
wanganxp 已提交
112 113 114
</script>

<style>
M
mahaifeng 已提交
115 116 117
	.button {
		margin: 15px;
	}
W
wanganxp 已提交
118
</style>