首页
导航
博客
电子书
算法
众创
代码
随贴
关于我们
您好,欢迎来到码863代码分享网! 请
[登录]
/
[注册]
搜 索
标题:
*
140
字
TAG标签:
(用空格隔开)
30
字
恢复历史版本:
请选择分类
html
python
javascript
php
sql
c
c++
c#
java
plain
所有人可见
仅自己可见
编辑器:UEditor
编辑器:TinyMCE
编辑器:Editor.md
HTML转MD
HTML转MD2
<p style="margin-top: 20px; margin-bottom: 20px; padding: 0px;">原文:http://jingyan.baidu.com/article/d3b74d64c517051f77e609ed.html</p><p style="margin-top: 20px; margin-bottom: 20px; padding: 0px;">为了净化网络,整顿诱导分享及诱导关注行为,微信于2014年12月30日发布了《微信公众平台关于整顿诱导分享及诱导关注行为的公告》,微信平台开发者发现,原有的微信分享功能不能用了,在iphone手机上,显示为当前页的链接,之前设置的图标和标题等都没有了。虽然在部分android手机上还可以用,但最近微信升级后,分享功能也不正常了。正在苦于微信分享该怎么解决时,微信于2015年1月10日即时发布了开放JS-SDK,为微信网站的开发提供了强大的js功能。这里仅就分享功能的使用进行一些描述。</p><p style="margin-top: 20px; margin-bottom: 20px; padding: 0px;">下面的代码,服务器端使用的是php。</p><h2 class="exp-content-head" style="margin: 40px 0px 0px; padding: 0px 0px 7px; font-size: 22px; font-weight: 400; font-stretch: normal; line-height: 24px; font-family: 微软雅黑; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(214, 214, 216);"><a style="color: rgb(45, 100, 179);" name="section-2"></a>步骤1 生成签名</h2><ol class="exp-conent-orderlist list-paddingleft-2" style="list-style-type: none;"><li class="exp-content-list " style=""><p>1</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">登录你的微信平台,点击“公众号设置”。</p><p><a class="exp-image-wraper" href="http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=1" log="type:20150831,pos:enter_step,expGroup:G0" target="_self" style="text-decoration: none; color: rgb(45, 100, 179); position: relative; display: block; float: left;"><img class="exp-image-default" alt="如何使用微信JS-SDK实际分享功能" src="/ueditor/php/upload/image/20160114/1452761641470122.jpg" style="border: 0px; display: block;"/><span class="exp-album-enter-mask" style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; opacity: 0; cursor: pointer; background: rgb(51, 51, 51);"></span></a></p><p><a class="exp-image-wraper" href="http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=2" log="type:20150831,pos:enter_step,expGroup:G0" target="_self" style="text-decoration: none; color: rgb(45, 100, 179); position: relative; display: block; float: left;"><img class="exp-image-default" alt="如何使用微信JS-SDK实际分享功能" src="/ueditor/php/upload/image/20160114/1452761641456610.jpg" style="border: 0px; display: block;"/><span class="exp-album-enter-mask" style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; opacity: 0; cursor: pointer; background: rgb(51, 51, 51);"></span></a></p></li><li class="exp-content-list " style=""><p>2</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">点击“功能设置”,然后点击“设置”。</p><p><a class="exp-image-wraper" href="http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=3" log="type:20150831,pos:enter_step,expGroup:G0" target="_self" style="text-decoration: none; color: rgb(45, 100, 179); position: relative; display: block; float: left;"><img class="exp-image-default" alt="如何使用微信JS-SDK实际分享功能" src="/ueditor/php/upload/image/20160114/1452761641756369.jpg" style="border: 0px; display: block;"/><span class="exp-album-enter-mask" style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; opacity: 0; cursor: pointer; background: rgb(51, 51, 51);"></span></a></p></li><li class="exp-content-list " style=""><p>3</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">设置JS接口安全域名。这里填写的是一级域名,不带www和http。最多可以设置三个域名。设置完后点击确定。(多说一句,相比以前的分享没有任何域名限制,这里设置安全域名,目的是为了当发现此公众平台发现诱导分享行为时,可以根据此域名追溯到所有分享出去的链接,以及通过这些链接增加的粉丝。这样,微信就可以牢牢控制了你的微信平台,一旦发现违规,让分享链接失效,删除掉诱导行为增加的粉丝,是瞬间就可以完成的。因此,微信平台的开发者,一定要合理来使用分享功能,不要因小失大。等到你的微信平台被封,估计哭都来不及)</p><p><a class="exp-image-wraper" href="http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=4" log="type:20150831,pos:enter_step,expGroup:G0" target="_self" style="text-decoration: none; color: rgb(45, 100, 179); position: relative; display: block; float: left;"><img class="exp-image-default" alt="如何使用微信JS-SDK实际分享功能" src="/ueditor/php/upload/image/20160114/1452761641116560.jpg" style="border: 0px; display: block;"/><span class="exp-album-enter-mask" style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; opacity: 0; cursor: pointer; background: rgb(51, 51, 51);"></span></a></p><p><a class="exp-image-wraper" href="http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=5" log="type:20150831,pos:enter_step,expGroup:G0" target="_self" style="text-decoration: none; color: rgb(45, 100, 179); position: relative; display: block; float: left;"><img class="exp-image-default" alt="如何使用微信JS-SDK实际分享功能" src="/ueditor/php/upload/image/20160114/1452761642263784.jpg" style="border: 0px; display: block;"/><span class="exp-album-enter-mask" style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; opacity: 0; cursor: pointer; background: rgb(51, 51, 51);"></span></a></p></li><li class="exp-content-list " style=""><p>4</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">在开发者中心中获取你的AppID和AppSecret,接下来在获取令牌时,需要这两个信息。</p><p><a class="exp-image-wraper" href="http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=6" log="type:20150831,pos:enter_step,expGroup:G0" target="_self" style="text-decoration: none; color: rgb(45, 100, 179); position: relative; display: block; float: left;"><img class="exp-image-default" alt="如何使用微信JS-SDK实际分享功能" src="/ueditor/php/upload/image/20160114/1452761642924451.jpg" style="border: 0px; display: block;"/><span class="exp-album-enter-mask" style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; opacity: 0; cursor: pointer; background: rgb(51, 51, 51);"></span></a></p></li><li class="exp-content-list " style=""><p>5</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">获取令牌。在服务器端完成,代码如下:</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">function wx_get_token() {</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $token = S('access_token');</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> if (!$token) {</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $res = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' .'你的AppID'.'&secret=' .'你的AppSecret');</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $res = json_decode($res, true);</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $token = $res['access_token'];</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> // 注意:这里需要将获取到的token缓存起来(或写到数据库中)</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> // 不能频繁的访问https://api.weixin.qq.com/cgi-bin/token,每日有次数限制</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> // 通过此接口返回的token的有效期目前为2小时。令牌失效后,JS-SDK也就不能用了。</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> // 因此,这里将token值缓存1小时,比2小时小。缓存失效后,再从接口获取新的token,这样</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> // 就可以避免token失效。</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> // S()是ThinkPhp的缓存函数,如果使用的是不ThinkPhp框架,可以使用你的缓存函数,或使用数据库来保存。</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> S('access_token', $token, 3600);</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> }</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> return $token;</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">}</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"><br/></p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"><strong>注意:返回的access_token长度至少要留够512字节。接口返回值:</strong></p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">{"access_token":"ACCESS_TOKEN","expires_in":7200}</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">{"access_token":"vdlThyTfyB0N5eMoi3n_aMFMKPuwkE0MgyGf_0h0fpzL8p_hsdUX8VGxz5oSXuq5dM69lxP9wBwN9Yzg-0kVHY33BykRC0YXZZZ-WdxEic4","expires_in":7200}</p><p><a class="exp-image-wraper" href="http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=7" log="type:20150831,pos:enter_step,expGroup:G0" target="_self" style="text-decoration: none; color: rgb(45, 100, 179); position: relative; display: block; float: left;"><img class="exp-image-default" alt="如何使用微信JS-SDK实际分享功能" src="/ueditor/php/upload/image/20160114/1452761642105621.jpg" style="border: 0px; display: block;"/><span class="exp-album-enter-mask" style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; opacity: 0; cursor: pointer; background: rgb(51, 51, 51);"></span></a></p></li><li class="exp-content-list " style=""><p>6</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">获取jsapi的ticket。jsapi_ticket是公众号用于调用微信JS接口的临时票据。正常情况下,jsapi_ticket的有效期为7200秒,通过access_token来获取。</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">function wx_get_jsapi_ticket(){</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $ticket = "";</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> do{</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $ticket = S('wx_ticket');</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> if (!empty($ticket)) {</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> break;</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> }</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $token = S('access_token');</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> if (empty($token)){</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> wx_get_token();</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> }</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $token = S('access_token');</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> if (empty($token)) {</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> logErr("get access token error.");</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> break;</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> }</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $url2 = sprintf("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi",</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $token);</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $res = file_get_contents($url2);</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $res = json_decode($res, true);</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $ticket = $res['ticket'];</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> // 注意:这里需要将获取到的ticket缓存起来(或写到数据库中)</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> // ticket和token一样,不能频繁的访问接口来获取,在每次获取后,我们把它保存起来。</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> S('wx_ticket', $ticket, 3600);</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> }while(0);</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> return $ticket;</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">}</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"><br/></p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">接口返回值:</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">{"errcode":0,"errmsg":"ok","ticket":"sM4AOVdWfPE4DxkXGEs8VMKv7FMCPm-I98-klC6SO3Q3AwzxqljYWtzTCxIH9hDOXZCo9cgfHI6kwbe_YWtOQg","expires_in":7200}</p><p><a class="exp-image-wraper" href="http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=8" log="type:20150831,pos:enter_step,expGroup:G0" target="_self" style="text-decoration: none; color: rgb(45, 100, 179); position: relative; display: block; float: left;"><img class="exp-image-default" alt="如何使用微信JS-SDK实际分享功能" src="/ueditor/php/upload/image/20160114/1452761642746644.jpg" style="border: 0px; display: block;"/><span class="exp-album-enter-mask" style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; opacity: 0; cursor: pointer; background: rgb(51, 51, 51);"></span></a></p></li><li class="exp-content-list " style=""><p>7</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">签名,将jsapi_ticket、noncestr、timestamp、分享的url按字母顺序连接起来,进行sha1签名。</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">noncestr是你设置的任意字符串。</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">timestamp为时间戳。</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"><br/></p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $timestamp = time();</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $wxnonceStr = "任意字符串";</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $wxticket = wx_get_jsapi_ticket();</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $wxOri = sprintf("jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s",</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $wxticket, $wxnonceStr, $timestamp,</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> '要分享的url(从http开始,如果有参数,包含参数)'</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> );</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> $wxSha1 = sha1($wxOri);</p><p><a class="exp-image-wraper" href="http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=9" log="type:20150831,pos:enter_step,expGroup:G0" target="_self" style="text-decoration: none; color: rgb(45, 100, 179); position: relative; display: block; float: left;"><img class="exp-image-default" alt="如何使用微信JS-SDK实际分享功能" src="/ueditor/php/upload/image/20160114/1452761643276651.jpg" style="border: 0px; display: block;"/><span class="exp-album-enter-mask" style="position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; opacity: 0; cursor: pointer; background: rgb(51, 51, 51);"></span></a></p><p><span class="last-item-end" style="color: rgb(204, 204, 204); font-size: 12px; line-height: 13px; position: absolute; bottom: 0px; left: -48px; display: block;">END</span></p></li></ol><h2 class="exp-content-head" style="margin: 40px 0px 0px; padding: 0px 0px 7px; font-size: 22px; font-weight: 400; font-stretch: normal; line-height: 24px; font-family: 微软雅黑; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(214, 214, 216);"><a style="color: rgb(45, 100, 179);" name="section-3"></a>步骤2 添加JS代码</h2><ol class="exp-conent-orderlist list-paddingleft-2" style="list-style-type: none;"><li class="exp-content-list " style=""><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">生成签名后,就可以使用js代码了。在你的html中,进行如下设置即可。</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"><script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script></p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"><script type="text/javascript"></p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">// 微信配置</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">wx.config({</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> debug: false, </p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> appId: "你的AppID", </p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> timestamp: '上一步生成的时间戳', </p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> nonceStr: '上一步中的字符串', </p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> signature: '上一步生成的签名',</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'] // 功能列表,我们要使用JS-SDK的什么功能</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">});</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在 页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready 函数中。</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">wx.ready(function(){</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> // 获取“分享到朋友圈”按钮点击状态及自定义分享内容接口</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> wx.onMenuShareTimeline({</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> title: '分享标题', // 分享标题</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> link:"分享的url,以http或https开头",</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> imgUrl: "分享图标的url,以http或https开头" // 分享图标</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> });</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> // 获取“分享给朋友”按钮点击状态及自定义分享内容接口</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> wx.onMenuShareAppMessage({</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> title: '分享标题', // 分享标题</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> desc: "分享描述", // 分享描述</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> link:"分享的url,以http或https开头",</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> imgUrl: "分享图标的url,以http或https开头", // 分享图标</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> type: 'link', // 分享类型,music、video或link,不填默认为link</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"> });</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;">});</p><p style="margin-top: 0px; margin-bottom: 0px; padding: 0px;"></script></p><p><a class="exp-image-wraper" href="http://jingyan.baidu.com/album/d3b74d64c517051f77e609ed.html?picindex=10" log="type:20150831,pos:enter_step,expGroup:G0" target="_self" style="text-decoration: none; color: rgb(45, 100, 179); position: relative; display: block; float: left;"><img class="exp-image-default" alt="如何使用微信JS-SDK实际分享功能" src="/ueditor/php/upload/image/20160114/1452761643940631.jpg" style="border: 0px; display: block;"/></a></p></li><li class="exp-content-list " style=""><p><br/></p></li></ol><p><br/></p>
CopyRight 2002~2023 精通2100网 联系邮箱:qqtxt@163.com
版权所有:精通2100网
湘ICP备2023018646号-1
MYSQl共执行 4 个查询,用时 0.0019383430480957 秒,PHP脚本用时 0.004521 秒,占用内存 0.667 MB,Gzip 已启用