微信JS-SDK实现自定义分享功能分享

  1 <?php
  2 namespace app\index\controller;
  3 class Jssdk extends Base{
  4     private $appId;
  5     private $appSecret;
  6 
  7     public function __construct($appId, $appSecret) {
  8         $this->appId = $appId;
  9         $this->appSecret = $appSecret;
 10     }
 11 
 12     public function getSignPackage() {
 13         $jsapiTicket = $this->getJsApiTicket();
 14 
 15         // 注意 URL 一定要动态获取,不能 hardcode.
 16         $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
 17         $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
 18 
 19         $timestamp = time();
 20         $nonceStr = $this->createNonceStr();
 21 
 22         // 这里参数的顺序要按照 key 值 ASCII 码升序排序
 23         $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
 24 
 25         $signature = sha1($string);
 26 
 27         $signPackage = array(
 28             "appId"     => $this->appId,
 29             "nonceStr"  => $nonceStr,
 30             "timestamp" => $timestamp,
 31             "url"       => $url,
 32             "signature" => $signature,
 33             "rawString" => $string
 34         );
 35         return $signPackage;
 36     }
 37 
 38     private function createNonceStr($length = 16) {
 39         $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
 40         $str = "";
 41         for ($i = 0; $i < $length; $i++) {
 42             $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
 43         }
 44         return $str;
 45     }
 46 
 47     private function getJsApiTicket() {
 48         // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
 49         $data = json_decode($this->get_php_file(APP_PATH."jsapi_ticket.php"));
 50         if ($data->expire_time < time()) {
 51             $accessToken = $this->getAccessToken();
 52             file_put_contents(APP_PATH."accessToken.txt",$accessToken);
 53             if($accessToken){
 54                 // 如果是企业号用以下 URL 获取 ticket
 55                 // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
 56                 $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
 57                 $res = json_decode($this->httpGet($url));
 58                 $ticket = $res->ticket;
 59                 if ($ticket) {
 60                     $data->expire_time = time() + 7000;
 61                     $data->jsapi_ticket = $ticket;
 62                     $this->set_php_file(APP_PATH."jsapi_ticket.php", json_encode($data));
 63                 }
 64             }
 65         } else {
 66             $ticket = $data->jsapi_ticket;
 67         }
 68 
 69         return $ticket;
 70     }
 71 
 72     private function getAccessToken() {
 73         // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
 74         $data = json_decode($this->get_php_file(APP_PATH."access_token.php"));
 75         //file_put_contents(APP_PATH."data.txt",$data);
 76         if ($data->expire_time < time()) {
 77             // 如果是企业号用以下URL获取access_token
 78             // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
 79             $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
 80             file_put_contents(APP_PATH."url_acc.txt",$url);
 81             $res = json_decode($this->httpGet($url));
 82             //file_put_contents(APP_PATH."res.txt",$res);
 83             $access_token = $res->access_token;
 84             if ($access_token) {
 85                 $data->expire_time = time() + 7000;
 86                 $data->access_token = $access_token;
 87                 $this->set_php_file(APP_PATH."access_token.php", json_encode($data));
 88             }
 89         } else {
 90             $access_token = $data->access_token;
 91         }
 92         return $access_token;
 93     }
 94 
 95     private function httpGet($url) {
 96         file_put_contents(APP_PATH."url_http.txt",$url);
 97         $curl = curl_init();
 98         curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 99         curl_setopt($curl, CURLOPT_TIMEOUT, 500);
100         // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
101         // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
102         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
103         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
104         curl_setopt($curl, CURLOPT_URL, $url);
105 
106         $res = curl_exec($curl);
107         curl_close($curl);
108 
109         return $res;
110     }
111 
112     private function get_php_file($filename) {
113         return trim(substr(file_get_contents($filename), 15));
114     }
115     private function set_php_file($filename, $content) {
116         $fp = fopen($filename, "w");
117         fwrite($fp, "<?php exit();?>" . $content);
118         fclose($fp);
119     }
120 }
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>

<script> wx.config({ debug: false, appId: "{$app_id}", timestamp: "{$share_info['timestamp']}", nonceStr: "{$share_info['nonceStr']}", signature: "{$share_info['signature']}", jsApiList: [ 'onMenuShareTimeline' ] }); wx.ready(function() { id = "{$id}", wx.onMenuShareTimeline({ title: "{$shop['title']}", // 分享标题 link: "{$url}/.?fid={$id}", // 分享链接 imgUrl: "{$url}__SHOPPIC__/{$shop.pic}", // 分享图标 success: function() { $.ajax({ type:"POST", url:"/index/fenxiao/ajaxShare", data:{id: id}, success:function(data){ if(data == 1){ alert('分享成功') }else{ alert('分享失败') } } }); }, cancel: function() { alert('分享失败'); // 用户取消分享后执行的回调函数 } }); }); wx.error(function(res){ alert('验证失败') }); </script>

  我也忘记在哪里看来的,分享接口用的是jweixin-1.0.0.js, onMenuShareTimeline 即将废除 但也不影响使用,废除以后换一下就ok.目前可以用.具体查看https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115

类中txt文档不用写入也没有事



 

posted @ 2019-03-28 11:03  清清沁沁  阅读(409)  评论(0编辑  收藏  举报