细节请参考前一篇文章:
JSSDK.PHP 修改下:
1 <?php 2 namespace jssdk; 3 4 class Jssdk { 5 private $appId; 6 private $appSecret; 7 private $url; 8 9 public function __construct($appId, $appSecret,$url) { 10 $this->appId = $appId; 11 $this->appSecret = $appSecret; 12 $this->url = $url; 13 } 14 15 public function getSignPackage() { 16 $jsapiTicket = $this->getJsApiTicket(); 17 18 // 注意 URL 一定要动态获取,不能 hardcode. 19 $protocol = (!empty($_SERVER[‘HTTPS‘]) && $_SERVER[‘HTTPS‘] !== ‘off‘ || $_SERVER[‘SERVER_PORT‘] == 443) ? "https://" : "http://"; 20 // $url = "$protocol$_SERVER[SERVER_NAME]$_SERVER[REQUEST_URI]"; 21 $url = $this->url; 22 23 $timestamp = time(); 24 $nonceStr = $this->createNonceStr(); 25 26 // 这里参数的顺序要按照 key 值 ASCII 码升序排序 27 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; 28 29 $signature = sha1($string); 30 31 $signPackage = array( 32 "appId" => $this->appId, 33 "nonceStr" => $nonceStr, 34 "timestamp" => $timestamp, 35 "url" => $url, 36 "signature" => $signature, 37 "rawString" => $string 38 ); 39 return $signPackage; 40 } 41 42 private function createNonceStr($length = 16) { 43 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 44 $str = ""; 45 for ($i = 0; $i < $length; $i++) { 46 $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 47 } 48 return $str; 49 } 50 51 private function getJsApiTicket() { 52 // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例 53 $data = json_decode($this->get_php_file(EXTEND_PATH .‘jssdk‘. DS ."jsapi_ticket.php")); 54 if ($data->expire_time < time()) { 55 $accessToken = $this->getAccessToken(); 56 // 如果是企业号用以下 URL 获取 ticket 57 // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken"; 58 $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; 59 $res = json_decode($this->httpGet($url)); 60 $ticket = $res->ticket; 61 if ($ticket) { 62 $data->expire_time = time() + 7000; 63 $data->jsapi_ticket = $ticket; 64 $this->set_php_file("jsapi_ticket.php", json_encode($data)); 65 } 66 } else { 67 $ticket = $data->jsapi_ticket; 68 } 69 70 return $ticket; 71 } 72 73 private function getAccessToken() { 74 // access_token 应该全局存储与更新,以下代码以写入到文件中做示例 75 $data = json_decode($this->get_php_file(EXTEND_PATH .‘jssdk‘. DS ."access_token.php")); 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 $res = json_decode($this->httpGet($url)); 81 $access_token = $res->access_token; 82 if ($access_token) { 83 $data->expire_time = time() + 7000; 84 $data->access_token = $access_token; 85 $this->set_php_file("access_token.php", json_encode($data)); 86 } 87 } else { 88 $access_token = $data->access_token; 89 } 90 return $access_token; 91 } 92 93 private function httpGet($url) { 94 $curl = curl_init(); 95 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 96 curl_setopt($curl, CURLOPT_TIMEOUT, 500); 97 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 98 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); 99 curl_setopt($curl, CURLOPT_URL, $url); 100 101 $res = curl_exec($curl); 102 curl_close($curl); 103 104 return $res; 105 } 106 107 private function get_php_file($filename) { 108 return trim(substr(file_get_contents($filename), 15)); 109 } 110 private function set_php_file($filename, $content) { 111 $fp = fopen($filename, "w"); 112 fwrite($fp, "<?php exit();?>" . $content); 113 fclose($fp); 114 } 115 }
controller:
$config = config(‘wxConfig‘); $jssdk = new Jssdk($config[‘id‘], $config[‘secret‘],$sign_url); $signPackage = $jssdk->GetSignPackage(); return json([‘code‘=>1,‘msg‘=>‘success!‘,‘data‘=>$signPackage],200);
前端:
<script src="/h/js/jquery.min.js?v=2.1.4"></script> <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script src="/static/js/share_wx.js"></script> <script> $.fn.share_wx({ desc: "xx", imgUrl: "xxurl" }); </script>
share_wx:
1 /** 2 * author:xuguo 3 * weixin jssdk 4 * xx.com 5 */ 6 (function($){ 7 var share_wx = {}; 8 /** 9 * 10 * @param obj 设置微信分享的信息 11 * { 12 * title: "xx", 13 * desc: "xx", 14 * link: "xx", 15 * imgUrl: "xx" 16 * } 17 */ 18 share_wx.share_wx = function(obj) { 19 var url = location.href.split(‘#‘)[0]; 20 $.ajax({ 21 url:‘/index/index/share_wx‘, 22 type:"get", 23 data:{‘sign_url‘:url}, 24 success:function (data) { 25 if(data.code == 1){ 26 wx_(data.data) 27 } 28 } 29 }); 30 31 /** 32 * 33 * @param options 34 * @private 35 */ 36 function wx_(options) { 37 wx.config({ 38 debug: false, 39 appId: options.appId, 40 timestamp: options.timestamp, 41 nonceStr: options.nonceStr, 42 signature: options.signature, 43 jsApiList: [ 44 "checkJsApi", 45 "onMenuShareTimeline", 46 "onMenuShareAppMessage", 47 "onMenuShareQQ", 48 "onMenuShareWeibo", 49 "onMenuShareQZone" 50 ] 51 }); 52 wx.ready(function() { 53 var shareData = obj; 54 wx.onMenuShareAppMessage(shareData); 55 wx.onMenuShareTimeline(shareData); 56 wx.onMenuShareQQ(shareData); 57 wx.onMenuShareWeibo(shareData); 58 wx.onMenuShareQZone(shareData); 59 }); 60 } 61 }; 62 $.fn.extend(share_wx); 63 })(jQuery);