ybtxwd

//公众号:亲测有效(根据开发文档步骤走:特别提示,一定要看文档,不要相信百度结果--!)

认证公众号,js安全域名,IP白名单等;
PHP 控制器
// 新版本微信下,如何设置"分享到朋友圈"的缩略图?
// https://zhuanlan.zhihu.com/p/32414728(参考知乎刘欢回答,略有修改)

 1 // 获取accesstoken
 2 if(S(\'accessToken\')){
 3     $accessToken = S(\'accessToken\');
 4 }else{
 5     $output = $this->http_post_data("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=xxxxxx&secret=xxxxx");//xxx对应修改,一下同;
 6     $res = json_decode($output, true);
 7     $accessToken = $res["access_token"]; 
 8     S(\'accessToken\',$accessToken,7000);
 9 }
10 //获取jsapi_ticket
11 $output = $this->http_post_data("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$accessToken."&type=jsapi");
12 $getTicket = json_decode($output, true);
13 $ticket = $getTicket[\'ticket\'];
14 //获取nonceStr
15 function createNonceStr($length = 16) {
16     $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
17     $str = "";
18     for ($i = 0; $i < $length; $i++) {
19         $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
20     }
21     return $str;
22 }
23 $nonceStr = createNonceStr();
24 //取timestamp
25 $timestamp = time();
26 //url值
27 $protocol = (!empty($_SERVER[\'HTTPS\']) && $_SERVER[\'HTTPS\'] !== \'off\' || $_SERVER[\'SERVER_PORT\'] == 443) ? "https://" : "http://";
28 $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
29 //拼到一起后sha编码
30 $string = "jsapi_ticket=".$ticket."&noncestr=".$nonceStr."&timestamp=".$timestamp."&url=".$url;
31 $signature = sha1($string);
32 $signPackage = array(
33     "appId" => "xxx",//对应修改
34     "nonceStr" => $nonceStr,
35     "timestamp" => $timestamp,
36     "url" => $url,
37     "signature" => $signature,
38     "rawString" => $string
39 );
40 $this->assign(\'signPackage\',$signPackage);    

模板:

 1 <!-- 引入微信js脚本库 -->
 2 <script type="text/JavaScript" src="http://res.wx.qq.com/open/js/jweixin-1.1.0.js"></script>
 3 <script type="text/javascript">
 4 wx.config({ 
 5     debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 
 6     appId: \'{$signPackage.appId}\', // 必填,公众号的唯一标识 
 7     timestamp: "{$signPackage.timestamp}", // 必填,生成签名的时间戳 
 8     nonceStr: \'{$signPackage.nonceStr}\', // 必填,生成签名的随机串 
 9     signature: \'{$signPackage.signature}\',// 必填,签名,见附录1 
10     jsApiList: [ 
11         \'onMenuShareTimeline\', 
12         \'onMenuShareAppMessage\', 
13     ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 
14 }); 
15 
16 wx.ready(function () { 
17     wx.onMenuShareTimeline({ 
18         title: "xxx", // 分享标题 
19         link: \'http://xxx.com/\', // 分享链接,将当前登录用户转为puid,以便于发展下线 
20         imgUrl: \'http://xxx.com/Public/Home/images/cover.jpg\', // 分享图标 
21         success: function () { 
22             // 用户确认分享后执行的回调函数 
23             alert(\'分享成功\'); 
24         }, 
25         cancel: function () { 
26             // 用户取消分享后执行的回调函数 
27         } 
28     }); 
29     wx.onMenuShareAppMessage({
30         title: "xxx", // 分享标题
31         desc: "xxx", // 分享描述
32         link: \'http://xxx.com/\', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
33         imgUrl: \'http:/xxx.com/Public/Home/images/cover.jpg\', // 分享图标
34         type: \'\', // 分享类型,music、video或link,不填默认为link
35         dataUrl: \'\', // 如果type是music或video,则要提供数据链接,默认为空
36         success: function () {
37             // 用户确认分享后执行的回调函数
38             alert(\'分享成功\'); 
39         },
40         cancel: function () {
41             // 用户取消分享后执行的回调函数
42         }
43     });
44     wx.error(function(res){ 
45         // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 
46         // alert("errorMSG:"+res); 
47     }); 
48 });
49 
50 </script>    

 

分类:

技术点:

相关文章:

  • 2021-09-27
  • 2021-12-29
  • 2021-09-27
  • 2021-03-31
  • 2022-12-23
  • 2021-11-12
  • 2022-01-02
  • 2021-12-29
猜你喜欢
  • 2021-12-02
  • 2022-01-16
  • 2022-12-23
  • 2022-01-02
  • 2021-07-26
  • 2022-12-23
相关资源
相似解决方案