微信公众号获取用户地理位置
## 最近在写一个微信社区售货机, 需要获取用户的位置信息, 因为是网页开发,所以需要用到js-sdk, 踩了不少坑, 所以记录一下
微信开放文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
1、绑定域名
登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
在"开发"中的"接口权限"中查看是否开启对应接口权限。
2、PHP获取js-sdk所需要的参数(以ThinkPHP5为例)
JS-SDK使用权限签名算法:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#62
public function weixin(){
if(empty(session('ticket_time'))){
$this->ticket();
}
$ticket_time = session('ticket_time');
if(time() - $ticket_time > 7000){
$ticket = $this->ticket();
}else{
$ticket = session('ticket');
}
$noncestr = 'qwertyuiopasdfghjkl';
$time = time();
$timestamp = $time;
$url = 'http://shj.demo.hengweiweb.cn/user/index';
$str = "jsapi_ticket=$ticket&noncestr=$noncestr×tamp=$timestamp&url=$url";
$sign = sha1($str);
$data = array(
'nonceStr' =>$noncestr,
'timestamp'=>strval($timestamp),
'signature'=>$sign,
'appId' =>'' //你的AppID
);
return $data;
}
public function getAccessToken(){
$appid = ''; //你的AppID
$secret = ''; //你的AppSecret
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$system = $<