Yooye
Yooye
  • 发布:2016-05-11 00:21
  • 更新:2019-01-12 14:01
  • 阅读:8334

关于微信分享的实践与问题汇总,比较适合小白~

分类:HTML5+

需求是要向微信朋友圈或者微信好友发送带【缩略图】【标题】【介绍性文字】【链接】的分享,也就是H5+的demo里面share.html里的链接分享

其实我自己也是第一次做这种需求,第一次不知道什么原因,分享出去的总是只有介绍性文字,而没有链接跟缩略图。后来经过请教官方群里的朋友以及自己的琢磨,成功实现了该需求,而且是运用非常小白的方式,下面做一个简单的总结:

1、把官方H5+的demo里的share.html直接copy到自己项目文件中,把对应引入文件的路径修改正确。

2、如果不想或者不会删减share.html里面跟【链接分享】无关的代码,就直接把原有的HTML代码display:none就可以了,这样,部分js不会因为找不到对应的DOM元素而报错(当然这个方法不推荐,只是给新手做参考),比如

        <div id="dcontent" class="dcontent" style="display: none;">

3、接下来就是在share.html页面内部的js中找到shareAction(sb, bh)函数,把控制【缩略图】【标题】【介绍性文字】【链接】的地方赋值为我们自己的内容即可。(在下面的代码的被**包起来的那几行)

            function shareAction(sb, bh) {  
                outSet("分享操作:");  
                if (!sb || !sb.s) {  
                    outLine("无效的分享服务!");  
                    return;  
                }  
                var msg = {  
                    **content: sharecontent.value**,  
                    extra: {  
                        scene: sb.x  
                    }  
                };  
                if (bh) {  
                    **msg.href = sharehref.value;**  
                    if (sharehrefTitle && sharehrefTitle.value != "") {  
                        msg.title = sharehrefTitle.value;  
                    }  
                    if (sharehrefDes && sharehrefDes.value != "") {  
                        msg.content = sharehrefDes.value;  
                    }  
//                  msg.thumbs = ["_www/logo.png"];  
                    **msg.thumbs = [document.getElementById('pic').src];**  
//                  _www/logo.png  
                    msg.pictures = ["_www/logo.png"];  
                } else {  
                    if (pic && pic.realUrl) {  
                        msg.pictures = [pic.realUrl];  
                    }  
                }  
                // 发送分享  
                if (sb.s.authenticated) {  
                    outLine("---已授权---");  
                    shareMessage(msg, sb.s);  
                } else {  
                    outLine("---未授权---");  
                    sb.s.authorize(function() {  
                        shareMessage(msg, sb.s);  
                    }, function(e) {  
                        outLine("认证授权失败:" + e.code + " - " + e.message);  
                    });  
                }  
            }

4、下面是我给以上函数赋值自己新的内容的代码。

mui("#spread-cont").on("tap","li",function(){  
                        var Href=document.getElementById('sharehref');  
                        var Title=document.getElementById('sharehrefTitle');  
                        var Des=document.getElementById('sharehrefDes');  
                        var shareCont=document.getElementById('sharecontent');  
                        var sharePic=document.getElementById('pic');  

                        Href.value=data[this.getAttribute("data-id")].Url;  
                        Title.value=data[this.getAttribute("data-id")].RwName;  
                        Des.value=data[this.getAttribute("data-id")].Content;  
                        shareCont.value=data[this.getAttribute("data-id")].Content;  
                        sharePic.src=data[this.getAttribute("data-id")].Rwtp;  
                        shareHref();  
                    });

5、里面的data[this.getAttribute("data-id")],是我通过ajax获取到的json数据,在该json数据里取出来我自己的【缩略图】【标题】【介绍性文字】【链接】,并进行赋值操作。

6、哈哈,上面的方法非常小白,好怕被大神看到啊~

3 关注 分享
BoredApe Trust t***@qq.com

要回复文章请先登录注册

x***@126.com

x***@126.com

https://blog.csdn.net/yanxinyun1990/article/details/85221037
2019-01-12 14:01
l***@qq.com

l***@qq.com

请问官方的demo在哪下载?
2019-01-05 16:47
h***@hotmail.com

h***@hotmail.com

我想问一下为什么一定要shareAction函数呢?它有什么作用?
2016-11-02 11:14