6.确保你获取用来签名的url是动态获取的,动态页面可参见实例代码中php的实现方式。如果是html的静态页面在前端通过ajax将url传到后台签名,前端需要用js获取当前页面除去'#'hash部分的链接(可用location.href.split('#')[0]获取,而且需要encodeURIComponent),因为页面一旦分享,微信客户端会在你的链接末尾加入其它参数,如果不是动态获取当前链接,将导致分享后的页面签名失败。
php文件test.php
<?php
require_once '../include/common.inc.php';
require_once './jssdk.php';
if($act == 'ajax'){
$jsdk = new JSSDK($weixin_appID,$weixin_appSecret);
$code = $jsdk->getSignPackage();
echo json_encode($code);
exit;
}
include template('test.htm',_TPLWeixinPath_);
html文件test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>获取地理位置</title>
<script src="./js/jquery-2.1.4.js"></script>
<script src="./js/test.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
</head>
<body>
<div style="width:100px;height:100px;border:1px solid green;background:pink;" id="sun"></div>
</body>
</html>
js文件test.js
//alert(1);
$(document).on('click', '#sun', function() {
$.ajax({
url: 'test.php',
data:{ act:'ajax'},
type:'post',
success:function(res){
//console.log(res);
var data = JSON.parse(res);
wx.config({
debug: true,
appId: data.appId,
timestamp: data.timestamp,
nonceStr: data.nonceStr,
signature: data.signature,
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'checkJsApi',
'openLocation',
'getLocation'
]
})
wx.ready(function(){
alert(1);
});
wx.error(function(res){
console.log(res);
alert(2);
})
}
})
})
以上的方法是成功的,可以获取到用户的经纬度,我现在的问题在于我不能在同一个php文件获取那个code的值,只能访问一个新的php文件,但是换了个新的文件,code值是有的,一直提示signature无效,我觉得应该是我最上面贴的那个6出问题了,可是看了好久没明白意思!有没有大神可以讲讲微信跟我们交接的一个流程,我们给微信提供了一系列的参数微信检查过后然后把用户的经纬度发送给我们?发送到哪里?又会带什么样的参数或者什么?能不能把我最上面贴的详细讲解一下。
或者有没有大佬知道生成signature参数当中的url能不能加参数的