作者 jinglong
1 个管道 的构建 通过 耗费 0 秒

调式微信分享

@@ -554,20 +554,28 @@ class LoginController extends HomeBaseController @@ -554,20 +554,28 @@ class LoginController extends HomeBaseController
554 return $str; 554 return $str;
555 } 555 }
556 556
557 - //获取微信分享签名  
558 - public function getSignature(){ 557 + //获取access_token
  558 + public function getWxAccessToken(){
559 $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appkey2.'&secret='.$this->appsecret2; 559 $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appkey2.'&secret='.$this->appsecret2;
560 $res = $this->http_get($url); 560 $res = $this->http_get($url);
561 $json_arr = json_decode($res,true); 561 $json_arr = json_decode($res,true);
562 - if(isset($json_arr['errcode'])&&!empty($json_arr['errcode'])){  
563 - //用户取消登录  
564 - $this->apiResponse(0, '获取配置失败');  
565 - }  
566 $token = $json_arr['access_token']; 562 $token = $json_arr['access_token'];
567 - $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';  
568 - $res = $this->http_get($url);  
569 - $json_arr = json_decode($res,true);  
570 - $ticket = $json_arr['ticket']; 563 + return $token;
  564 + }
  565 +
  566 + //获取微信分享签名
  567 + public function getSignature(){
  568 + if($_SESSION['ticket_expire_time'] > time() && $_SESSION['ticket']){
  569 + $ticket = $_SESSION['ticket'];
  570 + }else{
  571 + $token = $this->getWxAccessToken();
  572 + $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';
  573 + $res = $this->http_get($url);
  574 + $json_arr = json_decode($res,true);
  575 + $ticket = $json_arr['ticket'];
  576 + $_SESSION['ticket'] = $ticket;
  577 + $_SESSION['ticket_expire_time'] = time()+7000;
  578 + }
571 return $ticket; 579 return $ticket;
572 } 580 }
573 581
@@ -38,52 +38,51 @@ @@ -38,52 +38,51 @@
38 <script src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script> 38 <script src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
39 <script> 39 <script>
40 $(function () { 40 $(function () {
41 - var appid = '';  
42 - var timestamp = '';  
43 - var nonceStr = '';  
44 - var signature = '';  
45 - $.ajax({  
46 - type: 'POST',  
47 - url: '/portal/login/configShareWx',  
48 - data: {  
49 - },  
50 - dataType: 'json',  
51 - async: false,  
52 - success: function(data) { 41 + var appid = '';
  42 + var timestamp = '';
  43 + var nonceStr = '';
  44 + var signature = '';
  45 + $.ajax({
  46 + type: 'POST',
  47 + url: '/portal/login/configShareWx',
  48 + data: {
  49 + },
  50 + dataType: 'json',
  51 + async: false,
  52 + success: function(data) {
  53 + console.log(data);
  54 + if (data.code == 1) {
  55 + appid = data.appid;
  56 + timestamp = data.timestamp;
  57 + nonceStr = data.nonceStr;
  58 + signature = data.signature;
  59 + } else {
53 console.log(data); 60 console.log(data);
54 - if (data.code == 1) {  
55 - appid = data.appid;  
56 - timestamp = data.timestamp;  
57 - nonceStr = data.nonceStr;  
58 - signature = data.signature;  
59 - } else {  
60 - console.log(data);  
61 - }  
62 } 61 }
63 - });  
64 - wx.config({  
65 - debug: false,  
66 - appId: appid,  
67 - timestamp: timestamp,  
68 - nonceStr: nonceStr,  
69 - signature: signature,  
70 - jsApiList: [  
71 - 'checkJsApi',  
72 - 'updateAppMessageShareData',  
73 - ]  
74 - });  
75 - wx.ready(function () { //需在用户可能点击分享按钮前就先调用  
76 - wx.updateAppMessageShareData({  
77 - title: '分享标题', // 分享标题  
78 - desc: '分享描述', // 分享描述  
79 - link: 'http://www.starplanet.cn/portal/enjoy/getEnjoyDetail?id=81', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致  
80 - imgUrl: '', // 分享图标  
81 - success: function (e) {  
82 - // 设置成功  
83 - console.log(e);  
84 - }  
85 - })  
86 - }); 62 + }
  63 + });
  64 + wx.config({
  65 + debug: true,
  66 + appId: appid,
  67 + timestamp: timestamp,
  68 + nonceStr: nonceStr,
  69 + signature: signature,
  70 + jsApiList: [
  71 + 'updateAppMessageShareData',
  72 + ]
  73 + });
  74 + wx.ready(function () { //需在用户可能点击分享按钮前就先调用
  75 + wx.updateAppMessageShareData({
  76 + title: '分享标题', // 分享标题
  77 + desc: '分享描述', // 分享描述
  78 + link: 'http://www.starplanet.cn/portal/enjoy/getEnjoyDetail?id=81', // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
  79 + imgUrl: '', // 分享图标
  80 + success: function (e) {
  81 + // 设置成功
  82 + console.log(e);
  83 + }
  84 + })
  85 + });
87 }); 86 });
88 87
89 </script> 88 </script>