Browse Source

接入百度地图,计算距离等

lizhen_gitee 3 years ago
parent
commit
4215a6996b
3 changed files with 205 additions and 4 deletions
  1. 24 4
      application/api/controller/User.php
  2. 152 0
      application/api/controller/Usercenter.php
  3. 29 0
      application/common.php

+ 24 - 4
application/api/controller/User.php

@@ -281,6 +281,26 @@ class User extends Api
         $this->success();
     }
 
+    /*
+     * 修改用户的坐标
+     * */
+    public function change_longlat(){
+        $longitude = input_post('longitude');
+        $latitude  = input_post('latitude');
+        $cityname  = input_post('cityname');
+        if(empty($longitude) || empty($latitude) || empty($cityname)){
+            $this->error();
+        }
+
+        $data = [
+            'longitude' => $longitude,
+            'latitude'  => $latitude,
+            'cityname'  => $cityname,
+        ];
+        Db::name('user')->where('id',$this->auth->id)->update($data);
+        $this->success();
+    }
+
     /**
      * 修改邮箱
      *
@@ -288,7 +308,7 @@ class User extends Api
      * @param string $email   邮箱
      * @param string $captcha 验证码
      */
-    public function changeemail()
+    /*public function changeemail()
     {
         $user = $this->auth->getUser();
         $email = $this->request->post('email');
@@ -314,7 +334,7 @@ class User extends Api
 
         Ems::flush($email, 'changeemail');
         $this->success();
-    }
+    }*/
 
     /**
      * 修改手机号
@@ -367,7 +387,7 @@ class User extends Api
      * @param string $platform 平台名称
      * @param string $code     Code码
      */
-    public function third()
+    /*public function third()
     {
         $url = url('user/index');
         $platform = $this->request->post("platform");
@@ -390,7 +410,7 @@ class User extends Api
             }
         }
         $this->error(__('Operation failed'), $url);
-    }
+    }*/
 
     /**
      * 重置密码

+ 152 - 0
application/api/controller/Usercenter.php

@@ -0,0 +1,152 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+
+/**
+ * 会员中心
+ */
+class Usercenter extends Api
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = '*';
+
+
+
+
+    public function test()
+    {
+        $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438]);
+        dump($a);
+        $a = $this->calc_map_distance([118.339282,35.028445],[118.437399,35.017438],true);
+        dump($a);
+        $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,1);
+        dump($b);
+        $b = $this->getmapjuli(118.339282,35.028445,118.437399,35.017438,0);
+        dump($b);
+    }
+
+    //同城
+    public function samecity(){
+        $gender = input_post('gender','all');
+        $agemin = input_post('agemin',0);
+        $agemax = input_post('agemax',100);
+
+        $map = [
+            'user.status' => 1,
+            'user.cityname' => $this->auth->cityname,
+            'user.id' => ['neq',$this->auth->id],
+            'user.longitude' => ['neq',''],
+            'user.latitude' => ['neq',''],
+        ];
+        if($gender != 'all'){
+            $map['user.gender'] = $gender;
+        }
+
+        $map['user.birthday'] = ['between',[time() - $agemax * 31536000,time() - $agemin * 31536000]];
+
+        //dump($map);
+        $field = [
+            'user.id','user.username','user.nickname','user.birthday','user.height','user.longitude','user.latitude','user.avatar','user.audio_bio','user.bio','user.gender'
+        ];
+        $list = Db::name('user')->alias('user')->field($field)->where($map)->orderRaw('rand()')->autopage()->select();
+        //dump($list);
+
+        $list = list_domain_image($list,['avatar']);
+
+        foreach($list as $key => $one){
+            $one['age'] = birthtime_to_age($one['birthday']);
+            $one['distance'] = $this->calc_map_distance([$this->auth->longitude,$this->auth->latitude],[$one['longitude'],$one['latitude']]);
+
+            $list[$key] = $one;
+        }
+
+        $this->success('success',$list);
+
+    }
+
+    //附近
+    public function nearuser(){
+        
+    }
+
+
+    /**
+     * calc_map_distance() , 根据地图上的两个点各自的x,y坐标,计算出2点之间的直线距离
+     * @param array $point_1 第1个点的x,y坐标    array( 101 , 202 )
+     * @param array $point_2 第2个点的x,y坐标    array( 101 , 202 )
+     * @param bool $calc_as_string 是否计算为字符串公里距离 , 如果未否返回数字
+     * @return float | false | string
+     */
+    public function calc_map_distance( $point_1=array(  ) , $point_2=array(  ) , $calc_as_string=false ) {
+        if( empty( $point_1 ) || empty( $point_2 ) ){
+            return false;
+        }
+        // 经纬度不存在,或者经纬度超过最大范围 +-180 , +-90 ,返回false
+        $p1_x = $point_1[0];
+        $p1_y = $point_1[1];
+
+        $p2_x = $point_2[0];
+        $p2_y = $point_2[1];
+        if(
+            $p1_x < -180 || $p1_x > 180
+            || $p2_x < -180 || $p2_x > 180
+            || $p1_y < -90 || $p1_y > 90
+            || $p2_y < -90 || $p2_y > 90
+        ){
+            return '0公里';
+        }
+
+        // 根据2点各自的坐标,计算2点之间直线距离的公式
+        $distance = round(6378.138*2*asin(sqrt(pow(sin(( $p1_x *pi()/180-$p2_x*pi()/180)/2),2)+cos( $p1_x *pi()/180)*cos($p2_x*pi()/180)* pow(sin(( $p1_y *pi()/180-$p2_y*pi()/180)/2),2)))*1000);
+
+        // 是否计算为字符串公里距离
+        if( !$calc_as_string ){
+            return (string)round( $distance / 1000 , 1 ) . '公里';
+        }
+
+        // 如果计算为字符串公里距离
+        if( $distance / 1000 > 1 ){
+            $k = (string)round( $distance / 1000 , 1 );
+            $m = (string)$distance % 1000 ;
+            $distance = "{$k}公里{$m}米";
+        }
+        else{
+            $distance = "{$distance}米";
+        }
+        return $distance;
+    }
+
+    //地图api,根据两地坐标,获得两地距离,打卡用的
+    //type=0直线,type=1开车
+    public function getmapjuli($start_lon,$start_lat,$end_lon,$end_lat,$type = 0){
+        $result = 0;
+
+        $apiurl = 'https://restapi.amap.com/v3/distance?';
+
+        $param = [
+            'key' => '398c424811d1a59beac2f915323d334e',
+            'origins' => $start_lon.','.$start_lat,
+            'destination' => $end_lon.','.$end_lat,
+            'type' => $type,
+            'output' => 'json',
+        ];
+
+        $apiurl .= http_build_query($param);
+
+        $request_rs = json_decode(curl_get($apiurl),true);
+        if(isset($request_rs['status']) && $request_rs['status'] == 1){
+            if(isset($request_rs['results'][0]['distance']))
+            {
+                $result = $request_rs['results'][0]['distance'];
+            }
+        }
+
+        //dump($result);
+
+        return $result;
+    }
+
+}

+ 29 - 0
application/common.php

@@ -577,4 +577,33 @@ function birthtime_to_age($birthtime){
     }
 
     return $age;
+}
+/**
+ * 发起HTTP GET请求
+ */
+function curl_get($url)
+{
+    $oCurl = curl_init();
+    if(stripos($url, "https://") !== FALSE) {
+        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
+        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
+        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
+    }
+    curl_setopt($oCurl, CURLOPT_TIMEOUT, 3);
+    curl_setopt($oCurl, CURLOPT_URL, $url);
+    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
+    $sContent = curl_exec($oCurl);
+    $aStatus = curl_getinfo($oCurl);
+    $error = curl_error($oCurl);
+    curl_close($oCurl);
+    if($error) {
+        $sContent = file_get_contents($url);
+        return $sContent;
+    }
+
+    if(intval($aStatus["http_code"]) == 200) {
+        return $sContent;
+    } else {
+        return false;
+    }
 }