uniapp分享组件

这是一个uni-app应用中实现分享功能的组件,包括微信、微博、QQ等社交平台。组件使用了uni.getProvider获取分享服务,并根据服务类型动态构建分享内容。当分享类型为图片时,会进行图片压缩以满足分享要求。同时,提供了显示和隐藏分享面板的逻辑,以及复制链接的功能。
摘要由CSDN通过智能技术生成
<template>
	<view>
		<!-- @touchmove.stop.prevent="()=>{}" 禁用页面滚动 -->
		<view v-show="isShow" @click="showHandler" class="mask" @touchmove.stop.prevent="()=>{}"></view>

		<view v-show="isShow" class="share-body">
			<scroll-view scroll-x="true" class="share-scrroll noScorll">
				<view @click="share(item)" class="share-item" v-for="(item ,index) in providerList" :key="index">
					<image :src="item.icon"></image>
					<view class="">
						{{item.name}}
					</view>
				</view>

			</scroll-view>
			
			<view class="share-cancel" @click="showHandler">
				取消
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		
				props: {
					shareData: Object // 分享的数据
				},
				
				watch: {
					shareData(newVal) {
						if(newVal.mainImage) this.image = newVal.mainImage
   
						if(newVal.title) this.title = newVal.title
						this.href = 'http://www.127.me'
					}
				},
		data() {
			return {
				isShow: false,
				title: 'share',
				shareText: 'uni-app可以同时发布成原生App、小程序、H5,邀请你一起体验!',
				href: 'http://www.127.me',
				image: 'https://gd4.alicdn.com/imgextra/i4/3603079088/O1CN01dczOSM2H0LvTowhkl_!!3603079088.png',
				shareType: 0, //分享类型 0图为让你 1 文字 2图片
				providerList: [] //提供商
			}
		},
		created: function() {
			uni.getProvider({  
				service: 'share',
				success: (e) => {
					// console.log('success', e);
					let data = []
					for (let i = 0; i < e.provider.length; i++) {
						switch (e.provider[i]) {
							case 'weixin':
								data.push({
									name: '微信好友',
									id: 'weixin',
									sort: 0,
									icon: "/static/share/weixin.png"
								})
								data.push({
									name: '微信朋友圈',
									id: 'weixin',
									type: 'WXSenceTimeline',
									sort: 1,
									icon: "/static/share/pengyouquan.png"
								})
								break;
							case 'sinaweibo':
								data.push({
									name: '新浪微博',
									id: 'sinaweibo',
									sort: 2,
									icon: "/static/share/weibo.png"
								})
								break;
							case 'qq':
								data.push({
									name: '分享到QQ',
									id: 'qq',
									sort: 3,
									icon: "/static/share/qq.png"
								})
								break;
							default:
								break;
						}
					}
					data.push({
						name: '复制链接',
						id: 'copy',
						sort: 4,
						icon: "/static/share/link.png"
					})
					this.providerList = data.sort((x, y) => {
						return x.sort - y.sort
					});
				},
				fail: (e) => {
					console.log('获取分享通道失败', e);
					uni.showModal({
						content: '获取分享通道失败',
						showCancel: false
					})
				}
			});
		},
		methods: {
			//下载图片文件

			downFile(url) {
				return new Promise((resolve) => {
					uni.downloadFile({
						url,
						success: (res) => {
							resolve(res.tempFilePath)
						},
					})
				})

			},
			//显示隐藏
			showHandler() {
				this.isShow = !this.isShow
			},
			
			async share(e) {
				//复制链接
				if(e.id==='copy'){
				
					uni.setClipboardData({
					    data: 'hello',
					    success:  () =>{
					      //隐藏默认提示
						  uni.hideToast()
						  this.$util.msg('已复制到剪贴版')
						  this.isShow=false
					    }
					});
					return
				}
				
				if(e.id ==='sinaweibo'){
						this.image = await this.compress();
							uni.hideLoading()
							this.isShow =false
							
				}
				console.log('分享通道:' + e.id + '; 分享类型:' + this.shareType);
				uni.showLoading()
				if (!this.shareText && (this.shareType === 1 || this.shareType === 0)) {
					uni.showModal({
						content: '分享内容不能为空',
						showCancel: false
					})
					return;
				}

				if (!this.image && (this.shareType === 2 || this.shareType === 0)) {
					uni.showModal({
						content: '分享图片不能为空',
						showCancel: false
					})
					return;
				}
				
				let shareOPtions = {
					provider: e.id,
					scene: e.type && e.type === 'WXSenceTimeline' ? 'WXSenceTimeline' :
					'WXSceneSession', //WXSceneSession”分享到聊天界面,“WXSenceTimeline”分享到朋友圈,“WXSceneFavorite”分享到微信收藏     
					type: this.shareType,
					success: (e) => {
						uni.hideLoading()
						console.log('success', e);
						this.isShow=false
						uni.showModal({
							content: '已分享',
							showCancel: false
						})
					},
					fail: (e) => {
						console.log('fail', e)
						uni.showToast({
							title:'已取消分享',
							icon:'none'
						})
					},
					complete: function() {
						console.log('分享操作结束!')
					}
				}
				switch (this.shareType) {
					case 0:
						shareOPtions.summary = this.shareText;
						shareOPtions.imageUrl = this.image;
						shareOPtions.title = this.title;
						shareOPtions.href = this.href;
						break;
					case 1:
						shareOPtions.summary = this.shareText;
						break;
					case 2:
						shareOPtions.imageUrl = this.image;
						break;
					case 5:
						shareOPtions.imageUrl = this.image ? this.image :
							'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/b6304f00-5168-11eb-bd01-97bc1429a9ff.png'
						shareOPtions.title = '欢迎体验uniapp';
						shareOPtions.miniProgram = {
							id: 'gh_33446d7f7a26',
							path: '/pages/tabBar/component/component',
							webUrl: 'https://uniapp.dcloud.io',
							type: 0
						};
						break;
					default:
						break;
				} 

				if (shareOPtions.type === 0 && plus.os.name === 'iOS') { //如果是图文分享,且是ios平台,则压缩图片 
					shareOPtions.imageUrl = await this.compress();
				}
				if (shareOPtions.type === 1 && shareOPtions.provider === 'qq') { //如果是分享文字到qq,则必须加上href和title
					shareOPtions.title = this.title;
					shareOPtions.href = this.href;
					shareOPtions.imageUrl=this.image;
					console.log(123456465465,this.image)
				}
				uni.share(shareOPtions);
				uni.hideLoading()
			},
			compress() { //压缩图片 图文分享要求分享图片大小不能超过20Kb
				console.log('开始压缩');
				let img = this.image;
				return new Promise(async (res) => {
					if (img.startsWith('http')) {
						//下载网络图片到本地 返回本地
						img = await this.downFile(img)
					}
					// var localPath = plus.io.convertAbsoluteFileSystem(img.replace('file://', ''));
					// console.log('after' + localPath);
					// 压缩size
					plus.io.resolveLocalFileSystemURL(img, (entry) => {
						entry.file((file) => { // 可通过entry对象操作图片 
							console.log('getFile:' + JSON.stringify(file));
							if (file.size > 20480) { // 压缩后size 大于20Kb
								plus.zip.compressImage({
									src: img,
									dst: img.replace('.jpg', '2222.jpg').replace(
										'.JPG',
										'2222.JPG'),
									width: '10%',
									height: '10%',
									quality: 1,
									overwrite: true
								}, (event) => {
									console.log('success zip****' + event.size);
									let newImg = img.replace('.jpg', '2222.jpg')
										.replace(
											'.JPG', '2222.JPG');
									res(newImg);
								}, function(error) {
									uni.showModal({
										content: '分享图片太大,需要请重新选择图片!',
										showCancel: false
									})
								});
							}
						});
					}, (e) => {
						console.log('Resolve file URL failed: ' + e.message);
						uni.showModal({
							content: '分享图片太大,需要请重新选择图片!',
							showCancel: false
						})
					});
				})
			}

		}
	}
</script>

<style lang="scss">
	.mask {
		z-index: 99;
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.6);
	}

	.share-body {
		border-radius: 15rpx 0 0 15rpx;
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 100;

		.share-cancel {
			background-color: #fff;
			text-align: center;
			width: 100%;
			padding: 25rpx 0;
		}

		.share-scrroll {
			border-radius: 15rpx 15rpx 0 0;
			background-color: #f7f7f7;
			width: 100%;
			height: 200rpx;
			display: flex;
			white-space: nowrap;
			padding-top: 45rpx;

			.share-item {
				display: inline-flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				width: 25%;
			}

			image {
				width: 60rpx;
				height: 60rpx;
			}

			view {
				color: #666666;
				font-size: 25rpx;
				padding-top: 10rpx;
			}
		}
	}
</style>

<!-- 分享组件     this.$refs.share.showHandler() -->
			<Share ref="share" :shareData="course"/>

触发一个绑定事件的方法


yzc_share(){
			this.$refs.share.showHandler()
		}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值