微信JSSDK config顯示OK,checkJsApi顯示OK,然后就沒(méi)有任何反應(yīng)了,不彈分享界面,也不報(bào)錯(cuò)
我在網(wǎng)上看了很多案例,跟著做也沒(méi)成功,這是我的測(cè)試地址http://ecapex.top/wx/sample/p...
求大佬指點(diǎn)下,到底是什么原因
// 請(qǐng)把代碼文本粘貼到下方(請(qǐng)勿用圖片代替代碼)
這是前端代碼
<?php
require_once "jssdk.php";
$jssdk = new JSSDK("Appid", "APPSecret");
$signPackage = $jssdk->GetSignPackage();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js">
<script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.js"></script>
<script>
wx.config({
debug: true,
appId: '<?php echo $signPackage["appId"];?>',
timestamp: <?php echo $signPackage["timestamp"];?>,
nonceStr: '<?php echo $signPackage["nonceStr"];?>',
signature: '<?php echo $signPackage["signature"];?>',
jsApiList: [
// 所有要調(diào)用的 API 都要加到這個(gè)列表中
'updateAppMessageShareData',
'updateTimelineShareData',
'onMenuShareAppMessage',
'onMenuShareTimeline'
]
});
//將分享的數(shù)組組成json數(shù)組
window.share_config = {
"share": {
"imgUrl": "http://ecapex.top/upload/partertalents/min/20180629202242.jpg",//分享圖,默認(rèn)當(dāng)相對(duì)路徑處理,所以使用絕對(duì)路徑的的話,“http://”協(xié)議前綴必須在。
"desc" : "你真是太棒了",//摘要,如果分享到朋友圈的話,不顯示摘要。
"title" : '你真是非常的棒',//分享卡片標(biāo)題
"link": 'http://ecapex.top',//分享出去后的鏈接,這里可以將鏈接設(shè)置為另一個(gè)頁(yè)面。
"success":function(){//分享成功后的回調(diào)函數(shù)
},
'cancel': function () {
// 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
}
}
};
wx.ready(function () {
// 在這里調(diào)用 API
//判斷當(dāng)前客戶端版本是否支持指定JS接口
wx.checkJsApi({
jsApiList: [
// 所有要調(diào)用的 API 都要加到這個(gè)列表中
'updateAppMessageShareData',
'updateTimelineShareData',
'onMenuShareAppMessage',
'onMenuShareTimeline'
], // 需要檢測(cè)的JS接口列表,所有JS接口列表見(jiàn)附錄2,
success: function(res) {
console.log(res)
// 以鍵值對(duì)的形式返回,可用的api值true,不可用為false
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
}
});
});
function AppMessage() {
wx.updateAppMessageShareData(share_config.share);
}
function Timeline() {
wx.updateTimelineShareData(share_config.share);
}
function Messages() {
wx.onMenuShareAppMessage(share_config.share);
}
function timelines() {
wx.onMenuShareTimeline(share_config.share)
}
</script>
<button onclick="AppMessage()">自定義分享給朋友</button><br>
<button onclick="Timeline()">自定義分享到朋友圈</button><br>
<button onclick="Messages()">分享給朋友</button>
<button onclick="timelines()">分享到朋友圈</button>
</html>
這是后端代碼
<?php
class JSSDK {
private $appId;
private $appSecret;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
// 注意 URL 一定要?jiǎng)討B(tài)獲取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appId,
"nonceStr" => $nonceStr,//隨機(jī)字符串
"timestamp" => $timestamp,//時(shí)間戳
"url" => $url,//當(dāng)前網(wǎng)頁(yè)的URL
"signature" => $signature,//簽名
"rawString" => $string
);
return $signPackage;
}
//設(shè)置簽名的隨機(jī)串并按ASCII 碼從小到大排序
private function createNonceStr($length = 16) {
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//獲取ticket
private function getJsApiTicket() {
// jsapi_ticket 應(yīng)該全局存儲(chǔ)與更新,以下代碼以寫(xiě)入到文件中做示例
$data = json_decode($this->get_php_file("jsapi_ticket.php"));
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;
$this->set_php_file("jsapi_ticket.php", json_encode($data));
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
//獲取accesstoken
private function getAccessToken() {
// access_token 應(yīng)該全局存儲(chǔ)與更新,以下代碼以寫(xiě)入到文件中做示例
$data = json_decode($this->get_php_file("access_token.php"));
if ($data->expire_time < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$this->set_php_file("access_token.php", json_encode($data));
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
private function httpGet($url,$type='get',$res='json',$arr='')
{
$ch = curl_init();
if (class_exists('\CURLFile')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
} else {
if (defined('CURLOPT_SAFE_UPLOAD')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if($type=='post'){
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
if($res=='json'){
if( curl_errno($ch) ){
//請(qǐng)求失敗
return curl_error($ch);
curl_close($ch);
}else{
//請(qǐng)求成功
curl_close($ch);
return $output;
}
}
}
//獲取對(duì)應(yīng)文件內(nèi)容(accesstoken、ticket)
private function get_php_file($filename) {
return trim(substr(file_get_contents($filename), 15));
}
//設(shè)置對(duì)應(yīng)文件內(nèi)容(accesstoken、ticket)
private function set_php_file($filename, $content) {
$fp = fopen($filename, "w");
fwrite($fp, "<?php exit();?>" . $content);
fclose($fp);
}
}
有么有大佬幫我看看是什么原因?指導(dǎo)一下,在線等急
本來(lái)就不會(huì)彈框呀,在微信webview打開(kāi)網(wǎng)頁(yè),右上角有三個(gè)點(diǎn),點(diǎn)開(kāi)之后有分享朋友圈、分享給朋友等等選項(xiàng),如果你不調(diào)用的話,分享出來(lái)的是默認(rèn)的,如果你成功調(diào)用,分享出來(lái)的就是你config里的信息。(也就是你成功調(diào)用并不會(huì)彈出框 而應(yīng)該點(diǎn)開(kāi)右上角三個(gè)小點(diǎn),進(jìn)去分享一下,看看分享結(jié)果,也就是下面的信息)
"share": {
"imgUrl": "http://ecapex.top/upload/partertalents/min/20180629202242.jpg",//分享圖,默認(rèn)當(dāng)相對(duì)路徑處理,所以使用絕對(duì)路徑的的話,“http://”協(xié)議前綴必須在。
"desc" : "你真是太棒了",//摘要,如果分享到朋友圈的話,不顯示摘要。
"title" : '你真是非常的棒',//分享卡片標(biāo)題
"link": 'http://ecapex.top',//分享出去后的鏈接,這里可以將鏈接設(shè)置為另一個(gè)頁(yè)面。
"success":function(){//分享成功后的回調(diào)函數(shù)
},
'cancel': function () {
// 用戶取消分享后執(zhí)行的回調(diào)函數(shù)
}
}
北大青鳥(niǎo)APTECH成立于1999年。依托北京大學(xué)優(yōu)質(zhì)雄厚的教育資源和背景,秉承“教育改變生活”的發(fā)展理念,致力于培養(yǎng)中國(guó)IT技能型緊缺人才,是大數(shù)據(jù)專業(yè)的國(guó)家
北大青鳥(niǎo)中博軟件學(xué)院創(chuàng)立于2003年,作為華東區(qū)著名互聯(lián)網(wǎng)學(xué)院和江蘇省首批服務(wù)外包人才培訓(xùn)基地,中博成功培育了近30000名軟件工程師走向高薪崗位,合作企業(yè)超4
中公教育集團(tuán)創(chuàng)建于1999年,經(jīng)過(guò)二十年潛心發(fā)展,已由一家北大畢業(yè)生自主創(chuàng)業(yè)的信息技術(shù)與教育服務(wù)機(jī)構(gòu),發(fā)展為教育服務(wù)業(yè)的綜合性企業(yè)集團(tuán),成為集合面授教學(xué)培訓(xùn)、網(wǎng)
達(dá)內(nèi)教育集團(tuán)成立于2002年,是一家由留學(xué)海歸創(chuàng)辦的高端職業(yè)教育培訓(xùn)機(jī)構(gòu),是中國(guó)一站式人才培養(yǎng)平臺(tái)、一站式人才輸送平臺(tái)。2014年4月3日在美國(guó)成功上市,融資1
曾工作于聯(lián)想擔(dān)任系統(tǒng)開(kāi)發(fā)工程師,曾在博彥科技股份有限公司擔(dān)任項(xiàng)目經(jīng)理從事移動(dòng)互聯(lián)網(wǎng)管理及研發(fā)工作,曾創(chuàng)辦藍(lán)懿科技有限責(zé)任公司從事總經(jīng)理職務(wù)負(fù)責(zé)iOS教學(xué)及管理工作。
浪潮集團(tuán)項(xiàng)目經(jīng)理。精通Java與.NET 技術(shù), 熟練的跨平臺(tái)面向?qū)ο箝_(kāi)發(fā)經(jīng)驗(yàn),技術(shù)功底深厚。 授課風(fēng)格 授課風(fēng)格清新自然、條理清晰、主次分明、重點(diǎn)難點(diǎn)突出、引人入勝。
精通HTML5和CSS3;Javascript及主流js庫(kù),具有快速界面開(kāi)發(fā)的能力,對(duì)瀏覽器兼容性、前端性能優(yōu)化等有深入理解。精通網(wǎng)頁(yè)制作和網(wǎng)頁(yè)游戲開(kāi)發(fā)。
具有10 年的Java 企業(yè)應(yīng)用開(kāi)發(fā)經(jīng)驗(yàn)。曾經(jīng)歷任德國(guó)Software AG 技術(shù)顧問(wèn),美國(guó)Dachieve 系統(tǒng)架構(gòu)師,美國(guó)AngelEngineers Inc. 系統(tǒng)架構(gòu)師。