uniapp 分享到微信、QQ、朋友圈

使用uniapp开发app,因为前期没有申请分享所需要的appid,所以经过一番百度,这里使用的是plus的系统分享。uni.share()分享api请移步官网文档

特点:无需申请appid 但是写起来麻烦,调起应用可能会有点慢

说明:

1、ios直接走系统分享,安卓做分享的区分。这里着重说明的是安卓分享。**
2、因为微信分享的限制,分享到朋友圈只能打开朋友圈但无法把分享内容直接赋值上去,需要手动粘贴内容或者上传图片等。分享到微信只能选择其中一个类别,如分享了图片,那么文字可以复制到剪切板,但是无法直接分享给好友;如果只分享文字,则可以直接分享。**

逻辑:

  1. 选择分享的应用
  2. 检测应用是否已安装
  3. 下载保存图片、视频,文字复制到剪切板
  4. 开始分享

部分代码

定义ComponentName(主要)

// 分享到第三方
share:function(type) {
	this.cancel();
    this.isShare = true;
    this.shareType = type;
    // #ifdef APP-PLUS
    plus.nativeUI.showWaiting("应用检测中...");
    let ComponentName;
    if(plus.os.name == "Android") {
    	ComponentName = plus.android.importClass('android.content.ComponentName');
    }
    
    // 分享到微信
    if(type === 'wx') {
    	if(plus.os.name == "Android") {
    		this.componentname = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
    	}
    	
    	this.isInstall('com.tencent.mm','weixin://', type, '微信');
    } else if(type === 'line') {
    	if(plus.os.name == "Android") {// 分享到朋友圈	
    	this.componentname = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
    	}
    	
    	this.isInstall('com.tencent.mm','weixin://', type, '微信');
    } else if(type === 'weibo') {
    	// 分享到微博	
    	if(plus.os.name == "Android") {
    		this.componentname = new ComponentName("com.sina.weibo", "com.sina.weibo.composerinde.ComposerDispatchActivity");
    	}
    	
    	this.isInstall('com.sina.weibo','sinaweibo://', type, '微博');
    } else if(type === 'QQ') {
    	if(plus.os.name == "Android") {
    		this.componentname = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
    	}
    	
    	this.isInstall('com.tencent.mobileqq','mqq://', type, 'QQ');
    } else if(type === 'space') {
    	if(plus.os.name == "Android") {
    		this.componentname = new ComponentName("com.qzone", "com.qzonex.module.operation.ui.QZonePublishMoodActivity");
    	}
    	
    	this.isInstall('com.qzone','mqq://', type, 'QQ空间');
    }
    // #endif
	// #ifndef APP-PLUS
	this.isInstall('com.qzone','mqq://', 'space', 'QQ空间');
	// #endif
},

检测应用安装

// 验证app是否安装
isInstall:function(packageName, action, type, msg) {
             // this.save(true, type);
	if(plus.runtime.isApplicationExist({pname:packageName,action:action})){
		plus.nativeUI.closeWaiting();
		this.save(true, type);
	}else{
		plus.nativeUI.closeWaiting();
		this.$api.msg('您还未安装'+msg+', 请先进行安装')
	}
}

保存素材

uni.downloadFile({
	url: item,
	success:function(res) {
		uni.getImageInfo({
			src: res.tempFilePath,
			success:function(image){
				_this.filePath.push(image.path)
			}
		})
		uni.saveImageToPhotosAlbum({
			filePath: res.tempFilePath
		})
		
	}
})

分享(主要)

uniShareHandle:function(type) {
	let _this = this;
	// 设备信息
	let platform = uni.getSystemInfoSync().platform;
	let localComponentName;
	if(platform === 'ios') {
		// ios直接调用系统分享
		// #ifdef APP-PLUS
		plus.share.sendWithSystem({
			type: 'image',
			content: _this.copyWord,
			pictures: _this.filePath
		},function() {
			_this.$api.msg('分享成功');
		}, function(e) {
			_this.$api.msg('分享失败');
		})
		// #endif
		
	} else {
		if(type != 'line') {
			let Intent = plus.android.importClass('android.content.Intent');
			let ComponentName = plus.android.importClass('android.content.ComponentName');
			let ArrayList = plus.android.importClass('java.util.ArrayList');  
			let Uri = plus.android.importClass('android.net.Uri');  
			let Environment = plus.android.importClass('android.os.Environment');  
			let File = plus.android.importClass('java.io.File');
			let MediaStore = plus.android.importClass("android.provider.MediaStore");
			let act = plus.android.runtimeMainActivity();  
			let intent = new Intent();
			intent.setComponent(this.componentname);
			
			intent.setFlags("android.intent.FLAG_ACTIVITY_NEW_TASK");
			
			let localArrayList = new ArrayList();
			for(let i =0;i< this.filePath.length;i++){
				let filePath = plus.io.convertLocalFileSystemURL(this.filePath[i].replace('file://', ''));
				let img = Uri.parse(MediaStore.Images.Media.insertImage(act.getContentResolver(), new File(filePath).getAbsolutePath(), null, null))
				localArrayList.add(img);
			}
			if(this.imageList.length != 0) {
				intent.setAction("android.intent.action.SEND_MULTIPLE");
				intent.setType("image/*");
				intent.putParcelableArrayListExtra("android.intent.extra.STREAM", localArrayList);
			} else {
				intent.setAction("android.intent.action.SEND");
				intent.setType("text/plain");
				intent.putExtra(Intent.EXTRA_TEXT, _this.copyWord);
			}
			  
			act.startActivity(intent); 	
		} else {
			// 直接打开微信,手动进行朋友圈分享
			plus.runtime.launchApplication({
				pname: 'com.tencent.mm',
				action: 'weixin://'
			})
		}
		
	}
},
  • 5
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值