申请了JSSDK,配置完了以后,PC端打印日志都正常,到手机端就出问题了 ,各种invalid signature。
php代码如下:
php代码如下:
/*-- 微信JS-SDK API(获取Token) --*/ function getAccessToken() { $data = json_decode(file_get_contents("access_token.json")); if ($data->expire_time < time()) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=保密&secret=保密"; $res = json_decode($this->httpGet($url)); $access_token = $res->access_token; if ($access_token) { $data->expire_time = time() + 7000; $data->access_token = $access_token; $fp = fopen("access_token.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $access_token = $data->access_token; } return $access_token; } /*-- 微信JS-SDK API(获取Ticket) --*/ function getJsApiTicket() { $data = json_decode(file_get_contents("jsapi_ticket.json")); if ($data->expire_time < time()) { $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$accessToken; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; $fp = fopen("jsapi_ticket.json", "w"); fwrite($fp, json_encode($data)); fclose($fp); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } /*-- 微信JS-SDK API(生成签名) --*/ public function getSign() { $jsapiTicket = $this->getJsApiTicket(); $nonceStr = $this->createNonceStr(); $timestamp = time(); $url = $_POST["url"]; $string = "jsapi_ticket=".$jsapiTicket."&noncestr=".$nonceStr."×tamp=".$timestamp."&url=".$url; $signature = sha1($string); $signPackage = array( "appId" => "保密", "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); $this->myajaxReturn($signPackage, "success", "200"); }
js代码如下:
/*-- 微信分享(分享链接发生变更,重新请求签名) --*/ function getSign() { var tmpurl = location.href.split("#")[0]; var shareurl = tmpurl + "?type=1&id=" + user_id + "&dater=" + search_text; shareurl = encodeURIComponent(shareurl); $.ajax({ url: "服务端控制器(保密)/getSign", type: "POST", dataType: "json", timeout: 5000, data: { url: shareurl }, success : function(data) { console.log(data); registWXShare(data.data); }, error: function() { setTimeout(function() { registWXShare(); },1000); } }); } /*-- 微信分享(注册分享监听) --*/ function registWXShare(data) { // 微信分享初始化 wx.config({ debug: true, appId: data.appId, timestamp: data.timestamp, nonceStr: data.nonceStr, signature: data.signature, jsApiList: [ "onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ" ] }); // 微信分享监听 wx.ready(function() { var shareData = { title: "这是活动的介绍页", desc: "这里是发送给好友的时候的简介", link: data.url, imgUrl: imgurl }; // 分享给朋友 wx.onMenuShareAppMessage({ title: "互联网之子", desc: "在长大的过程中,本人才慢慢发现,本人身边的全部事,别人跟本人说的全部事,那些所谓本来如此,注定如此的事,它们其实没有非得如此,事情是可以改变的。更重要的是,有些事既然错了,那就该做出改变。", link: data.url, imgUrl: imgurl, trigger: function (res) {}, success: function (res) { alert("已分享"); }, cancel: function (res) { alert("已取消"); }, fail: function (res) { alert(JSON.stringify(res)); } }); // 分享到朋友圈 wx.onMenuShareTimeline({ title: "互联网之子", link: data.url, imgUrl: imgurl, trigger: function (res) {}, success: function (res) { alert("已分享"); }, cancel: function (res) { alert("已取消"); }, fail: function (res) { alert(JSON.stringify(res)); } }); // 分享到QQ wx.onMenuShareQQ({ title: "互联网之子", desc: "在长大的过程中,本人才慢慢发现,本人身边的全部事,别人跟本人说的全部事,那些所谓本来如此,注定如此的事,它们其实没有非得如此,事情是可以改变的。更重要的是,有些事既然错了,那就该做出改变。", link: data.url, imgUrl: imgurl, trigger: function (res) {}, complete: function (res) { alert(JSON.stringify(res)); }, success: function (res) { alert("已分享"); }, cancel: function (res) { alert("已取消"); }, fail: function (res) { alert(JSON.stringify(res)); } }); wx.onMenuShareAppMessage(shareData); wx.onMenuShareTimeline(shareData); wx.onMenuShareQQ(shareData); }); // 失败监听 wx.error(function (res) { alert(res.errMsg); }); }
内心已然是崩溃的了。求高手协助。
解决方案