mui 实现文章分享功能

弄了很 久,终于弄出来了,不容易啊。

看代码:

<script type="text/javascript" charset="utf-8">
		               //分享操作 
		        var shares = {};
		        // 初始化服务列表
		        mui.plusReady(function() {
			
		            plus.share.getServices(function(s) {
		                if (s && s.length > 0) {
		                    for (var i = 0; i < s.length; i++) {
		                        var t = s[i];
		                        shares[t.id] = t;
		                    }
		                }
		            }, function() {
		                console.log("获取分享服务列表失败");
		            });
		           click();
		        });
		function     click(){
		 
		         //分享链接点击事件
		        document.getElementById("share").addEventListener('tap', function() {
		            var ids = [{
		                    id: "weixin",
		                    ex: "WXSceneSession"
		                }, {
		                    id: "weixin",
		                    ex: "WXSceneTimeline"
		                }, {
		                    id: "sinaweibo"
		                }, {
		                    id: "tencentweibo"
		                }, {
		                    id: "qq"
		                }],
		                bts = [{
		                    title: "发送给微信好友"
		                }, {
		                    title: "分享到微信朋友圈"
		                }, {
		                    title: "分享到新浪微博"
		                }, {
		                    title: "分享到腾讯微博"
		                }, {
		                    title: "分享到QQ"
		                }];
		            plus.nativeUI.actionSheet({
		                cancel: "取消",
		                buttons: bts
		            }, function(e) {
		                var i = e.index;
		                if (i > 0) {
		                    var s_id = ids[i - 1].id;
		                    var share = shares[s_id];
		                    if (share.authenticated) {
		                        shareMessage(share, ids[i - 1].ex);
		                    } else {
		                        share.authorize(function() {
		                            shareMessage(share, ids[i - 1].ex);
		                        }, function(e) {
		                            console.log("认证授权失败:" + e.code + " - " + e.message);
		                        });
		                    }
		                }
		            });
		        });
		        	
		}
		        // 分享执行动作
		        function shareMessage(share, ex) {
			  var  title=$(".pet_article_title").html();
			   
			  var  content=$(".content").text();
			  
			  var  headimg=$(".pet_article_user_ico  img").attr("src");
			  
			  var  url=document.getElementById("urlTest").value;
			  
			  alert(url)
		                var msg = {
		                    extra: {
		                        scene: ex
		                    }
		                };
		                msg.href = url;
		                msg.title = title;
		                msg.content = content;
		                if (~share.id.indexOf('weibo')) {
		                    msg.content += ";体验地址:http://www.dcloud.io/hellomui/";
		                }
		                msg.thumbs = [headimg];
		                share.send(msg, function() {
		                    console.log("分享到\"" + share.description + "\"成功! ");
		                }, function(e) {
		                    console.log("分享到\"" + share.description + "\"失败: " + e.code + " - " + e.message);
		                });
		            } 
		    </script>

重点讲下:

//分享链接点击事件

document.getElementById(“share”).addEventListener(‘tap’, function() {。。。。

这里是你点击的分享事件。

**var title=$(".pet_article_title").html();

		  var  content=$(".content").text();
		  
		  var  headimg=$(".pet_article_user_ico  img").attr("src");
		  
		  var  url=document.getElementById("urlTest").value;**

这里的 标题(title),文本(content),图片(headimg),文章路径(url),需要你自己提供。

接下来看看效果:

在这里插入图片描述
点击进行分享

在这里插入图片描述

点击发送微信好友

在这里插入图片描述
选择好友进行发送:

在这里插入图片描述
分享给QQ:

在这里插入图片描述

在这里插入图片描述
这样就分享成功了,其他的不做解释了,不懂的可以咨询我。

😀😀😀😂😂🥱

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你的美,让我痴迷

你的好,我会永远记住你的。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值