微信公眾號獲取使用者地理位置並列出附近的門店
阿新 • • 發佈:2019-01-10
//通過sql語句查詢距離5公里之內的門店
$sql = "select * from (select shop_id,shop_name,shop_tel,shop_position,shop_logo, ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`shop_wei`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`shop_wei`*PI()/180)*POW(SIN(($longitude*PI()/180-`shop_jing`*PI()/180)/2),2)))*1000) AS distance from sp_shop order by distance ) as a where a.distance<=5000";
// $sql = "select * from (select shop_id,shop_name,shop_tel,shop_position,shop_logo, ROUND(6378.138*2*ASIN(SQRT(POW(SIN((36.09297*PI()/180-`shop_wei`*PI()/180)/2),2)+COS(36.09297*PI()/180)*COS(`shop_wei`*PI()/180)*POW(SIN((120.3743*PI()/180-`shop_jing`*PI()/180)/2),2)))*1000) AS distance from sp_shop order by distance ) as a where a.distance<=5000";
$shopInfo = M()->query($sql);
echo json_encode($shopInfo);exit;
}else{
if(session('openid')){
//獲取微信簽名包資訊(使用者地理位置的獲取)填寫的配置資訊中,需要寫入的東西(呼叫簽名包封裝的類:http://blog.csdn.net/bj123467/article/details/72910160)
$jssdk = new \Home\Model\WechatModel();
$signPackage = $jssdk->GetSignPackage();
$this->assign('signPackage', $signPackage);
$this->display();
}else{
//判斷該使用者是否存在
$model = new \Home\Model\WechatModel();
$openid_accesstoken = $model->openId();
$rst = M('user')->where(array('user_openid' => $openid_accesstoken['openid']))->find();
if($rst){
session('openid',$openid_accesstoken['openid']);
session('user_id', $rst['user_id']);
$jssdk = new \Home\Model\WechatModel();
$signPackage = $jssdk->GetSignPackage();
$this->assign('signPackage', $signPackage);
$this->display();exit;
}else{
//如果不存在獲取微信使用者的基本資訊
$userInfo = $model->getOpenId($openid_accesstoken['openid'],$openid_accesstoken['access_token']);
$data = array(
'user_img' => $userInfo['headimgurl'],
'user_openid' => $userInfo['openid'],
'user_name' => filter($userInfo['nickname']),
'user_register_time' => time(),
'city' => $userInfo['province'].'-'.$userInfo['city'],
);
$id = M('user')->add($data);
session('openid', $userInfo['openid']);
session('user_id',$id);
$jssdk = new \Home\Model\WechatModel();
$signPackage = $jssdk->GetSignPackage();
$this->assign('signPackage', $signPackage);
$this->display();
}
}
}
$sql = "select * from (select shop_id,shop_name,shop_tel,shop_position,shop_logo, ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`shop_wei`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`shop_wei`*PI()/180)*POW(SIN(($longitude*PI()/180-`shop_jing`*PI()/180)/2),2)))*1000) AS distance from sp_shop order by distance ) as a where a.distance<=5000";
// $sql = "select * from (select shop_id,shop_name,shop_tel,shop_position,shop_logo, ROUND(6378.138*2*ASIN(SQRT(POW(SIN((36.09297*PI()/180-`shop_wei`*PI()/180)/2),2)+COS(36.09297*PI()/180)*COS(`shop_wei`*PI()/180)*POW(SIN((120.3743*PI()/180-`shop_jing`*PI()/180)/2),2)))*1000) AS distance from sp_shop order by distance ) as a where a.distance<=5000";
$shopInfo = M()->query($sql);
echo json_encode($shopInfo);exit;
}else{
if(session('openid')){
//獲取微信簽名包資訊(使用者地理位置的獲取)填寫的配置資訊中,需要寫入的東西(呼叫簽名包封裝的類:http://blog.csdn.net/bj123467/article/details/72910160)
$jssdk = new \Home\Model\WechatModel();
$signPackage = $jssdk->GetSignPackage();
$this->assign('signPackage', $signPackage);
$this->display();
}else{
//判斷該使用者是否存在
$model = new \Home\Model\WechatModel();
$openid_accesstoken = $model->openId();
$rst = M('user')->where(array('user_openid' => $openid_accesstoken['openid']))->find();
if($rst){
session('openid',$openid_accesstoken['openid']);
session('user_id', $rst['user_id']);
$jssdk = new \Home\Model\WechatModel();
$signPackage = $jssdk->GetSignPackage();
$this->assign('signPackage', $signPackage);
$this->display();exit;
}else{
//如果不存在獲取微信使用者的基本資訊
$userInfo = $model->getOpenId($openid_accesstoken['openid'],$openid_accesstoken['access_token']);
$data = array(
'user_img' => $userInfo['headimgurl'],
'user_openid' => $userInfo['openid'],
'user_name' => filter($userInfo['nickname']),
'user_register_time' => time(),
'city' => $userInfo['province'].'-'.$userInfo['city'],
);
$id = M('user')->add($data);
session('openid', $userInfo['openid']);
session('user_id',$id);
$jssdk = new \Home\Model\WechatModel();
$signPackage = $jssdk->GetSignPackage();
$this->assign('signPackage', $signPackage);
$this->display();
}
}
}