微信公众号根据粉丝当前位置匹配附近几公里范围的数据 代码整理
微信公众号根据粉丝当前位置匹配附近几公里范围的数据,首先微信公众号开启获取地理位置的接口权限。
1,Jssdk类包的引入Jssdk.class.php生成注册微信官方分享按钮的权限参数,上篇文章已经提过核心包文件直接从那复制即可,传送
2,前台页面的微信授权菜单,获取地理位置返回的数据:
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script> wx.config({ debug: false, appId: '{pigcms:$signPackage.appId}', timestamp: '{pigcms:$signPackage.timestamp}', nonceStr: '{pigcms:$signPackage.nonceStr}', signature: '{pigcms:$signPackage.signature}', jsApiList: [ 'checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'getLocation', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'openLocation', 'chooseImage', 'chooseWXPay', 'previewImage' ] }) </script>
获取当前粉丝地理位置坐标:
<script> wx.ready(function(){ wx.getLocation({ type: 'wgs84', success: function (res) { var latitude = res.latitude; var longitude = res.longitude; var wecha_id = "{$wecha_id}"; $.get("此处为处理坐标转换地图位置的php方法" , {"lat" : latitude , "lng" : longitude,"wecha_id":wecha_id} ,function(result){ result = eval("("+result+")"); if(result.status != 2){ if(result.status == 1){ layer.open({type: 2,time: 2,success:function(){ location.reload(); } }); }else{ layer.open({ content: result.info, style: 'background-color:rgba(51, 44, 43, 0.72); color:#fff; border:none;', time: 2 }); } } }); } }); }); </script>
3,php程序处理:
//默认页面数据调取
public function dataIndex() { $city = isset ( $this->city ) ? $this->city: '城市名'; $pid = D ( 'area' )->where ( array ('name' => $city ) )->getField ( 'id' ); $type = isset ( $_GET ['type'] ) ? intval ( $_GET ['type'] ) : 1; $token = $this->_get('token'); $wecha_id = $this->_get('wecha_id'); $nearBy = 5000; $model = D('user_point'); $pointInfo = $model->where(array('openid'=>$wecha_id))->field('lat,lng')->find(); /* 坐标转换 */ $pointInfo = self::pointChange($pointInfo['lat'], $pointInfo['lng']); switch ($type) { case '1' : /* 生成获取坐标的参数*/ import("@.ORG.Jssdk");/*第一步提到的核心包*/ $model = D("Wxuser"); $wxInfo = $model->where(array('token' => $token))->find(); $jssdk = new JSSDK($wxInfo['appid'], $wxInfo['appsecret']); $this->signPackage = $jssdk->GetSignPackage(); $list = D ( 'company' )->where ( array ( 'token' => $token, 'isbranch' => 1, 'pid' => $pid ) )->select (); break; case '2' : $list = D ( 'company' )->where ( array ( 'token' => $this->token, 'isbranch' => 1, 'pid' => $pid ) )->select (); break; case '3' : $catid = D ( 'dish_order' )->where ( array ( 'wecha_id' => $this->wecha_id ) )->getField ( 'catid', true ); $list = D ( 'company' )->where ( array ( 'token' => $this->token, 'isbranch' => 1, 'id' => array ( 'in', $catid ) ) )->select (); break; } /* 附近距离 */ foreach ($list as $key => $value) { $distance = self::getDistance_map($pointInfo['lat'], $pointInfo['lng'], $value['latitude'], $value['longitude']); if($type == 1){ if($nearBy < $distance){ unset($list[$key]); }else{ $list[$key]['nearby'] = $distance/1000; } }else{ $list[$key]['nearby'] = $distance/1000; } } $this->type = $type; $this->assign ( 'list', $list ); $this->display (); } /* 附近 */ /** * 计算当前商家位置是否在范围内 * @param 当前位置经度 $lat_a * @param 计算经度 $lng_a * @param 当前位置维度 $lat_b * @param 计算纬度 $lng_b * @author bieanju * @return number 距离 */ public function getDistance_map($lat_a, $lng_a, $lat_b, $lng_b) { //R是地球半径(米) $R = 6366000; $pk = doubleval(180 / 3.14169); $a1 = doubleval($lat_a / $pk); $a2 = doubleval($lng_a / $pk); $b1 = doubleval($lat_b / $pk); $b2 = doubleval($lng_b / $pk); $t1 = doubleval(cos($a1) * cos($a2) * cos($b1) * cos($b2)); $t2 = doubleval(cos($a1) * sin($a2) * cos($b1) * sin($b2)); $t3 = doubleval(sin($a1) * sin($b1)); $tt = doubleval(acos($t1 + $t2 + $t3)); return round($R * $tt); } public function savePresonPoint(){ $data['lat'] = $this->_get('lat'); $data['lng'] = $this->_get('lng'); $data['openid'] = $this->_get('wecha_id'); $data['create_time'] = time(); $pointLocationTime = 600;//10分钟重新定位 $model = D('UserPoint'); if(empty($data['openid'])){ echo json_encode(array('status'=>0,'info'=>'网络异常请重新加载!')); }else{ $pointInfo = $model->where(array("openid" => $data['openid']))->field('id,create_time')->find(); if(!$pointInfo){ $model->add($data); echo json_encode(array('status'=>1)); }else{ if($pointInfo['create_time']-time() > $pointLocationTime){ if($model->where(array('id' => $pointInfo['id']))->save($data) !== false){ echo json_encode(array('status'=>1)); } }else{ echo json_encode(array('status'=>2)); } } } } public function pointChange($x,$y){ if (C ( 'baidu_map' )) { $transUrl = 'http://api.map.baidu.com/ag/coord/convert?from=2&to=4&x=' . $x . '&y=' . $y; $json = Http::fsockopenDownload ( $transUrl ); if ($json == false) { $json = file_get_contents ( $transUrl ); } $arr = json_decode ( $json, true ); $x = base64_decode ( $arr ['x'] ); $y = base64_decode ( $arr ['y'] ); } else { $amap = new amap (); $lact = $amap->coordinateConvert ( $y, $x, 'gps' ); $x = $lact ['latitude']; $y = $lact ['longitude']; } return array('lat'=>$x,'lng'=>$y); }
一个人静静坐在电脑面前写代码的感觉,那是什么感觉?那是武林高手闭关修炼的感觉。