问题描述
微信JSSDK config显示OK,checkJsApi显示OK,然后就没有任何反应了,不弹分享界面,也不报错
问题出现的环境背景及自己尝试过哪些方法
我在网上看了很多案例,跟着做也没成功,这是我的测试地址http://ecapex.top/wx/sample/p...
求大佬指点下,到底是什么原因
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
这是前端代码
<?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: [
// 所有要调用的 API 都要加到这个列表中
'updateAppMessageShareData',
'updateTimelineShareData',
'onMenuShareAppMessage',
'onMenuShareTimeline'
]
});
//将分享的数组组成json数组
window.share_config = {
"share": {
"imgUrl": "http://ecapex.top/upload/partertalents/min/20180629202242.jpg",//分享图,默认当相对路径处理,所以使用绝对路径的的话,“http://”协议前缀必须在。
"desc" : "你真是太棒了",//摘要,如果分享到朋友圈的话,不显示摘要。
"title" : '你真是非常的棒',//分享卡片标题
"link": 'http://ecapex.top',//分享出去后的链接,这里可以将链接设置为另一个页面。
"success":function(){//分享成功后的回调函数
},
'cancel': function () {
// 用户取消分享后执行的回调函数
}
}
};
wx.ready(function () {
// 在这里调用 API
//判断当前客户端版本是否支持指定JS接口
wx.checkJsApi({
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'updateAppMessageShareData',
'updateTimelineShareData',
'onMenuShareAppMessage',
'onMenuShareTimeline'
], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function(res) {
console.log(res)
// 以键值对的形式返回,可用的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 一定要动态获取,不能 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();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appId,
"nonceStr" => $nonceStr,//随机字符串
"timestamp" => $timestamp,//时间戳
"url" => $url,//当前网页的URL
"signature" => $signature,//签名
"rawString" => $string
);
return $signPackage;
}
//设置签名的随机串并按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 应该全局存储与更新,以下代码以写入到文件中做示例
$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 应该全局存储与更新,以下代码以写入到文件中做示例
$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) ){
//请求失败
return curl_error($ch);
curl_close($ch);
}else{
//请求成功
curl_close($ch);
return $output;
}
}
}
//获取对应文件内容(accesstoken、ticket)
private function get_php_file($filename) {
return trim(substr(file_get_contents($filename), 15));
}
//设置对应文件内容(accesstoken、ticket)
private function set_php_file($filename, $content) {
$fp = fopen($filename, "w");
fwrite($fp, "<?php exit();?>" . $content);
fclose($fp);
}
}
你期待的结果是什么?实际看到的错误信息又是什么?
有么有大佬帮我看看是什么原因?指导一下,在线等急
本来就不会弹框呀,在微信webview打开网页,右上角有三个点,点开之后有分享朋友圈、分享给朋友等等选项,如果你不调用的话,分享出来的是默认的,如果你成功调用,分享出来的就是你config里的信息。(也就是你成功调用并不会弹出框 而应该点开右上角三个小点,进去分享一下,看看分享结果,也就是下面的信息)